fluentd 0.14.4-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of fluentd might be problematic. Click here for more details.

Files changed (328) hide show
  1. checksums.yaml +7 -0
  2. data/.github/ISSUE_TEMPLATE.md +6 -0
  3. data/.gitignore +26 -0
  4. data/.travis.yml +45 -0
  5. data/AUTHORS +2 -0
  6. data/CONTRIBUTING.md +35 -0
  7. data/COPYING +14 -0
  8. data/ChangeLog +276 -0
  9. data/Gemfile +9 -0
  10. data/README.md +51 -0
  11. data/Rakefile +53 -0
  12. data/Vagrantfile +17 -0
  13. data/appveyor.yml +41 -0
  14. data/bin/fluent-debug +5 -0
  15. data/example/copy_roundrobin.conf +39 -0
  16. data/example/filter_stdout.conf +22 -0
  17. data/example/in_forward.conf +11 -0
  18. data/example/in_http.conf +14 -0
  19. data/example/in_out_forward.conf +17 -0
  20. data/example/in_syslog.conf +15 -0
  21. data/example/in_tail.conf +14 -0
  22. data/example/in_tcp.conf +13 -0
  23. data/example/in_udp.conf +13 -0
  24. data/example/multi_filters.conf +61 -0
  25. data/example/out_buffered_null.conf +32 -0
  26. data/example/out_copy.conf +20 -0
  27. data/example/out_file.conf +13 -0
  28. data/example/out_forward.conf +35 -0
  29. data/example/out_forward_buf_file.conf +23 -0
  30. data/example/v0_12_filter.conf +78 -0
  31. data/example/v1_literal_example.conf +36 -0
  32. data/fluent.conf +139 -0
  33. data/fluentd.gemspec +51 -0
  34. data/lib/fluent/agent.rb +194 -0
  35. data/lib/fluent/command/bundler_injection.rb +45 -0
  36. data/lib/fluent/command/cat.rb +319 -0
  37. data/lib/fluent/command/debug.rb +102 -0
  38. data/lib/fluent/command/fluentd.rb +273 -0
  39. data/lib/fluent/compat/call_super_mixin.rb +67 -0
  40. data/lib/fluent/compat/exec_util.rb +129 -0
  41. data/lib/fluent/compat/file_util.rb +54 -0
  42. data/lib/fluent/compat/filter.rb +68 -0
  43. data/lib/fluent/compat/formatter.rb +111 -0
  44. data/lib/fluent/compat/formatter_utils.rb +85 -0
  45. data/lib/fluent/compat/handle_tag_and_time_mixin.rb +62 -0
  46. data/lib/fluent/compat/handle_tag_name_mixin.rb +53 -0
  47. data/lib/fluent/compat/input.rb +49 -0
  48. data/lib/fluent/compat/output.rb +677 -0
  49. data/lib/fluent/compat/output_chain.rb +60 -0
  50. data/lib/fluent/compat/parser.rb +180 -0
  51. data/lib/fluent/compat/parser_utils.rb +40 -0
  52. data/lib/fluent/compat/propagate_default.rb +62 -0
  53. data/lib/fluent/compat/record_filter_mixin.rb +34 -0
  54. data/lib/fluent/compat/set_tag_key_mixin.rb +50 -0
  55. data/lib/fluent/compat/set_time_key_mixin.rb +69 -0
  56. data/lib/fluent/compat/socket_util.rb +165 -0
  57. data/lib/fluent/compat/string_util.rb +34 -0
  58. data/lib/fluent/compat/structured_format_mixin.rb +26 -0
  59. data/lib/fluent/compat/type_converter.rb +90 -0
  60. data/lib/fluent/config.rb +56 -0
  61. data/lib/fluent/config/basic_parser.rb +123 -0
  62. data/lib/fluent/config/configure_proxy.rb +366 -0
  63. data/lib/fluent/config/dsl.rb +149 -0
  64. data/lib/fluent/config/element.rb +218 -0
  65. data/lib/fluent/config/error.rb +26 -0
  66. data/lib/fluent/config/literal_parser.rb +251 -0
  67. data/lib/fluent/config/parser.rb +107 -0
  68. data/lib/fluent/config/section.rb +212 -0
  69. data/lib/fluent/config/types.rb +136 -0
  70. data/lib/fluent/config/v1_parser.rb +190 -0
  71. data/lib/fluent/configurable.rb +176 -0
  72. data/lib/fluent/daemon.rb +15 -0
  73. data/lib/fluent/engine.rb +220 -0
  74. data/lib/fluent/env.rb +27 -0
  75. data/lib/fluent/event.rb +287 -0
  76. data/lib/fluent/event_router.rb +259 -0
  77. data/lib/fluent/filter.rb +21 -0
  78. data/lib/fluent/formatter.rb +23 -0
  79. data/lib/fluent/input.rb +21 -0
  80. data/lib/fluent/label.rb +38 -0
  81. data/lib/fluent/load.rb +36 -0
  82. data/lib/fluent/log.rb +445 -0
  83. data/lib/fluent/match.rb +141 -0
  84. data/lib/fluent/mixin.rb +31 -0
  85. data/lib/fluent/msgpack_factory.rb +62 -0
  86. data/lib/fluent/output.rb +26 -0
  87. data/lib/fluent/output_chain.rb +23 -0
  88. data/lib/fluent/parser.rb +23 -0
  89. data/lib/fluent/plugin.rb +161 -0
  90. data/lib/fluent/plugin/bare_output.rb +63 -0
  91. data/lib/fluent/plugin/base.rb +130 -0
  92. data/lib/fluent/plugin/buf_file.rb +154 -0
  93. data/lib/fluent/plugin/buf_memory.rb +34 -0
  94. data/lib/fluent/plugin/buffer.rb +603 -0
  95. data/lib/fluent/plugin/buffer/chunk.rb +160 -0
  96. data/lib/fluent/plugin/buffer/file_chunk.rb +323 -0
  97. data/lib/fluent/plugin/buffer/memory_chunk.rb +90 -0
  98. data/lib/fluent/plugin/exec_util.rb +22 -0
  99. data/lib/fluent/plugin/file_util.rb +22 -0
  100. data/lib/fluent/plugin/file_wrapper.rb +120 -0
  101. data/lib/fluent/plugin/filter.rb +93 -0
  102. data/lib/fluent/plugin/filter_grep.rb +75 -0
  103. data/lib/fluent/plugin/filter_record_transformer.rb +342 -0
  104. data/lib/fluent/plugin/filter_stdout.rb +53 -0
  105. data/lib/fluent/plugin/formatter.rb +45 -0
  106. data/lib/fluent/plugin/formatter_csv.rb +47 -0
  107. data/lib/fluent/plugin/formatter_hash.rb +29 -0
  108. data/lib/fluent/plugin/formatter_json.rb +44 -0
  109. data/lib/fluent/plugin/formatter_ltsv.rb +41 -0
  110. data/lib/fluent/plugin/formatter_msgpack.rb +29 -0
  111. data/lib/fluent/plugin/formatter_out_file.rb +78 -0
  112. data/lib/fluent/plugin/formatter_single_value.rb +34 -0
  113. data/lib/fluent/plugin/formatter_stdout.rb +74 -0
  114. data/lib/fluent/plugin/in_debug_agent.rb +64 -0
  115. data/lib/fluent/plugin/in_dummy.rb +135 -0
  116. data/lib/fluent/plugin/in_exec.rb +149 -0
  117. data/lib/fluent/plugin/in_forward.rb +366 -0
  118. data/lib/fluent/plugin/in_gc_stat.rb +52 -0
  119. data/lib/fluent/plugin/in_http.rb +422 -0
  120. data/lib/fluent/plugin/in_monitor_agent.rb +401 -0
  121. data/lib/fluent/plugin/in_object_space.rb +90 -0
  122. data/lib/fluent/plugin/in_syslog.rb +204 -0
  123. data/lib/fluent/plugin/in_tail.rb +838 -0
  124. data/lib/fluent/plugin/in_tcp.rb +41 -0
  125. data/lib/fluent/plugin/in_udp.rb +37 -0
  126. data/lib/fluent/plugin/in_unix.rb +201 -0
  127. data/lib/fluent/plugin/input.rb +33 -0
  128. data/lib/fluent/plugin/multi_output.rb +95 -0
  129. data/lib/fluent/plugin/out_buffered_null.rb +59 -0
  130. data/lib/fluent/plugin/out_buffered_stdout.rb +70 -0
  131. data/lib/fluent/plugin/out_copy.rb +42 -0
  132. data/lib/fluent/plugin/out_exec.rb +114 -0
  133. data/lib/fluent/plugin/out_exec_filter.rb +393 -0
  134. data/lib/fluent/plugin/out_file.rb +167 -0
  135. data/lib/fluent/plugin/out_forward.rb +646 -0
  136. data/lib/fluent/plugin/out_null.rb +27 -0
  137. data/lib/fluent/plugin/out_relabel.rb +28 -0
  138. data/lib/fluent/plugin/out_roundrobin.rb +80 -0
  139. data/lib/fluent/plugin/out_stdout.rb +48 -0
  140. data/lib/fluent/plugin/out_stream.rb +130 -0
  141. data/lib/fluent/plugin/output.rb +1020 -0
  142. data/lib/fluent/plugin/owned_by_mixin.rb +42 -0
  143. data/lib/fluent/plugin/parser.rb +175 -0
  144. data/lib/fluent/plugin/parser_apache.rb +28 -0
  145. data/lib/fluent/plugin/parser_apache2.rb +84 -0
  146. data/lib/fluent/plugin/parser_apache_error.rb +26 -0
  147. data/lib/fluent/plugin/parser_csv.rb +33 -0
  148. data/lib/fluent/plugin/parser_json.rb +79 -0
  149. data/lib/fluent/plugin/parser_ltsv.rb +50 -0
  150. data/lib/fluent/plugin/parser_multiline.rb +104 -0
  151. data/lib/fluent/plugin/parser_nginx.rb +28 -0
  152. data/lib/fluent/plugin/parser_none.rb +36 -0
  153. data/lib/fluent/plugin/parser_regexp.rb +73 -0
  154. data/lib/fluent/plugin/parser_syslog.rb +82 -0
  155. data/lib/fluent/plugin/parser_tsv.rb +37 -0
  156. data/lib/fluent/plugin/socket_util.rb +22 -0
  157. data/lib/fluent/plugin/storage.rb +84 -0
  158. data/lib/fluent/plugin/storage_local.rb +132 -0
  159. data/lib/fluent/plugin/string_util.rb +22 -0
  160. data/lib/fluent/plugin_helper.rb +42 -0
  161. data/lib/fluent/plugin_helper/child_process.rb +298 -0
  162. data/lib/fluent/plugin_helper/compat_parameters.rb +224 -0
  163. data/lib/fluent/plugin_helper/event_emitter.rb +80 -0
  164. data/lib/fluent/plugin_helper/event_loop.rb +118 -0
  165. data/lib/fluent/plugin_helper/formatter.rb +149 -0
  166. data/lib/fluent/plugin_helper/inject.rb +125 -0
  167. data/lib/fluent/plugin_helper/parser.rb +147 -0
  168. data/lib/fluent/plugin_helper/retry_state.rb +177 -0
  169. data/lib/fluent/plugin_helper/storage.rb +331 -0
  170. data/lib/fluent/plugin_helper/thread.rb +147 -0
  171. data/lib/fluent/plugin_helper/timer.rb +90 -0
  172. data/lib/fluent/plugin_id.rb +63 -0
  173. data/lib/fluent/process.rb +504 -0
  174. data/lib/fluent/registry.rb +99 -0
  175. data/lib/fluent/root_agent.rb +314 -0
  176. data/lib/fluent/rpc.rb +94 -0
  177. data/lib/fluent/supervisor.rb +680 -0
  178. data/lib/fluent/system_config.rb +122 -0
  179. data/lib/fluent/test.rb +56 -0
  180. data/lib/fluent/test/base.rb +85 -0
  181. data/lib/fluent/test/driver/base.rb +179 -0
  182. data/lib/fluent/test/driver/base_owned.rb +70 -0
  183. data/lib/fluent/test/driver/base_owner.rb +125 -0
  184. data/lib/fluent/test/driver/event_feeder.rb +98 -0
  185. data/lib/fluent/test/driver/filter.rb +57 -0
  186. data/lib/fluent/test/driver/formatter.rb +30 -0
  187. data/lib/fluent/test/driver/input.rb +31 -0
  188. data/lib/fluent/test/driver/multi_output.rb +52 -0
  189. data/lib/fluent/test/driver/output.rb +76 -0
  190. data/lib/fluent/test/driver/parser.rb +30 -0
  191. data/lib/fluent/test/driver/test_event_router.rb +45 -0
  192. data/lib/fluent/test/filter_test.rb +77 -0
  193. data/lib/fluent/test/formatter_test.rb +65 -0
  194. data/lib/fluent/test/helpers.rb +79 -0
  195. data/lib/fluent/test/input_test.rb +172 -0
  196. data/lib/fluent/test/log.rb +73 -0
  197. data/lib/fluent/test/output_test.rb +156 -0
  198. data/lib/fluent/test/parser_test.rb +70 -0
  199. data/lib/fluent/time.rb +175 -0
  200. data/lib/fluent/timezone.rb +133 -0
  201. data/lib/fluent/unique_id.rb +39 -0
  202. data/lib/fluent/version.rb +21 -0
  203. data/lib/fluent/winsvc.rb +71 -0
  204. data/test/compat/test_calls_super.rb +166 -0
  205. data/test/compat/test_parser.rb +82 -0
  206. data/test/config/assertions.rb +42 -0
  207. data/test/config/test_config_parser.rb +507 -0
  208. data/test/config/test_configurable.rb +1194 -0
  209. data/test/config/test_configure_proxy.rb +386 -0
  210. data/test/config/test_dsl.rb +415 -0
  211. data/test/config/test_element.rb +403 -0
  212. data/test/config/test_literal_parser.rb +297 -0
  213. data/test/config/test_section.rb +184 -0
  214. data/test/config/test_system_config.rb +120 -0
  215. data/test/config/test_types.rb +171 -0
  216. data/test/helper.rb +119 -0
  217. data/test/plugin/data/2010/01/20100102-030405.log +0 -0
  218. data/test/plugin/data/2010/01/20100102-030406.log +0 -0
  219. data/test/plugin/data/2010/01/20100102.log +0 -0
  220. data/test/plugin/data/log/bar +0 -0
  221. data/test/plugin/data/log/foo/bar.log +0 -0
  222. data/test/plugin/data/log/test.log +0 -0
  223. data/test/plugin/test_bare_output.rb +118 -0
  224. data/test/plugin/test_base.rb +75 -0
  225. data/test/plugin/test_buf_file.rb +571 -0
  226. data/test/plugin/test_buf_memory.rb +42 -0
  227. data/test/plugin/test_buffer.rb +1200 -0
  228. data/test/plugin/test_buffer_chunk.rb +168 -0
  229. data/test/plugin/test_buffer_file_chunk.rb +771 -0
  230. data/test/plugin/test_buffer_memory_chunk.rb +265 -0
  231. data/test/plugin/test_file_util.rb +96 -0
  232. data/test/plugin/test_filter.rb +353 -0
  233. data/test/plugin/test_filter_grep.rb +119 -0
  234. data/test/plugin/test_filter_record_transformer.rb +600 -0
  235. data/test/plugin/test_filter_stdout.rb +211 -0
  236. data/test/plugin/test_formatter_csv.rb +94 -0
  237. data/test/plugin/test_formatter_json.rb +30 -0
  238. data/test/plugin/test_formatter_ltsv.rb +52 -0
  239. data/test/plugin/test_formatter_msgpack.rb +28 -0
  240. data/test/plugin/test_formatter_out_file.rb +95 -0
  241. data/test/plugin/test_formatter_single_value.rb +38 -0
  242. data/test/plugin/test_in_debug_agent.rb +28 -0
  243. data/test/plugin/test_in_dummy.rb +188 -0
  244. data/test/plugin/test_in_exec.rb +133 -0
  245. data/test/plugin/test_in_forward.rb +635 -0
  246. data/test/plugin/test_in_gc_stat.rb +39 -0
  247. data/test/plugin/test_in_http.rb +442 -0
  248. data/test/plugin/test_in_monitor_agent.rb +329 -0
  249. data/test/plugin/test_in_object_space.rb +64 -0
  250. data/test/plugin/test_in_syslog.rb +205 -0
  251. data/test/plugin/test_in_tail.rb +1001 -0
  252. data/test/plugin/test_in_tcp.rb +102 -0
  253. data/test/plugin/test_in_udp.rb +121 -0
  254. data/test/plugin/test_in_unix.rb +126 -0
  255. data/test/plugin/test_input.rb +122 -0
  256. data/test/plugin/test_multi_output.rb +180 -0
  257. data/test/plugin/test_out_buffered_null.rb +79 -0
  258. data/test/plugin/test_out_buffered_stdout.rb +122 -0
  259. data/test/plugin/test_out_copy.rb +160 -0
  260. data/test/plugin/test_out_exec.rb +155 -0
  261. data/test/plugin/test_out_exec_filter.rb +262 -0
  262. data/test/plugin/test_out_file.rb +383 -0
  263. data/test/plugin/test_out_forward.rb +590 -0
  264. data/test/plugin/test_out_null.rb +29 -0
  265. data/test/plugin/test_out_relabel.rb +28 -0
  266. data/test/plugin/test_out_roundrobin.rb +146 -0
  267. data/test/plugin/test_out_stdout.rb +92 -0
  268. data/test/plugin/test_out_stream.rb +93 -0
  269. data/test/plugin/test_output.rb +568 -0
  270. data/test/plugin/test_output_as_buffered.rb +1604 -0
  271. data/test/plugin/test_output_as_buffered_overflow.rb +250 -0
  272. data/test/plugin/test_output_as_buffered_retries.rb +839 -0
  273. data/test/plugin/test_output_as_buffered_secondary.rb +817 -0
  274. data/test/plugin/test_output_as_standard.rb +374 -0
  275. data/test/plugin/test_owned_by.rb +35 -0
  276. data/test/plugin/test_parser_apache.rb +42 -0
  277. data/test/plugin/test_parser_apache2.rb +38 -0
  278. data/test/plugin/test_parser_apache_error.rb +45 -0
  279. data/test/plugin/test_parser_base.rb +32 -0
  280. data/test/plugin/test_parser_csv.rb +104 -0
  281. data/test/plugin/test_parser_json.rb +107 -0
  282. data/test/plugin/test_parser_labeled_tsv.rb +129 -0
  283. data/test/plugin/test_parser_multiline.rb +100 -0
  284. data/test/plugin/test_parser_nginx.rb +48 -0
  285. data/test/plugin/test_parser_none.rb +53 -0
  286. data/test/plugin/test_parser_regexp.rb +277 -0
  287. data/test/plugin/test_parser_syslog.rb +66 -0
  288. data/test/plugin/test_parser_time.rb +46 -0
  289. data/test/plugin/test_parser_tsv.rb +121 -0
  290. data/test/plugin/test_storage.rb +167 -0
  291. data/test/plugin/test_storage_local.rb +8 -0
  292. data/test/plugin/test_string_util.rb +26 -0
  293. data/test/plugin_helper/test_child_process.rb +608 -0
  294. data/test/plugin_helper/test_compat_parameters.rb +242 -0
  295. data/test/plugin_helper/test_event_emitter.rb +51 -0
  296. data/test/plugin_helper/test_event_loop.rb +52 -0
  297. data/test/plugin_helper/test_formatter.rb +252 -0
  298. data/test/plugin_helper/test_inject.rb +487 -0
  299. data/test/plugin_helper/test_parser.rb +263 -0
  300. data/test/plugin_helper/test_retry_state.rb +399 -0
  301. data/test/plugin_helper/test_storage.rb +521 -0
  302. data/test/plugin_helper/test_thread.rb +164 -0
  303. data/test/plugin_helper/test_timer.rb +131 -0
  304. data/test/scripts/exec_script.rb +32 -0
  305. data/test/scripts/fluent/plugin/formatter_known.rb +8 -0
  306. data/test/scripts/fluent/plugin/out_test.rb +81 -0
  307. data/test/scripts/fluent/plugin/out_test2.rb +80 -0
  308. data/test/scripts/fluent/plugin/parser_known.rb +4 -0
  309. data/test/test_config.rb +179 -0
  310. data/test/test_configdsl.rb +148 -0
  311. data/test/test_event.rb +329 -0
  312. data/test/test_event_router.rb +331 -0
  313. data/test/test_event_time.rb +184 -0
  314. data/test/test_filter.rb +121 -0
  315. data/test/test_formatter.rb +319 -0
  316. data/test/test_input.rb +31 -0
  317. data/test/test_log.rb +572 -0
  318. data/test/test_match.rb +137 -0
  319. data/test/test_mixin.rb +351 -0
  320. data/test/test_output.rb +214 -0
  321. data/test/test_plugin_classes.rb +136 -0
  322. data/test/test_plugin_helper.rb +81 -0
  323. data/test/test_process.rb +48 -0
  324. data/test/test_root_agent.rb +278 -0
  325. data/test/test_supervisor.rb +339 -0
  326. data/test/test_time_formatter.rb +186 -0
  327. data/test/test_unique_id.rb +47 -0
  328. metadata +823 -0
