Windows 系统资源一键监控脚本

最近公司在统计虚拟机的资源占用情况,魔改了一个脚本用于快速统计。

CPU 占用统计 cpu.vbs

On Error Resume Next
Set objProc  = GetObject("winmgmts:\\.\root\cimv2:win32_processor='cpu0'")
Wscript.Echo "CPU 占用率:" & objProc.LoadPercentage & "%"

内存占用统计 ram.vbs

  
strComputer = "."
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set colOS = objWMI.InstancesOf("Win32_OperatingSystem")
for each objOS in colOS
strReturn = "内存总数: " &  round(objOS.TotalVisibleMemorySize / 1024) & " MB" & vbCrLf &"内存可用数: " & round(objOS.FreePhysicalMemory / 1024) & " MB" & vbCrLf &"内存使用率 :" & Round(((objOS.TotalVisibleMemorySize-objOS.FreePhysicalMemory)/objOS.TotalVisibleMemorySize)*100) & "%"
Wscript.Echo strReturn
next

硬盘占用统计 hard.vbs

Set fsoobj = CreateObject("Scripting.FileSystemObject")
 DriversInfo = GetDriversInfo
 DriversInfo = Replace(DriversInfo, "|", vbCrLf)
 sReturn ="硬盘信息:" & vbCrLf & DriversInfo
 Wscript.Echo sReturn
Function GetDriversInfo()

   GetDriversInfo = ""
   AllSpaces = 0
   AllFreeSpaces = 0
   Set drvObj = fsoobj.Drives
   For Each D In drvObj
       Err.Clear
       If D.DriveLetter <> "A" Then
           If D.isReady Then
               GetDriversInfo = GetDriversInfo & "分区:" & D.DriveLetter & vbCrLf
               GetDriversInfo = GetDriversInfo & "可用空间:" & cSize(D.FreeSpace) & vbCrLf
               GetDriversInfo = GetDriversInfo & "总大小:" & cSize( D.TotalSize) & vbCrLf
               GetDriversInfo = GetDriversInfo & "使用率 :" & (100*((D.TotalSize-D.FreeSpace)/D.TotalSize)) &"%" & vbCrLf
               GetDriversInfo = GetDriversInfo & "|"
               AllSpaces = AllSpaces + D.TotalSize 
               AllFreeSpaces = AllFreeSpaces + D.FreeSpace
             Else
           End If
         Else
       End If
   Next
   GetDriversInfo = GetDriversInfo & "总硬盘可用空间:" & cSize(AllFreeSpaces) & vbCrLf
   GetDriversInfo = GetDriversInfo & "总硬盘空间:" & cSize(AllSpaces) & vbCrLf
   GetDriversInfo = GetDriversInfo & "总硬盘使用率 :" & (100*((AllSpaces-AllFreeSpaces)/AllSpaces)) &"%" & vbCrLf
   GetDriversInfo = GetDriversInfo & "|"
End Function

 Function cSize(tSize)

     If tSize >= 1073741824 Then
         cSize = Int((tSize / 1073741824) * 1000) / 1000 & " GB"
       ElseIf tSize >= 1048576 Then
         cSize = Int((tSize / 1048576) * 1000) / 1000 & " MB"
       ElseIf tSize >= 1024 Then
         cSize = Int((tSize / 1024) * 1000) / 1000 & " KB"
       Else
         cSize = tSize & "B"
     End If

End Function

另外可以使用系统自带命令 systeminfo 获取更多系统信息。
把所有信息保存到文件之后可以使用 mailsend-go 发送 SMTP 邮件进行消息通知,由于消息可以能会乱码,还需要 iconv 进行 GB2312 到 UTF8 转换。

主脚本如下,一些消息变量需要设置:

@ECHO OFF&PUSHD %~DP0
setlocal EnableDelayedExpansion&color 3e & cd /d "%~dp0"
TITLE System Monitor
set title="%date%-%time% 127.0.0.1 Server Info"
set smtpserver="smtp.163.com"
set smtpport=25
set user="user@163.com"
set password="password"
set sendto="user@163.com"

del info.txt
del info.old

echo %title%
echo -----------------------------------
cscript //nologo cpu.vbs 
echo -----------------------------------
cscript //nologo ram.vbs
echo ----------------------------------- 
cscript //nologo hard.vbs 
echo ----------------------------------- 
systeminfo
echo ----------------------------------- 
echo 另存信息到文件中,请等待...

echo %title% >> info.txt
echo ----------------------------------- >> info.txt
cscript //nologo cpu.vbs >> info.txt
echo ----------------------------------- >> info.txt
cscript //nologo ram.vbs >> info.txt
echo ----------------------------------- >> info.txt
cscript //nologo hard.vbs >> info.txt
echo ----------------------------------- >> info.txt
systeminfo >> info.txt

echo 文件编码转换中,请等待...
ren info.txt info.old
iconv -f GB2312 -t UTF-8 < info.old > info.txt

echo 发送服务器信息到邮箱中,请等待...
mailsend-go -sub %title% -smtp %smtpserver% -port %smtpport% auth  -user  %user% -pass %password% -to %sendto% -from %user% -subject %title% -cs "utf8" body -file info.txt 
rem pause > nul
exit

将脚本另存到 D:\win 文件夹下,使用以下批处理创建每天下午5点自动收集并发送邮箱。

@ECHO OFF&PUSHD %~DP0
setlocal EnableDelayedExpansion&color 3e & cd /d "%~dp0"
echo ----------------------------------- 
echo 创建自动定时任务
schtasks  /create  /tn  getsysinfo /tr  D:\win\run.bat  /sc  DAILY /st  17:30:00
echo ----------------------------------- 
echo 查看自动定时任务
schtasks  /Query  /tn getsysinfo
echo ----------------------------------- 
rem 删除自动定时任务
rem schtasks /Delete /tn getsysinfo
pause > nul
exit

源码:GitHub

评论已关闭