integration_nagios_test.rb 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. # encoding: utf-8
  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. test 'base tests' do
  7. Setting.set('nagios_integration', true)
  8. # not matching sender
  9. email_raw_string = "To: support@example.com
  10. Subject: ** PROBLEM Service Alert: host.internal.loc/CPU Load is WARNING **
  11. MIME-Version: 1.0
  12. Content-Type: text/plain; charset=us-ascii
  13. Content-Transfer-Encoding: quoted-printable
  14. Message-Id: <20160131094621.29ECD400F29C-nagios-1@monitoring.znuny.com>
  15. From: nagios_not_matching@monitoring.example.com (nagios)
  16. ***** Nagios *****
  17. Notification Type: PROBLEM
  18. Service: CPU Load
  19. Host: host.internal.loc
  20. Address: 1.1.1.1
  21. State: PROBLEM
  22. Date/Time: 2016-01-31 10:46:20 +0100
  23. Additional Info:
  24. WARNING - load average: 3.44, 0.99, 0.35
  25. "
  26. ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  27. assert_equal('new', ticket_p.state.name)
  28. assert(ticket_p.preferences)
  29. assert_not(ticket_p.preferences['integration'])
  30. assert_not(ticket_p.preferences['nagios'])
  31. # matching sender - CPU Load/host.internal.loc
  32. email_raw_string = "To: support@example.com
  33. Subject: ** PROBLEM Service Alert: host.internal.loc/CPU Load is WARNING **
  34. MIME-Version: 1.0
  35. Content-Type: text/plain; charset=us-ascii
  36. Content-Transfer-Encoding: quoted-printable
  37. Message-Id: <20160131094621.29ECD400F29C-nagios-2@monitoring.znuny.com>
  38. From: nagios@monitoring.example.com (nagios)
  39. ***** Nagios *****
  40. Notification Type: PROBLEM
  41. Service: CPU Load
  42. Host: host.internal.loc
  43. Address: 1.1.1.1
  44. State: WARNING
  45. Date/Time: 2016-01-31 10:46:20 +0100
  46. Additional Info:
  47. WARNING - load average: 3.44, 0.99, 0.35
  48. "
  49. ticket_1, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  50. assert_equal('new', ticket_1.state.name)
  51. assert(ticket_1.preferences)
  52. assert(ticket_1.preferences['integration'])
  53. assert_equal('nagios', ticket_1.preferences['integration'])
  54. assert(ticket_1.preferences['nagios'])
  55. assert_equal('host.internal.loc', ticket_1.preferences['nagios']['host'])
  56. assert_equal('CPU Load', ticket_1.preferences['nagios']['service'])
  57. assert_equal('WARNING', ticket_1.preferences['nagios']['state'])
  58. # matching sender - Disk Usage 123/host.internal.loc
  59. email_raw_string = "To: support@example.com
  60. Subject: ** PROBLEM Service Alert: host.internal.loc/Disk Usage 123 is WARNING **
  61. MIME-Version: 1.0
  62. Content-Type: text/plain; charset=us-ascii
  63. Content-Transfer-Encoding: quoted-printable
  64. Message-Id: <20160131094621.29ECD400F29C-nagios-3@monitoring.znuny.com>
  65. From: nagios@monitoring.example.com (nagios)
  66. ***** Nagios *****
  67. Notification Type: PROBLEM
  68. Service: Disk Usage 123
  69. Host: host.internal.loc
  70. Address: 1.1.1.1
  71. State: WARNING
  72. Date/Time: 2016-01-31 10:46:20 +0100
  73. Additional Info:
  74. WARNING - load average: 3.44, 0.99, 0.35
  75. "
  76. ticket_2, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  77. assert_equal('new', ticket_2.state.name)
  78. assert(ticket_2.preferences)
  79. assert(ticket_2.preferences['integration'])
  80. assert_equal('nagios', ticket_2.preferences['integration'])
  81. assert(ticket_2.preferences['nagios'])
  82. assert_equal('host.internal.loc', ticket_2.preferences['nagios']['host'])
  83. assert_equal('Disk Usage 123', ticket_2.preferences['nagios']['service'])
  84. assert_equal('WARNING', ticket_2.preferences['nagios']['state'])
  85. assert_not_equal(ticket_2.id, ticket_1.id)
  86. # matching sender - follow up - CPU Load/host.internal.loc
  87. email_raw_string = "To: support@example.com
  88. Subject: ** PROBLEM Service Alert: host.internal.loc/CPU Load is WARNING **
  89. MIME-Version: 1.0
  90. Content-Type: text/plain; charset=us-ascii
  91. Content-Transfer-Encoding: quoted-printable
  92. Message-Id: <20160131094621.29ECD400F29C-nagios-4@monitoring.znuny.com>
  93. From: nagios@monitoring.example.com (nagios)
  94. ***** Nagios *****
  95. Notification Type: PROBLEM
  96. Service: CPU Load
  97. Host: host.internal.loc
  98. Address: 1.1.1.1
  99. State: WARNING
  100. Date/Time: 2016-01-31 10:46:20 +0100
  101. Additional Info:
  102. WARNING - load average: 3.44, 0.99, 0.35
  103. "
  104. ticket_1_1, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  105. assert_equal('new', ticket_1_1.state.name)
  106. assert(ticket_1_1.preferences)
  107. assert(ticket_1_1.preferences['integration'])
  108. assert_equal('nagios', ticket_1_1.preferences['integration'])
  109. assert(ticket_1_1.preferences['nagios'])
  110. assert_equal('host.internal.loc', ticket_1_1.preferences['nagios']['host'])
  111. assert_equal('CPU Load', ticket_1_1.preferences['nagios']['service'])
  112. assert_equal('WARNING', ticket_1_1.preferences['nagios']['state'])
  113. assert_equal(ticket_1.id, ticket_1_1.id)
  114. # matching sender - follow up - recovery - CPU Load/host.internal.loc
  115. email_raw_string = "To: support@example.com
  116. Subject: ** PROBLEM Service Alert: host.internal.loc/CPU Load is WARNING **
  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: nagios@monitoring.example.com (nagios)
  122. ***** Nagios *****
  123. Notification Type: PROBLEM
  124. Service: CPU Load
  125. Host: host.internal.loc
  126. Address: 1.1.1.1
  127. State: OK
  128. Date/Time: 2016-01-31 10:48:02 +0100
  129. Additional Info:
  130. "
  131. ticket_1_2, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  132. assert_equal(ticket_1.id, ticket_1_2.id)
  133. assert_equal('closed', ticket_1_2.state.name)
  134. assert(ticket_1_2.preferences)
  135. assert(ticket_1_2.preferences['integration'])
  136. assert_equal('nagios', ticket_1_2.preferences['integration'])
  137. assert(ticket_1_2.preferences['nagios'])
  138. assert_equal('host.internal.loc', ticket_1_2.preferences['nagios']['host'])
  139. assert_equal('CPU Load', ticket_1_2.preferences['nagios']['service'])
  140. assert_equal('WARNING', ticket_1_2.preferences['nagios']['state'])
  141. # host down
  142. email_raw_string = "To: support@example.com
  143. Subject: ** PROBLEM Host Alert: apn4711.dc.example.com is DOWN **
  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-nagios-5@monitoring.znuny.com>
  149. From: nagios@monitoring.example.com (nagios)
  150. ***** Nagios *****
  151. Notification Type: PROBLEM
  152. Host: apn4711.dc.example.com
  153. Address: 127.0.0.1
  154. State: DOWN
  155. Date/Time: 2017-01-14 11:33:02 +0100
  156. Additional Info: CRITICAL - Host Unreachable (127.0.0.1)
  157. Comment: [] =
  158. "
  159. ticket_3, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  160. assert_equal('new', ticket_3.state.name)
  161. assert(ticket_3.preferences)
  162. assert(ticket_3.preferences['integration'])
  163. assert_equal('nagios', ticket_3.preferences['integration'])
  164. assert(ticket_3.preferences['nagios'])
  165. assert_equal('apn4711.dc.example.com', ticket_3.preferences['nagios']['host'])
  166. assert_equal(nil, ticket_3.preferences['nagios']['service'])
  167. assert_equal('DOWN', ticket_3.preferences['nagios']['state'])
  168. assert_not_equal(ticket_3.id, ticket_1.id)
  169. # host up
  170. email_raw_string = "To: support@example.com
  171. Subject: ** RECOVERY Host Alert: apn4711.dc.example.com is UP **
  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-nagios-5@monitoring.znuny.com>
  177. From: nagios@monitoring.example.com (nagios)
  178. ***** Nagios *****
  179. Notification Type: RECOVERY
  180. Host: apn4711.dc.example.com
  181. Address: 127.0.0.1
  182. State: UP
  183. Date/Time: 2017-01-14 12:07:11 +0100
  184. Additional Info: PING OK - Packet loss = 0%, RTA = 21.37 ms
  185. Comment: [] =
  186. "
  187. ticket_3_1, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  188. assert_equal(ticket_3.id, ticket_3_1.id)
  189. assert_equal('closed', ticket_3_1.state.name)
  190. assert(ticket_3_1.preferences)
  191. assert(ticket_3_1.preferences['integration'])
  192. assert_equal('nagios', ticket_3_1.preferences['integration'])
  193. assert(ticket_3_1.preferences['nagios'])
  194. assert_equal('apn4711.dc.example.com', ticket_3.preferences['nagios']['host'])
  195. assert_equal(nil, ticket_3_1.preferences['nagios']['service'])
  196. assert_equal('DOWN', ticket_3_1.preferences['nagios']['state'])
  197. #Setting.set('nagios_integration', false)
  198. end
  199. end