Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing Email Subject Line

Status
Not open for further replies.

ponoodle

IS-IT--Management
Dec 10, 2010
135
0
0
US
I have a VM running Ubuntu 8.04 that we use as our Nagios server. I have been trying to set it up so that I can reply to the alert emails I receive and acknowledge problems. I am using the directions from this page.
Here is the code from that page.
Code:
#!/usr/bin/perl
 
$correctpassword = 'whatever';   # more of a sanity check than a password and can be anything
$subject = "$ARGV[0]";
$now = `/bin/date +%s`;
chomp $now;
$commandfile = '/usr/local/nagios/var/rw/nagios.cmd';
 
if ($subject =~ /Host/ ){      # this parses the subject of your email
        ($password, $what, $junk, $junk, $junk, $junk, $junk, $host) = split(/ /, $subject);
        ($host) = ($host) =~ /(.*)\!/;
} else {
        ($foo, $bar) = split(/\//, $subject);
        ($password, $what, $junk, $junk, $junk, $junk, $junk, $host) = split(/\ /, $foo);
        ($service) = $bar =~ /^(.*) is.*$/;
}
 
$password =~ s/^\s+//;
$password =~ s/\s+$//;
 
print "$password\t$what\t$host\t$service\n";
 
unless ($password =~ /$correctpassword/i) {
        print "exiting...wrong password\n";
        exit 1;
}
 
# ack - this is where the acknowledgement happens
# you could get creative with this and pass all kinds of things via email
# a list of external commands here: [URL unfurl="true"]http://www.nagios.org/development/apis/externalcommands/[/URL]
if ($subject =~ /Host/ ) {
        $ack =
"ACKNOWLEDGE_HOST_PROBLEM;$host;1;1;1;email;email;acknowledged through email";
} else {
        $ack = "ACKNOWLEDGE_SVC_PROBLEM;$host;$service;1;1;1;email;acknowledged through email";
}
 
if ($what =~ /ack/i) {
        sub_print("$ack");
} else {
        print "no valid commands...exiting\n";
        exit 1;
}
 
 
sub sub_print {
        $narf=shift;
        open(F, ">$commandfile") or die "cant";
        print F "[$now] $narf\n";
        close F;
}

This works for services but not for hosts. In the syslog, I get this: "nagios: EXTERNAL COMMAND: ACKNOWLEDGE_HOST_PROBLEM;;1;1;1;email;email;acknowledged through email"
It seems that the $host variable is not working for host problems. When I reply to the Nagios email, the subject would be "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" for host alerts, and "** $NOTIFICATIONTYPE$ Service Alert: $HOSTNAME$/$SERVICEDESC$ is $SERVICESTATE$ **" for services.
If it helps, this is from the procmail.log.
Code:
procmail: [17841] Mon Mar 28 11:36:42 2011
procmail: Assigning "PATH=/usr/bin"
procmail: Assigning "MATCH="
procmail: Matched ""PASSWORD" ack RE: ** PROBLEM Host Alert: "HOSTNAME" is DOWN **"
procmail: Match on "^Subject:[    ]*\/[^  ].*"
procmail: Executing "/usr/lib/nagios/eventhandlers/processmail,"PASSWORD" ack RE: ** PROBLEM Host Alert: "HOSTNAME" is DOWN **"
procmail: Assigning "LASTFOLDER=/usr/lib/nagios/eventhandlers/processmail "PASSWORD" ack RE: ** PROBLEM Host Alert: "HOSTNAME" is DOWN **"
procmail: Notified comsat: "nagios@:/usr/lib/nagios/eventhandlers/processmail "PASSWORD" ack RE: ** PROBLEM Host Alert: "HOSTNAME" is DOWN **"
From ME@MYEMAIL.COM  Mon Mar 28 11:36:42 2011
 Subject: "PASSWORD" ack RE: ** PROBLEM Host Alert: "HOSTNAME" is DOWN **
  Folder: /usr/lib/nagios/eventhandlers/processmail "PASSWORD" ack RE: ** PRO     1685
Anybody have any ideas as to why it won't work for host alerts?

Thanks
 
Are you sure that $ARGV[0] actually contains the whole subject line and not just the first word? This might account for why it only 'works' for services becaues it goes down the else path.

Try putting in a
Code:
print '$subject=', $subject, "\n";
before the match attempt to see what you are actually getting passed through. While we are at it, let's go with
Code:
use strict;
use warnings;
at the top, too.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
I added what you suggested, but I don't know where to look for the print output. I did grab the log though.

Code:
procmail: [18404] Mon Apr  4 14:45:44 2011
procmail: Assigning "PATH=/usr/bin"
procmail: Assigning "MATCH="
procmail: Matched "password ack RE: ** PROBLEM Host Alert: HOST is DOWN **"
procmail: Match on "^Subject:[    ]*\/[^  ].*"
procmail: Executing "/usr/lib/nagios/eventhandlers/processmail,password ack RE: ** PROBLEM Host Alert: HOST is DOWN **"
Global symbol "$correctpassword" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 7.
Global symbol "$subject" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 8.
Global symbol "$now" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 9.
Global symbol "$now" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 10.
Global symbol "$commandfile" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 11.
Global symbol "$subject" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 13.
Global symbol "$subject" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 15.
Global symbol "$password" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 16.
Global symbol "$what" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 16.
Global symbol "$junk" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 16.
Global symbol "$junk" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 16.
Global symbol "$junk" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 16.
Global symbol "$junk" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 16.
Global symbol "$junk" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 16.
Global symbol "$host" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 16.
Global symbol "$subject" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 16.
Global symbol "$host" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 17.
Global symbol "$host" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 17.
Global symbol "$foo" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 19.
Global symbol "$bar" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 19.
Global symbol "$subject" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 19.
Global symbol "$password" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 20.
Global symbol "$what" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 20.
Global symbol "$junk" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 20.
Global symbol "$junk" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 20.
Global symbol "$junk" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 20.
Global symbol "$junk" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 20.
Global symbol "$junk" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 20.
Global symbol "$host" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 20.
Global symbol "$foo" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 20.
Global symbol "$service" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 21.
Global symbol "$bar" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 21.
Global symbol "$password" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 24.
Global symbol "$password" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 25.
Global symbol "$password" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 27.
Global symbol "$what" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 27.
Global symbol "$host" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 27.
Global symbol "$service" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 27.
Global symbol "$password" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 29.
Global symbol "$correctpassword" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 29.
Global symbol "$subject" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 37.
Global symbol "$ack" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 38.
Global symbol "$host" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 38.
Global symbol "$ack" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 40.
Global symbol "$host" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 40.
Global symbol "$service" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 40.
Global symbol "$what" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 43.
Global symbol "$ack" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 44.
Global symbol "$narf" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 52.
Global symbol "$commandfile" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 53.
Global symbol "$now" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 54.
Global symbol "$narf" requires explicit package name at /usr/lib/nagios/eventhandlers/processmail line 54.
Execution of /usr/lib/nagios/eventhandlers/processmail aborted due to compilation errors.
procmail: Error while writing to "/usr/lib/nagios/eventhandlers/processmail"
procmail: Assigning "LASTFOLDER=/usr/lib/nagios/eventhandlers/processmail password ack RE: ** PROBLEM Host Alert: HOST is DOWN **"
procmail: Locking "/var/mail/nagios.lock"
procmail: Assigning "LASTFOLDER=/var/mail/nagios"
procmail: Opening "/var/mail/nagios"
procmail: Acquiring kernel-lock
procmail: Unlocking "/var/mail/nagios.lock"
procmail: Notified comsat: "nagios@19966:/var/mail/nagios"
From me@myemail.com  Mon Apr  4 14:45:44 2011
 Subject: password ack RE: ** PROBLEM Host Alert: HOST is DOWN **
  Folder: /var/mail/nagios                                                 1704
If it isn't obvious, 100% of what I know about Perl, I learned trying to fix this problem. And that is not much.

Thanks again.
 
OK, after some more reading I think I fixed some of those errors. Here is the code now.
Code:
#!/usr/bin/perl
use strict;
use warnings;

my $correctpassword = 'whatever';   # more of a sanity check than a password and can be anything
my $subject = "$ARGV[0]";
my $now = `/bin/date +%s`;
chomp $now;
my $commandfile = '/usr/local/nagios/var/rw/nagios.cmd';
my $password;
my $what;
my $junk;
my $host;
my $foo;
my $bar;
my $service;
my $ack;
my $narf

print '$subject=', $subject, "\n";

if ($subject =~ /Host/ ){      # this parses the subject of your email
        ($password, $what, $junk, $junk, $junk, $junk, $junk, $host) = split(/ /, $subject);
        ($host) = ($host) =~ /(.*)\!/;
} else {
        ($foo, $bar) = split(/\//, $subject);
        ($password, $what, $junk, $junk, $junk, $junk, $junk, $host) = split(/\ /, $foo);
        ($service) = $bar =~ /^(.*) is.*$/;
}

$password =~ s/^\s+//;
$password =~ s/\s+$//;

print "$password\t$what\t$host\t$service\n";

unless ($password =~ /$correctpassword/i) {
        print "exiting...wrong password\n";
        exit 1;
}

# ack - this is where the acknowledgement happens
# you could get creative with this and pass all kinds of things via email
# a list of external commands here: [URL unfurl="true"]http://www.nagios.org/development/apis/externalcommands/[/URL]
if ($subject =~ /Host/ ) {
        $ack = "ACKNOWLEDGE_HOST_PROBLEM;$host;1;1;1;email;email;acknowledged through email";
} else {
        $ack = "ACKNOWLEDGE_SVC_PROBLEM;$host;$service;1;1;1;email;acknowledged through email";
}

if ($what =~ /ack/i) {
        sub_print("$ack");
} else {
        print "no valid commands...exiting\n";
        exit 1;
}


sub sub_print {
        $narf=shift;
        open(F, ">$commandfile") or die "cant";
        print F "[$now] $narf\n";
        close F;
}

and here is the new log.
Code:
procmail: [25907] Thu Apr  7 10:31:08 2011
procmail: Assigning "PATH=/usr/bin"
procmail: Assigning "MATCH="
procmail: Matched "whatever ack RE: ** PROBLEM Host Alert: HOST is DOWN **"
procmail: Match on "^Subject:[    ]*\/[^  ].*"
procmail: Executing "/usr/lib/nagios/eventhandlers/processmail,whatever ack RE: ** PROBLEM Host Alert: HOST is DOWN **"
syntax error at /usr/lib/nagios/eventhandlers/processmail line 20, near "$narf

print"
Execution of /usr/lib/nagios/eventhandlers/processmail aborted due to compilation errors.
procmail: Error while writing to "/usr/lib/nagios/eventhandlers/processmail"
procmail: Assigning "LASTFOLDER=/usr/lib/nagios/eventhandlers/processmail whatever ack RE: ** PROBLEM Host Alert: HOST is DOWN **"
procmail: Locking "/var/mail/nagios.lock"
procmail: Assigning "LASTFOLDER=/var/mail/nagios"
procmail: Opening "/var/mail/nagios"
procmail: Acquiring kernel-lock
procmail: Unlocking "/var/mail/nagios.lock"
procmail: Notified comsat: "nagios@23373:/var/mail/nagios"
From me@myemail.com  Thu Apr  7 10:31:08 2011
 Subject: whatever ack RE: ** PROBLEM Host Alert: HOST is DOWN **
  Folder: /var/mail/nagios                                                 1704

Any help would be awesome. Thanks
 
So the last one was actually pretty easy. I added the semicolon after $narf and that fixed the last error. Now I am getting this, and I am finding that it is not so simple.
Code:
procmail: Assigning "PATH=/usr/bin"
procmail: Assigning "MATCH="
procmail: Matched "PASSWORD ack RE: ** PROBLEM Host Alert: HOST is DOWN **"
procmail: Match on "^Subject:[    ]*\/[^  ].*"
procmail: Executing "/usr/lib/nagios/eventhandlers/processmail,PASSWORD ack RE: ** PROBLEM Host Alert: HOST is DOWN **"
procmail: Assigning "LASTFOLDER=/usr/lib/nagios/eventhandlers/processmail PASSWORD ack RE: ** PROBLEM Host Alert: HOST is DOWN **"
procmail: Notified comsat: "nagios@:/usr/lib/nagios/eventhandlers/processmail PASSWORD ack RE: ** PROBLEM Host Alert: HOST is DOWN **"
From me@myemail.com  Mon Apr 11 07:21:42 2011
 Subject: PASSWORD ack RE: ** PROBLEM Host Alert: HOST is DOWN **
  Folder: /usr/lib/nagios/eventhandlers/processmail PASSWORD ack RE: ** PRO     1683
Use of uninitialized value in concatenation (.) or string at /usr/lib/nagios/eventhandlers/processmail line 34.
Use of uninitialized value in concatenation (.) or string at /usr/lib/nagios/eventhandlers/processmail line 34.
Use of uninitialized value in concatenation (.) or string at /usr/lib/nagios/eventhandlers/processmail line 45.
 
I commented out this line and everything works fine now.
Code:
($host) = ($host) =~ /(.*)\!/;
If anybody knows why, I am still curious. It was just a lucky guess.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top