Board logo

标题: [系统相关] 实现服务器集群的监控脚本 [打印本页]

作者: 梦缘隋都    时间: 2015-3-17 15:12     标题: 实现服务器集群的监控脚本

初衷是想监控一堆windows服务器,她们IP和密码都不同,不知道能不能实现用一台服务器上执行脚本,获取满足条件的服务器的信息,即 IP地址,超过80%的盘符还有CPUs使用超过90%,内存剩余率小于0.2,目前我写的是单个的,代码如下:
@echo off
setlocal enabledelayedexpansion

:disk
@echo off

setlocal enabledelayedexpansion


for /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=%%c"

set /a usage=100-!usage!

del temp.vbs 2>nul

if !usage! gtr 50 echo,盘符%%j 总空间!size!GB 剩余空间!freesize!GB 使用率!usage!%%

)



:cpu
for /f "tokens=2 delims==" %%a in ('wmic path Win32_PerfFormattedData_PerfOS_Processor get PercentProcessorTime /value^|findstr "PercentProcessorTime"') do (
  set UseCPU=%%a
  goto :mem
)
:mem

for /f "tokens=2 delims==" %%a in ('wmic path Win32_PhysicalMemory get * /value^|findstr "Capacity"') do (  
  set  BBB=%%a
call :division !BBB! 1073741824  quote 2
set /a TolMem=TolMem+quote


  )

for /f "tokens=2 delims==" %%a in ('wmic path Win32_PerfFormattedData_PerfOS_Memory get * /value^|findstr "AvailableBytes"') do (
  set  BBB=%%a
call :division !BBB! 1073741824  quote 2

set AavMem=!quote!
)
call :division !AavMem! !TolMem!  quote 2

echo 当前总内存:%TolMem%GB
echo 当前可用内存:%AavMem%GB
echo CPU使用率: %UseCPU%%%
echo 内存剩余率:%quote%
pause

:division

setlocal

set str1=%1

set str2=%2

if "%~4" neq "" set u=%4

for %%i in (str1 str2) do if "!%%i:~,1!" == "-" set /a d+=1

if "%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 1"

for /l %%i in (1 1 2) do (

    set "str%%i=!str%%i:-=!"

    set /a "n=str%%i_2=0"

    for %%a in (!str%%i:.^= !) do (

        set /a n+=1

        set s=s%%a&set str%%i_!n!=0

        for %%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_2

for /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%%i"

set len2=1

for %%j in (%var%) do if "!S:~%%j!" neq "" set/a len2+=%%j&set "S=!S:~%%j!"

set /a len=len2+1

if !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+7

for /l %%i in (1 1 9) do (

    set "T=0"

    for /l %%j in (8 8 !Len_i!) do (

        set /a "T=1!i:~-%%j,8!*%%i+T"

        set 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.61GB
PU使用率: 2%
内存剩余率:0.40
请按任意键继续. . .
作者: DAIC    时间: 2015-3-17 16:15

推荐使用 Nagios, Zabbix 等免费监控工具
作者: 梦缘隋都    时间: 2015-3-23 12:23

回复 1# 梦缘隋都
这个我倒是知道,现在是想用批处理看能不能解决,好推广使用




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2