integration_icinga_test.rb 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. # encoding: utf-8
  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. test 'base tests' do
  9. Setting.set('icinga_integration', true)
  10. # not matching sender
  11. email_raw_string = "To: support@example.com
  12. Subject: PROBLEM - host.internal.loc - CPU Load is WARNING
  13. User-Agent: Heirloom mailx 12.5 7/5/10
  14. MIME-Version: 1.0
  15. Content-Type: text/plain; charset=us-ascii
  16. Content-Transfer-Encoding: quoted-printable
  17. Message-Id: <20160131094621.29ECD400F29C-icinga-1@monitoring.znuny.com>
  18. From: icinga_not_matching@monitoring.example.com (icinga)
  19. ***** Icinga *****
  20. Notification Type: PROBLEM
  21. Service: CPU Load
  22. Host: host.internal.loc
  23. Address:=20
  24. State: WARNING
  25. Date/Time: 2016-01-31 10:46:20 +0100
  26. Additional Info: WARNING - load average: 3.44, 0.99, 0.35
  27. Comment: [] =
  28. "
  29. ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  30. assert_equal('new', ticket_p.state.name)
  31. assert(ticket_p.preferences)
  32. assert_not(ticket_p.preferences['integration'])
  33. assert_not(ticket_p.preferences['icinga'])
  34. # matching sender - CPU Load/host.internal.loc
  35. email_raw_string = "To: support@example.com
  36. Subject: PROBLEM - host.internal.loc - CPU Load is WARNING
  37. User-Agent: Heirloom mailx 12.5 7/5/10
  38. MIME-Version: 1.0
  39. Content-Type: text/plain; charset=us-ascii
  40. Content-Transfer-Encoding: quoted-printable
  41. Message-Id: <20160131094621.29ECD400F29C-icinga-2@monitoring.znuny.com>
  42. From: icinga@monitoring.example.com (icinga)
  43. ***** Icinga *****
  44. Notification Type: PROBLEM
  45. Service: CPU Load
  46. Host: host.internal.loc
  47. Address:=20
  48. State: WARNING
  49. Date/Time: 2016-01-31 10:46:20 +0100
  50. Additional Info: WARNING - load average: 3.44, 0.99, 0.35
  51. Comment: [] =
  52. "
  53. ticket_1, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  54. assert_equal('new', ticket_1.state.name)
  55. assert(ticket_1.preferences)
  56. assert(ticket_1.preferences['integration'])
  57. assert_equal('icinga', ticket_1.preferences['integration'])
  58. assert(ticket_1.preferences['icinga'])
  59. assert_equal('host.internal.loc', ticket_1.preferences['icinga']['host'])
  60. assert_equal('CPU Load', ticket_1.preferences['icinga']['service'])
  61. assert_equal('WARNING', ticket_1.preferences['icinga']['state'])
  62. # matching sender - Disk Usage 123/host.internal.loc
  63. email_raw_string = "To: support@example.com
  64. Subject: PROBLEM - host.internal.loc - Disk Usage 123 is WARNING
  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-3@monitoring.znuny.com>
  70. From: icinga@monitoring.example.com (icinga)
  71. ***** Icinga *****
  72. Notification Type: PROBLEM
  73. Service: Disk Usage 123
  74. Host: host.internal.loc
  75. Address:=20
  76. State: WARNING
  77. Date/Time: 2016-01-31 10:46:20 +0100
  78. Additional Info: WARNING - load average: 3.44, 0.99, 0.35
  79. Comment: [] =
  80. "
  81. ticket_2, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  82. assert_equal('new', ticket_2.state.name)
  83. assert(ticket_2.preferences)
  84. assert(ticket_2.preferences['integration'])
  85. assert_equal('icinga', ticket_2.preferences['integration'])
  86. assert(ticket_2.preferences['icinga'])
  87. assert_equal('host.internal.loc', ticket_2.preferences['icinga']['host'])
  88. assert_equal('Disk Usage 123', ticket_2.preferences['icinga']['service'])
  89. assert_equal('WARNING', ticket_2.preferences['icinga']['state'])
  90. assert_not_equal(ticket_2.id, ticket_1.id)
  91. # matching sender - follow up - CPU Load/host.internal.loc
  92. email_raw_string = "To: support@example.com
  93. Subject: PROBLEM - host.internal.loc - CPU Load is WARNING
  94. User-Agent: Heirloom mailx 12.5 7/5/10
  95. MIME-Version: 1.0
  96. Content-Type: text/plain; charset=us-ascii
  97. Content-Transfer-Encoding: quoted-printable
  98. Message-Id: <20160131094621.29ECD400F29C-icinga-4@monitoring.znuny.com>
  99. From: icinga@monitoring.example.com (icinga)
  100. ***** Icinga *****
  101. Notification Type: PROBLEM
  102. Service: CPU Load
  103. Host: host.internal.loc
  104. Address:=20
  105. State: WARNING
  106. Date/Time: 2016-01-31 10:46:20 +0100
  107. Additional Info: WARNING - load average: 3.44, 0.99, 0.35
  108. Comment: [] =
  109. "
  110. ticket_1_1, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  111. assert_equal('new', ticket_1_1.state.name)
  112. assert(ticket_1_1.preferences)
  113. assert(ticket_1_1.preferences['integration'])
  114. assert_equal('icinga', ticket_1_1.preferences['integration'])
  115. assert(ticket_1_1.preferences['icinga'])
  116. assert_equal('host.internal.loc', ticket_1_1.preferences['icinga']['host'])
  117. assert_equal('CPU Load', ticket_1_1.preferences['icinga']['service'])
  118. assert_equal('WARNING', ticket_1_1.preferences['icinga']['state'])
  119. assert_equal(ticket_1.id, ticket_1_1.id)
  120. # matching sender - follow up - recovery - CPU Load/host.internal.loc
  121. email_raw_string = "To: support@example.com
  122. Subject: PROBLEM - host.internal.loc - CPU Load is WARNING
  123. User-Agent: Heirloom mailx 12.5 7/5/10
  124. MIME-Version: 1.0
  125. Content-Type: text/plain; charset=us-ascii
  126. Content-Transfer-Encoding: quoted-printable
  127. Message-Id: <20160131094621.29ECD400F29C-icinga-5@monitoring.znuny.com>
  128. From: icinga@monitoring.example.com (icinga)
  129. ***** Icinga *****
  130. Notification Type: RECOVERY
  131. Service: CPU Load
  132. Host: host.internal.loc
  133. Address:=20
  134. State: OK
  135. Date/Time: 2016-01-31 10:48:02 +0100
  136. Additional Info: OK - load average: 1.62, 1.17, 0.49
  137. Comment: [] =
  138. "
  139. ticket_1_2, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  140. assert_equal(ticket_1.id, ticket_1_2.id)
  141. assert_equal('closed', ticket_1_2.state.name)
  142. assert(ticket_1_2.preferences)
  143. assert(ticket_1_2.preferences['integration'])
  144. assert_equal('icinga', ticket_1_2.preferences['integration'])
  145. assert(ticket_1_2.preferences['icinga'])
  146. assert_equal('host.internal.loc', ticket_1_2.preferences['icinga']['host'])
  147. assert_equal('CPU Load', ticket_1_2.preferences['icinga']['service'])
  148. assert_equal('WARNING', ticket_1_2.preferences['icinga']['state'])
  149. #Setting.set('icinga_integration', false)
  150. end
  151. end