
實現服務器集群的監控腳本初衷是想監控一堆windows服務器,她們IP和密碼都不同,不知道能不能實現用一臺服務器上執行腳本,獲取滿足條件的服務器的信息,即 IP地址,超過80%的盤符還有CPUs使用超過90%,內存剩余率小于0.2,目前我寫的是單個的,代碼如下:@echo offsetlocal enabledelayedexpansion:disk@echo offsetlocal enabledelayedexpansionfor /f tokens=1-3 delims= %%i in ('wmic logicaldisk where drivetype=3 get name^,size^,freespace ^|findstr [0-9]') do (>temp.vbs echo,a=%%i>>temp.vbs echo,b=%%k>>temp.vbs echo,s=b/1024/1024/1024>>temp.vbs echo,fs=a/1024/1024/1024>>temp.vbs echo,n=a/b*100>>temp.vbs echo,wscript.echo round^(s,2^),round^(fs,2^),round^(n^)for /f tokens=1-3 delims= %%a in ('cscript /nologo temp.vbs') do set size=%%a & set freesize=%%b & set usage=%%cset /a usage=100-!usage!del temp.vbs 2>nulif !usage! gtr 50 echo,盤符%%j 總空間!size!GB 剩余空間!freesize!GB 使用率!usage!%%):cpufor /f tokens=2 delims== %%a in ('wmic path Win32_PerfFormattedData_PerfOS_Processor get PercentProcessorTime /value^|findstr PercentProcessorTime') do (set UseCPU=%%agoto :mem):memfor /f tokens=2 delims== %%a in ('wmic path Win32_PhysicalMemory get * /value^|findstr Capacity') do (setBBB=%%acall :division !BBB! 1073741824quote 2set /a TolMem=TolMem+quote)for /f tokens=2 delims== %%a in ('wmic path Win32_PerfFormattedData_PerfOS_Memory get * /value^|findstr AvailableBytes') do (setBBB=%%acall :division !BBB! 1073741824quote 2set AavMem=!quote!)call :division !AavMem! !TolMem!quote 2echo 當前總內存:%TolMem%GBecho 當前可用內存:%AavMem%GBecho CPU使用率: %UseCPU%%%echo 內存剩余率:%quote%pause:divisionsetlocalset str1=%1set str2=%2if %~4 neq set u=%4for %%i in (str1 str2) do if !%%i:~,1! == - set /a d+=1if %d% == 1 (set d=-) else set d=set l=00000000&for /l %%i in (1 1 7) do set l=!l!!l!set var=4096 2048 1024 512 256 128 64 32 16 8 4 2 1for /l %%i in (1 1 2) do (set str%%i=!str%%i:-=!set /a n=str%%i_2=0for %%a in (!str%%i:.^= !) do (set /a n+=1set s=s%%a&set str%%i_!n!=0for %%b in (%var%) do if !S:~%%b! neq set/a str%%i_!n!+=%%b&set S=!S:~%%b!set /a len%%i+=str%%i_!n!)set str%%i=!str%%i:.=!)if !str1_2! gtr !str2_2! (set /a len2+=str1_2-str2_2) else set /a len1+=str2_2-str1_2for /l %%i in (1 1 2) do (set str%%i=!str%%i!!l!for %%j in (!len%%i!) do set str%%i=!str%%i:~,%%j!)for /f tokens=* delims=0 %%i in (!str2!) do set s=%%i&set str2=0%%iset len2=1for %%j in (%var%) do if !S:~%%j! neq set/a len2+=%%j&set S=!S:~%%j!set /a len=len2+1if !len1! lss !len2! set len1=!len2!&set str1=!l:~-%len2%,-%len1%!!str1!set /a len1+=u&set str1=0!str1!!l:~,%u%!set str=!str1:~,%len2%!set i=0000000!str2!&set /a Len_i=Len2+7for /l %%i in (1 1 9) do (set T=0for /l %%j in (8 8 !Len_i!) do (set /a T=1!i:~-%%j,8!*%%i+Tset Num%%i=!T:~-8!!Num%%i!&set /a T=!T:~,-8!-%%i)set Num%%i=!T!!Num%%i!set Num%%i=0000000!Num%%i:~-%Len%!)for /L %%a in (!len2! 1 !Len1!) do (set str=!L!!str!!str1:~%%a,1!set str=!str:~-%Len%!if !str! geq !str2! ( set M=1&set i=0000000!str! for /l %%i in (2 1 9) do if !i! geq !Num%%i! set M=%%i set sun=!sun!!M!&set str=&set T=0 for %%i in (!M!) do ( for /l %%j in (8 8 !Len_i!) do ( set /a T=3!i:~-%%j,8!-1!Num%%i:~-%%j,8!-!T:~,1!%%2 set str=!T:~1!!str! ) )) else set sun=!sun!0)if defined u if %u% gtr 0 set sun=!sun:~,-%u%!.!sun:~-%u%!endlocal&set %3=%d%%sun%運行結果如下:盤符C: 總空間50.32GB 剩余空間21GB 使用率58%盤符H: 總空間11.72GB 剩余空間3.42GB 使用率71%當前總內存:4GB當前可用內存:1.61GBPU使用率: 2%內存剩余率:0.40請按任意鍵繼續. . .

