I am trying to use some example code I have found various places including here ( Tracking CPU and Memory usage per process ) to monitor process cpu of an individual process, and output a text file with a status of "green" "yellow" or "red" based on some alert parameters. (this to be used with BBWIN and XYMON).
I have it almost working in a general sense, but for some reason it loops through twice every time I call it, and the second time it fails to evaluate the command properly (returning "please wait..")
Below is my sample code (not cleaned up 100%) and below that is the output. Any help greatly appreciated. (FYI, this is on server 2012R2, and for whatever reason delayed expansion does NOT work on this server, hence the For Do 'call' method instead below)
@echo off
set process=%~1
set outputfile=%~2
set status=green
set y_alert=1
set r_alert=10
for /f "tokens=2 delims=," %%c in ('typeperf "\Process(%process%)\%% Processor Time" -si 1 -sc 1 ^| find /V "\\"') do (
if %%~c==-1 (
goto :failed
) else (
call :eval %%c
)
)
goto:eof
:eval
set _percent=%~1
echo Process CPU is %_percent%
if %_percent% GEQ %r_alert% (
set status=red
echo Status Set to %status%
) else (
if %_percent% GEQ %y_alert% (
set status=yellow
echo Status Set to %status%
)
)
echo Debug-status is %status%
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
For /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a:%%b)
echo %status% its-ps-pt9est4.ps.syr.edu %mydate%_%mytime% > %outputfile%
echo Process Percent >> %outputfile%
echo %process% %_percent% >> %outputfile%
goto:eof
:failed
echo ERROR: Process %process% terminated or does not exist > %outputfile%
goto end
Output from running this with the following command:
C:\Apps-SU>proccpu4.bat wlsvcX64 test
Process CPU is 0.000000
Debug-status is green
Process CPU is please
Status Set to green
Debug-status is red