监控系统运行状态并生成日志的批处理

     公司有一个服务器监控的需求,需要监控上百台服务器的资源占用情况,包括CPU使用率,内存占用,硬盘空闲空间大小,网络连接速度等。

     初步技术方案为:使用批处理生成系统的资源情况日志,利用计划任务程序每隔5分钟生成日志,然后利用Logstash进行日志采集到管理服务器上,用kibana进行分析与呈现。

  程序流程:建立文件目录,分别取到CPU占用率,内存,C盘大小,网络连接等值,然后写入日志文件。

     批处理如下:

 

  1 @echo off

  2 ::获取年月日

  3 set year=%date:~0,4%

  4 set month=%date:~5,2%

  5 set day=%date:~8,2%

  6 

  7 rem 初始化常量

  8 set batPath=d:\SysStatus

  9 set logPath=d:\SysStatus\log

 10 set batName=RecordSysStatus.bat

 11 set host=www.baidu.com

 12 

 13 ::定义变量

 14 rem cpu使用率

 15 set /a cpu=0

 16 rem  物理内存总量

 17 set memPhysicalTotal=sf

 18 rem  可用物理内存

 19 set memAvailable=ds

 20 rem C盘剩余

 21 set Cfreespace=0

 22 rem C盘总空间

 23 set Csize=0

 24 rem 最短

 25 set netMin=0ms

 26 rem 最长

 27 set netMax=0ms

 28 rem 平均

 29 set netAva=0ms

 30 rem 进程

 31 set processlist=list

 32 

 33 ::创建目录和复制文件

 34 if not exist %batPath% md %batPath%

 35 if not exist %logPath% md %logPath%

 36 if not exist %logPath%\%year%-%month% md %logPath%\%year%-%month%

 37 if not exist %batPath%\%batName% copy %~0 %batPath%

 38 set file=%logPath%\%year%-%month%\%year%%month%%day%.txt

 39 

 40 ::获取cpu使用率

 41 for /f  %%a in ('wmic cpu get LoadPercentage ') do (

 42 if not "%%a"=="LoadPercentage" set /a cpu=%%a 

 43 )

 44 

 45 rem 取内存数据

 46 for /f "tokens=2"   %%a  in ('systeminfo ^| findstr 物理内存总量') do (

 47 set memPhysicalTotal=%%a

 48 )

 49 set memPhysicalTotal=%memPhysicalTotal:,=%

 50 

 51 for /f "tokens=2"   %%a in ('systeminfo ^| findstr 可用的物理内存') do (

 52 set memAvailable=%%a

 53 )

 54 set memAvailable=%memAvailable:,=%

 55 

 56 rem 获得C盘剩余空间

 57 for /f %%a in ('wmic LogicalDisk where "DeviceID='C:'" get freespace') do (

 58 if  %%a gtr 0  set  Cfreespace=%%a

 59 )

 60 

 61 rem 获得C盘总空间

 62 for /f %%a in ('wmic LogicalDisk where "DeviceID='C:'" get Size') do (

 63 if  %%a gtr 0  set  Csize=%%a

 64 )

 65 

 66 rem 将byte表示的容量换算成以G表示的容量

 67 @echo off&setlocal enabledelayedexpansion

 68 call:division %Cfreespace% 1073741824 CGfree 1

 69 call:division %Csize% 1073741824 CGsize 1

 70 

 71 rem 计算百分比

 72 call:division %Cfreespace%00 %Csize% CfreePercent 1

 73 call:division %memAvailable%00 %memPhysicalTotal% memAvaiPercent 1

 74 

 75 

 76 rem 计算网络连接速度,ping host

 77 for /f "tokens=2,4,6 delims=,= " %%a in ('ping %host% ^| findstr 最长') do (

 78 ::echo 连接速度:最短:%%a 最长:%%b平均:%%c

 79 set netAva=%%c

 80 set netMax=%%b

 81 set netMin=%%a

 82 )

 83 

 84 rem 占用内存超100M进程

 85 setlocal EnableDelayedExpansion    

 86 set processlist=[

 87 for /f "skip=1tokens=2,6" %%a in ('wmic process where  "WorkingSetSize>100090688"  list brief ') do (

 88 set processlist=!processlist!{"Name":"%%a","WorkingSetSize":%%b},

 89 )

 90 set processlist=%processlist%]

 91 

 92 

 93 

 94 rem 输出到日志文件

 95 echo %date:~0,11%%time:~0,8% CPU使用率:%cpu%%% 可用内存:%memAvailable%M 内存总量:%memPhysicalTotal%M 内存可用率:%memAvaiPercent:~1,4%%% C盘可用:%CGfree%G C盘容量:%CGsize%G C盘可用率:%CfreePercent%%% 网络连接速度:最短:%netMin% 最长:%netMax% 平均:%netAva% 超100M进程:%processlist% >> %file%

 96 

 97 exit

 98 

 99 

100 

101 rem 除法运算,调用方法为call:division [除数] [被除数] [商] [精确位数] 如:call:division 12 234 quot 3

102 :division

103 setlocal

104 set str1=%1

105 set str2=%2

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

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

108 if "%d%" == "1" (set d=-) else set "d="

109 set l=00000000&for /l %%i in (1 1 7) do set "l=!l!!l!"

110 set "var=4096 2048 1024 512 256 128 64 32 16 8 4 2 1"

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

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

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

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

115         set /a n+=1

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

117         for %%b in (%var%) do if "!S:~%%b!" neq "" set/a str%%i_!n!+=%%b&set "S=!S:~%%b!"

118         set /a len%%i+=str%%i_!n!

119     )

120         set str%%i=!str%%i:.=!

121 )

122 if !str1_2! gtr !str2_2! (set /a len2+=str1_2-str2_2) else set /a len1+=str2_2-str1_2

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

124     set str%%i=!str%%i!!l!

125     for %%j in (!len%%i!) do set " str%%i=!str%%i:~,%%j!"

126 )

127 for /f "tokens=* delims=0" %%i in ("!str2!") do set s=%%i&set "str2=0%%i"

128 set len2=1

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

130 set /a len=len2+1

131 if !len1! lss !len2! set len1=!len2!&set "str1=!l:~-%len2%,-%len1%!!str1!"

132 set /a len1+=u&set str1=0!str1!!l:~,%u%!

133 set str=!str1:~,%len2%!

134 set "i=0000000!str2!"&set /a Len_i=Len2+7

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

136     set "T=0"

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

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

139         set Num%%i=!T:~-8!!Num%%i!&set /a "T=!T:~,-8!-%%i"

140     )

141     set Num%%i=!T!!Num%%i!

142     set "Num%%i=0000000!Num%%i:~-%Len%!"

143 )

144 for /L %%a in (!len2! 1 !Len1!) do (

145     set "str=!L!!str!!str1:~%%a,1!"

146     set "str=!str:~-%Len%!"

147     if "!str!" geq "!str2!" (

148        set M=1&set i=0000000!str!

149        for /l %%i in (2 1 9) do if "!i!" geq "!Num%%i!" set "M=%%i"

150            set sun=!sun!!M!&set str=&set T=0

151            for %%i in (!M!) do (

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

153                    set /a "T=3!i:~-%%j,8!-1!Num%%i:~-%%j,8!-!T:~,1!%%2"

154                    set "str=!T:~1!!str!"

155                )

156            )

157     ) else set sun=!sun!0

158 )

159 if defined u if "%u%" gtr "0" set sun=!sun:~,-%u%!.!sun:~-%u%!

160 endlocal&set %3=%d%%sun%

 

你可能感兴趣的:(批处理)