integration_icinga_test.rb 27 KB

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