send notifications
to have a notification with nagmin
i use the table Notifications, without alert service if host is down
I have modify nagmin_event,nagmin_notify and Notify.pm and add 2 scripts : nagmin_notification ( just
for test) and SendNotify.pm ( call by nagmin_event )
nagmin_event _______________________________________________________________________
#!/usr/bin/perl
#-------------------------------------------------------------------
# NagMIN is Copyright 2003-2004 Frederick Reimers.
#-------------------------------------------------------------------
# Please read the legal notices (docs/legal.txt) and the license
# (docs/license.txt) that came with this distribution before using
# this software.
#-------------------------------------------------------------------
# http://nagmin.sourceforge.net
#-------------------------------------------------------------------
our ($nagminRoot, $configFile);
BEGIN {
$configFile = "NagMIN.conf";
$nagminRoot = "/usr/local/nagmin/";
unshift ( <at> INC, $nagminRoot."/lib");
}
#-----------------DO NOT MODIFY BELOW THIS LINE--------------------
use strict;
use NagMIN::Config;
use NagMIN::Event;
use NagMIN::SendNotify;
Config->new($nagminRoot,$configFile);
Event->main();
sleep(1);
SendNotify->main();
sleep(1);
_______________________________________________________________________________
nagmin_notification _______________________________________________________________________
#!/usr/bin/perl
#-------------------------------------------------------------------
# NagMIN is Copyright 2003-2004 Frederick Reimers.
#-------------------------------------------------------------------
# Please read the legal notices (docs/legal.txt) and the license
# (docs/license.txt) that came with this distribution before using
# this software.
#-------------------------------------------------------------------
# http://nagmin.sourceforge.net
#-------------------------------------------------------------------
our ($nagminRoot, $configFile);
BEGIN {
$configFile = "NagMIN.conf";
$nagminRoot = "/usr/local/nagmin/";
unshift ( <at> INC, $nagminRoot."/lib");
}
#-----------------DO NOT MODIFY BELOW THIS LINE--------------------
use strict;
use NagMIN::Config;
use NagMIN::SendNotify;
Config->new($nagminRoot,$configFile);
SendNotify->main();
_______________________________________________________________________________
nagmin_notify _______________________________________________________________________
#!/usr/bin/perl
#-------------------------------------------------------------------
# NagMIN is Copyright 2003-2004 Frederick Reimers.
#-------------------------------------------------------------------
# Please read the legal notices (docs/legal.txt) and the license
# (docs/license.txt) that came with this distribution before using
# this software.
#-------------------------------------------------------------------
# http://nagmin.sourceforge.net
#-------------------------------------------------------------------
our ($nagminRoot, $configFile);
BEGIN {
$configFile = "NagMIN.conf";
$nagminRoot = "/usr/local/nagmin/";
unshift ( <at> INC, $nagminRoot."/lib");
}
#-----------------DO NOT MODIFY BELOW THIS LINE--------------------
use strict;
use NagMIN::Config;
use NagMIN::Notify;
Config->new($nagminRoot,$configFile);
sleep(2);
Notify->main();
_______________________________________________________________________________
SendNotify.pm _______________________________________________________________________
package SendNotify;
#----------------------------------------------------------
# SendNotify.pm v 2.2.0
#
# NagMIN Cron Script for processing notifications written to
# the NagMIN Notifications table by nagmin_notify
#
# Copyright (c) 2003 Frederick Reimers
#
# Latest version of this software: http://sourceforge.net/nagmin
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307
#----------------------------------------------------------
use strict;
use DBI;
use NagMIN::Config;
our %info;
our ($connectString,$dbh,$sql,$sth,$message,$subject,$host_name,$cmd,$h,$s);
our ( <at> contacts, <at> contact_groups, <at> groups, <at> host_name, <at> contact,$i,$j,$k,$l);
our ( <at> host_state, <at> service_description, <at> service_state, <at> output, <at> timet);
sub main {
%info = Config->info();
# Connect to database
$connectString = "DBI:$info{'dbType'}:";
$connectString .= "$info{'dbName'}:$info{'dbHost'}:$info{'dbPort'}";
$dbh = DBI->connect($connectString,
$info{'dbUser'},
$info{'dbPassword'});
my $notification_table='HOST';
my <at> row= ();
my $timeset = time;
$timeset = $timeset - 900;
$timeset = $timeset - 36000;
#print $timeset;
$sql = "select Host.contact_groups,HostAlerts.* ";
$sql .= "from HostAlerts,Host ";
$sql .= "where HostAlerts.host_name=Host.host_name AND timet >= ".$timeset;
$sth = $dbh->prepare($sql);
$sth->execute();
while ( ( <at> row) = $sth->fetchrow_array()) {
push ( <at> contact_groups,$row[0]);
push ( <at> host_name,$row[1]);
push ( <at> host_state,$row[2]);
push ( <at> output,$row[3]);
push ( <at> timet,$row[7]);
}
for ($i=0;$i< <at> contact_groups;$i++) {
my <at> contact_host= ();
<at> contact_host=get_contact( <at> contact_groups[$i]);
for ($j=0;$j< <at> contact_host;$j++) {
my $notification_method= '';
my <at> notification_commands= ();
$sql = "select host_notification_commands from Contact where contact_name='". <at> contact_host[$j]."'";
$sth = $dbh->prepare($sql);
$sth->execute();
while ( ( <at> row) = $sth->fetchrow_array()) { push ( <at> notification_commands,$row[0]); }
if ( <at> notification_commands[0] eq 'nagmin_host_notify_email' ) { $notification_method='EMAIL';}
elsif ( <at> notification_commands[0] eq 'nagmin_host_notify_pager' ) { $notification_method='PAGER';}
if (!($notification_method eq '')) {set_notify($timeset,$notification_table,$notification_method, <at> contact_host[$j], <at> host_name[$i], <at> host_state[$i], <at> output[$i], <at> timet[$i],'','');}
}
}
my <at> row2= ();
my $notification_table='SERVICE';
<at> contact_groups= ();
<at> host_name = ();
<at> host_state = ();
<at> output = ();
<at> timet = ();
$sql = "select Host.contact_groups,ServiceAlerts.* ";
$sql .= "from ServiceAlerts,Host ";
$sql .= "where ServiceAlerts.host_name=Host.host_name AND timet >= ".$timeset;
$sth = $dbh->prepare($sql);
$sth->execute();
while ( ( <at> row2) = $sth->fetchrow_array()) {
push ( <at> contact_groups,$row2[0]);
push ( <at> host_name,$row2[1]);
push ( <at> host_state,$row2[2]);
push ( <at> service_description,$row2[3]);
push ( <at> service_state,$row2[4]);
push ( <at> output,$row2[5]);
push ( <at> timet,$row2[10]);
}
for ($i=0;$i< <at> contact_groups;$i++) {
my <at> contact_host= ();
<at> contact_host=get_contact( <at> contact_groups[$i]);
for ($j=0;$j< <at> contact_host;$j++) {
my $notification_method= '';
my <at> notification_commands= ();
$sql = "select service_notification_commands from Contact where contact_name='". <at> contact_host[$j]."'";
$sth = $dbh->prepare($sql);
$sth->execute();
while ( ( <at> row) = $sth->fetchrow_array()) { push ( <at> notification_commands,$row[0]); }
if ( <at> notification_commands[0] eq 'nagmin_service_notify_email' ) { $notification_method='EMAIL';}
elsif ( <at> notification_commands[0] eq 'nagmin_service_notify_pager' ) {
$notification_method='PAGER';}
if ( <at> host_state[$i] == 'UP') {
if (!($notification_method eq ''))
{set_notify($timeset,$notification_table,$notification_method, <at> contact_host[$j], <at> host_name[$i], <at> host_state[$i], <at> output[$i], <at> timet[$i], <at> service_description[$i], <at> service_state[$i]);}
}
}
}
$sth->finish();
$dbh->disconnect();
exit;
}
sub get_contact {
my <at> contact_host= ();
my %is_contact_host= ();
my <at> groups= ();
my <at> row1= ();
<at> groups=split(/,/, $_[0]);
for ($j=0;$j< <at> groups;$j++) {
$sql = "select members from ContactGroup where contactgroup_name='". <at> groups[$j]."'";
$sth = $dbh->prepare($sql);
$sth->execute();
<at> contact= ();
while ( ( <at> row1) = $sth->fetchrow_array()) { push ( <at> contact,$row1[0]); }
for ($k=0;$k< <at> contact;$k++) {
<at> contacts=split(/,/, <at> contact[$k]);
undef %is_contact_host;
for ( <at> contact_host) { $is_contact_host{$_} = 1; }
for ($l=0;$l< <at> contacts;$l++) {
if (not $is_contact_host{ <at> contacts[$l]}) {push ( <at> contact_host, <at> contacts[$l]);} }
}
}
return <at> contact_host;
}
sub set_notify {
my <at> row1= ();
my $mytimet=$_[7]-1800;
$sql = "select * from Notifications where notification_table='".$_[1]."' and notification_method='".$_[2];
$sql .= "' and contact_name='".$_[3]."' and host_name='".$_[4]."' and host_state='".$_[5]."' ";
$sql .= " and output='".$_[6]."' and timet>= '".$mytimet."' ";
if ( ! ($_[8] eq '')) {$sql .= " and service_description='".$_[8]."' ";}
if ( ! ($_[9] eq '')) {$sql .= " and service_state='".$_[9]."' ";}
$sth = $dbh->prepare($sql);
$sth->execute();
$k=0;
while ( ( <at> row1) = $sth->fetchrow_array()) { $k++; }
if ( $k==0 ) {
$sql="insert into Notifications values ('$_[1]','$_[2]','$_[3]','$_[4]','$_[5]','$_[6]','','','$_[7]','$_[8]','$_[9]',NULL,NULL)";
$sth = $dbh->prepare($sql);
$sth->execute();
}
}
1;
_______________________________________________________________________________
Notify.pm
_______________________________________________________________________
package Notify;
#----------------------------------------------------------
# Notify.pm v 2.2.0
#
# NagMIN Cron Script for processing notifications written to
# the NagMIN Notifications table by nagmin_notify
#
# Copyright (c) 2003 Frederick Reimers
#
# Latest version of this software: http://sourceforge.net/nagmin
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307
#----------------------------------------------------------
use strict;
use DBI;
use NagMIN::Config;
our %info;
our ($connectString,$dbh,$sql,$sth,$message,$subject,$host_name,$cmd,$h,$s);
our ($k,$index,$m, <at> service_description, <at> j, <at> host_name, <at> table, <at> contact_pager);
our ($cnt, <at> service_plugin_output, <at> notification_type, <at> output, <at> service_status, <at> host_status);
our ($i,$process, <at> id,$current_method, <at> contact_email, <at> method,$notify,$current_contact);
our ( <at> contact_name, <at> row, <at> host_state, <at> notification_number, <at> time_stamp, <at> service_state);
our ( <at> sent, <at> last_change, <at> problem_acknowledged, <at> plugin_output, <at> service_last_change);
our ( <at> service_problem_acknowledged);
sub main {
%info = Config->info();
# Connect to database
$connectString = "DBI:$info{'dbType'}:";
$connectString .= "$info{'dbName'}:$info{'dbHost'}:$info{'dbPort'}";
$dbh = DBI->connect($connectString,
$info{'dbUser'},
$info{'dbPassword'});
$sql = "select Notifications.*,hoststatus.host_status,hoststatus.last_state_change,";
$sql .= "hoststatus.problem_acknowledged,hoststatus.plugin_output, ";
$sql .= "servicestatus.service_status,servicestatus.last_state_change,";
$sql .=
"servicestatus.problem_acknowledged,servicestatus.plugin_output,Contact.email,Contact.pager ";
$sql .= "from Notifications,hoststatus,Contact ";
$sql .= "LEFT JOIN servicestatus ON ";
$sql .= "Notifications.host_name=servicestatus.host_name ";
$sql .= "AND Notifications.service_description=servicestatus.service_description ";
$sql .= "WHERE Notifications.host_name=hoststatus.host_name ";
$sql .= "AND Notifications.contact_name=Contact.contact_name ";
$sql .= "AND Notifications.sent IS NULL ";
$sql .= "ORDER BY contact_name,notification_method,host_name,service_description,id";
$sth = $dbh->prepare($sql);
$sth->execute();
while ( ( <at> row) = $sth->fetchrow_array()) {
push ( <at> table,$row[0]);
push ( <at> method,$row[1]);
push ( <at> contact_name,$row[2]);
push ( <at> host_name,$row[3]);
push ( <at> host_state,$row[4]);
push ( <at> output,$row[5]);
push ( <at> notification_type,$row[6]);
push ( <at> notification_number,$row[7]);
push ( <at> time_stamp,$row[8]);
push ( <at> service_description,$row[9]);
push ( <at> service_state,$row[10]);
push ( <at> sent,$row[11]);
push ( <at> id,$row[12]);
push ( <at> host_status,$row[13]);
push ( <at> last_change,$row[14]);
push ( <at> problem_acknowledged,$row[15]);
push ( <at> plugin_output,$row[16]);
push ( <at> service_status,$row[17]);
push ( <at> service_last_change,$row[18]);
push ( <at> service_problem_acknowledged,$row[19]);
push ( <at> service_plugin_output,$row[20]);
push ( <at> contact_email,$row[21]);
push ( <at> contact_pager,$row[22]);
}
$current_contact="";
$current_method="";
$index="";
for ($i=0;$i< <at> table;$i++) {
$notify=0;
$notify=1;
$m=$i;
$current_contact = $contact_name[$i];
$current_method=$method[$i];
if ($current_method eq "EMAIL" && defined($contact_email[$i])) { &email }
if ($current_method eq "PAGER" && defined($contact_pager[$i])) {
if ($contact_email[$i] ne $contact_pager[$i]) { &pager }
}
$process=1;
# if ($table[$i] eq "SERVICE" && $service_status[$i] eq "OK") {
# $process=0;
# }
# if ($table[$i] eq "HOST" && $host_status[$i] eq "UP") {
# $process=0;
# }
if ($process) {
$sql = "update Notifications set sent=\"1\" where id=\"$id[$i]\"";
} else {
$sql = "update Notifications set sent=\"2\" where id=\"$id[$i]\"";
}
$sth = $dbh->prepare($sql);
$sth->execute();
}
$sth->finish();
$dbh->disconnect();
exit;
}
sub email {
$subject="Nagios: ";
$message="";
$k=$m;
if ($table[$k] eq "SERVICE") {
$subject = "$host_name[$k] $service_description[$k]:$service_state[$k]";
$message = localtime( <at> time_stamp[$k])."\n";
if ($notification_type[$k] eq "ACKNOWLEDGEMENT") {
$message .= "$host_name[$k]($host_status[$k]) - $service_description[$k]($service_state[$k])(ACK)\n\t$output[$k]\n";
} else {
$message .= "$host_name[$k]($host_status[$k]) - $service_description[$k]($service_state[$k])\n\t$output[$k]\n";
#$message .= "$host_name[$k]($host_status[$k]) - $service_description[$k]($service_status[$k])\n\t$service_plugin_output[$k]\n";
}
} else {
$subject = "$host_name[$k] : $host_state[$k] ";
$message = localtime( <at> time_stamp[$k])."\n";
if ($notification_type[$k] eq "ACKNOWLEDGEMENT") {
$message .= "$host_name[$k]($host_state[$k])(ACK)\n\t$output[$k]\n";
} else {
$message .= "$host_name[$k]($host_state[$k])\n\t$output[$k]\n";
}
}
$cmd = "/bin/echo \"$message\" | /usr/bin/mail -s \"$subject\" $contact_email[$m]";
system ($cmd);
sleep(1);
$index="";
return;
}
sub pager {
<at> j = split(";",$index);
$cnt= <at> j;
if ($cnt==1) {
$subject="$cnt Msg";
} else {
$subject="$cnt Msgs";
}
$message="";
foreach $k ( <at> j) {
$s = $service_status[$k];
$h = $host_status[$k];
if ($table[$k] eq "SERVICE") {
if ($notification_type[$k] eq "ACKNOWLEDGEMENT") {
$message .= "$host_name[$k]($h) $service_description[$k](ACK);";
} else {
$message .= "$host_name[$k]($h) $service_description[$k]($s);";
}
} else {
if ($notification_type[$k] eq "ACKNOWLEDGEMENT") {
$message .= "$host_name[$k](ACK);";
} else {
$message .= "$host_name[$k]($h);";
}
}
}
$message =~ s/ /_/g;
$message =~ s/-/_/g;
$message =~ s/;/ /g;
$cmd = "/bin/echo \"$message\" | /usr/bin/mail -s \"$subject\" $contact_pager[$m]";
system ($cmd);
$index="";
return;
}
1;
______________________________________________________________________________________________
--
--
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Nagmin-users mailing list
Nagmin-users@...
https://lists.sourceforge.net/lists/listinfo/nagmin-users