Chinaunix首页 | 论坛 | 博客
  • 博客访问: 337238
  • 博文数量: 85
  • 博客积分: 1420
  • 博客等级: 上尉
  • 技术积分: 787
  • 用 户 组: 普通用户
  • 注册时间: 2007-01-10 09:02
文章分类

全部博文(85)

文章存档

2011年(5)

2010年(51)

2009年(29)

我的朋友

分类:

2010-01-18 09:57:38

#! /usr/sbin/perl -w
use SNMP;
use Proc::PID::File;
use Term::ANSIColor;
use Mail::Sender;


#这一句功能就是每次执行检测之前对系统做一次检测,看上次的任务结束否。
die "Already running!" if Proc::PID::File->running();

#我这里设定了一个被监控主机的配置文件,这个文件里存放着,我们要监控哪些地址,哪些#OID,及设定一个报警数值等。
#例如:ip,oid,报警数值(用“,”做分割)
#     192.168.1.1,1.1.1.1.1.1.1.1.1.1.1,2222
#
my $conf_file_path="/etc/iplist.conf";
 
#看到函数名称应该知道,这个函数是用来获得OID数据的,不过我感觉参数传递写的有些不怎#么样,呵呵!或许有更好的办法我不知道。
sub getoid_val(){
 ######To get argument list...
 my $hostip=$_[0];
 my $commu=$_[1];
 my $snmpoid=$_[2];
 
 $sess = new SNMP::Session(DestHost =>"$hostip", 
                           Community =>"$commu", 
                               Version => "2c" ,
                                Retries => "2");

 $currestab_val = $sess->get("$snmpoid");
 return $currestab_val;
}
 
 
#这里利用上面的函数来获得oid数据,然后把这些数据与我们配置文件中设定的报警值进行比#较,检查设备运行的某些数值是不是已经达到警戒线。
sub checkload (){
 ######To get argument list...
 my $hostip=$_[0];
 my $commu=$_[1];
 my $snmpoid=$_[2];
 my $alarmval=$_[3];
 
 #调用getoid_val获取实时数据
 my $oidval= &getoid_val($hostip,$commu,$snmpoid);
 
 if (($oidval > $alarmval) || ($oidval == -1) || ($oidval == 0)){

         print color 'bold red';
         print "_Check Host : $hostip |-Current OID : $snmpoid |-Value Is :[ $oidval ] |-[ Alarm ]!!!\n";
  &mailsender("Host : $hostip |OID : $snmpoid |-Value Is :[ $oidval ] |-[ Alarm......]\n");
 
}else{

         print color 'bold green';
         print "_Check Host : $hostip |-Current OID : $snmpoid |-Value Is : $oidval |-[ OK ]!!!\n";
 
}
#为了看着直观些,对输出的文字做了一个颜色的改变,以下是恢复默认。 
print color 'reset';
return;
}
 
#为了更好的发挥我们程序的报警功能,这里加了一个邮件发送功能。
sub mailsender(){
 ######To get argument list...
 my $mailmsg=$_[0];

 $sender = new Mail::Sender;
 
if ($sender->MailMsg({
             smtp => "192.168.11.128",
             from => "sender name",
             to   => "dst email address",
          subject => 'Alarm mail',
              msg => "$mailmsg",
           authid => "email user name",
          authpwd => "email password"
}) < 0) {die "$Mail::Sender::Error\n";}
 print "send email ! .......\n"
}

#打开配置文件读取ip,oid,alarm_val
open(CFGFH,"<", $conf_file_path) || die("can't open Ip list file: /etc/iplist.conf");

@host_ip_array = ;

foreach $conf_file_line (@host_ip_array){      
  next if (my $key1 = $conf_file_line =~ m/^#/);
  next if (my $key2 = $conf_file_line =~ m/^\n|\r/); 
  my ($conf_host_ip,$oid_value,$alarm_val)=split(/,/,$conf_file_line);
 
###It will delete all spaces in character string.
 for ($conf_host_ip) {
        s/^\s+//;
         s/\s+$//;
         s/\s+/ /g;
 }

 next if ($conf_host_ip !~ m/\d+.\d+.\d+.\d+/);
 next if ((!$oid_value) or ($oid_value !~ m/\d+/));
 
 for ($oid_value) {
         s/^\s+//;
         s/\s+$//;
        s/\s+/ /g;
 }
 for ($alarm_val) {
         s/^\s+//;
         s/\s+$//;
        s/\s+/ /g;
 }
 
#执行远程设备状态检查
 &checkload($conf_host_ip,"public",$oid_value,$alarm_val);
}
print "\n";
close(CFGFH);
exit;
阅读(1940) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~