diff --git a/xCAT-server-2.0/lib/xcat/monitoring/snmpmon.pm b/xCAT-server-2.0/lib/xcat/monitoring/snmpmon.pm index 04da624b8..441affb4f 100644 --- a/xCAT-server-2.0/lib/xcat/monitoring/snmpmon.pm +++ b/xCAT-server-2.0/lib/xcat/monitoring/snmpmon.pm @@ -318,15 +318,34 @@ sub getDescription { " Description: snmpmon sets up the snmptrapd on the management server to receive SNMP traps for different nodes. It also sets the trap destination for Blade - Center Managment Module, RSA II, IPMIs that are managed by the xCAT cluster. - By default, the xCAT trap handler will log all events in the syslog and only - email the critial and the warning events to the mail aliance called 'alerts'. - You can use the settings to override the default behavior. Use command - 'startmon snmpmon' to star monitoring and 'stopmon snmpmon' to stop it. - The SNMP trap handler is disabled on the service node. + Center Management Module, RSA II, IPMIs that are managed by the xCAT cluster. + xCAT has categorized some events into different event priorities (critical, + warning and informational) based on the MIBs we know such as MM, RSA II and + IPMI. All the unknown events are categorized as 'warning'. By default, + the xCAT trap handler will log all events into the syslog and only + email the critical and the warning events to the mail alias called 'alerts'. + You can use the settings to override the default behavior. + Use command 'startmon snmpmon' to star monitoring and 'stopmon snmpmon' + to stop it. Settings: ignore: specifies the events that will be ignored. It's a comma separated pairs of oid=value. For example, BLADESPPALT-MIB::spTrapAppType=4,BLADESPPALT-MIB::spTrapAppType=4. - emailrsp: specifies the events that will get email notification.\n" + email: specifies the events that will get email notification. + log: specifies the events that will get logged. + runcmd: specifies the events that will be passed to the user defined scripts. + cmds: specifies the command names that will be invoked for the events + specified in the runcmd row. + + Special keywords for specifying events: + All -- all events. + None -- none of the events. + Critical -- all critical events. + Warning -- all warning events. + Informational -- all informational events. + + For example, you can have the following setting: + email CRITICAL,BLADESPPALT-MIB::pTrapPriority=4 + This means send email for all the critical events, or the BladeCenter + system events.\n" } diff --git a/xCAT-server-2.0/sbin/xcat_traphandler b/xCAT-server-2.0/sbin/xcat_traphandler index 7aa461770..fd9188ad1 100755 --- a/xCAT-server-2.0/sbin/xcat_traphandler +++ b/xCAT-server-2.0/sbin/xcat_traphandler @@ -8,20 +8,33 @@ use lib "$::XCATROOT/lib/perl"; use Sys::Syslog; use xCAT::Table; use xCAT_plugin::ipmi; +use xCAT_monitoring::monitorctrl; # admin needs to create a mail aliase called alerts # put "alerts: emailadd,emailaddr.." to /etc/aliases file # then run newaliases command my $MAILTO="alerts"; -my $message; -my $briefmsg; +my $message, $briefmsg; my $pair; my $node1; my $info; my $severity_type; my $severity; +#get settings +$EMAIL=0, $LOG=0, $RUNCMD=0, $INGNORE=0; +%hashI=(),%hashE=(),%hashL=(),%hashR=(); +my %settings=xCAT_monitoring::monitorctrl->getPluginSettings("snmpmon"); +my $i=$settings{'ignore'}; +if ($i) { %hashI =parseSettings($i); } +my $e=$settings{'email'}; +if ($e) { %hashE=parseSettings($e); } +my $l=$settings{'log'}; +if ($l) { %hashL=parseSettings($l); } +my $r=$settings{'runcmd'}; +if ($r) { %hashR=parseSettings($r); } + # read host name my $host=; chomp($host); @@ -45,6 +58,9 @@ chomp($trapidline); $oid=shift @a; $value=join(' ', @a); $message .= " $oid=$value\n"; +checkWithOid($oid, $value); +#print "I=$IGNORE,E=$EMAIL, L=$LOG, R=$RUNCMD\n"; +if ($IGNORE) { print "out\n"; exit 0;} #for ipmi traps, the values is: SNMPv2-SMI::enterprises.3183.1.1.0.x, x is the ipmi trap id. $value =~ s/(SNMPv2-SMI::enterprises\.3183\.1\.1\.0\.)//g; @@ -88,7 +104,6 @@ while ($temp=) { $oid=shift @a; $value=join(' ', @a); - $message .= " $oid=$value\n"; #for BladeCenter MM traps and RSA II traps, creat a brief message if ($oid =~ /BLADESPPALT-MIB::spTrapAppId|RSASPPALT-MIB::ibmSpTrapAppId/) { @@ -159,29 +174,68 @@ while ($temp=) { $severity_type="Critical"; } } - } + } + + $message .= " $oid=$value\n"; + + #check agains the settings + $value =~ s/^\"(.*)\"$/$1/; + checkWithOid($oid, $value); + #print "I=$IGNORE,E=$EMAIL, L=$LOG, R=$RUNCMD\n"; + if ($IGNORE) { print "out\n"; exit 0;} } -if ($node1) {$info= getMoreInfo($node1);} +if (!$severity_type) { $severity_type="Warning"; } +if ((!$IGNORE) && exists($hashI{$severity_type})) { print "out\n"; exit 0; } +if ((!$EMAIL) && exists($hashE{$severity_type})) { $EMAIL=1; } +if ((!$LOG) && exists($hashL{$severity_type})) { $LOG=1; } +if ((!$RUNCMD) && exists($hashR{severity_type})) { $RUNCMD=1; } +#print "I=$IGNORE,E=$EMAIL, L=$LOG, R=$RUNCMD\n"; -($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time); -$datetime=sprintf "%2d-%02d-%04d %02d:%02d:%02d", $mon+1,$mday,$year+1900,$hour,$min,$sec; -my $head="SNMP $severity received from $host($ip) on $datetime\n$briefmsg\n"; -my $middle="Trap details:\n$message\n"; -my $end; -if ($info) { - $end ="Additonal Info from xCAT:\n$info\n"; +#email 'alerts' aliase +if ($EMAIL || ((keys(%hashE)==0) && ($severity_type =~/Critical|Warning/))) { + print "email\n"; + ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time); + $datetime=sprintf "%2d-%02d-%04d %02d:%02d:%02d", $mon+1,$mday,$year+1900,$hour,$min,$sec; + my $head="SNMP $severity received from $host($ip) on $datetime\n$briefmsg\n"; + my $middle="Trap details:\n$message\n"; + my $end; + if ($node1) {$info= getMoreInfo($node1);} + if ($info) { + $end ="Additonal Info from xCAT:\n$info\n"; + } + + #email the full message to the alerts aliase + my $cmd="echo \'$head$middle$end\' \| mail -s \"$severity_type: Cluster SNMP Alert\!\" $MAILTO"; + `$cmd`; } -#TODO: decide responses based on severity. settings in monitoring tb. -#email the full message to the alerts aliase -my $cmd="echo \'$head$middle$end\' \| mail -s \"$severity_type: Cluster SNMP Alert\!\" $MAILTO"; -`$cmd`; - -# TODO: log the the brief message from the Blad Center MM to the syslog. For other traps, log all. - +#log to syslog if needed. default is log all +if ($LOG || (keys(%hashL)==0)) { + print "log\n"; + my $head="SNMP $severity received from $host($ip) on $datetime"; + my $body; + if ($briefmsg) { $body=$briefmsg;} + else { $body=$message; } + openlog("xCATMon Event","","local4"); + if ($severity_type eq "Informational") { + syslog("local4|info", "$head\n$body\n"); + } else { + syslog("local4|err", "$head\n$body\n"); + } + closelog(); +} +#run user defined commands if needed. +if ($RUNCMD) { + print "runcmd\n"; + my $scripts=$settings{'cmds'}; + while ($scripts =~ s/^([^,]+)(,)*//) { + my $cmd="echo \'host=$host\nip=$ip\n$message\n\' \| $1"; + `$cmd`; + } +} #-------------------------------------------------------------------------------- @@ -244,6 +298,74 @@ sub getMoreInfo { } +#-------------------------------------------------------------------------------- +=head3 parseSettings + This function takes a setting string which looks like "key1=value1,key2=value2..." + and returns a hash (key1=>value1, key2=>value2...). + Arguments: + setting the setting string. + Returns: + A hash. +=cut +#-------------------------------------------------------------------------------- +sub parseSettings { + my $settings=shift; + my %ret=(); + while (($settings =~ s/^(Informational|Warning|Critical|All|None)()(,)*//) || + ($settings =~ s/^([^\=]+)=(\"[^\"]+\")(,)*//) || + ($settings =~ s/^([^\=]+)=([^\"\,]+)(,)*//)) { + # print "$1=$2\n"; + $ret{$1}=$2; + } + + return %ret; +} + +#-------------------------------------------------------------------------------- +=head3 checkWithOid + This function checks the input strings with the setting to see what + actions need to be done for this event. + Arguments: + oid the oid string + value the value string + Returns: + none. The variables $EMAIL, $LOG, $IGNORE and $RUNCMD may be changed. +=cut +#-------------------------------------------------------------------------------- +sub checkWithOid { + $o=shift; + $v=shift; + + sub checking { + my $hashX=shift; + + if (exists($hashX->{'All'})) { return 1; } + if (exists($hashX->{'None'})) { return 0; } + if (exists($hashX->{$o}) && ($hashX->{$oid} eq $v)) {return 1; } + return 0; + } + + if ((!$IGNORE) && (keys(%hashI)>0)) { + $IGNORE=checking(\%hashI); + if ($IGNORE) { return;} + } + + if ((!$EMAIL) && (keys(%hashE)>0)) { + $EMAIL=checking(\%hashE); + } + + if ((!$LOG) && (keys(%hashL)>0)) { + $LOG=checking(\%hashL); + } + + if ((!$RUNCMD) && (keys(%hashR)>0)) { + $RUNCMD=checking(\%hashR); + } +} + + + +