monit_spec.rb 6.8 KB

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