integration_monit_test.rb 8.0 KB

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