@@ -0,0 +1,680 @@
1
+ #
2
+ # Fluentd
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ require 'etc'
18
+ require 'fcntl'
19
+
20
+ require 'fluent/config'
21
+ require 'fluent/env'
22
+ require 'fluent/engine'
23
+ require 'fluent/log'
24
+ require 'fluent/plugin'
25
+ require 'fluent/rpc'
26
+ require 'fluent/system_config'
27
+ require 'serverengine'
28
+ require 'shellwords'
29
+
30
+ if Fluent.windows?
31
+ require 'windows/library'
32
+ require 'windows/synchronize'
33
+ require 'windows/system_info'
34
+ include Windows::Library
35
+ include Windows::Synchronize
36
+ include Windows::SystemInfo
37
+ require 'win32/ipc'
38
+ require 'win32/event'
39
+ end
40
+
41
+ module Fluent
42
+ module ServerModule
43
+ def before_run
44
+ @start_time = Time.now
45
+
46
+ if config[:rpc_endpoint]
47
+ @rpc_endpoint = config[:rpc_endpoint]
48
+ @enable_get_dump = config[:enable_get_dump]
49
+ run_rpc_server
50
+ end
51
+ install_supervisor_signal_handlers
52
+
53
+ if config[:signame]
54
+ @signame = config[:signame]
55
+ install_windows_event_handler
56
+ end
57
+
58
+ socket_manager_path = ServerEngine::SocketManager::Server.generate_path
59
+ ServerEngine::SocketManager::Server.open(socket_manager_path)
60
+ ENV['SERVERENGINE_SOCKETMANAGER_PATH'] = socket_manager_path.to_s
61
+ end
62
+
63
+ def after_run
64
+ stop_rpc_server if @rpc_endpoint
65
+ end
66
+
67
+ def run_rpc_server
68
+ @rpc_server = RPC::Server.new(@rpc_endpoint, $log)
69
+
70
+ # built-in RPC for signals
71
+ @rpc_server.mount_proc('/api/processes.interruptWorkers') { |req, res|
72
+ $log.debug "fluentd RPC got /api/processes.interruptWorkers request"
73
+ Process.kill :INT, $$
74
+ nil
75
+ }
76
+ @rpc_server.mount_proc('/api/processes.killWorkers') { |req, res|
77
+ $log.debug "fluentd RPC got /api/processes.killWorkers request"
78
+ Process.kill :TERM, $$
79
+ nil
80
+ }
81
+ @rpc_server.mount_proc('/api/processes.flushBuffersAndKillWorkers') { |req, res|
82
+ $log.debug "fluentd RPC got /api/processes.flushBuffersAndKillWorkers request"
83
+ if Fluent.windows?
84
+ $log.warn "operation 'flushBuffersAndKillWorkers' is not supported on Windows now."
85
+ else
86
+ Process.kill :USR1, $$
87
+ Process.kill :TERM, $$
88
+ end
89
+ nil
90
+ }
91
+ @rpc_server.mount_proc('/api/plugins.flushBuffers') { |req, res|
92
+ $log.debug "fluentd RPC got /api/plugins.flushBuffers request"
93
+ unless Fluent.windows?
94
+ Process.kill :USR1, $$
95
+ end
96
+ nil
97
+ }
98
+ @rpc_server.mount_proc('/api/config.reload') { |req, res|
99
+ $log.debug "fluentd RPC got /api/config.reload request"
100
+ if Fluent.windows?
101
+ # restart worker with auto restarting by killing
102
+ kill_worker
103
+ else
104
+ Process.kill :HUP, $$
105
+ end
106
+ nil
107
+ }
108
+ @rpc_server.mount_proc('/api/config.dump') { |req, res|
109
+ $log.debug "fluentd RPC got /api/config.dump request"
110
+ $log.info "dump in-memory config"
111
+ supervisor_dump_config_handler
112
+ nil
113
+ }
114
+
115
+ @rpc_server.mount_proc('/api/config.getDump') { |req, res|
116
+ $log.debug "fluentd RPC got /api/config.dump request"
117
+ $log.info "get dump in-memory config via HTTP"
118
+ res.body = supervisor_get_dump_config_handler
119
+ [nil, nil, res]
120
+ } if @enable_get_dump
121
+
122
+ @rpc_server.start
123
+ end
124
+
125
+ def stop_rpc_server
126
+ @rpc_server.shutdown
127
+ end
128
+
129
+ def install_supervisor_signal_handlers
130
+ trap :HUP do
131
+ $log.debug "fluentd supervisor process get SIGHUP"
132
+ supervisor_sighup_handler
133
+ end unless Fluent.windows?
134
+
135
+ trap :USR1 do
136
+ $log.debug "fluentd supervisor process get SIGUSR1"
137
+ supervisor_sigusr1_handler
138
+ end unless Fluent.windows?
139
+ end
140
+
141
+ def install_windows_event_handler
142
+ Thread.new do
143
+ ev = Win32::Event.new(@signame)
144
+ begin
145
+ ev.reset
146
+ until WaitForSingleObject(ev.handle, 0) == WAIT_OBJECT_0
147
+ sleep 1
148
+ end
149
+ kill_worker
150
+ stop(true)
151
+ ensure
152
+ ev.close
153
+ end
154
+ end
155
+ end
156
+
157
+ def supervisor_sighup_handler
158
+ kill_worker
159
+ end
160
+
161
+ def supervisor_sigusr1_handler
162
+ if log = config[:logger_initializer]
163
+ log.reopen!
164
+ end
165
+
166
+ if pid = config[:worker_pid]
167
+ Process.kill(:USR1, pid)
168
+ # don't rescue Erro::ESRSH here (invalid status)
169
+ end
170
+ end
171
+
172
+ def kill_worker
173
+ if pid = config[:worker_pid]
174
+ if Fluent.windows?
175
+ Process.kill :KILL, pid
176
+ else
177
+ Process.kill :TERM, pid
178
+ end
179
+ end
180
+ end
181
+
182
+ def supervisor_dump_config_handler
183
+ $log.info config[:fluentd_conf].to_s
184
+ end
185
+
186
+ def supervisor_get_dump_config_handler
187
+ {conf: config[:fluentd_conf].to_s}
188
+ end
189
+ end
190
+
191
+ module WorkerModule
192
+ def spawn(process_manager)
193
+ main_cmd = config[:main_cmd]
194
+ @pm = process_manager.spawn(main_cmd)
195
+ end
196
+
197
+ def after_start
198
+ config[:worker_pid] = @pm.pid
199
+ end
200
+ end
201
+
202
+ class Supervisor
203
+ def self.load_config(path, params = {})
204
+
205
+ pre_loadtime = 0
206
+ pre_loadtime = params['pre_loadtime'].to_i if params['pre_loadtime']
207
+ pre_config_mtime = nil
208
+ pre_config_mtime = params['pre_config_mtime'] if params['pre_config_mtime']
209
+ config_mtime = File.mtime(path)
210
+
211
+ # reuse previous config if last load time is within 5 seconds and mtime of the config file is not changed
212
+ if Time.now - Time.at(pre_loadtime) < 5 and config_mtime == pre_config_mtime
213
+ return params['pre_conf']
214
+ end
215
+
216
+ config_fname = File.basename(path)
217
+ config_basedir = File.dirname(path)
218
+ config_data = File.read(path)
219
+ inline_config = params['inline_config']
220
+ if inline_config == '-'
221
+ config_data << "\n" << STDIN.read
222
+ elsif inline_config
223
+ config_data << "\n" << inline_config.gsub("\\n","\n")
224
+ end
225
+ fluentd_conf = Fluent::Config.parse(config_data, config_fname, config_basedir, params['use_v1_config'])
226
+ system_config = SystemConfig.create(fluentd_conf)
227
+
228
+ log_level = system_config.log_level || params['log_level']
229
+ suppress_repeated_stacktrace = system_config.suppress_repeated_stacktrace || params['suppress_repeated_stacktrace']
230
+ log_path = params['log_path']
231
+ chuser = params['chuser']
232
+ chgroup = params['chgroup']
233
+ rpc_endpoint = system_config.rpc_endpoint
234
+ enable_get_dump = system_config.enable_get_dump
235
+
236
+ log_opts = {suppress_repeated_stacktrace: suppress_repeated_stacktrace}
237
+ logger_initializer = Supervisor::LoggerInitializer.new(log_path, log_level, chuser, chgroup, log_opts)
238
+ # this #init sets initialized logger to $log
239
+ logger_initializer.init
240
+ logger = $log
241
+
242
+ command_sender = Fluent.windows? ? "pipe" : "signal"
243
+
244
+ # ServerEngine's "daemonize" option is boolean, and path of pid file is brought by "pid_path"
245
+ pid_path = params['daemonize']
246
+ daemonize = !!params['daemonize']
247
+ main_cmd = params['main_cmd']
248
+ signame = params['signame']
249
+
250
+ se_config = {
251
+ worker_type: 'spawn',
252
+ workers: 1,
253
+ log_stdin: false,
254
+ log_stdout: false,
255
+ log_stderr: false,
256
+ enable_heartbeat: true,
257
+ auto_heartbeat: false,
258
+ unrecoverable_exit_codes: [2],
259
+ stop_immediately_at_unrecoverable_exit: true,
260
+ logger: logger,
261
+ log: logger.out,
262
+ log_path: log_path,
263
+ log_level: log_level,
264
+ logger_initializer: logger_initializer,
265
+ chuser: chuser,
266
+ chgroup: chgroup,
267
+ chumask: 0,
268
+ suppress_repeated_stacktrace: suppress_repeated_stacktrace,
269
+ daemonize: daemonize,
270
+ rpc_endpoint: rpc_endpoint,
271
+ enable_get_dump: enable_get_dump,
272
+ windows_daemon_cmdline: [ServerEngine.ruby_bin_path,
273
+ File.join(File.dirname(__FILE__), 'daemon.rb'),
274
+ ServerModule.name,
275
+ WorkerModule.name,
276
+ path,
277
+ JSON.dump(params)],
278
+ command_sender: command_sender,
279
+ fluentd_conf: fluentd_conf,
280
+ main_cmd: main_cmd,
281
+ signame: signame,
282
+ }
283
+ if daemonize
284
+ se_config[:pid_path] = pid_path
285
+ end
286
+ pre_params = params.dup
287
+ params['pre_loadtime'] = Time.now.to_i
288
+ params['pre_config_mtime'] = config_mtime
289
+ params['pre_conf'] = se_config
290
+ # prevent pre_conf from being too big by reloading many times.
291
+ pre_params['pre_conf'] = nil
292
+ params['pre_conf'][:windows_daemon_cmdline][5] = JSON.dump(pre_params)
293
+
294
+ return se_config
295
+ end
296
+
297
+ class LoggerInitializer
298
+ def initialize(path, level, chuser, chgroup, opts)
299
+ @path = path
300
+ @level = level
301
+ @chuser = chuser
302
+ @chgroup = chgroup
303
+ @opts = opts
304
+ end
305
+
306
+ def init
307
+ if @path && @path != "-"
308
+ @io = File.open(@path, "a")
309
+ if @chuser || @chgroup
310
+ chuid = @chuser ? ServerEngine::Privilege.get_etc_passwd(@chuser).uid : nil
311
+ chgid = @chgroup ? ServerEngine::Privilege.get_etc_group(@chgroup).gid : nil
312
+ File.chown(chuid, chgid, @path)
313
+ end
314
+ else
315
+ @io = STDOUT
316
+ end
317
+
318
+ dl_opts = {}
319
+ # subtract 1 to match serverengine daemon logger side logging severity.
320
+ dl_opts[:log_level] = @level - 1
321
+ logger = ServerEngine::DaemonLogger.new(@io, dl_opts)
322
+ $log = Fluent::Log.new(logger, @opts)
323
+ $log.enable_color(false) if @path
324
+ $log.enable_debug if @level <= Fluent::Log::LEVEL_DEBUG
325
+ end
326
+
327
+ def stdout?
328
+ @io == STDOUT
329
+ end
330
+
331
+ def reopen!
332
+ if @path && @path != "-"
333
+ @io.reopen(@path, "a")
334
+ end
335
+ self
336
+ end
337
+
338
+ def level=(level)
339
+ @level = level
340
+ $log.level = level
341
+ end
342
+ end
343
+
344
+ def self.default_options
345
+ {
346
+ config_path: Fluent::DEFAULT_CONFIG_PATH,
347
+ plugin_dirs: [Fluent::DEFAULT_PLUGIN_DIR],
348
+ log_level: Fluent::Log::LEVEL_INFO,
349
+ log_path: nil,
350
+ daemonize: nil,
351
+ libs: [],
352
+ setup_path: nil,
353
+ chuser: nil,
354
+ chgroup: nil,
355
+ suppress_interval: 0,
356
+ suppress_repeated_stacktrace: true,
357
+ without_source: false,
358
+ use_v1_config: true,
359
+ supervise: true,
360
+ standalone_worker: false,
361
+ signame: nil,
362
+ winsvcreg: nil,
363
+ }
364
+ end
365
+
366
+ def initialize(opt)
367
+ @daemonize = opt[:daemonize]
368
+ @supervise = opt[:supervise]
369
+ @standalone_worker= opt[:standalone_worker]
370
+ @config_path = opt[:config_path]
371
+ @inline_config = opt[:inline_config]
372
+ @use_v1_config = opt[:use_v1_config]
373
+ @log_path = opt[:log_path]
374
+ @dry_run = opt[:dry_run]
375
+ @show_plugin_config = opt[:show_plugin_config]
376
+ @libs = opt[:libs]
377
+ @plugin_dirs = opt[:plugin_dirs]
378
+ @chgroup = opt[:chgroup]
379
+ @chuser = opt[:chuser]
380
+ @rpc_server = nil
381
+ @process_name = nil
382
+
383
+ @log_level = opt[:log_level]
384
+ @suppress_interval = opt[:suppress_interval]
385
+ @suppress_config_dump = opt[:suppress_config_dump]
386
+ @without_source = opt[:without_source]
387
+ @signame = opt[:signame]
388
+
389
+ @suppress_repeated_stacktrace = opt[:suppress_repeated_stacktrace]
390
+ log_opts = {suppress_repeated_stacktrace: @suppress_repeated_stacktrace}
391
+ @log = LoggerInitializer.new(@log_path, @log_level, @chuser, @chgroup, log_opts)
392
+ @finished = false
393
+ end
394
+
395
+ def run_supervisor
396
+ @log.init
397
+ show_plugin_config if @show_plugin_config
398
+ read_config
399
+ set_system_config
400
+
401
+ dry_run if @dry_run
402
+ supervise
403
+ end
404
+
405
+ def options
406
+ {
407
+ 'config_path' => @config_path,
408
+ 'pid_file' => @daemonize,
409
+ 'plugin_dirs' => @plugin_dirs,
410
+ 'log_path' => @log_path
411
+ }
412
+ end
413
+
414
+ def run_worker
415
+ begin
416
+ require 'sigdump/setup'
417
+ rescue Exception
418
+ # ignore LoadError and others (related with signals): it may raise these errors in Windows
419
+ end
420
+ @log.init
421
+ Process.setproctitle("worker:#{@process_name}") if @process_name
422
+
423
+ show_plugin_config if @show_plugin_config
424
+ read_config
425
+ set_system_config
426
+
427
+ install_main_process_signal_handlers
428
+
429
+ $log.info "starting fluentd-#{Fluent::VERSION} without supervision"
430
+
431
+ main_process do
432
+ create_socket_manager if @standalone_worker
433
+ change_privilege
434
+ init_engine
435
+ run_configure
436
+ run_engine
437
+ exit 0
438
+ end
439
+ end
440
+
441
+ private
442
+
443
+ def create_socket_manager
444
+ socket_manager_path = ServerEngine::SocketManager::Server.generate_path
445
+ ServerEngine::SocketManager::Server.open(socket_manager_path)
446
+ ENV['SERVERENGINE_SOCKETMANAGER_PATH'] = socket_manager_path.to_s
447
+ end
448
+
449
+ def dry_run
450
+ $log.info "starting fluentd-#{Fluent::VERSION} as dry run mode"
451
+ change_privilege
452
+ init_engine
453
+ run_configure
454
+ exit 0
455
+ rescue => e
456
+ $log.error "dry run failed: #{e}"
457
+ exit 1
458
+ end
459
+
460
+ def show_plugin_config
461
+ $log.info "Show config for #{@show_plugin_config}"
462
+ @system_config = SystemConfig.new
463
+ init_engine
464
+ name, type = @show_plugin_config.split(":")
465
+ plugin = Plugin.__send__("new_#{name}", type)
466
+ dumped_config = "\n"
467
+ level = 0
468
+ plugin.class.ancestors.reverse_each do |plugin_class|
469
+ if plugin_class.respond_to?(:dump)
470
+ $log.on_debug do
471
+ dumped_config << plugin_class.name
472
+ dumped_config << "\n"
473
+ level = 1
474
+ end
475
+ dumped_config << plugin_class.dump(level)
476
+ end
477
+ end
478
+ $log.info dumped_config
479
+ exit 0
480
+ rescue => e
481
+ $log.error "show config failed: #{e}"
482
+ exit 1
483
+ end
484
+
485
+ def supervise
486
+ $log.info "starting fluentd-#{Fluent::VERSION}"
487
+
488
+ rubyopt = ENV["RUBYOPT"]
489
+ if Fluent.windows?
490
+ # Shellwords doesn't work on windows, then used gsub for adapting space char instead of Shellwords
491
+ fluentd_spawn_cmd = ServerEngine.ruby_bin_path + " -Eascii-8bit:ascii-8bit "
492
+ fluentd_spawn_cmd << ' "' + rubyopt.gsub('"', '""') + '" ' if rubyopt
493
+ fluentd_spawn_cmd << ' "' + $0.gsub('"', '""') + '" '
494
+ $fluentdargv.each{|a|
495
+ fluentd_spawn_cmd << ('"' + a.gsub('"', '""') + '" ')
496
+ }
497
+ else
498
+ fluentd_spawn_cmd = ServerEngine.ruby_bin_path + " -Eascii-8bit:ascii-8bit "
499
+ fluentd_spawn_cmd << ' ' + rubyopt + ' ' if rubyopt
500
+ fluentd_spawn_cmd << $0.shellescape + ' '
501
+ $fluentdargv.each{|a|
502
+ fluentd_spawn_cmd << (a.shellescape + " ")
503
+ }
504
+ end
505
+
506
+ fluentd_spawn_cmd << ("--under-supervisor")
507
+ $log.info "spawn command to main: " + fluentd_spawn_cmd
508
+
509
+ params = {}
510
+ params['main_cmd'] = fluentd_spawn_cmd
511
+ params['daemonize'] = @daemonize
512
+ params['inline_config'] = @inline_config
513
+ params['log_path'] = @log_path
514
+ params['log_level'] = @log_level
515
+ params['chuser'] = @chuser
516
+ params['chgroup'] = @chgroup
517
+ params['use_v1_config'] = @use_v1_config
518
+ params['suppress_repeated_stacktrace'] = @suppress_repeated_stacktrace
519
+ params['signame'] = @signame
520
+
521
+ se = ServerEngine.create(ServerModule, WorkerModule){
522
+ Fluent::Supervisor.load_config(@config_path, params)
523
+ }
524
+ se.run
525
+ end
526
+
527
+ def install_main_process_signal_handlers
528
+ # Fluentd worker process (worker of ServerEngine) don't use code in serverengine to set signal handlers,
529
+ # because it does almost nothing.
530
+ # This method is the only method to set signal handlers in Fluentd worker process.
531
+
532
+ # When user use Ctrl + C not SIGINT, SIGINT is sent to all process in same process group.
533
+ # ServerEngine server process will send SIGTERM to child(spawned) processes by that SIGINT, so
534
+ # worker process SHOULD NOT do anything with SIGINT, SHOULD just ignore.
535
+ trap :INT do
536
+ $log.debug "fluentd main process get SIGINT"
537
+
538
+ # When Fluentd is launched without supervisor, worker should handle ctrl-c by itself
539
+ if @standalone_worker
540
+ @finished = true
541
+ $log.debug "getting start to shutdown main process"
542
+ Fluent::Engine.stop
543
+ end
544
+ end
545
+
546
+ trap :TERM do
547
+ $log.debug "fluentd main process get SIGTERM"
548
+ unless @finished
549
+ @finished = true
550
+ $log.debug "getting start to shutdown main process"
551
+ Fluent::Engine.stop
552
+ end
553
+ end
554
+
555
+ trap :USR1 do
556
+ flush_buffer
557
+ end unless Fluent.windows?
558
+
559
+ if Fluent.windows?
560
+ command_pipe = STDIN.dup
561
+ STDIN.reopen(File::NULL, "rb")
562
+ command_pipe.binmode
563
+ command_pipe.sync = true
564
+
565
+ Thread.new do
566
+ loop do
567
+ cmd = command_pipe.gets.chomp
568
+ case cmd
569
+ when "GRACEFUL_STOP", "IMMEDIATE_STOP"
570
+ $log.debug "fluentd main process get #{cmd} command"
571
+ @finished = true
572
+ $log.debug "getting start to shutdown main process"
573
+ Fluent::Engine.stop
574
+ break
575
+ else
576
+ $log.warn "fluentd main process get unknown command [#{cmd}]"
577
+ end
578
+ end
579
+ end
580
+ end
581
+ end
582
+
583
+ def flush_buffer
584
+ $log.debug "fluentd main process get SIGUSR1"
585
+ $log.info "force flushing buffered events"
586
+ @log.reopen!
587
+
588
+ # Creating new thread due to mutex can't lock
589
+ # in main thread during trap context
590
+ Thread.new {
591
+ begin
592
+ Fluent::Engine.flush!
593
+ $log.debug "flushing thread: flushed"
594
+ rescue Exception => e
595
+ $log.warn "flushing thread error: #{e}"
596
+ end
597
+ }.run
598
+ end
599
+
600
+ def main_process(&block)
601
+ Process.setproctitle("worker:#{@process_name}") if @process_name
602
+
603
+ configuration_error = false
604
+
605
+ begin
606
+ block.call
607
+ rescue Fluent::ConfigError
608
+ $log.error "config error", file: @config_path, error: $!.to_s
609
+ $log.debug_backtrace
610
+ unless @log.stdout?
611
+ logger = ServerEngine::DaemonLogger.new(STDOUT)
612
+ log = Fluent::Log.new(logger)
613
+ log.level = @log_level
614
+ console = log.enable_debug
615
+ console.error "config error", file: @config_path, error: $!.to_s
616
+ console.debug_backtrace
617
+ end
618
+ configuration_error = true
619
+ rescue
620
+ $log.error "unexpected error", error: $!.to_s
621
+ $log.error_backtrace
622
+ unless @log.stdout?
623
+ logger = ServerEngine::DaemonLogger.new(STDOUT)
624
+ log = Fluent::Log.new(logger)
625
+ log.level = @log_level
626
+ console = log.enable_debug
627
+ console.error "unexpected error", error: $!.to_s
628
+ console.error_backtrace
629
+ end
630
+ end
631
+
632
+ exit!(configuration_error ? 2 : 1)
633
+ end
634
+
635
+ def read_config
636
+ $log.info "reading config file", path: @config_path
637
+ @config_fname = File.basename(@config_path)
638
+ @config_basedir = File.dirname(@config_path)
639
+ @config_data = File.read(@config_path)
640
+ if @inline_config == '-'
641
+ @config_data << "\n" << STDIN.read
642
+ elsif @inline_config
643
+ @config_data << "\n" << @inline_config.gsub("\\n","\n")
644
+ end
645
+ @conf = Fluent::Config.parse(@config_data, @config_fname, @config_basedir, @use_v1_config)
646
+ end
647
+
648
+ def set_system_config
649
+ @system_config = SystemConfig.create(@conf) # @conf is set in read_config
650
+ @system_config.apply(self)
651
+ end
652
+
653
+ def change_privilege
654
+ ServerEngine::Privilege.change(@chuser, @chgroup)
655
+ end
656
+
657
+ def init_engine
658
+ Fluent::Engine.init(@system_config)
659
+
660
+ @libs.each {|lib|
661
+ require lib
662
+ }
663
+
664
+ @plugin_dirs.each {|dir|
665
+ if Dir.exist?(dir)
666
+ dir = File.expand_path(dir)
667
+ Fluent::Engine.add_plugin_dir(dir)
668
+ end
669
+ }
670
+ end
671
+
672
+ def run_configure
673
+ Fluent::Engine.run_configure(@conf)
674
+ end
675
+
676
+ def run_engine
677
+ Fluent::Engine.run
678
+ end
679
+ end
680
+ end