integration_nagios_test.rb 14 KB

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