Chinaunix首页 | 论坛 | 博客
  • 博客访问: 634539
  • 博文数量: 184
  • 博客积分: 10057
  • 博客等级: 上将
  • 技术积分: 2505
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-31 16:34
文章分类

全部博文(184)

文章存档

2010年(5)

2009年(104)

2008年(75)

我的朋友

分类:

2009-02-22 00:05:30

最近在学习perl,就用perl写了一个监控windows主机的脚本,大家给看看

CODE:
#!/usr/bin/perl -w
use Win32::OLE qw[in];

my $host = $ARGV[0] || '.';
my $wmi = Win32::OLE->GetObject( "winmgmts://$host/root/cimv2" )
        or die Win32::FormatMessage( Win32::OLE::LastError() );

my %instances = (
        Win32_PhysicalMemory => \&get_pmem,
        Win32_PerfRawData_PerfOS_Memory => \&get_amem,
        Win32_Processor => \&get_load,
        Win32_LogicalDisk => \&get_disk,
);

while(1) {
        my $out = get_perf_data();
        print $out;
        print "\n";
        sleep(30);
}

sub get_perf_data {
        my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
        $year = $year + 1900;
        $mon  = $mon + 1;
        my $str = sprintf "%4.4d-%2.2d-%2.2d",$year,$mon,$mday;
        my $timestr = sprintf "%2.2d:%2.2d:%2.2d",$hour,$min,$sec;
       
        my $mem;
        foreach ( keys %instances ) {
                my $class = $wmi->InstancesOf( $_ );
                $mem .= $instances{ $_ }->( $class );
        }
       
        my $out = "##\nCollect Time: ".$str." ".$timestr."\n".$mem."%%\r\n";
        return $out;
}

# get cpu loadavg
sub get_load {
        my $class = shift;
        my $total="";
        my $i = 0;
        $i++,$total = $total."CPU No. $i: ".$_->{LoadPercentage}."%\n" foreach in($class);
        return $total;
}

# get total memory size
sub get_pmem {
        my $class = shift;
        my $total;
        $total += $_->{Capacity} foreach in($class);
        return "Physical Memory: $total Bytes\n";
}

# get available memory size
sub get_amem {
        my $class = shift;
        my $amem;
        $amem .= join ' ', $_->{AvailableBytes} foreach in($class);
        return "Available Memory: $amem Bytes\n";
}

# get free disk sizes
sub get_disk {
        my $class = shift;
        my $total = "";
        $total .= "DISK ".$_->{DeviceID}." Free: ".$_->{FreeSpace}." Bytes\n" foreach in($class);
               
        return $total
}
阅读(484) | 评论(0) | 转发(0) |
0

上一篇:Perl开发工具大比拼

下一篇:用别人的...

给主人留下些什么吧!~~