init.sls 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. #
  2. # Icinga2
  3. #
  4. {% set roles = salt['pillar.get']('nodes:' ~ grains.id ~ ':roles', []) %}
  5. include:
  6. - apt
  7. - sudo
  8. - needrestart
  9. /etc/apt/trusted.gpg.d/icinga.gpg:
  10. file.managed:
  11. - source: salt://icinga2/icinga.gpg
  12. /etc/apt/sources.list.d/icinga.list:
  13. file.managed:
  14. - source: salt://icinga2/icinga.list.tmpl
  15. - template: jinja
  16. - require:
  17. - file: /etc/apt/trusted.gpg.d/icinga.gpg
  18. # Install icinga2 package
  19. icinga2:
  20. pkg.installed:
  21. - name: icinga2
  22. - require:
  23. - file: /etc/apt/sources.list.d/icinga.list
  24. service.running:
  25. - enable: True
  26. - reload: True
  27. # Install plugins (official + our own)
  28. monitoring-plugin-pkgs:
  29. pkg.installed:
  30. - pkgs:
  31. - monitoring-plugins
  32. - nagios-plugins-contrib
  33. - libyaml-syck-perl
  34. - libmonitoring-plugin-perl
  35. - curl
  36. - lsof
  37. - python3-dnspython
  38. - python3-tz
  39. - watch_in:
  40. - service: icinga2
  41. ffho-plugins:
  42. file.recurse:
  43. - name: /usr/local/share/monitoring-plugins/
  44. - source: salt://icinga2/plugins/
  45. - file_mode: 755
  46. - dir_mode: 755
  47. - user: root
  48. - group: root
  49. # Install sudoers file for Icinga2 checks
  50. /etc/sudoers.d/icinga2:
  51. file.managed:
  52. - source: salt://icinga2/icinga2.sudoers
  53. - mode: 0440
  54. # Icinga2 master config (for master and all nodes)
  55. /etc/icinga2/icinga2.conf:
  56. file.managed:
  57. - source:
  58. - salt://icinga2/icinga2.conf.H_{{ grains.id }}
  59. - salt://icinga2/icinga2.conf.{{ grains.os }}.{{ grains.oscodename }}
  60. - salt://icinga2/icinga2.conf
  61. - require:
  62. - pkg: icinga2
  63. - watch_in:
  64. - service: icinga2
  65. # Add FFHOPluginDir
  66. /etc/icinga2/constants.conf:
  67. file.managed:
  68. - source: salt://icinga2/constants.conf
  69. - require:
  70. - pkg: icinga2
  71. - watch_in:
  72. - service: icinga2
  73. /etc/icinga2/secrets.conf:
  74. file.managed:
  75. - source: salt://icinga2/secrets.conf.tmpl
  76. - template: jinja
  77. - mode: 600
  78. - require:
  79. - pkg: icinga2
  80. - watch_in:
  81. - service: icinga2
  82. # Connect "master" and client zones
  83. /etc/icinga2/zones.conf:
  84. file.managed:
  85. - source:
  86. - salt://icinga2/zones.conf.H_{{ grains.id }}
  87. - salt://icinga2/zones.conf
  88. - template: jinja
  89. - require:
  90. - pkg: icinga2
  91. - watch_in:
  92. - service: icinga2
  93. # Install CA cert + host cert + key readable for icinga
  94. /var/lib/icinga2/certs:
  95. file.directory:
  96. - makedirs: True
  97. /var/lib/icinga2/certs/ca.crt:
  98. file.managed:
  99. - source: salt://certs/ffho-cacert.pem
  100. - user: nagios
  101. - group: nagios
  102. - mode: 644
  103. - require:
  104. - pkg: icinga2
  105. - file: /var/lib/icinga2/certs
  106. - watch_in:
  107. - sevice: icinga2
  108. {% set pillar_name = 'nodes:' ~ grains['id'] ~ ':certs:' ~ grains['id'] %}
  109. /var/lib/icinga2/certs/{{ grains['id'] }}.crt:
  110. file.managed:
  111. - contents_pillar: {{ pillar_name }}:cert
  112. - user: nagios
  113. - group: nagios
  114. - mode: 644
  115. - require:
  116. - pkg: icinga2
  117. - file: /var/lib/icinga2/certs
  118. - watch_in:
  119. - service: icinga2
  120. /var/lib/icinga2/certs/{{ grains['id'] }}.key:
  121. file.managed:
  122. - contents_pillar: {{ pillar_name }}:privkey
  123. - user: nagios
  124. - group: nagios
  125. - mode: 440
  126. - require:
  127. - pkg: icinga2
  128. - file: /var/lib/icinga2/certs
  129. - watch_in:
  130. - service: icinga2
  131. # Activate Icinga2 features: API
  132. {% for feature in ['api'] %}
  133. /etc/icinga2/features-enabled/{{ feature }}.conf:
  134. file.symlink:
  135. - target: "../features-available/{{ feature }}.conf"
  136. - user: nagios
  137. - group: nagios
  138. - require:
  139. - pkg: icinga2
  140. - watch_in:
  141. - service: icinga2
  142. {% endfor %}
  143. # Install command definitions
  144. /etc/icinga2/commands.d:
  145. file.recurse:
  146. - source: salt://icinga2/commands.d
  147. - template: jinja
  148. - file_mode: 644
  149. - dir_mode: 755
  150. - user: root
  151. - group: root
  152. - clean: true
  153. - require:
  154. - pkg: icinga2
  155. - watch_in:
  156. - service: icinga2
  157. # Create directory for ffho specific configs
  158. /etc/icinga2/ffho-conf.d:
  159. file.directory:
  160. - makedirs: true
  161. - require:
  162. - pkg: icinga2
  163. ################################################################################
  164. # Icinga2 Server #
  165. ################################################################################
  166. {% if 'icinga2server' in roles %}
  167. # Link ffho-conf.d as master zone
  168. /etc/icinga2/zones.d/master:
  169. file.symlink:
  170. - target: "/etc/icinga2/ffho-conf.d/"
  171. - require:
  172. - pkg: icinga2
  173. - watch_in:
  174. - service: icinga2
  175. # Users and Notifications
  176. /etc/icinga2/ffho-conf.d/users.conf:
  177. file.managed:
  178. - source: salt://icinga2/users.conf.tmpl
  179. - template: jinja
  180. - require:
  181. - pkg: icinga2
  182. - watch_in:
  183. - service: icinga2
  184. /etc/icinga2/ffho-conf.d/notifications.conf:
  185. file.managed:
  186. - source: salt://icinga2/notifications.conf.tmpl
  187. - template: jinja
  188. - require:
  189. - pkg: icinga2
  190. - watch_in:
  191. - service: icinga2
  192. # Install command definitions
  193. /etc/icinga2/ffho-conf.d/services:
  194. file.recurse:
  195. - source: salt://icinga2/services
  196. - file_mode: 644
  197. - dir_mode: 755
  198. - user: root
  199. - group: root
  200. - clean: true
  201. - template: jinja
  202. - require:
  203. - pkg: icinga2
  204. - watch_in:
  205. - service: icinga2
  206. # Create client node/zone objects
  207. Create /etc/icinga2/ffho-conf.d/hosts/generated/:
  208. file.directory:
  209. - name: /etc/icinga2/ffho-conf.d/hosts/generated/
  210. - makedirs: true
  211. - require:
  212. - pkg: icinga2
  213. Cleanup /etc/icinga2/ffho-conf.d/hosts/generated/:
  214. file.directory:
  215. - name: /etc/icinga2/ffho-conf.d/hosts/generated/
  216. - clean: true
  217. - watch_in:
  218. - service: icinga2
  219. # Generate config file for every client known to pillar
  220. {% for node_id, node_config in salt['pillar.get']('nodes', {}).items () %}
  221. {# Only monitor hosts which are active or staged. #}
  222. {% if node_config.get ('status', '') not in [ '', 'active', 'staged' ] %}
  223. {% continue %}
  224. {% endif %}
  225. /etc/icinga2/ffho-conf.d/hosts/generated/{{ node_id }}.conf:
  226. file.managed:
  227. - source: salt://icinga2/host.conf.tmpl
  228. - template: jinja
  229. - context:
  230. node_id: {{ node_id }}
  231. node_config: {{ node_config }}
  232. - require:
  233. - file: Create /etc/icinga2/ffho-conf.d/hosts/generated/
  234. - require_in:
  235. - file: Cleanup /etc/icinga2/ffho-conf.d/hosts/generated/
  236. - watch_in:
  237. - service: icinga2
  238. {% endfor %}
  239. # Create configuration for network devices
  240. Create /etc/icinga2/ffho-conf.d/net/wbbl/:
  241. file.directory:
  242. - name: /etc/icinga2/ffho-conf.d/net/wbbl/
  243. - makedirs: true
  244. - require:
  245. - pkg: icinga2
  246. Cleanup /etc/icinga2/ffho-conf.d/net/wbbl/:
  247. file.directory:
  248. - name: /etc/icinga2/ffho-conf.d/net/wbbl/
  249. - clean: true
  250. - watch_in:
  251. - service: icinga2
  252. # Generate config files for every WBBL device known to pillar
  253. {% for link_id, link_config in salt['pillar.get']('net:wbbl', {}).items () %}
  254. /etc/icinga2/ffho-conf.d/net/wbbl/{{ link_id }}.conf:
  255. file.managed:
  256. - source: salt://icinga2/wbbl.conf.tmpl
  257. - template: jinja
  258. - context:
  259. link_id: {{ link_id }}
  260. link_config: {{ link_config }}
  261. - require:
  262. - file: Create /etc/icinga2/ffho-conf.d/net/wbbl/
  263. - require_in:
  264. - file: Cleanup /etc/icinga2/ffho-conf.d/net/wbbl/
  265. - watch_in:
  266. - service: icinga2
  267. {% endfor %}
  268. ################################################################################
  269. # Icinga2 Client #
  270. ################################################################################
  271. {% else %}
  272. # Nodes should accept config and commands from Icinga2 server
  273. /etc/icinga2/features-available/api.conf:
  274. file.managed:
  275. - source: salt://icinga2/api.conf
  276. - require:
  277. - pkg: icinga2
  278. - watch_in:
  279. - service: icinga2
  280. # Client should not notify by themselves
  281. /etc/icinga2/features-enabled/notification.conf:
  282. file.absent:
  283. - require:
  284. - pkg: icinga2
  285. - watch_in:
  286. - service: icinga2
  287. {% endif %}
  288. ################################################################################
  289. # Check related stuff #
  290. ################################################################################
  291. salt-cron-state-apply:
  292. cron.present:
  293. - identifier: SALT_CRON_STATE_APPLY
  294. - name: "/usr/bin/salt-call state.highstate --state-verbose=False test=True > /var/cache/salt/state_apply.tmp 2>/dev/null ; mv /var/cache/salt/state_apply.tmp /var/cache/salt/state_apply"
  295. - user: root
  296. - minute: random
  297. - hour: "*/6"