1
0

syslog_ng-state-usage.rst 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. .. _syslog-ng-sate-usage:
  2. Syslog-ng usage
  3. ===============
  4. Overview
  5. --------
  6. Syslog\_ng state module is for generating syslog-ng
  7. configurations. You can do the following things:
  8. - generate syslog-ng configuration from YAML,
  9. - use non-YAML configuration,
  10. - start, stop or reload syslog-ng.
  11. There is also an execution module, which can check the syntax of the
  12. configuration, get the version and other information about syslog-ng.
  13. Configuration
  14. -------------
  15. Users can create syslog-ng configuration statements with the
  16. :py:func:`syslog_ng.config <salt.states.syslog_ng.config>` function. It requires
  17. a `name` and a `config` parameter. The `name` parameter determines the name of
  18. the generated statement and the `config` parameter holds a parsed YAML structure.
  19. A statement can be declared in the following forms (both are equivalent):
  20. .. code-block:: yaml
  21. source.s_localhost:
  22. syslog_ng.config:
  23. - config:
  24. - tcp:
  25. - ip: "127.0.0.1"
  26. - port: 1233
  27. .. code-block:: yaml
  28. s_localhost:
  29. syslog_ng.config:
  30. - config:
  31. source:
  32. - tcp:
  33. - ip: "127.0.0.1"
  34. - port: 1233
  35. The first one is called short form, because it needs less typing. Users can use lists
  36. and dictionaries to specify their configuration. The format is quite self describing and
  37. there are more examples [at the end](#examples) of this document.
  38. Quotation
  39. ---------
  40. The quotation can be tricky sometimes but here are some rules to follow:
  41. * when a string meant to be ``"string"`` in the generated configuration, it should be like ``'"string"'`` in the YAML document
  42. * similarly, users should write ``"'string'"`` to get ``'string'`` in the generated configuration
  43. Full example
  44. ------------
  45. The following configuration is an example, how a complete syslog-ng configuration looks like:
  46. .. code-block:: yaml
  47. # Set the location of the configuration file
  48. set_location:
  49. module.run:
  50. - name: syslog_ng.set_config_file
  51. - m_name: "/home/tibi/install/syslog-ng/etc/syslog-ng.conf"
  52. # The syslog-ng and syslog-ng-ctl binaries are here. You needn't use
  53. # this method if these binaries can be found in a directory in your PATH.
  54. set_bin_path:
  55. module.run:
  56. - name: syslog_ng.set_binary_path
  57. - m_name: "/home/tibi/install/syslog-ng/sbin"
  58. # Writes the first lines into the config file, also erases its previous
  59. # content
  60. write_version:
  61. module.run:
  62. - name: syslog_ng.write_version
  63. - m_name: "3.6"
  64. # There is a shorter form to set the above variables
  65. set_variables:
  66. module.run:
  67. - name: syslog_ng.set_parameters
  68. - version: "3.6"
  69. - binary_path: "/home/tibi/install/syslog-ng/sbin"
  70. - config_file: "/home/tibi/install/syslog-ng/etc/syslog-ng.conf"
  71. # Some global options
  72. options.global_options:
  73. syslog_ng.config:
  74. - config:
  75. - time_reap: 30
  76. - mark_freq: 10
  77. - keep_hostname: "yes"
  78. source.s_localhost:
  79. syslog_ng.config:
  80. - config:
  81. - tcp:
  82. - ip: "127.0.0.1"
  83. - port: 1233
  84. destination.d_log_server:
  85. syslog_ng.config:
  86. - config:
  87. - tcp:
  88. - "127.0.0.1"
  89. - port: 1234
  90. log.l_log_to_central_server:
  91. syslog_ng.config:
  92. - config:
  93. - source: s_localhost
  94. - destination: d_log_server
  95. some_comment:
  96. module.run:
  97. - name: syslog_ng.write_config
  98. - config: |
  99. # Multi line
  100. # comment
  101. # Another mode to use comments or existing configuration snippets
  102. config.other_comment_form:
  103. syslog_ng.config:
  104. - config: |
  105. # Multi line
  106. # comment
  107. The :py:func:`syslog_ng.reloaded <salt.states.syslog_ng.reloaded>` function can generate syslog-ng configuration from YAML. If the statement (source, destination, parser,
  108. etc.) has a name, this function uses the id as the name, otherwise (log
  109. statement) it's purpose is like a mandatory comment.
  110. After execution this example the syslog\_ng state will generate this
  111. file:
  112. .. code-block:: text
  113. #Generated by Salt on 2014-08-18 00:11:11
  114. @version: 3.6
  115. options {
  116. time_reap(
  117. 30
  118. );
  119. mark_freq(
  120. 10
  121. );
  122. keep_hostname(
  123. yes
  124. );
  125. };
  126. source s_localhost {
  127. tcp(
  128. ip(
  129. 127.0.0.1
  130. ),
  131. port(
  132. 1233
  133. )
  134. );
  135. };
  136. destination d_log_server {
  137. tcp(
  138. 127.0.0.1,
  139. port(
  140. 1234
  141. )
  142. );
  143. };
  144. log {
  145. source(
  146. s_localhost
  147. );
  148. destination(
  149. d_log_server
  150. );
  151. };
  152. # Multi line
  153. # comment
  154. # Multi line
  155. # comment
  156. Users can include arbitrary texts in the generated configuration with
  157. using the ``config`` statement (see the example above).
  158. Syslog_ng module functions
  159. --------------------------
  160. You can use :py:func:`syslog_ng.set_binary_path <salt.modules.syslog_ng.set_binary_path>`
  161. to set the directory which contains the
  162. syslog-ng and syslog-ng-ctl binaries. If this directory is in your PATH,
  163. you don't need to use this function. There is also a :py:func:`syslog_ng.set_config_file <salt.modules.syslog_ng.set_config_file>`
  164. function to set the location of the configuration file.
  165. Examples
  166. --------
  167. Simple source
  168. ~~~~~~~~~~~~~
  169. .. code-block:: text
  170. source s_tail {
  171. file(
  172. "/var/log/apache/access.log",
  173. follow_freq(1),
  174. flags(no-parse, validate-utf8)
  175. );
  176. };
  177. .. code-block:: yaml
  178. s_tail:
  179. # Salt will call the source function of syslog_ng module
  180. syslog_ng.config:
  181. - config:
  182. source:
  183. - file:
  184. - file: ''"/var/log/apache/access.log"''
  185. - follow_freq : 1
  186. - flags:
  187. - no-parse
  188. - validate-utf8
  189. OR
  190. .. code-block:: yaml
  191. s_tail:
  192. syslog_ng.config:
  193. - config:
  194. source:
  195. - file:
  196. - ''"/var/log/apache/access.log"''
  197. - follow_freq : 1
  198. - flags:
  199. - no-parse
  200. - validate-utf8
  201. OR
  202. .. code-block:: yaml
  203. source.s_tail:
  204. syslog_ng.config:
  205. - config:
  206. - file:
  207. - ''"/var/log/apache/access.log"''
  208. - follow_freq : 1
  209. - flags:
  210. - no-parse
  211. - validate-utf8
  212. Complex source
  213. ~~~~~~~~~~~~~~
  214. .. code-block:: text
  215. source s_gsoc2014 {
  216. tcp(
  217. ip("0.0.0.0"),
  218. port(1234),
  219. flags(no-parse)
  220. );
  221. };
  222. .. code-block:: yaml
  223. s_gsoc2014:
  224. syslog_ng.config:
  225. - config:
  226. source:
  227. - tcp:
  228. - ip: 0.0.0.0
  229. - port: 1234
  230. - flags: no-parse
  231. Filter
  232. ~~~~~~
  233. .. code-block:: text
  234. filter f_json {
  235. match(
  236. "@json:"
  237. );
  238. };
  239. .. code-block:: yaml
  240. f_json:
  241. syslog_ng.config:
  242. - config:
  243. filter:
  244. - match:
  245. - ''"@json:"''
  246. Template
  247. ~~~~~~~~
  248. .. code-block:: text
  249. template t_demo_filetemplate {
  250. template(
  251. "$ISODATE $HOST $MSG "
  252. );
  253. template_escape(
  254. no
  255. );
  256. };
  257. .. code-block:: yaml
  258. t_demo_filetemplate:
  259. syslog_ng.config:
  260. -config:
  261. template:
  262. - template:
  263. - '"$ISODATE $HOST $MSG\n"'
  264. - template_escape:
  265. - "no"
  266. Rewrite
  267. ~~~~~~~
  268. .. code-block:: text
  269. rewrite r_set_message_to_MESSAGE {
  270. set(
  271. "${.json.message}",
  272. value("$MESSAGE")
  273. );
  274. };
  275. .. code-block:: yaml
  276. r_set_message_to_MESSAGE:
  277. syslog_ng.config:
  278. - config:
  279. rewrite:
  280. - set:
  281. - '"${.json.message}"'
  282. - value : '"$MESSAGE"'
  283. Global options
  284. ~~~~~~~~~~~~~~
  285. .. code-block:: text
  286. options {
  287. time_reap(30);
  288. mark_freq(10);
  289. keep_hostname(yes);
  290. };
  291. .. code-block:: yaml
  292. global_options:
  293. syslog_ng.config:
  294. - config:
  295. options:
  296. - time_reap: 30
  297. - mark_freq: 10
  298. - keep_hostname: "yes"
  299. Log
  300. ~~~
  301. .. code-block:: text
  302. log {
  303. source(s_gsoc2014);
  304. junction {
  305. channel {
  306. filter(f_json);
  307. parser(p_json);
  308. rewrite(r_set_json_tag);
  309. rewrite(r_set_message_to_MESSAGE);
  310. destination {
  311. file(
  312. "/tmp/json-input.log",
  313. template(t_gsoc2014)
  314. );
  315. };
  316. flags(final);
  317. };
  318. channel {
  319. filter(f_not_json);
  320. parser {
  321. syslog-parser(
  322. );
  323. };
  324. rewrite(r_set_syslog_tag);
  325. flags(final);
  326. };
  327. };
  328. destination {
  329. file(
  330. "/tmp/all.log",
  331. template(t_gsoc2014)
  332. );
  333. };
  334. };
  335. .. code-block:: yaml
  336. l_gsoc2014:
  337. syslog_ng.config:
  338. - config:
  339. log:
  340. - source: s_gsoc2014
  341. - junction:
  342. - channel:
  343. - filter: f_json
  344. - parser: p_json
  345. - rewrite: r_set_json_tag
  346. - rewrite: r_set_message_to_MESSAGE
  347. - destination:
  348. - file:
  349. - '"/tmp/json-input.log"'
  350. - template: t_gsoc2014
  351. - flags: final
  352. - channel:
  353. - filter: f_not_json
  354. - parser:
  355. - syslog-parser: []
  356. - rewrite: r_set_syslog_tag
  357. - flags: final
  358. - destination:
  359. - file:
  360. - "/tmp/all.log"
  361. - template: t_gsoc2014