integration_nagios_test.rb 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'test_helper'
  3. class IntegrationNagiosTest < ActiveSupport::TestCase
  4. # according
  5. # https://github.com/NagiosEnterprises/nagioscore/blob/754218e67653929a58938b99ef6b6039b6474fe4/sample-config/template-object/commands.cfg.in#L35
  6. setup do
  7. Setting.set('nagios_integration', true)
  8. Setting.set('nagios_sender', 'nagios2@monitoring.example.com')
  9. end
  10. test 'base tests' do
  11. # matching sender - CPU Load/host.internal.loc
  12. email_raw_string = "To: support@example.com
  13. Subject: ** PROBLEM Service Alert: host.internal.loc/CPU Load is WARNING **
  14. MIME-Version: 1.0
  15. Content-Type: text/plain; charset=us-ascii
  16. Content-Transfer-Encoding: quoted-printable
  17. Message-Id: <20160131094621.29ECD400F29C-nagios-2@monitoring.znuny.com>
  18. From: nagios2@monitoring.example.com (nagios)
  19. ***** Nagios *****
  20. Notification Type: PROBLEM
  21. Service: CPU Load
  22. Host: host.internal.loc
  23. Address: 1.1.1.1
  24. State: WARNING
  25. Date/Time: 2016-01-31 10:46:20 +0100
  26. Additional Info:
  27. WARNING - load average: 3.44, 0.99, 0.35
  28. "
  29. ticket_1, _article_p, _user_p, _mail = Channel::EmailParser.new.process({}, email_raw_string)
  30. assert_equal('new', ticket_1.state.name)
  31. assert(ticket_1.preferences)
  32. assert(ticket_1.preferences['nagios'])
  33. assert_equal('host.internal.loc', ticket_1.preferences['nagios']['host'])
  34. assert_equal('CPU Load', ticket_1.preferences['nagios']['service'])
  35. assert_equal('WARNING', ticket_1.preferences['nagios']['state'])
  36. # matching sender - Disk Usage 123/host.internal.loc
  37. email_raw_string = "To: support@example.com
  38. Subject: ** PROBLEM Service Alert: host.internal.loc/Disk Usage 123 is WARNING **
  39. MIME-Version: 1.0
  40. Content-Type: text/plain; charset=us-ascii
  41. Content-Transfer-Encoding: quoted-printable
  42. Message-Id: <20160131094621.29ECD400F29C-nagios-3@monitoring.znuny.com>
  43. From: nagios2@monitoring.example.com (nagios)
  44. ***** Nagios *****
  45. Notification Type: PROBLEM
  46. Service: Disk Usage 123
  47. Host: host.internal.loc
  48. Address: 1.1.1.1
  49. State: WARNING
  50. Date/Time: 2016-01-31 10:46:20 +0100
  51. Additional Info:
  52. WARNING - load average: 3.44, 0.99, 0.35
  53. "
  54. ticket_2, _article_p, _user_p, _mail = Channel::EmailParser.new.process({}, email_raw_string)
  55. assert_equal('new', ticket_2.state.name)
  56. assert(ticket_2.preferences)
  57. assert(ticket_2.preferences['nagios'])
  58. assert_equal('host.internal.loc', ticket_2.preferences['nagios']['host'])
  59. assert_equal('Disk Usage 123', ticket_2.preferences['nagios']['service'])
  60. assert_equal('WARNING', ticket_2.preferences['nagios']['state'])
  61. assert_not_equal(ticket_2.id, ticket_1.id)
  62. # matching sender - follow-up - CPU Load/host.internal.loc
  63. email_raw_string = "To: support@example.com
  64. Subject: ** PROBLEM Service Alert: host.internal.loc/CPU Load is WARNING **
  65. MIME-Version: 1.0
  66. Content-Type: text/plain; charset=us-ascii
  67. Content-Transfer-Encoding: quoted-printable
  68. Message-Id: <20160131094621.29ECD400F29C-nagios-4@monitoring.znuny.com>
  69. From: nagios2@monitoring.example.com (nagios)
  70. ***** Nagios *****
  71. Notification Type: PROBLEM
  72. Service: CPU Load
  73. Host: host.internal.loc
  74. Address: 1.1.1.1
  75. State: WARNING
  76. Date/Time: 2016-01-31 10:46:20 +0100
  77. Additional Info:
  78. WARNING - load average: 3.44, 0.99, 0.35
  79. "
  80. ticket_1_1, _article_p, _user_p, _mail = Channel::EmailParser.new.process({}, email_raw_string)
  81. assert_equal('new', ticket_1_1.state.name)
  82. assert(ticket_1_1.preferences)
  83. assert(ticket_1_1.preferences['nagios'])
  84. assert_equal('host.internal.loc', ticket_1_1.preferences['nagios']['host'])
  85. assert_equal('CPU Load', ticket_1_1.preferences['nagios']['service'])
  86. assert_equal('WARNING', ticket_1_1.preferences['nagios']['state'])
  87. assert_equal(ticket_1.id, ticket_1_1.id)
  88. # matching sender - follow-up - recovery - CPU Load/host.internal.loc
  89. email_raw_string = "To: support@example.com
  90. Subject: ** PROBLEM Service Alert: host.internal.loc/CPU Load is WARNING **
  91. MIME-Version: 1.0
  92. Content-Type: text/plain; charset=us-ascii
  93. Content-Transfer-Encoding: quoted-printable
  94. Message-Id: <20160131094621.29ECD400F29C-nagios-5@monitoring.znuny.com>
  95. From: nagios2@monitoring.example.com (nagios)
  96. ***** Nagios *****
  97. Notification Type: PROBLEM
  98. Service: CPU Load
  99. Host: host.internal.loc
  100. Address: 1.1.1.1
  101. State: OK
  102. Date/Time: 2016-01-31 10:48:02 +0100
  103. Additional Info:
  104. "
  105. ticket_1_2, _article_p, _user_p, _mail = Channel::EmailParser.new.process({}, email_raw_string)
  106. assert_equal(ticket_1.id, ticket_1_2.id)
  107. assert_equal('closed', ticket_1_2.state.name)
  108. assert(ticket_1_2.preferences)
  109. assert(ticket_1_2.preferences['nagios'])
  110. assert_equal('host.internal.loc', ticket_1_2.preferences['nagios']['host'])
  111. assert_equal('CPU Load', ticket_1_2.preferences['nagios']['service'])
  112. assert_equal('WARNING', ticket_1_2.preferences['nagios']['state'])
  113. # host down
  114. email_raw_string = "To: support@example.com
  115. Subject: ** PROBLEM Host Alert: apn4711.dc.example.com is DOWN **
  116. User-Agent: Heirloom mailx 12.5 7/5/10
  117. MIME-Version: 1.0
  118. Content-Type: text/plain; charset=us-ascii
  119. Content-Transfer-Encoding: quoted-printable
  120. Message-Id: <20160131094621.29ECD400F29C-nagios-5@monitoring.znuny.com>
  121. From: nagios2@monitoring.example.com (nagios)
  122. ***** Nagios *****
  123. Notification Type: PROBLEM
  124. Host: apn4711.dc.example.com
  125. Address: 127.0.0.1
  126. State: DOWN
  127. Date/Time: 2017-01-14 11:33:02 +0100
  128. Additional Info: CRITICAL - Host Unreachable (127.0.0.1)
  129. Comment: [] =
  130. "
  131. ticket_3, _article_p, _user_p, _mail = Channel::EmailParser.new.process({}, email_raw_string)
  132. assert_equal('new', ticket_3.state.name)
  133. assert(ticket_3.preferences)
  134. assert(ticket_3.preferences['nagios'])
  135. assert_equal('apn4711.dc.example.com', ticket_3.preferences['nagios']['host'])
  136. assert_nil(ticket_3.preferences['nagios']['service'])
  137. assert_equal('DOWN', ticket_3.preferences['nagios']['state'])
  138. assert_not_equal(ticket_3.id, ticket_1.id)
  139. # host up
  140. email_raw_string = "To: support@example.com
  141. Subject: ** RECOVERY Host Alert: apn4711.dc.example.com is UP **
  142. User-Agent: Heirloom mailx 12.5 7/5/10
  143. MIME-Version: 1.0
  144. Content-Type: text/plain; charset=us-ascii
  145. Content-Transfer-Encoding: quoted-printable
  146. Message-Id: <20160131094621.29ECD400F29C-nagios-5@monitoring.znuny.com>
  147. From: nagios2@monitoring.example.com (nagios)
  148. ***** Nagios *****
  149. Notification Type: RECOVERY
  150. Host: apn4711.dc.example.com
  151. Address: 127.0.0.1
  152. State: UP
  153. Date/Time: 2017-01-14 12:07:11 +0100
  154. Additional Info: PING OK - Packet loss = 0%, RTA = 21.37 ms
  155. Comment: [] =
  156. "
  157. ticket_3_1, _article_p, _user_p, _mail = Channel::EmailParser.new.process({}, email_raw_string)
  158. assert_equal(ticket_3.id, ticket_3_1.id)
  159. assert_equal('closed', ticket_3_1.state.name)
  160. assert(ticket_3_1.preferences)
  161. assert(ticket_3_1.preferences['nagios'])
  162. assert_equal('apn4711.dc.example.com', ticket_3.preferences['nagios']['host'])
  163. assert_nil(ticket_3_1.preferences['nagios']['service'])
  164. assert_equal('DOWN', ticket_3_1.preferences['nagios']['state'])
  165. # Setting.set('nagios_integration', false)
  166. end
  167. test 'not matching sender tests' do
  168. # not matching sender
  169. email_raw_string = "To: support@example.com
  170. Subject: ** PROBLEM Service Alert: host.internal.loc/CPU Load is WARNING **
  171. MIME-Version: 1.0
  172. Content-Type: text/plain; charset=us-ascii
  173. Content-Transfer-Encoding: quoted-printable
  174. Message-Id: <20160131094621.29ECD400F29C-nagios-1@monitoring.znuny.com>
  175. From: nagios_not_matching@monitoring.example.com (nagios)
  176. ***** Nagios *****
  177. Notification Type: PROBLEM
  178. Service: CPU Load
  179. Host: host.internal.loc
  180. Address: 1.1.1.1
  181. State: PROBLEM
  182. Date/Time: 2016-01-31 10:46:20 +0100
  183. Additional Info:
  184. WARNING - load average: 3.44, 0.99, 0.35
  185. "
  186. ticket_p, _article_p, _user_p, _mail = Channel::EmailParser.new.process({}, email_raw_string)
  187. assert_equal('new', ticket_p.state.name)
  188. assert(ticket_p.preferences)
  189. assert_not(ticket_p.preferences['nagios'])
  190. Setting.set('nagios_sender', 'regex:icinga2@monitoring.example.com')
  191. # not matching sender
  192. email_raw_string = "To: support@example.com
  193. Subject: ** PROBLEM Service Alert: host.internal.loc/CPU Load is WARNING **
  194. MIME-Version: 1.0
  195. Content-Type: text/plain; charset=us-ascii
  196. Content-Transfer-Encoding: quoted-printable
  197. Message-Id: <20160131094621.29ECD400F29C-nagios-1@monitoring.znuny.com>
  198. From: nagios_not_matching@monitoring.example.com (nagios)
  199. ***** Nagios *****
  200. Notification Type: PROBLEM
  201. Service: CPU Load
  202. Host: host.internal.loc
  203. Address: 1.1.1.1
  204. State: PROBLEM
  205. Date/Time: 2016-01-31 10:46:20 +0100
  206. Additional Info:
  207. WARNING - load average: 3.44, 0.99, 0.35
  208. "
  209. ticket_p, _article_p, _user_p, _mail = Channel::EmailParser.new.process({}, email_raw_string)
  210. assert_equal('new', ticket_p.state.name)
  211. assert(ticket_p.preferences)
  212. assert_not(ticket_p.preferences['nagios'])
  213. # not matching sender
  214. email_raw_string = "To: support@example.com
  215. Subject: ** PROBLEM Service Alert: host.internal.loc/CPU Load is WARNING **
  216. MIME-Version: 1.0
  217. Content-Type: text/plain; charset=us-ascii
  218. Content-Transfer-Encoding: quoted-printable
  219. Message-Id: <20160131094621.29ECD400F29C-nagios-1@monitoring.znuny.com>
  220. Return-Path: bob@example.com
  221. ***** Nagios *****
  222. Notification Type: PROBLEM
  223. Service: CPU Load
  224. Host: host.internal.loc
  225. Address: 1.1.1.1
  226. State: PROBLEM
  227. Date/Time: 2016-01-31 10:46:20 +0100
  228. Additional Info:
  229. WARNING - load average: 3.44, 0.99, 0.35
  230. "
  231. ticket_p, _article_p, _user_p, _mail = Channel::EmailParser.new.process({}, email_raw_string)
  232. assert_equal('new', ticket_p.state.name)
  233. assert(ticket_p.preferences)
  234. assert_not(ticket_p.preferences['nagios'])
  235. end
  236. test 'matching sender tests' do
  237. # matching sender - follow-up - CPU Load/host.internal.loc
  238. email_raw_string = "To: support@example.com
  239. Subject: ** PROBLEM Service Alert: host.internal.loc/CPU Load is WARNING **
  240. MIME-Version: 1.0
  241. Content-Type: text/plain; charset=us-ascii
  242. Content-Transfer-Encoding: quoted-printable
  243. Message-Id: <20160131094621.29ECD400F29C-nagios-2@monitoring.znuny.com>
  244. From: nagios2@monitoring.example.com (nagios)
  245. ***** Nagios *****
  246. Notification Type: PROBLEM
  247. Service: CPU Load
  248. Host: host.internal.loc
  249. Address: 1.1.1.1
  250. State: WARNING
  251. Date/Time: 2016-01-31 10:46:20 +0100
  252. Additional Info:
  253. WARNING - load average: 3.44, 0.99, 0.35
  254. "
  255. ticket_1, _article_p, _user_p, _mail = Channel::EmailParser.new.process({}, email_raw_string)
  256. assert_equal('new', ticket_1.state.name)
  257. assert(ticket_1.preferences)
  258. assert(ticket_1.preferences['nagios'])
  259. assert_equal('host.internal.loc', ticket_1.preferences['nagios']['host'])
  260. assert_equal('CPU Load', ticket_1.preferences['nagios']['service'])
  261. assert_equal('WARNING', ticket_1.preferences['nagios']['state'])
  262. Setting.set('icinga_sender', 'regex:icinga2@monitoring.example.com')
  263. # matching sender I
  264. email_raw_string = "To: support@example.com
  265. Subject: ** PROBLEM Service Alert: host.internal.loc/CPU Load is WARNING **
  266. MIME-Version: 1.0
  267. Content-Type: text/plain; charset=us-ascii
  268. Content-Transfer-Encoding: quoted-printable
  269. Message-Id: <20160131094621.29ECD400F29C-nagios-2@monitoring.znuny.com>
  270. From: nagios2@monitoring.example.com (nagios)
  271. ***** Nagios *****
  272. Notification Type: PROBLEM
  273. Service: CPU Load
  274. Host: host1.internal.loc
  275. Address: 1.1.1.1
  276. State: WARNING
  277. Date/Time: 2016-01-31 10:46:20 +0100
  278. Additional Info:
  279. WARNING - load average: 3.44, 0.99, 0.35
  280. "
  281. ticket_1, _article_p, _user_p, _mail = Channel::EmailParser.new.process({}, email_raw_string)
  282. assert_equal('new', ticket_1.state.name)
  283. assert(ticket_1.preferences)
  284. assert(ticket_1.preferences['nagios'])
  285. assert_equal('host1.internal.loc', ticket_1.preferences['nagios']['host'])
  286. assert_equal('CPU Load', ticket_1.preferences['nagios']['service'])
  287. assert_equal('WARNING', ticket_1.preferences['nagios']['state'])
  288. # matching sender I
  289. Setting.set('icinga_sender', 'regex:(icinga2|abc123)@monitoring.example.com')
  290. email_raw_string = "To: support@example.com
  291. Subject: ** PROBLEM Service Alert: host.internal.loc/CPU Load is WARNING **
  292. MIME-Version: 1.0
  293. Content-Type: text/plain; charset=us-ascii
  294. Content-Transfer-Encoding: quoted-printable
  295. Message-Id: <20160131094621.29ECD400F29C-nagios-2@monitoring.znuny.com>
  296. From: nagios2@monitoring.example.com (nagios)
  297. ***** Nagios *****
  298. Notification Type: PROBLEM
  299. Service: CPU Load
  300. Host: host2.internal.loc
  301. Address: 1.1.1.1
  302. State: WARNING
  303. Date/Time: 2016-01-31 10:46:20 +0100
  304. Additional Info:
  305. WARNING - load average: 3.44, 0.99, 0.35
  306. "
  307. ticket_1, _article_p, _user_p, _mail = Channel::EmailParser.new.process({}, email_raw_string)
  308. assert_equal('new', ticket_1.state.name)
  309. assert(ticket_1.preferences)
  310. assert(ticket_1.preferences['nagios'])
  311. assert_equal('host2.internal.loc', ticket_1.preferences['nagios']['host'])
  312. assert_equal('CPU Load', ticket_1.preferences['nagios']['service'])
  313. assert_equal('WARNING', ticket_1.preferences['nagios']['state'])
  314. end
  315. test 'recover without problem tests' do
  316. # host up without problem
  317. email_raw_string = "To: support@example.com
  318. Subject: ** RECOVERY Host Alert: apn4711.dc.example.com is UP **
  319. User-Agent: Heirloom mailx 12.5 7/5/10
  320. MIME-Version: 1.0
  321. Content-Type: text/plain; charset=us-ascii
  322. Content-Transfer-Encoding: quoted-printable
  323. Message-Id: <20160131094621.29ECD400F29C-nagios-5@monitoring.znuny.com>
  324. From: nagios2@monitoring.example.com (nagios)
  325. ***** Nagios *****
  326. Notification Type: RECOVERY
  327. Host: apn4711.dc.example.com
  328. Address: 127.0.0.1
  329. State: UP
  330. Date/Time: 2017-01-14 12:07:11 +0100
  331. Additional Info: PING OK - Packet loss = 0%, RTA = 21.37 ms
  332. Comment: [] =
  333. "
  334. ticket_1, _article_p, _user_p, _mail = Channel::EmailParser.new.process({}, email_raw_string)
  335. ticket_count = Ticket.count
  336. assert_not(ticket_1)
  337. assert_equal(ticket_count, Ticket.count)
  338. end
  339. end