From 1c941ec317adc3f0e86e43b00bb0a2eda1f50001 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Fri, 29 Sep 2017 16:59:10 +0200 Subject: [PATCH] Improved state detection for host down/up with new icinga email templates. --- app/models/channel/filter/monitoring_base.rb | 10 ++-- test/unit/integration_icinga_test.rb | 62 +++++++++++++++++++- 2 files changed, 66 insertions(+), 6 deletions(-) diff --git a/app/models/channel/filter/monitoring_base.rb b/app/models/channel/filter/monitoring_base.rb index eade38c80..f06d5477e 100644 --- a/app/models/channel/filter/monitoring_base.rb +++ b/app/models/channel/filter/monitoring_base.rb @@ -46,17 +46,19 @@ class Channel::Filter::MonitoringBase # check min. params return if result['host'].blank? - # get state from body + # get state by body - ichinga new templates if result['state'].blank? if mail[:body] =~ /==>.*\sis\s(.+?)\!\s+?<==/ result['state'] = $1 end end - # get state from subject + # get state by subject - ichinga new templates "state:" is not in body anymore + # Subject: [PROBLEM] Ping IPv4 on host1234.dc.example.com is WARNING! + # Subject: [PROBLEM] Host host1234.dc.example.com is DOWN! if result['state'].blank? - if mail[:subject] =~ /on\s.+?\sis\s(.+?)\!/ - result['state'] = $1 + if mail[:subject] =~ /(on|Host)\s.+?\sis\s(.+?)\!/ + result['state'] = $2 end end diff --git a/test/unit/integration_icinga_test.rb b/test/unit/integration_icinga_test.rb index e5c6b8052..2106bd6c4 100644 --- a/test/unit/integration_icinga_test.rb +++ b/test/unit/integration_icinga_test.rb @@ -319,7 +319,7 @@ Comment: [] = Subject: [PROBLEM] Ping IPv4 on apn4711.dc.example.com is WARNING! From: icinga2@monitoring.example.com (icinga) -***** Service Monitoring on monitoring.znuny.com ***** +***** Service Monitoring on monitoring.example.com ***** Ping IPv4 on apn4711.dc.example.com is WARNING! @@ -344,7 +344,7 @@ IPv4: 127.0.0.1=" Subject: [RECOVERY] Ping IPv4 on apn4711.dc.example.com is OK! From: icinga2@monitoring.example.com (icinga) -***** Service Monitoring on monitoring.znuny.com ***** +***** Service Monitoring on monitoring.example.com ***** Ping IPv4 on apn4711.dc.example.com is OK! @@ -363,6 +363,64 @@ IPv4: 127.0.0.1=" assert_equal('apn4711.dc.example.com', ticket_4.preferences['icinga']['host']) assert_equal('Ping IPv4', ticket_4.preferences['icinga']['service']) assert_equal('WARNING', ticket_4_1.preferences['icinga']['state']) + + # host down + email_raw_string = "To: support@example.com +Subject: [PROBLEM] Host apn4709.dc.example.com is DOWN! +User-Agent: Heirloom mailx 12.5 7/5/10 +MIME-Version: 1.0 +Content-Type: text/plain; charset=us-ascii +Content-Transfer-Encoding: quoted-printable +From: icinga2@monitoring.example.com (icinga) + +***** Host Monitoring on monitoring.example.com ***** + +apn4709.dc.example.com is DOWN! + +Info: CRITICAL - Plugin timed out + + +When: 2017-09-29 14:19:40 +0200 +Host: apn4709.dc.example.com +IPv4:=09 127.0.0.1=" + + ticket_5, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) + assert_equal('new', ticket_5.state.name) + assert(ticket_5.preferences) + assert(ticket_5.preferences['icinga']) + assert_equal('apn4709.dc.example.com', ticket_5.preferences['icinga']['host']) + assert_nil(ticket_5.preferences['icinga']['service']) + assert_equal('DOWN', ticket_5.preferences['icinga']['state']) + assert_not_equal(ticket_5.id, ticket_1.id) + + # host up + email_raw_string = "To: support@example.com +Subject: [RECOVERY] Host apn4709.dc.example.com is UP! +User-Agent: Heirloom mailx 12.5 7/5/10 +MIME-Version: 1.0 +Content-Type: text/plain; charset=us-ascii +Content-Transfer-Encoding: quoted-printable +From: icinga2@monitoring.example.com (icinga) + +***** Host Monitoring on monitoring.example.com ***** + +apn4709.dc.example.com is UP! + +Info: PING OK - Packet loss =3D 0%, RTA =3D 20.20 ms + +When: 2017-09-29 14:23:36 +0200 +Host: apn4709.dc.example.com +IPv4:=09 127.0.0.1= +" + ticket_5_1, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) + assert_equal(ticket_5.id, ticket_5_1.id) + assert_equal('closed', ticket_5_1.state.name) + assert(ticket_5_1.preferences) + assert(ticket_5_1.preferences['icinga']) + assert_equal('apn4709.dc.example.com', ticket_5.preferences['icinga']['host']) + assert_nil(ticket_5_1.preferences['icinga']['service']) + assert_equal('DOWN', ticket_5_1.preferences['icinga']['state']) + end test 'not matching sender tests' do