integration_icinga_test.rb 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class IntegrationIcingaTest < ActiveSupport::TestCase
  4. # according
  5. # https://github.com/Icinga/icinga2/blob/master/etc/icinga2/scripts/mail-service-notification.sh
  6. # http://docs.icinga.org/icinga2/latest/doc/module/icinga2/chapter/monitoring-basics#host-states
  7. # http://docs.icinga.org/icinga2/latest/doc/module/icinga2/chapter/monitoring-basics#service-states
  8. test 'base tests' do
  9. Setting.set('icinga_integration', true)
  10. # not matching sender
  11. email_raw_string = "To: support@example.com
  12. Subject: PROBLEM - host.internal.loc - CPU Load is WARNING
  13. User-Agent: Heirloom mailx 12.5 7/5/10
  14. MIME-Version: 1.0
  15. Content-Type: text/plain; charset=us-ascii
  16. Content-Transfer-Encoding: quoted-printable
  17. Message-Id: <20160131094621.29ECD400F29C-icinga-1-0@monitoring.znuny.com>
  18. From: icinga_not_matching@monitoring.example.com (icinga)
  19. ***** Icinga *****
  20. Notification Type: PROBLEM
  21. Service: CPU Load
  22. Host: host.internal.loc
  23. Address:=20
  24. State: WARNING
  25. Date/Time: 2016-01-31 10:46:20 +0100
  26. Additional Info: WARNING - load average: 3.44, 0.99, 0.35
  27. Comment: [] =
  28. "
  29. ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  30. assert_equal('new', ticket_p.state.name)
  31. assert(ticket_p.preferences)
  32. assert_not(ticket_p.preferences['integration'])
  33. assert_not(ticket_p.preferences['icinga'])
  34. # RBL check
  35. email_raw_string = "To: support@example.com
  36. Subject: [PROBLEM] RBL check on apn4711.dc.example.com is CRITICAL!
  37. User-Agent: Heirloom mailx 12.5 7/5/10
  38. MIME-Version: 1.0
  39. Content-Type: text/plain; charset=us-ascii
  40. Content-Transfer-Encoding: quoted-printable
  41. Message-Id: <20160131094621.29ECD400F29C-icinga-1-1@monitoring.znuny.com>
  42. From: icinga@monitoring.example.com (icinga)
  43. ***** Icinga 2 Service Monitoring on apn4711.dc.example.com *****
  44. =3D=3D> RBL check on apn4711.dc.example.com is CRITICAL! <=3D=3D
  45. Info: CHECK_RBL CRITICAL - apn4711.dc.example.com BLACKLISTED on 1 server of=
  46. 38 (ix.dnsbl.example.com)=20
  47. When: 2017-08-06 22:18:43 +0200
  48. Service: RBL check (Display Name: \"RBL check\")
  49. Host: apn4711.dc.example.com (Display Name: \"apn4711.dc.example.com\")
  50. IPv4: 127.0.0.1="
  51. ticket_0, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  52. assert_equal('new', ticket_0.state.name)
  53. assert(ticket_0.preferences)
  54. assert(ticket_0.preferences['integration'])
  55. assert_equal('icinga', ticket_0.preferences['integration'])
  56. assert(ticket_0.preferences['icinga'])
  57. assert_equal('apn4711.dc.example.com (Display Name: "apn4711.dc.example.com")', ticket_0.preferences['icinga']['host'])
  58. assert_equal('CHECK_RBL CRITICAL - apn4711.dc.example.com BLACKLISTED on 1 server of 38 (ix.dnsbl.example.com)', ticket_0.preferences['icinga']['info'])
  59. assert_equal('RBL check (Display Name: "RBL check")', ticket_0.preferences['icinga']['service'])
  60. assert_equal('CRITICAL', ticket_0.preferences['icinga']['state'])
  61. # RBL check II
  62. email_raw_string = "To: support@example.com
  63. Subject: [PROBLEM] RBL check on apn4711.dc.example.com is CRITICAL!
  64. User-Agent: Heirloom mailx 12.5 7/5/10
  65. MIME-Version: 1.0
  66. Content-Type: text/plain; charset=us-ascii
  67. Content-Transfer-Encoding: quoted-printable
  68. Message-Id: <20160131094621.29ECD400F29C-icinga-1-2@monitoring.znuny.com>
  69. From: icinga@monitoring.example.com (icinga)
  70. ***** Icinga 2 Service Monitoring on apn4711.dc.example.com *****
  71. =3D=3D> RBL check on apn4711.dc.example.com is CRITICAL! <=3D=3D
  72. Info: CHECK_RBL CRITICAL - apn4711.dc.example.com BLACKLISTED on 1 server of=
  73. 38 (ix.dnsbl.example.com)=20
  74. When: 2017-08-06 22:18:43 +0200
  75. Service: RBL check (Display Name: \"RBL check\")
  76. Host: apn4711.dc.example.com (Display Name: \"apn4711.dc.example.com\")
  77. IPv4: 127.0.0.1="
  78. ticket_0_1, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  79. assert_equal('new', ticket_0_1.state.name)
  80. assert(ticket_0_1.preferences)
  81. assert(ticket_0_1.preferences['integration'])
  82. assert_equal('icinga', ticket_0_1.preferences['integration'])
  83. assert(ticket_0_1.preferences['icinga'])
  84. assert_equal('apn4711.dc.example.com (Display Name: "apn4711.dc.example.com")', ticket_0_1.preferences['icinga']['host'])
  85. assert_equal('CHECK_RBL CRITICAL - apn4711.dc.example.com BLACKLISTED on 1 server of 38 (ix.dnsbl.example.com)', ticket_0_1.preferences['icinga']['info'])
  86. assert_equal('RBL check (Display Name: "RBL check")', ticket_0_1.preferences['icinga']['service'])
  87. assert_equal('CRITICAL', ticket_0_1.preferences['icinga']['state'])
  88. assert_equal(ticket_0_1.id, ticket_0.id)
  89. email_raw_string = "To: support@example.com
  90. Subject: [PROBLEM] RBL check on apn4711.dc.example.com is OK!
  91. User-Agent: Heirloom mailx 12.5 7/5/10
  92. MIME-Version: 1.0
  93. Content-Type: text/plain; charset=us-ascii
  94. Content-Transfer-Encoding: quoted-printable
  95. Message-Id: <20160131094621.29ECD400F29C-icinga-1-2@monitoring.znuny.com>
  96. From: icinga@monitoring.example.com (icinga)
  97. ***** Icinga 2 Service Monitoring on apn4711.dc.example.com *****
  98. =3D=3D> RBL check on apn4711.dc.example.com is OK! <=3D=3D
  99. Info: CHECK_RBL OK - apn4711.dc.example.com BLACKLISTED on 1 server of=
  100. 38 (ix.dnsbl.example.com)=20
  101. When: 2017-08-06 22:18:43 +0200
  102. Service: RBL check (Display Name: \"RBL check\")
  103. Host: apn4711.dc.example.com (Display Name: \"apn4711.dc.example.com\")
  104. IPv4: 127.0.0.1="
  105. ticket_0_2, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  106. assert_equal('closed', ticket_0_2.state.name)
  107. assert(ticket_0_2.preferences)
  108. assert(ticket_0_2.preferences['integration'])
  109. assert_equal('icinga', ticket_0_2.preferences['integration'])
  110. assert(ticket_0_2.preferences['icinga'])
  111. assert_equal('apn4711.dc.example.com (Display Name: "apn4711.dc.example.com")', ticket_0_2.preferences['icinga']['host'])
  112. assert_equal('CHECK_RBL CRITICAL - apn4711.dc.example.com BLACKLISTED on 1 server of 38 (ix.dnsbl.example.com)', ticket_0_2.preferences['icinga']['info'])
  113. assert_equal('RBL check (Display Name: "RBL check")', ticket_0_2.preferences['icinga']['service'])
  114. assert_equal('CRITICAL', ticket_0_2.preferences['icinga']['state'])
  115. assert_equal(ticket_0_2.id, ticket_0.id)
  116. # matching sender - CPU Load/host.internal.loc
  117. email_raw_string = "To: support@example.com
  118. Subject: PROBLEM - host.internal.loc - CPU Load is WARNING
  119. User-Agent: Heirloom mailx 12.5 7/5/10
  120. MIME-Version: 1.0
  121. Content-Type: text/plain; charset=us-ascii
  122. Content-Transfer-Encoding: quoted-printable
  123. Message-Id: <20160131094621.29ECD400F29C-icinga-2@monitoring.znuny.com>
  124. From: icinga@monitoring.example.com (icinga)
  125. ***** Icinga *****
  126. Notification Type: PROBLEM
  127. Service: CPU Load
  128. Host: host.internal.loc
  129. Address:=20
  130. State: WARNING
  131. Date/Time: 2016-01-31 10:46:20 +0100
  132. Additional Info: WARNING - load average: 3.44, 0.99, 0.35
  133. Comment: [] =
  134. "
  135. ticket_1, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  136. assert_equal('new', ticket_1.state.name)
  137. assert(ticket_1.preferences)
  138. assert(ticket_1.preferences['integration'])
  139. assert_equal('icinga', ticket_1.preferences['integration'])
  140. assert(ticket_1.preferences['icinga'])
  141. assert_equal('host.internal.loc', ticket_1.preferences['icinga']['host'])
  142. assert_equal('CPU Load', ticket_1.preferences['icinga']['service'])
  143. assert_equal('WARNING', ticket_1.preferences['icinga']['state'])
  144. # matching sender - Disk Usage 123/host.internal.loc
  145. email_raw_string = "To: support@example.com
  146. Subject: PROBLEM - host.internal.loc - Disk Usage 123 is WARNING
  147. User-Agent: Heirloom mailx 12.5 7/5/10
  148. MIME-Version: 1.0
  149. Content-Type: text/plain; charset=us-ascii
  150. Content-Transfer-Encoding: quoted-printable
  151. Message-Id: <20160131094621.29ECD400F29C-icinga-3@monitoring.znuny.com>
  152. From: icinga@monitoring.example.com (icinga)
  153. ***** Icinga *****
  154. Notification Type: PROBLEM
  155. Service: Disk Usage 123
  156. Host: host.internal.loc
  157. Address:=20
  158. State: WARNING
  159. Date/Time: 2016-01-31 10:46:20 +0100
  160. Additional Info: WARNING - load average: 3.44, 0.99, 0.35
  161. Comment: [] =
  162. "
  163. ticket_2, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  164. assert_equal('new', ticket_2.state.name)
  165. assert(ticket_2.preferences)
  166. assert(ticket_2.preferences['integration'])
  167. assert_equal('icinga', ticket_2.preferences['integration'])
  168. assert(ticket_2.preferences['icinga'])
  169. assert_equal('host.internal.loc', ticket_2.preferences['icinga']['host'])
  170. assert_equal('Disk Usage 123', ticket_2.preferences['icinga']['service'])
  171. assert_equal('WARNING', ticket_2.preferences['icinga']['state'])
  172. assert_not_equal(ticket_2.id, ticket_1.id)
  173. # matching sender - follow up - CPU Load/host.internal.loc
  174. email_raw_string = "To: support@example.com
  175. Subject: PROBLEM - host.internal.loc - CPU Load is WARNING
  176. User-Agent: Heirloom mailx 12.5 7/5/10
  177. MIME-Version: 1.0
  178. Content-Type: text/plain; charset=us-ascii
  179. Content-Transfer-Encoding: quoted-printable
  180. Message-Id: <20160131094621.29ECD400F29C-icinga-4@monitoring.znuny.com>
  181. From: icinga@monitoring.example.com (icinga)
  182. ***** Icinga *****
  183. Notification Type: PROBLEM
  184. Service: CPU Load
  185. Host: host.internal.loc
  186. Address:=20
  187. State: WARNING
  188. Date/Time: 2016-01-31 10:46:20 +0100
  189. Additional Info: WARNING - load average: 3.44, 0.99, 0.35
  190. Comment: [] =
  191. "
  192. ticket_1_1, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  193. assert_equal('new', ticket_1_1.state.name)
  194. assert(ticket_1_1.preferences)
  195. assert(ticket_1_1.preferences['integration'])
  196. assert_equal('icinga', ticket_1_1.preferences['integration'])
  197. assert(ticket_1_1.preferences['icinga'])
  198. assert_equal('host.internal.loc', ticket_1_1.preferences['icinga']['host'])
  199. assert_equal('CPU Load', ticket_1_1.preferences['icinga']['service'])
  200. assert_equal('WARNING', ticket_1_1.preferences['icinga']['state'])
  201. assert_equal(ticket_1.id, ticket_1_1.id)
  202. # matching sender - follow up - recovery - CPU Load/host.internal.loc
  203. email_raw_string = "To: support@example.com
  204. Subject: PROBLEM - host.internal.loc - CPU Load is WARNING
  205. User-Agent: Heirloom mailx 12.5 7/5/10
  206. MIME-Version: 1.0
  207. Content-Type: text/plain; charset=us-ascii
  208. Content-Transfer-Encoding: quoted-printable
  209. Message-Id: <20160131094621.29ECD400F29C-icinga-5@monitoring.znuny.com>
  210. From: icinga@monitoring.example.com (icinga)
  211. ***** Icinga *****
  212. Notification Type: RECOVERY
  213. Service: CPU Load
  214. Host: host.internal.loc
  215. Address:=20
  216. State: OK
  217. Date/Time: 2016-01-31 10:48:02 +0100
  218. Additional Info: OK - load average: 1.62, 1.17, 0.49
  219. Comment: [] =
  220. "
  221. ticket_1_2, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  222. assert_equal(ticket_1.id, ticket_1_2.id)
  223. assert_equal('closed', ticket_1_2.state.name)
  224. assert(ticket_1_2.preferences)
  225. assert(ticket_1_2.preferences['integration'])
  226. assert_equal('icinga', ticket_1_2.preferences['integration'])
  227. assert(ticket_1_2.preferences['icinga'])
  228. assert_equal('host.internal.loc', ticket_1_2.preferences['icinga']['host'])
  229. assert_equal('CPU Load', ticket_1_2.preferences['icinga']['service'])
  230. assert_equal('WARNING', ticket_1_2.preferences['icinga']['state'])
  231. # host down
  232. email_raw_string = "To: support@example.com
  233. Subject: PROBLEM - apn4711.dc.example.com is DOWN
  234. User-Agent: Heirloom mailx 12.5 7/5/10
  235. MIME-Version: 1.0
  236. Content-Type: text/plain; charset=us-ascii
  237. Content-Transfer-Encoding: quoted-printable
  238. Message-Id: <20160131094621.29ECD400F29C-icinga-5@monitoring.znuny.com>
  239. From: icinga@monitoring.example.com (icinga)
  240. ***** Icinga *****
  241. Notification Type: PROBLEM
  242. Host: apn4711.dc.example.com
  243. Address: 127.0.0.1
  244. State: DOWN
  245. Date/Time: 2017-01-14 11:33:02 +0100
  246. Additional Info: CRITICAL - Host Unreachable (127.0.0.1)
  247. Comment: [] =
  248. "
  249. ticket_3, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  250. assert_equal('new', ticket_3.state.name)
  251. assert(ticket_3.preferences)
  252. assert(ticket_3.preferences['integration'])
  253. assert_equal('icinga', ticket_3.preferences['integration'])
  254. assert(ticket_3.preferences['icinga'])
  255. assert_equal('apn4711.dc.example.com', ticket_3.preferences['icinga']['host'])
  256. assert_nil(ticket_3.preferences['icinga']['service'])
  257. assert_equal('DOWN', ticket_3.preferences['icinga']['state'])
  258. assert_not_equal(ticket_3.id, ticket_1.id)
  259. # host up
  260. email_raw_string = "To: support@example.com
  261. Subject: RECOVERY - apn4711.dc.example.com is UP
  262. User-Agent: Heirloom mailx 12.5 7/5/10
  263. MIME-Version: 1.0
  264. Content-Type: text/plain; charset=us-ascii
  265. Content-Transfer-Encoding: quoted-printable
  266. Message-Id: <20160131094621.29ECD400F29C-icinga-5@monitoring.znuny.com>
  267. From: icinga@monitoring.example.com (icinga)
  268. ***** Icinga *****
  269. Notification Type: RECOVERY
  270. Host: apn4711.dc.example.com
  271. Address: 127.0.0.1
  272. State: UP
  273. Date/Time: 2017-01-14 12:07:11 +0100
  274. Additional Info: PING OK - Packet loss = 0%, RTA = 21.37 ms
  275. Comment: [] =
  276. "
  277. ticket_3_1, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  278. assert_equal(ticket_3.id, ticket_3_1.id)
  279. assert_equal('closed', ticket_3_1.state.name)
  280. assert(ticket_3_1.preferences)
  281. assert(ticket_3_1.preferences['integration'])
  282. assert_equal('icinga', ticket_3_1.preferences['integration'])
  283. assert(ticket_3_1.preferences['icinga'])
  284. assert_equal('apn4711.dc.example.com', ticket_3.preferences['icinga']['host'])
  285. assert_nil(ticket_3_1.preferences['icinga']['service'])
  286. assert_equal('DOWN', ticket_3_1.preferences['icinga']['state'])
  287. #Setting.set('icinga_integration', false)
  288. end
  289. end