1

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
Community
  • 1
  • 1
  • 2
    Are you sure `typeperf "\Process(%process%)\%% Processor Time" -si 1 -sc 1 ^| find /V "\\"` returns only one line? – Dennis van Gils Apr 26 '16 at 13:23
  • Also note that batch can't handle decimals, so for instance `1.0000 geq 0.00000000` might not work – Dennis van Gils Apr 26 '16 at 13:25
  • So, you are correct - it is returning two lines... Not sure why. '"04/26/2016 09:48:01.526","0.000000" Exiting, please wait...' The command completed successfully. – Jeffrey Brinkerhoff Apr 26 '16 at 13:49
  • OK, I added another find clause, stripping out the "please" line. ^| find /V "please" I think that cleaned that up. Now I will need to address the decimal issue. Searching for a good idea on how to convert to integer. – Jeffrey Brinkerhoff Apr 26 '16 at 13:57
  • 3
    `set _percent=%percent:.=&rem;%` would replace the decimal with `&rem;`, effectively stripping off the floating point data and flooring the value of `%_percent%`. Bonus: you can print colors on the console by invoking a powershell command. Example: instead of `echo Status set to %status%`, try `powershell "write-host 'Status set to %status%' -f %status%"` – rojo Apr 26 '16 at 14:21

0 Answers0