init.sls 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. # Connect "master" and client zones
  74. /etc/icinga2/zones.conf:
  75. file.managed:
  76. - source:
  77. - salt://icinga2/zones.conf.H_{{ grains.id }}
  78. - salt://icinga2/zones.conf
  79. - template: jinja
  80. - require:
  81. - pkg: icinga2
  82. - watch_in:
  83. - service: icinga2
  84. # Install CA cert + host cert + key readable for icinga
  85. /var/lib/icinga2/certs:
  86. file.directory:
  87. - makedirs: True
  88. /var/lib/icinga2/certs/ca.crt:
  89. file.managed:
  90. - source: salt://certs/ffho-cacert.pem
  91. - user: nagios
  92. - group: nagios
  93. - mode: 644
  94. - require:
  95. - pkg: icinga2
  96. - file: /var/lib/icinga2/certs
  97. - watch_in:
  98. - sevice: icinga2
  99. {% set pillar_name = 'nodes:' ~ grains['id'] ~ ':certs:' ~ grains['id'] %}
  100. /var/lib/icinga2/certs/{{ grains['id'] }}.crt:
  101. file.managed:
  102. - contents_pillar: {{ pillar_name }}:cert
  103. - user: nagios
  104. - group: nagios
  105. - mode: 644
  106. - require:
  107. - pkg: icinga2
  108. - file: /var/lib/icinga2/certs
  109. - watch_in:
  110. - service: icinga2
  111. /var/lib/icinga2/certs/{{ grains['id'] }}.key:
  112. file.managed:
  113. - contents_pillar: {{ pillar_name }}:privkey
  114. - user: nagios
  115. - group: nagios
  116. - mode: 440
  117. - require:
  118. - pkg: icinga2
  119. - file: /var/lib/icinga2/certs
  120. - watch_in:
  121. - service: icinga2
  122. # Activate Icinga2 features: API
  123. {% for feature in ['api'] %}
  124. /etc/icinga2/features-enabled/{{ feature }}.conf:
  125. file.symlink:
  126. - target: "../features-available/{{ feature }}.conf"
  127. - user: nagios
  128. - group: nagios
  129. - require:
  130. - pkg: icinga2
  131. - watch_in:
  132. - service: icinga2
  133. {% endfor %}
  134. # Install command definitions
  135. /etc/icinga2/commands.d:
  136. file.recurse:
  137. - source: salt://icinga2/commands.d
  138. - template: jinja
  139. - file_mode: 644
  140. - dir_mode: 755
  141. - user: root
  142. - group: root
  143. - clean: true
  144. - require:
  145. - pkg: icinga2
  146. - watch_in:
  147. - service: icinga2
  148. # Create directory for ffho specific configs
  149. /etc/icinga2/ffho-conf.d:
  150. file.directory:
  151. - makedirs: true
  152. - require:
  153. - pkg: icinga2
  154. ################################################################################
  155. # Icinga2 Server #
  156. ################################################################################
  157. {% if 'icinga2server' in roles %}
  158. # Link ffho-conf.d as master zone
  159. /etc/icinga2/zones.d/master:
  160. file.symlink:
  161. - target: "/etc/icinga2/ffho-conf.d/"
  162. - require:
  163. - pkg: icinga2
  164. - watch_in:
  165. - service: icinga2
  166. # Users and Notifications
  167. /etc/icinga2/ffho-conf.d/users.conf:
  168. file.managed:
  169. - source: salt://icinga2/users.conf.tmpl
  170. - template: jinja
  171. - require:
  172. - pkg: icinga2
  173. - watch_in:
  174. - service: icinga2
  175. /etc/icinga2/ffho-conf.d/notifications.conf:
  176. file.managed:
  177. - source: salt://icinga2/notifications.conf.tmpl
  178. - template: jinja
  179. - require:
  180. - pkg: icinga2
  181. - watch_in:
  182. - service: icinga2
  183. # Install command definitions
  184. /etc/icinga2/ffho-conf.d/services:
  185. file.recurse:
  186. - source: salt://icinga2/services
  187. - file_mode: 644
  188. - dir_mode: 755
  189. - user: root
  190. - group: root
  191. - clean: true
  192. - template: jinja
  193. - require:
  194. - pkg: icinga2
  195. - watch_in:
  196. - service: icinga2
  197. # Create client node/zone objects
  198. Create /etc/icinga2/ffho-conf.d/hosts/generated/:
  199. file.directory:
  200. - name: /etc/icinga2/ffho-conf.d/hosts/generated/
  201. - makedirs: true
  202. - require:
  203. - pkg: icinga2
  204. Cleanup /etc/icinga2/ffho-conf.d/hosts/generated/:
  205. file.directory:
  206. - name: /etc/icinga2/ffho-conf.d/hosts/generated/
  207. - clean: true
  208. - watch_in:
  209. - service: icinga2
  210. # Generate config file for every client known to pillar
  211. {% for node_id, node_config in salt['pillar.get']('nodes', {}).items () %}
  212. {# Only monitor hosts which are active or staged. #}
  213. {% if node_config.get ('status', '') not in [ '', 'active', 'staged' ] %}
  214. {% continue %}
  215. {% endif %}
  216. /etc/icinga2/ffho-conf.d/hosts/generated/{{ node_id }}.conf:
  217. file.managed:
  218. - source: salt://icinga2/host.conf.tmpl
  219. - template: jinja
  220. - context:
  221. node_id: {{ node_id }}
  222. node_config: {{ node_config }}
  223. - require:
  224. - file: Create /etc/icinga2/ffho-conf.d/hosts/generated/
  225. - require_in:
  226. - file: Cleanup /etc/icinga2/ffho-conf.d/hosts/generated/
  227. - watch_in:
  228. - service: icinga2
  229. {% endfor %}
  230. # Create configuration for network devices
  231. Create /etc/icinga2/ffho-conf.d/net/wbbl/:
  232. file.directory:
  233. - name: /etc/icinga2/ffho-conf.d/net/wbbl/
  234. - makedirs: true
  235. - require:
  236. - pkg: icinga2
  237. Cleanup /etc/icinga2/ffho-conf.d/net/wbbl/:
  238. file.directory:
  239. - name: /etc/icinga2/ffho-conf.d/net/wbbl/
  240. - clean: true
  241. - watch_in:
  242. - service: icinga2
  243. # Generate config files for every WBBL device known to pillar
  244. {% for link_id, link_config in salt['pillar.get']('net:wbbl', {}).items () %}
  245. /etc/icinga2/ffho-conf.d/net/wbbl/{{ link_id }}.conf:
  246. file.managed:
  247. - source: salt://icinga2/wbbl.conf.tmpl
  248. - template: jinja
  249. - context:
  250. link_id: {{ link_id }}
  251. link_config: {{ link_config }}
  252. - require:
  253. - file: Create /etc/icinga2/ffho-conf.d/net/wbbl/
  254. - require_in:
  255. - file: Cleanup /etc/icinga2/ffho-conf.d/net/wbbl/
  256. - watch_in:
  257. - service: icinga2
  258. {% endfor %}
  259. ################################################################################
  260. # Icinga2 Client #
  261. ################################################################################
  262. {% else %}
  263. # Nodes should accept config and commands from Icinga2 server
  264. /etc/icinga2/features-available/api.conf:
  265. file.managed:
  266. - source: salt://icinga2/api.conf
  267. - require:
  268. - pkg: icinga2
  269. - watch_in:
  270. - service: icinga2
  271. # Client should not notify by themselves
  272. /etc/icinga2/features-enabled/notification.conf:
  273. file.absent:
  274. - require:
  275. - pkg: icinga2
  276. - watch_in:
  277. - service: icinga2
  278. {% endif %}
  279. ################################################################################
  280. # Check related stuff #
  281. ################################################################################
  282. salt-cron-state-apply:
  283. cron.present:
  284. - identifier: SALT_CRON_STATE_APPLY
  285. - 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"
  286. - user: root
  287. - minute: random
  288. - hour: "*/6"