转自:http://www.lazysa.com/applications/perl_monitor_windows.html
此脚本的主要功能为:
1、获取windows主机的CPU,内存,硬盘,负载等信息。
2、将获取的信息打印出来,并以邮件的形式通知到指定邮箱。
#!/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."%%rn"; 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 Bytesn"; } # get available memory size sub get_amem { my $class = shift; my $amem; $amem .= join ' ', $_->{AvailableBytes} foreach in($class); return "Available Memory: $amem Bytesn"; } # get free disk sizes sub get_disk { my $class = shift; my $total = ""; $total .= "DISK ".$_->{DeviceID}." Free: ".$_->{FreeSpace}." Bytesn" foreach in($class); return $total; }
关于perl的一个网站(常用模块)
http://www.linuxsir.org/bbs/thread77473.html