integration_icinga_test.rb 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. require 'test_helper'
  2. class IntegrationIcingaTest < ActiveSupport::TestCase
  3. # according
  4. # https://github.com/Icinga/icinga2/blob/master/etc/icinga2/scripts/mail-service-notification.sh
  5. # http://docs.icinga.org/icinga2/latest/doc/module/icinga2/chapter/monitoring-basics#host-states
  6. # http://docs.icinga.org/icinga2/latest/doc/module/icinga2/chapter/monitoring-basics#service-states
  7. setup do
  8. Setting.set('icinga_integration', true)
  9. Setting.set('icinga_sender', 'icinga2@monitoring.example.com')
  10. end
  11. test 'base tests' do
  12. # RBL check
  13. email_raw_string = "To: support@example.com
  14. Subject: [PROBLEM] RBL check on apn4711.dc.example.com is CRITICAL!
  15. User-Agent: Heirloom mailx 12.5 7/5/10
  16. MIME-Version: 1.0
  17. Content-Type: text/plain; charset=us-ascii
  18. Content-Transfer-Encoding: quoted-printable
  19. Message-Id: <20160131094621.29ECD400F29C-icinga-1-1@monitoring.znuny.com>
  20. From: icinga2@monitoring.example.com (icinga)
  21. ***** Icinga 2 Service Monitoring on apn4711.dc.example.com *****
  22. =3D=3D> RBL check on apn4711.dc.example.com is CRITICAL! <=3D=3D
  23. Info: CHECK_RBL CRITICAL - apn4711.dc.example.com BLACKLISTED on 1 server of=
  24. 38 (ix.dnsbl.example.com)=20
  25. When: 2017-08-06 22:18:43 +0200
  26. Service: RBL check (Display Name: \"RBL check\")
  27. Host: apn4711.dc.example.com (Display Name: \"apn4711.dc.example.com\")
  28. IPv4: 127.0.0.1="
  29. ticket_0, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  30. assert_equal('new', ticket_0.state.name)
  31. assert(ticket_0.preferences)
  32. assert(ticket_0.preferences['icinga'])
  33. assert_equal('apn4711.dc.example.com (Display Name: "apn4711.dc.example.com")', ticket_0.preferences['icinga']['host'])
  34. assert_equal('CHECK_RBL CRITICAL - apn4711.dc.example.com BLACKLISTED on 1 server of 38 (ix.dnsbl.example.com)', ticket_0.preferences['icinga']['info'])
  35. assert_equal('RBL check (Display Name: "RBL check")', ticket_0.preferences['icinga']['service'])
  36. assert_equal('CRITICAL', ticket_0.preferences['icinga']['state'])
  37. # RBL check II
  38. email_raw_string = "To: support@example.com
  39. Subject: [PROBLEM] RBL check on apn4711.dc.example.com is CRITICAL!
  40. User-Agent: Heirloom mailx 12.5 7/5/10
  41. MIME-Version: 1.0
  42. Content-Type: text/plain; charset=us-ascii
  43. Content-Transfer-Encoding: quoted-printable
  44. Message-Id: <20160131094621.29ECD400F29C-icinga-1-2@monitoring.znuny.com>
  45. From: icinga2@monitoring.example.com (icinga)
  46. ***** Icinga 2 Service Monitoring on apn4711.dc.example.com *****
  47. =3D=3D> RBL check on apn4711.dc.example.com is CRITICAL! <=3D=3D
  48. Info: CHECK_RBL CRITICAL - apn4711.dc.example.com BLACKLISTED on 1 server of=
  49. 38 (ix.dnsbl.example.com)=20
  50. When: 2017-08-06 22:18:43 +0200
  51. Service: RBL check (Display Name: \"RBL check\")
  52. Host: apn4711.dc.example.com (Display Name: \"apn4711.dc.example.com\")
  53. IPv4: 127.0.0.1="
  54. ticket_0_1, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  55. assert_equal('new', ticket_0_1.state.name)
  56. assert(ticket_0_1.preferences)
  57. assert(ticket_0_1.preferences['icinga'])
  58. assert_equal('apn4711.dc.example.com (Display Name: "apn4711.dc.example.com")', ticket_0_1.preferences['icinga']['host'])
  59. 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'])
  60. assert_equal('RBL check (Display Name: "RBL check")', ticket_0_1.preferences['icinga']['service'])
  61. assert_equal('CRITICAL', ticket_0_1.preferences['icinga']['state'])
  62. assert_equal(ticket_0_1.id, ticket_0.id)
  63. email_raw_string = "To: support@example.com
  64. Subject: [PROBLEM] RBL check on apn4711.dc.example.com is OK!
  65. User-Agent: Heirloom mailx 12.5 7/5/10
  66. MIME-Version: 1.0
  67. Content-Type: text/plain; charset=us-ascii
  68. Content-Transfer-Encoding: quoted-printable
  69. Message-Id: <20160131094621.29ECD400F29C-icinga-1-2@monitoring.znuny.com>
  70. From: icinga2@monitoring.example.com (icinga)
  71. ***** Icinga 2 Service Monitoring on apn4711.dc.example.com *****
  72. =3D=3D> RBL check on apn4711.dc.example.com is OK! <=3D=3D
  73. Info: CHECK_RBL OK - apn4711.dc.example.com BLACKLISTED on 1 server of=
  74. 38 (ix.dnsbl.example.com)=20
  75. When: 2017-08-06 22:18:43 +0200
  76. Service: RBL check (Display Name: \"RBL check\")
  77. Host: apn4711.dc.example.com (Display Name: \"apn4711.dc.example.com\")
  78. IPv4: 127.0.0.1="
  79. ticket_0_2, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  80. assert_equal('closed', ticket_0_2.state.name)
  81. assert(ticket_0_2.preferences)
  82. assert(ticket_0_2.preferences['icinga'])
  83. assert_equal('apn4711.dc.example.com (Display Name: "apn4711.dc.example.com")', ticket_0_2.preferences['icinga']['host'])
  84. 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'])
  85. assert_equal('RBL check (Display Name: "RBL check")', ticket_0_2.preferences['icinga']['service'])
  86. assert_equal('CRITICAL', ticket_0_2.preferences['icinga']['state'])
  87. assert_equal(ticket_0_2.id, ticket_0.id)
  88. # matching sender - CPU Load/host.internal.loc
  89. email_raw_string = "To: support@example.com
  90. Subject: PROBLEM - host.internal.loc - CPU Load is WARNING
  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-2@monitoring.znuny.com>
  96. From: icinga2@monitoring.example.com (icinga)
  97. ***** Icinga *****
  98. Notification Type: PROBLEM
  99. Service: CPU Load
  100. Host: host.internal.loc
  101. Address:=20
  102. State: WARNING
  103. Date/Time: 2016-01-31 10:46:20 +0100
  104. Additional Info: WARNING - load average: 3.44, 0.99, 0.35
  105. Comment: [] =
  106. "
  107. ticket_1, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  108. assert_equal('new', ticket_1.state.name)
  109. assert(ticket_1.preferences)
  110. assert(ticket_1.preferences['icinga'])
  111. assert_equal('host.internal.loc', ticket_1.preferences['icinga']['host'])
  112. assert_equal('CPU Load', ticket_1.preferences['icinga']['service'])
  113. assert_equal('WARNING', ticket_1.preferences['icinga']['state'])
  114. # matching sender - Disk Usage 123/host.internal.loc
  115. email_raw_string = "To: support@example.com
  116. Subject: PROBLEM - host.internal.loc - Disk Usage 123 is WARNING
  117. User-Agent: Heirloom mailx 12.5 7/5/10
  118. MIME-Version: 1.0
  119. Content-Type: text/plain; charset=us-ascii
  120. Content-Transfer-Encoding: quoted-printable
  121. Message-Id: <20160131094621.29ECD400F29C-icinga-3@monitoring.znuny.com>
  122. From: icinga2@monitoring.example.com (icinga)
  123. ***** Icinga *****
  124. Notification Type: PROBLEM
  125. Service: Disk Usage 123
  126. Host: host.internal.loc
  127. Address:=20
  128. State: WARNING
  129. Date/Time: 2016-01-31 10:46:20 +0100
  130. Additional Info: WARNING - load average: 3.44, 0.99, 0.35
  131. Comment: [] =
  132. "
  133. ticket_2, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  134. assert_equal('new', ticket_2.state.name)
  135. assert(ticket_2.preferences)
  136. assert(ticket_2.preferences['icinga'])
  137. assert_equal('host.internal.loc', ticket_2.preferences['icinga']['host'])
  138. assert_equal('Disk Usage 123', ticket_2.preferences['icinga']['service'])
  139. assert_equal('WARNING', ticket_2.preferences['icinga']['state'])
  140. assert_not_equal(ticket_2.id, ticket_1.id)
  141. # matching sender - follow up - CPU Load/host.internal.loc
  142. email_raw_string = "To: support@example.com
  143. Subject: PROBLEM - host.internal.loc - CPU Load is WARNING
  144. User-Agent: Heirloom mailx 12.5 7/5/10
  145. MIME-Version: 1.0
  146. Content-Type: text/plain; charset=us-ascii
  147. Content-Transfer-Encoding: quoted-printable
  148. Message-Id: <20160131094621.29ECD400F29C-icinga-4@monitoring.znuny.com>
  149. From: icinga2@monitoring.example.com (icinga)
  150. ***** Icinga *****
  151. Notification Type: PROBLEM
  152. Service: CPU Load
  153. Host: host.internal.loc
  154. Address:=20
  155. State: WARNING
  156. Date/Time: 2016-01-31 10:46:20 +0100
  157. Additional Info: WARNING - load average: 3.44, 0.99, 0.35
  158. Comment: [] =
  159. "
  160. ticket_1_1, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  161. assert_equal('new', ticket_1_1.state.name)
  162. assert(ticket_1_1.preferences)
  163. assert(ticket_1_1.preferences['icinga'])
  164. assert_equal('host.internal.loc', ticket_1_1.preferences['icinga']['host'])
  165. assert_equal('CPU Load', ticket_1_1.preferences['icinga']['service'])
  166. assert_equal('WARNING', ticket_1_1.preferences['icinga']['state'])
  167. assert_equal(ticket_1.id, ticket_1_1.id)
  168. # matching sender - follow up - recovery - CPU Load/host.internal.loc
  169. email_raw_string = "To: support@example.com
  170. Subject: PROBLEM - host.internal.loc - CPU Load is WARNING
  171. User-Agent: Heirloom mailx 12.5 7/5/10
  172. MIME-Version: 1.0
  173. Content-Type: text/plain; charset=us-ascii
  174. Content-Transfer-Encoding: quoted-printable
  175. Message-Id: <20160131094621.29ECD400F29C-icinga-5@monitoring.znuny.com>
  176. From: icinga2@monitoring.example.com (icinga)
  177. ***** Icinga *****
  178. Notification Type: RECOVERY
  179. Service: CPU Load
  180. Host: host.internal.loc
  181. Address:=20
  182. State: OK
  183. Date/Time: 2016-01-31 10:48:02 +0100
  184. Additional Info: OK - load average: 1.62, 1.17, 0.49
  185. Comment: [] =
  186. "
  187. ticket_1_2, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  188. assert_equal(ticket_1.id, ticket_1_2.id)
  189. assert_equal('closed', ticket_1_2.state.name)
  190. assert(ticket_1_2.preferences)
  191. assert(ticket_1_2.preferences['icinga'])
  192. assert_equal('host.internal.loc', ticket_1_2.preferences['icinga']['host'])
  193. assert_equal('CPU Load', ticket_1_2.preferences['icinga']['service'])
  194. assert_equal('WARNING', ticket_1_2.preferences['icinga']['state'])
  195. # host down
  196. email_raw_string = "To: support@example.com
  197. Subject: PROBLEM - apn4711.dc.example.com is DOWN
  198. User-Agent: Heirloom mailx 12.5 7/5/10
  199. MIME-Version: 1.0
  200. Content-Type: text/plain; charset=us-ascii
  201. Content-Transfer-Encoding: quoted-printable
  202. Message-Id: <20160131094621.29ECD400F29C-icinga-5@monitoring.znuny.com>
  203. From: icinga2@monitoring.example.com (icinga)
  204. ***** Icinga *****
  205. Notification Type: PROBLEM
  206. Host: apn4711.dc.example.com
  207. Address: 127.0.0.1
  208. State: DOWN
  209. Date/Time: 2017-01-14 11:33:02 +0100
  210. Additional Info: CRITICAL - Host Unreachable (127.0.0.1)
  211. Comment: [] =
  212. "
  213. ticket_3, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  214. assert_equal('new', ticket_3.state.name)
  215. assert(ticket_3.preferences)
  216. assert(ticket_3.preferences['icinga'])
  217. assert_equal('apn4711.dc.example.com', ticket_3.preferences['icinga']['host'])
  218. assert_nil(ticket_3.preferences['icinga']['service'])
  219. assert_equal('DOWN', ticket_3.preferences['icinga']['state'])
  220. assert_not_equal(ticket_3.id, ticket_1.id)
  221. # host up
  222. email_raw_string = "To: support@example.com
  223. Subject: RECOVERY - apn4711.dc.example.com is UP
  224. User-Agent: Heirloom mailx 12.5 7/5/10
  225. MIME-Version: 1.0
  226. Content-Type: text/plain; charset=us-ascii
  227. Content-Transfer-Encoding: quoted-printable
  228. Message-Id: <20160131094621.29ECD400F29C-icinga-5@monitoring.znuny.com>
  229. From: icinga2@monitoring.example.com (icinga)
  230. ***** Icinga *****
  231. Notification Type: RECOVERY
  232. Host: apn4711.dc.example.com
  233. Address: 127.0.0.1
  234. State: UP
  235. Date/Time: 2017-01-14 12:07:11 +0100
  236. Additional Info: PING OK - Packet loss = 0%, RTA = 21.37 ms
  237. Comment: [] =
  238. "
  239. ticket_3_1, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  240. assert_equal(ticket_3.id, ticket_3_1.id)
  241. assert_equal('closed', ticket_3_1.state.name)
  242. assert(ticket_3_1.preferences)
  243. assert(ticket_3_1.preferences['icinga'])
  244. assert_equal('apn4711.dc.example.com', ticket_3.preferences['icinga']['host'])
  245. assert_nil(ticket_3_1.preferences['icinga']['service'])
  246. assert_equal('DOWN', ticket_3_1.preferences['icinga']['state'])
  247. # ping down
  248. email_raw_string = "To: support@example.com
  249. Subject: [PROBLEM] Ping IPv4 on apn4711.dc.example.com is WARNING!
  250. From: icinga2@monitoring.example.com (icinga)
  251. ***** Service Monitoring on monitoring.example.com *****
  252. Ping IPv4 on apn4711.dc.example.com is WARNING!
  253. Info: PING WARNING - Packet loss =3D 0%, RTA =3D 160.57 ms
  254. When: 2017-09-28 09:41:03 +0200
  255. Service: Ping IPv4
  256. Host: apn4711.dc.example.com
  257. IPv4: 127.0.0.1="
  258. ticket_4, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  259. assert_equal('new', ticket_4.state.name)
  260. assert(ticket_4.preferences)
  261. assert(ticket_4.preferences['icinga'])
  262. assert_equal('apn4711.dc.example.com', ticket_4.preferences['icinga']['host'])
  263. assert_equal('Ping IPv4', ticket_4.preferences['icinga']['service'])
  264. assert_equal('WARNING', ticket_4.preferences['icinga']['state'])
  265. assert_not_equal(ticket_4.id, ticket_1.id)
  266. # ping up
  267. email_raw_string = "To: support@example.com
  268. Subject: [RECOVERY] Ping IPv4 on apn4711.dc.example.com is OK!
  269. From: icinga2@monitoring.example.com (icinga)
  270. ***** Service Monitoring on monitoring.example.com *****
  271. Ping IPv4 on apn4711.dc.example.com is OK!
  272. Info: PING OK - Packet loss =3D 0%, RTA =3D 20.23 ms
  273. When: 2017-09-28 11:42:01 +0200
  274. Service: Ping IPv4
  275. Host: apn4711.dc.example.com
  276. IPv4: 127.0.0.1="
  277. ticket_4_1, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  278. assert_equal(ticket_4.id, ticket_4_1.id)
  279. assert_equal('closed', ticket_4_1.state.name)
  280. assert(ticket_4_1.preferences)
  281. assert(ticket_4_1.preferences['icinga'])
  282. assert_equal('apn4711.dc.example.com', ticket_4.preferences['icinga']['host'])
  283. assert_equal('Ping IPv4', ticket_4.preferences['icinga']['service'])
  284. assert_equal('WARNING', ticket_4_1.preferences['icinga']['state'])
  285. # host down
  286. email_raw_string = "To: support@example.com
  287. Subject: [PROBLEM] Host apn4709.dc.example.com is DOWN!
  288. User-Agent: Heirloom mailx 12.5 7/5/10
  289. MIME-Version: 1.0
  290. Content-Type: text/plain; charset=us-ascii
  291. Content-Transfer-Encoding: quoted-printable
  292. From: icinga2@monitoring.example.com (icinga)
  293. ***** Host Monitoring on monitoring.example.com *****
  294. apn4709.dc.example.com is DOWN!
  295. Info: CRITICAL - Plugin timed out
  296. When: 2017-09-29 14:19:40 +0200
  297. Host: apn4709.dc.example.com
  298. IPv4:=09 127.0.0.1="
  299. ticket_5, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  300. assert_equal('new', ticket_5.state.name)
  301. assert(ticket_5.preferences)
  302. assert(ticket_5.preferences['icinga'])
  303. assert_equal('apn4709.dc.example.com', ticket_5.preferences['icinga']['host'])
  304. assert_nil(ticket_5.preferences['icinga']['service'])
  305. assert_equal('DOWN', ticket_5.preferences['icinga']['state'])
  306. assert_not_equal(ticket_5.id, ticket_1.id)
  307. # host up
  308. email_raw_string = "To: support@example.com
  309. Subject: [RECOVERY] Host apn4709.dc.example.com is UP!
  310. User-Agent: Heirloom mailx 12.5 7/5/10
  311. MIME-Version: 1.0
  312. Content-Type: text/plain; charset=us-ascii
  313. Content-Transfer-Encoding: quoted-printable
  314. From: icinga2@monitoring.example.com (icinga)
  315. ***** Host Monitoring on monitoring.example.com *****
  316. apn4709.dc.example.com is UP!
  317. Info: PING OK - Packet loss =3D 0%, RTA =3D 20.20 ms
  318. When: 2017-09-29 14:23:36 +0200
  319. Host: apn4709.dc.example.com
  320. IPv4:=09 127.0.0.1=
  321. "
  322. ticket_5_1, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  323. assert_equal(ticket_5.id, ticket_5_1.id)
  324. assert_equal('closed', ticket_5_1.state.name)
  325. assert(ticket_5_1.preferences)
  326. assert(ticket_5_1.preferences['icinga'])
  327. assert_equal('apn4709.dc.example.com', ticket_5.preferences['icinga']['host'])
  328. assert_nil(ticket_5_1.preferences['icinga']['service'])
  329. assert_equal('DOWN', ticket_5_1.preferences['icinga']['state'])
  330. end
  331. test 'not matching sender tests' do
  332. # not matching sender
  333. email_raw_string = "To: support@example.com
  334. Subject: PROBLEM - host.internal.loc - CPU Load is WARNING
  335. User-Agent: Heirloom mailx 12.5 7/5/10
  336. MIME-Version: 1.0
  337. Content-Type: text/plain; charset=us-ascii
  338. Content-Transfer-Encoding: quoted-printable
  339. Message-Id: <20160131094621.29ECD400F29C-icinga-1-0@monitoring.znuny.com>
  340. From: icinga_not_matching@monitoring.example.com (icinga)
  341. ***** Icinga *****
  342. Notification Type: PROBLEM
  343. Service: CPU Load
  344. Host: host.internal.loc
  345. Address:=20
  346. State: WARNING
  347. Date/Time: 2016-01-31 10:46:20 +0100
  348. Additional Info: WARNING - load average: 3.44, 0.99, 0.35
  349. Comment: [] =
  350. "
  351. ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  352. assert_equal('new', ticket_p.state.name)
  353. assert(ticket_p.preferences)
  354. assert_not(ticket_p.preferences['icinga'])
  355. Setting.set('icinga_sender', 'regex:icinga2@monitoring.example.com')
  356. # not matching sender
  357. email_raw_string = "To: support@example.com
  358. Subject: PROBLEM - host.internal.loc - CPU Load is WARNING
  359. User-Agent: Heirloom mailx 12.5 7/5/10
  360. MIME-Version: 1.0
  361. Content-Type: text/plain; charset=us-ascii
  362. Content-Transfer-Encoding: quoted-printable
  363. Message-Id: <20160131094621.29ECD400F29C-icinga-1-0@monitoring.znuny.com>
  364. From: icinga_not_matching@monitoring.example.com (icinga)
  365. ***** Icinga *****
  366. Notification Type: PROBLEM
  367. Service: CPU Load
  368. Host: host.internal.loc
  369. Address:=20
  370. State: WARNING
  371. Date/Time: 2016-01-31 10:46:20 +0100
  372. Additional Info: WARNING - load average: 3.44, 0.99, 0.35
  373. Comment: [] =
  374. "
  375. ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  376. assert_equal('new', ticket_p.state.name)
  377. assert(ticket_p.preferences)
  378. assert_not(ticket_p.preferences['icinga'])
  379. # not matching sender
  380. email_raw_string = "To: support@example.com
  381. Subject: PROBLEM - host.internal.loc - CPU Load is WARNING
  382. User-Agent: Heirloom mailx 12.5 7/5/10
  383. MIME-Version: 1.0
  384. Content-Type: text/plain; charset=us-ascii
  385. Content-Transfer-Encoding: quoted-printable
  386. Message-Id: <20160131094621.29ECD400F29C-icinga-1-0@monitoring.znuny.com>
  387. Return-Path: bob@example.com
  388. ***** Icinga *****
  389. Notification Type: PROBLEM
  390. Service: CPU Load
  391. Host: host.internal.loc
  392. Address:=20
  393. State: WARNING
  394. Date/Time: 2016-01-31 10:46:20 +0100
  395. Additional Info: WARNING - load average: 3.44, 0.99, 0.35
  396. Comment: [] =
  397. "
  398. ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  399. assert_equal('new', ticket_p.state.name)
  400. assert(ticket_p.preferences)
  401. assert_not(ticket_p.preferences['icinga'])
  402. end
  403. test 'matching sender tests' do
  404. # matching sender - follow up - CPU Load/host.internal.loc
  405. email_raw_string = "To: support@example.com
  406. Subject: PROBLEM - host.internal.loc - CPU Load is WARNING
  407. User-Agent: Heirloom mailx 12.5 7/5/10
  408. MIME-Version: 1.0
  409. Content-Type: text/plain; charset=us-ascii
  410. Content-Transfer-Encoding: quoted-printable
  411. Message-Id: <20160131094621.29ECD400F29C-icinga-4@monitoring.znuny.com>
  412. From: icinga2@monitoring.example.com (icinga)
  413. ***** Icinga *****
  414. Notification Type: PROBLEM
  415. Service: CPU Load
  416. Host: host.internal.loc
  417. Address:=20
  418. State: WARNING
  419. Date/Time: 2016-01-31 10:46:20 +0100
  420. Additional Info: WARNING - load average: 3.44, 0.99, 0.35
  421. Comment: [] =
  422. "
  423. ticket_1_1, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  424. assert_equal('new', ticket_1_1.state.name)
  425. assert(ticket_1_1.preferences)
  426. assert(ticket_1_1.preferences['icinga'])
  427. assert_equal('host.internal.loc', ticket_1_1.preferences['icinga']['host'])
  428. assert_equal('CPU Load', ticket_1_1.preferences['icinga']['service'])
  429. assert_equal('WARNING', ticket_1_1.preferences['icinga']['state'])
  430. Setting.set('icinga_sender', 'regex:icinga2@monitoring.example.com')
  431. # matching sender I
  432. email_raw_string = "To: support@example.com
  433. Subject: PROBLEM - host1.internal.loc - CPU Load is WARNING
  434. User-Agent: Heirloom mailx 12.5 7/5/10
  435. MIME-Version: 1.0
  436. Content-Type: text/plain; charset=us-ascii
  437. Content-Transfer-Encoding: quoted-printable
  438. Message-Id: <20160131094621.29ECD400F29C-icinga-4@monitoring.znuny.com>
  439. From: icinga2@monitoring.example.com (icinga)
  440. ***** Icinga *****
  441. Notification Type: PROBLEM
  442. Service: CPU Load
  443. Host: host1.internal.loc
  444. Address:=20
  445. State: WARNING
  446. Date/Time: 2016-01-31 10:46:20 +0100
  447. Additional Info: WARNING - load average: 3.44, 0.99, 0.35
  448. Comment: [] =
  449. "
  450. ticket_1_1, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  451. assert_equal('new', ticket_1_1.state.name)
  452. assert(ticket_1_1.preferences)
  453. assert(ticket_1_1.preferences['icinga'])
  454. assert_equal('host1.internal.loc', ticket_1_1.preferences['icinga']['host'])
  455. assert_equal('CPU Load', ticket_1_1.preferences['icinga']['service'])
  456. assert_equal('WARNING', ticket_1_1.preferences['icinga']['state'])
  457. # matching sender I
  458. Setting.set('icinga_sender', 'regex:(icinga2|abc123)@monitoring.example.com')
  459. email_raw_string = "To: support@example.com
  460. Subject: PROBLEM - host2.internal.loc - CPU Load is WARNING
  461. User-Agent: Heirloom mailx 12.5 7/5/10
  462. MIME-Version: 1.0
  463. Content-Type: text/plain; charset=us-ascii
  464. Content-Transfer-Encoding: quoted-printable
  465. Message-Id: <20160131094621.29ECD400F29C-icinga-4@monitoring.znuny.com>
  466. From: icinga2@monitoring.example.com (icinga)
  467. ***** Icinga *****
  468. Notification Type: PROBLEM
  469. Service: CPU Load
  470. Host: host2.internal.loc
  471. Address:=20
  472. State: WARNING
  473. Date/Time: 2016-01-31 10:46:20 +0100
  474. Additional Info: WARNING - load average: 3.44, 0.99, 0.35
  475. Comment: [] =
  476. "
  477. ticket_1_1, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  478. assert_equal('new', ticket_1_1.state.name)
  479. assert(ticket_1_1.preferences)
  480. assert(ticket_1_1.preferences['icinga'])
  481. assert_equal('host2.internal.loc', ticket_1_1.preferences['icinga']['host'])
  482. assert_equal('CPU Load', ticket_1_1.preferences['icinga']['service'])
  483. assert_equal('WARNING', ticket_1_1.preferences['icinga']['state'])
  484. end
  485. test 'recover without problem tests' do
  486. # host up without problem
  487. email_raw_string = "To: support@example.com
  488. Subject: RECOVERY - apn4711.dc.example.com is UP
  489. User-Agent: Heirloom mailx 12.5 7/5/10
  490. MIME-Version: 1.0
  491. Content-Type: text/plain; charset=us-ascii
  492. Content-Transfer-Encoding: quoted-printable
  493. Message-Id: <20160131094621.29ECD400F29C-icinga-5@monitoring.znuny.com>
  494. From: icinga2@monitoring.example.com (icinga)
  495. ***** Icinga *****
  496. Notification Type: RECOVERY
  497. Host: apn4711.dc.example.com
  498. Address: 127.0.0.1
  499. State: UP
  500. Date/Time: 2017-01-14 12:07:11 +0100
  501. Additional Info: PING OK - Packet loss = 0%, RTA = 21.37 ms
  502. Comment: [] =
  503. "
  504. ticket_1, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  505. ticket_count = Ticket.count
  506. assert_not(ticket_1)
  507. assert_equal(ticket_count, Ticket.count)
  508. end
  509. test 'icinga email autoclose' do
  510. Setting.set('icinga_sender', 'zaihan@example.com')
  511. email_raw_string = 'Return-Path: <support@example.com>
  512. Received: from 04747418efb9 ([175.137.28.47])
  513. by smtp.example.com with ESMTPSA id r14sm6448824pfa.163.2018.04.03.10.10.59
  514. for <support@example.com>
  515. (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
  516. Tue, 03 Apr 2018 10:11:00 -0700 (PDT)
  517. From: "zaihan@example.com" <zaihan@example.com>
  518. X-Google-Original-From: "zaihan@example.com" <zaihan@example.com>
  519. Message-ID: <301139.467253478-sendEmail@04747418efb9>
  520. To: "support@example.com" <support@example.com>
  521. Subject: PROBLEM - Awesell - is DOWN
  522. Date: Tue, 3 Apr 2018 17:11:04 +0000
  523. X-Mailer: sendEmail-1.56
  524. MIME-Version: 1.0
  525. Content-Type: multipart/related; boundary="----MIME delimiter for sendEmail-587258.191387267"
  526. This is a multi-part message in MIME format. To properly display this message you need a MIME-Version 1.0 compliant Email program.
  527. ------MIME delimiter for sendEmail-587258.191387267
  528. Content-Type: text/plain;
  529. charset="iso-8859-1"
  530. Content-Transfer-Encoding: 7bit
  531. ***** Host Monitoring on 04747418efb9 *****
  532. Awesell is DOWN!
  533. Info: PING CRITICAL - Packet loss = 100%
  534. When: 2018-04-03 17:11:04 +0000
  535. Host: Awesell
  536. IPv4: 192.168.1.8
  537. ------MIME delimiter for sendEmail-587258.191387267--'
  538. ticket_0, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  539. assert_equal('new', ticket_0.state.name)
  540. assert(ticket_0.preferences)
  541. assert(ticket_0.preferences['icinga'])
  542. assert_equal('DOWN', ticket_0.preferences['icinga']['state'])
  543. email_raw_string = 'Return-Path: <support@example.com>
  544. Received: from 04747418efb9 ([175.137.28.47])
  545. by smtp.example.com with ESMTPSA id b73sm6127782pga.62.2018.04.03.10.31.00
  546. for <support@example.com>
  547. (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
  548. Tue, 03 Apr 2018 10:31:01 -0700 (PDT)
  549. From: "zaihan@example.com" <zaihan@example.com>
  550. X-Google-Original-From: "zaihan@example.com" <zaihan@example.com>
  551. Message-ID: <601339.882795827-sendEmail@04747418efb9>
  552. To: "support@example.com" <support@example.com>
  553. Subject: RECOVERY - Awesell - is UP
  554. Date: Tue, 3 Apr 2018 17:31:05 +0000
  555. X-Mailer: sendEmail-1.56
  556. MIME-Version: 1.0
  557. Content-Type: multipart/related; boundary="----MIME delimiter for sendEmail-322998.239033954"
  558. This is a multi-part message in MIME format. To properly display this message you need a MIME-Version 1.0 compliant Email program.
  559. ------MIME delimiter for sendEmail-322998.239033954
  560. Content-Type: text/plain;
  561. charset="iso-8859-1"
  562. Content-Transfer-Encoding: 7bit
  563. ***** Host Monitoring on 04747418efb9 *****
  564. Awesell is UP!
  565. Info: PING OK - Packet loss = 68%, RTA = 0.59 ms
  566. When: 2018-04-03 17:31:05 +0000
  567. Host: Awesell
  568. IPv4: 192.168.1.8
  569. ------MIME delimiter for sendEmail-322998.239033954--
  570. '
  571. ticket_1, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  572. assert_equal('closed', ticket_1.state.name)
  573. assert(ticket_1.preferences)
  574. assert(ticket_1.preferences['icinga'])
  575. assert_equal('DOWN', ticket_1.preferences['icinga']['state'])
  576. end
  577. test 'match also values without new line at the end of a line' do
  578. email_raw_string = 'Return-Path: <icinga2@monitoring.example.com>
  579. Date: Tue, 21 Aug 2018 03:05:01 +0200
  580. To: hostmaster@example.com
  581. Subject: [PROBLEM] OS Updates (yum) on host.example.com is CRITICAL!
  582. User-Agent: Heirloom mailx 12.5 7/5/10
  583. MIME-Version: 1.0
  584. Content-Type: text/plain; charset=us-ascii
  585. Content-Transfer-Encoding: quoted-printable
  586. Message-Id: <20180821010501.4A182846DBA@monitoring.example.com>
  587. From: icinga2@monitoring.example.com (icinga)
  588. ***** Service Monitoring on monitoring.example.com *****
  589. OS Updates (yum) on host.example.com is CRITICAL!
  590. Info: CHECK_UPDATES CRITICAL - 12 non-critical updates available=20
  591. audit-libs.x86_64
  592. dracut.x86_64
  593. initscripts.x86_64
  594. kpartx.x86_64
  595. libblkid.x86_64
  596. libmount.x86_64
  597. libuuid.x86_64
  598. mariadb-libs.x86_64
  599. systemd.x86_64
  600. systemd-libs.x86_64
  601. systemd-sysv.x86_64
  602. util-linux.x86_64
  603. When: 2018-08-21 03:05:01 +0200
  604. Service: OS Updates (yum)
  605. Host: host.example.com'
  606. ticket_1, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  607. assert_equal('new', ticket_1.state.name)
  608. assert(ticket_1.preferences)
  609. assert(ticket_1.preferences['icinga'])
  610. assert_equal('CRITICAL', ticket_1.preferences['icinga']['state'])
  611. assert_equal('CHECK_UPDATES CRITICAL - 12 non-critical updates available', ticket_1.preferences['icinga']['info'])
  612. assert_equal('OS Updates (yum)', ticket_1.preferences['icinga']['service'])
  613. assert_equal('host.example.com', ticket_1.preferences['icinga']['host'])
  614. end
  615. end