From d22164631db949a98f497f90b90e0827d7ddc517 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 21 Aug 2018 08:03:51 +0200 Subject: [PATCH] Improved regex to also match host without new line at the end. --- app/models/channel/filter/monitoring_base.rb | 3 +- test/unit/integration_icinga_test.rb | 47 ++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/app/models/channel/filter/monitoring_base.rb b/app/models/channel/filter/monitoring_base.rb index 1c7dd74ee..17c8db226 100644 --- a/app/models/channel/filter/monitoring_base.rb +++ b/app/models/channel/filter/monitoring_base.rb @@ -30,7 +30,8 @@ class Channel::Filter::MonitoringBase # get mail attibutes like host and state result = {} - mail[:body].gsub(%r{(Service|Host|State|Address|Date/Time|Additional\sInfo|Info|Action|Description):(.+?)\n}i) do |_match| + + mail[:body].gsub(%r{(Service|Host|State|Address|Date/Time|Additional\sInfo|Info|Action|Description):(.+?)(\n|$)}i) do |_match| key = $1 if key key = key.downcase diff --git a/test/unit/integration_icinga_test.rb b/test/unit/integration_icinga_test.rb index 5fecd80d3..0743c8aa6 100644 --- a/test/unit/integration_icinga_test.rb +++ b/test/unit/integration_icinga_test.rb @@ -745,5 +745,52 @@ IPv4: 192.168.1.8 assert(ticket_1.preferences) assert(ticket_1.preferences['icinga']) assert_equal('DOWN', ticket_1.preferences['icinga']['state']) + + end + + test 'match also values without new line at the end of a line' do + + email_raw_string = 'Return-Path: +Date: Tue, 21 Aug 2018 03:05:01 +0200 +To: hostmaster@example.com +Subject: [PROBLEM] OS Updates (yum) on host.example.com is CRITICAL! +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 +Message-Id: <20180821010501.4A182846DBA@monitoring.example.com> +From: icinga2@monitoring.example.com (icinga) + +***** Service Monitoring on monitoring.example.com ***** + +OS Updates (yum) on host.example.com is CRITICAL! + +Info: CHECK_UPDATES CRITICAL - 12 non-critical updates available=20 +audit-libs.x86_64 +dracut.x86_64 +initscripts.x86_64 +kpartx.x86_64 +libblkid.x86_64 +libmount.x86_64 +libuuid.x86_64 +mariadb-libs.x86_64 +systemd.x86_64 +systemd-libs.x86_64 +systemd-sysv.x86_64 +util-linux.x86_64 + +When: 2018-08-21 03:05:01 +0200 +Service: OS Updates (yum) +Host: host.example.com' + + ticket_1, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) + assert_equal('new', ticket_1.state.name) + assert(ticket_1.preferences) + assert(ticket_1.preferences['icinga']) + assert_equal('CRITICAL', ticket_1.preferences['icinga']['state']) + assert_equal('CHECK_UPDATES CRITICAL - 12 non-critical updates available', ticket_1.preferences['icinga']['info']) + assert_equal('OS Updates (yum)', ticket_1.preferences['icinga']['service']) + assert_equal('host.example.com', ticket_1.preferences['icinga']['host']) + end end