用perl 监控windows

用perl 监控windows

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

[Copy to clipboard] [ - ]
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
}

牛~

我以前写的显示CPU占用率的程序费了好大劲···
牛,不错
在找个数据库定期写入
再处理,一个很好的报告了


QUOTE:
原帖由 jeffwang8001 于 2007-1-25 17:24 发表
最近在学习perl,就用perl写了一个监控windows主机的脚本,大家给看看
[code]
#!/usr/bin/perl -w
use Win32::OLE qw[in];

my $host = $ARGV[0] || '.';
my $wmi = Win32::OLE->GetObject( "winmg ...

我看了下Win32::OLE 但是我不知道
$wmi->InstancesOf( $_ )
$_->{LoadPercentage}
$_->{Capacity}
$_->{DeviceID}
$_->{FreeSpace}
怎么来的????要是系统的,我怎么找到呢?
没人来解释一下吗?
这个属于WMI的范畴, 去MSDN上看。
http://msdn2.microsoft.com/en-us/library/aa394084.aspx
强,学习
路过学习下哈哈
很好,厉害
恩 收藏了 谢谢楼主