integration_monit_test.rb 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'test_helper'
  3. class IntegrationMonitTest < ActiveSupport::TestCase
  4. # according
  5. # https://mmonit.com/monit/documentation/#ALERT-MESSAGES
  6. setup do
  7. Setting.set('monit_integration', true)
  8. Setting.set('monit_sender', 'monit@monitoring.example.com')
  9. end
  10. test 'base tests' do
  11. # Service
  12. email_raw_string = "Message-Id: <20160131094621.29ECD400F29C-monit-1-1@monitoring.zammad.com>
  13. From: monit@monitoring.example.com
  14. To: admin@example
  15. Subject: monit alert -- Timeout php-fpm
  16. Date: Thu, 24 Aug 2017 08:30:42 GMT
  17. Content-Type: text/plain; charset=utf-8
  18. Content-Transfer-Encoding: 8bit
  19. X-Mailer: Monit 5.23.0
  20. MIME-Version: 1.0
  21. Timeout Service php-fpm
  22. Date: Thu, 24 Aug 2017 10:30:42
  23. Action: unmonitor
  24. Host: web1.example
  25. Description: service restarted 6 times within 3 cycles(s) - unmonitor
  26. Your faithful employee,
  27. Monit
  28. "
  29. ticket_0, _article_p, _user_p, _mail = Channel::EmailParser.new.process({}, email_raw_string)
  30. assert_equal('new', ticket_0.state.name)
  31. assert(ticket_0.preferences)
  32. assert(ticket_0.preferences['monit'])
  33. assert_equal('unmonitor', ticket_0.preferences['monit']['action'])
  34. assert_equal('web1.example', ticket_0.preferences['monit']['host'])
  35. assert_equal('service restarted 6 times within 3 cycles(s) - unmonitor', ticket_0.preferences['monit']['description'])
  36. assert_equal('php-fpm', ticket_0.preferences['monit']['service'])
  37. assert_equal('CRITICAL', ticket_0.preferences['monit']['state'])
  38. email_raw_string = "Message-Id: <20160131094621.29ECD400F29C-monit-1-2@monitoring.zammad.com>
  39. From: monit@monitoring.example.com
  40. To: admin@example
  41. Subject: monit alert -- Action done php-fpm
  42. Date: Thu, 24 Aug 2017 08:30:42 GMT
  43. Content-Type: text/plain; charset=utf-8
  44. Content-Transfer-Encoding: 8bit
  45. X-Mailer: Monit 5.23.0
  46. MIME-Version: 1.0
  47. Action done Service php-fpm
  48. Date: Thu, 24 Aug 2017 10:37:39
  49. Action: alert
  50. Host: web1.example
  51. Description: monitor action done
  52. Your faithful employee,
  53. Monit"
  54. ticket_0_1, _article_p, _user_p, _mail = Channel::EmailParser.new.process({}, email_raw_string)
  55. assert_equal('closed', ticket_0_1.state.name)
  56. assert(ticket_0_1.preferences)
  57. assert(ticket_0_1.preferences['monit'])
  58. assert_equal('unmonitor', ticket_0.preferences['monit']['action'])
  59. assert_equal('web1.example', ticket_0_1.preferences['monit']['host'])
  60. assert_equal('service restarted 6 times within 3 cycles(s) - unmonitor', ticket_0_1.preferences['monit']['description'])
  61. assert_equal('php-fpm', ticket_0_1.preferences['monit']['service'])
  62. assert_equal('CRITICAL', ticket_0_1.preferences['monit']['state'])
  63. assert_equal(ticket_0_1.id, ticket_0.id)
  64. # Service
  65. email_raw_string = "Message-Id: <20160131094621.29ECD400F29C-monit-2-1@monitoring.zammad.com>
  66. From: monit@monitoring.example.com
  67. To: admin@example
  68. Subject: monit alert -- Connection failed host.example
  69. Date: Thu, 24 Aug 2017 08:30:42 GMT
  70. Content-Type: text/plain; charset=utf-8
  71. Content-Transfer-Encoding: quoted-printable
  72. X-Mailer: Monit 5.23.0
  73. MIME-Version: 1.0
  74. Connection failed Service host.example=20
  75. Date: Fri, 25 Aug 2017 02:28:31
  76. Action: alert
  77. Host: web5.host.example
  78. Description: failed protocol test [HTTP] at [host.example]:80 [TCP/I=
  79. P] -- HTTP: Error receiving data -- Resource temporarily unavailable
  80. Your faithful employee,
  81. Monit"
  82. ticket_1, _article_p, _user_p, _mail = Channel::EmailParser.new.process({}, email_raw_string)
  83. assert_equal('new', ticket_1.state.name)
  84. assert(ticket_1.preferences)
  85. assert(ticket_1.preferences['monit'])
  86. assert_equal('alert', ticket_1.preferences['monit']['action'])
  87. assert_equal('web5.host.example', ticket_1.preferences['monit']['host'])
  88. assert_equal('failed protocol test [HTTP] at [host.example]:80 [TCP/IP] -- HTTP: Error receiving data -- Resource temporarily unavailable', ticket_1.preferences['monit']['description'])
  89. assert_equal('host.example', ticket_1.preferences['monit']['service'])
  90. assert_equal('CRITICAL', ticket_1.preferences['monit']['state'])
  91. email_raw_string = "Message-Id: <20160131094621.29ECD400F29C-monit-2-2@monitoring.zammad.com>
  92. From: monit@monitoring.example.com
  93. To: admin@example
  94. Subject: monit alert -- Connection succeeded host.example
  95. Date: Thu, 24 Aug 2017 08:30:42 GMT
  96. Content-Type: text/plain; charset=utf-8
  97. Content-Transfer-Encoding: quoted-printable
  98. X-Mailer: Monit 5.23.0
  99. MIME-Version: 1.0
  100. Connection succeeded Service host.example=20
  101. Date: Fri, 25 Aug 2017 02:29:13
  102. Action: alert
  103. Host: web5.host.example
  104. Description: connection succeeded to [host.example]:80 [TCP/IP]
  105. Your faithful employee,
  106. Monit"
  107. ticket_1_1, _article_p, _user_p, _mail = Channel::EmailParser.new.process({}, email_raw_string)
  108. assert_equal('closed', ticket_1_1.state.name)
  109. assert(ticket_1_1.preferences)
  110. assert(ticket_1_1.preferences['monit'])
  111. assert_equal('alert', ticket_1.preferences['monit']['action'])
  112. assert_equal('web5.host.example', ticket_1_1.preferences['monit']['host'])
  113. assert_equal('failed protocol test [HTTP] at [host.example]:80 [TCP/IP] -- HTTP: Error receiving data -- Resource temporarily unavailable', ticket_1_1.preferences['monit']['description'])
  114. assert_equal('host.example', ticket_1_1.preferences['monit']['service'])
  115. assert_equal('CRITICAL', ticket_1_1.preferences['monit']['state'])
  116. assert_equal(ticket_1_1.id, ticket_1.id)
  117. # Resource Limit
  118. email_raw_string = "Message-Id: <20160131094621.29ECD400F29C-monit-3-1@monitoring.zammad.com>
  119. From: monit@monitoring.example.com
  120. To: admin@example
  121. Subject: monit alert -- Resource limit matched web5.example.net
  122. Date: Thu, 24 Aug 2017 08:30:42 GMT
  123. Content-Type: text/plain; charset=utf-8
  124. Content-Transfer-Encoding: quoted-printable
  125. X-Mailer: Monit 5.23.0
  126. MIME-Version: 1.0
  127. Resource limit matched Service web5.example.net=20
  128. Date: Fri, 25 Aug 2017 02:02:08
  129. Action: exec
  130. Host: web5.example.net
  131. Description: loadavg(1min) of 10.7 matches resource limit [loadavg(1min) >=
  132. 6.0]
  133. Your faithful employee,
  134. Monit"
  135. ticket_2, _article_p, _user_p, _mail = Channel::EmailParser.new.process({}, email_raw_string)
  136. assert_equal('new', ticket_2.state.name)
  137. assert(ticket_2.preferences)
  138. assert(ticket_2.preferences['monit'])
  139. assert_equal('exec', ticket_2.preferences['monit']['action'])
  140. assert_equal('web5.example.net', ticket_2.preferences['monit']['host'])
  141. assert_equal('loadavg(1min) of 10.7 matches resource limit [loadavg(1min) > 6.0]', ticket_2.preferences['monit']['description'])
  142. assert_equal('web5.example.net', ticket_2.preferences['monit']['service'])
  143. assert_equal('CRITICAL', ticket_2.preferences['monit']['state'])
  144. email_raw_string = "Message-Id: <20160131094621.29ECD400F29C-monit-3-2@monitoring.zammad.com>
  145. From: monit@monitoring.example.com
  146. To: admin@example
  147. Subject: monit alert -- Resource limit succeeded web5.example.net
  148. Date: Thu, 24 Aug 2017 08:30:42 GMT
  149. Content-Type: text/plain; charset=utf-8
  150. Content-Transfer-Encoding: quoted-printable
  151. X-Mailer: Monit 5.23.0
  152. MIME-Version: 1.0
  153. Resource limit succeeded Service web5.example.net=20
  154. Date: Fri, 25 Aug 2017 02:05:18
  155. Action: alert
  156. Host: web5.example.net
  157. Description: loadavg(1min) check succeeded [current loadavg(1min) =3D 4.8]
  158. Your faithful employee,
  159. Monit"
  160. ticket_2_1, _article_p, _user_p, _mail = Channel::EmailParser.new.process({}, email_raw_string)
  161. assert_equal('closed', ticket_2_1.state.name)
  162. assert(ticket_2_1.preferences)
  163. assert(ticket_2_1.preferences['monit'])
  164. assert_equal('exec', ticket_2.preferences['monit']['action'])
  165. assert_equal('web5.example.net', ticket_2_1.preferences['monit']['host'])
  166. assert_equal('loadavg(1min) of 10.7 matches resource limit [loadavg(1min) > 6.0]', ticket_2_1.preferences['monit']['description'])
  167. assert_equal('web5.example.net', ticket_2_1.preferences['monit']['service'])
  168. assert_equal('CRITICAL', ticket_2_1.preferences['monit']['state'])
  169. assert_equal(ticket_2_1.id, ticket_2.id)
  170. end
  171. end