写了个脚本,每天上班前运行,将检查结果报告E-mail给自己,转发到短信通知的外网邮箱,上班前就可知道有没有故障和服务器的情况了。
检查的内容不断完善,现在可知道考勤机、打印机、监控、UPS、路由器、交换机等全部有IP设备是否连通。文件共享服务器、网站等是否可正常访问。Windows服务器的启动时间、内存使用、硬盘可用空间等情况。
看邮件就对全部设备和服务器情况一目了然,不需逐个检查,在用户上班前就知道情况先。
检查是否连通用Ping返回结果,文件访问用FileExists,网站类检查InetRead返回结果,Windows服务器类用WMI查询。
查询域内Windows服务器部分代码:
;查询Windows系统服务器 Local $QueryServerIP = StringSplit($WinServerIPlist, ",") For $i = 1 To UBound($QueryServerIP) -1 Local $QueryServer = Ping($QueryServerIP[$i], 1000) If $QueryServer Then ; If a value greater than 0 was returned then display the following message. Dim $BodyTable, $partSpaceInfo $objWMIService = ObjGet("winmgmts:\\" & $QueryServerIP[$i] & "\root\cimv2") $colItems = $objWMIService.ExecQuery("Select * from Win32_OperatingSystem") For $colItem in $colItems $osVersion = $colItem.Version If $colItem.FreePhysicalMemory < $AlarmFreePhysicalMemory Then $FreePhysicalMemory = ‘<font size="3" color="#FE642E">‘ & Round($colItem.FreePhysicalMemory/1048576, 2) & "G</font>" Else $FreePhysicalMemory = Round($colItem.FreePhysicalMemory/1048576, 2) & "G" EndIf $TotalVisibleMemorySize = Round($colItem.TotalVisibleMemorySize/1048576, 2) & "G" $LastBootTime = StringTrimRight($colItem.LastBootUpTime, 11) $LastBootTime = StringRegExpReplace($LastBootTime, ‘(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)‘, ‘$1-$2-$3 $4:$5:$6‘) If _DateDiff( ‘d‘, $LastBootTime, _NowCalc()) < 1 Then ;小于1,即启动时间未超过1天 $LastBootTime = ‘<font size="3" color="#FE642E">‘ & $LastBootTime & ‘</font>‘ EndIf Next If $osVersion < 5.1 Then ;处理Windows 2000 $colItems = $objWMIService.ExecQuery("Select * from Win32_PerfRawData_PerfOS_System") For $colItem in $colItems $intPerfTimeStamp = $colItem.Timestamp_Object $intPerfTimeFreq = $colItem.Frequency_Object $intCounter = $colItem.SystemUpTime $osversion = $colItem.Version Next ;计算Win2000启动时间 $iUptimeInSec = ($intPerfTimeStamp - $intCounter)/$intPerfTimeFreq $bootUpDays = $iUptimeInSec/60/60/24 $seconds = ChangeToSeconds($bootUpDays) $LastBootTime = _DateAdd( ‘s‘, -$seconds, _NowCalc()) If _DateDiff( ‘d‘, $LastBootTime, _NowCalc()) < 1 Then ;小于1,即启动时间未超过1天 $LastBootTime = ‘<font size="3" color="#FE642E">‘ & $LastBootTime & ‘</font>‘ EndIf EndIf $colItems = $objWMIService.ExecQuery ("Select * from Win32_LogicalDiskToPartition") Local $partSpaceInfo = "" ;清空$partSpaceInfo,否则累加 For $objItem In $colItems $DiskFH = StringLeft(StringRight($objItem.Dependent, 3), 2) $colDiskItems = $objWMIService.ExecQuery ("Select * from Win32_LogicalDisk Where DeviceID=‘" & $DiskFH & "‘") For $colDiskItem In $colDiskItems ; $DiskFHSpaceInfo = StringReplace($DiskFH, ":", "盘可用") & CalcKMG($colDiskItem.FreeSpace) & "/总" & CalcKMG($colDiskItem.Size) & @CR $DiskFHFreeSpace = CalcKMG($colDiskItem.FreeSpace) If $colDiskItem.FreeSpace < $AlarmFreeSpace Then $DiskFHFreeSpace = ‘<font size="3" color="#FE642E">‘ & CalcKMG($colDiskItem.FreeSpace) & ‘</font>‘ Else $DiskFHFreeSpace = CalcKMG($colDiskItem.FreeSpace) EndIf $DiskFHSpaceInfo = $DiskFH & $DiskFHFreeSpace & "/" & CalcKMG($colDiskItem.Size) & @CR Next $partSpaceInfo = $partSpaceInfo & $DiskFHSpaceInfo Next $BodyTable = $BodyTable & ‘<TR><TD>‘ & ServerName($QueryServerIP[$i]) & ‘</TD><TD>‘ & $QueryServerIP[$i] & ‘</TD><TD>‘ & $QueryServer & ‘毫秒</TD><TD>‘ & $LastBootTime& ‘</TD><TD>‘ & $FreePhysicalMemory & "/" & $TotalVisibleMemorySize & ‘</TD><TD>‘ & $partSpaceInfo & ‘</TD></TR>‘ Else $BodyTable = $BodyTable & & ‘<TR><TD>‘ & ServerName($QueryServerIP[$i]) & ‘</TD><TD>‘ & $QueryServerIP[$i] & ‘</TD><TD><font size="3" color="red">不通</font></TD><TD>‘ & $LastBootTime & ‘</TD><TD>‘ & $FreePhysicalMemory & "/" & $TotalVisibleMemorySize & ‘</TD><TD>‘ & $partSpaceInfo & ‘</TD></TR>‘ $Subject = ‘有故障 - ‘ & StringReplace($Subject, ‘有故障 - ‘, "") EndIf Next $Body = $Body & $BodyTable & "</Table><BR><BR><BR>"
本文出自 “暂时博客” 博客,请务必保留此出处http://gzzhang.blog.51cto.com/5312382/1546939
原文地址:http://gzzhang.blog.51cto.com/5312382/1546939