integration_nagios_test.rb 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. #Setting.set('nagios_integration', false)
  142. end
  143. end