Page 1 of 1

typeperf commands in batch script issue

Posted: 29 Apr 2014 10:35
by sambasiva
Hi,

Appreciate any help with below script.



@echo off
setlocal enabledelayedexpansion

set time_interval=%1
set iteration_set=
:logMemCpuUsage
if "!iteration_set!" EQU "1" (
echo inside if
for /f "skip=1 tokens=* delims=EOL" %%a in ('typeperf "\Processor(_Total)\%% Processor Time" -SC 1 -y') do echo samba:%%a
for /f "skip=1 tokens=* delims=EOL" %%i in ('typeperf "\Memory\Available MBytes" -SC 1 -y') do echo siva:%%i
) else (
echo inside else
typeperf "\processor(_Total)\%% Processor Time" -SC 1 -y
typeperf "\Memory\Available MBytes" -SC 1 -y
set iteration_set=1
)
timeout /t %time_interval% /nobreak >nul
goto :logMemCpuUsage


1)Actual requirement is to log the CPU usage and Available RAM at every inputted interval of seconds.
2)When I execute the typeperf for processor Time out side the for loop it prints the output properly.

C:\>typeperf "\Processor(_Total)\% Processor Time" -SC 1 -y
"(PDH-CSV 4.0)","\\SBATTAGI-LAP\Processor(_Total)\% Processor Time"
"04/29/2014 21:57:12.660","9.909430"

The command completed successfully.

C:\>

But I wanted to log the first line of this output only for the time and subsequently log only second line (to a csv file) and ignore the last line which says "The command completed successfully" as well.

For this I added a if condition and inside that I am running processor time command using for statement and skipping the first line.
Surprisingly the output inside the for statement is still having the first line and also it has extra lines of output which was not there when I run the same command outside of for statement.

The output I am getting is:
--------------------------
C:\>C:\MemCpuUsage.cmd 5
inside else

"(PDH-CSV 4.0)","\\SBATTAGI-LAP\processor(_Total)\% Processor Time"
"04/29/2014 22:02:59.470","7.179410"

The command completed successfully.

"(PDH-CSV 4.0)","\\SBATTAGI-LAP\Memory\Available MBytes"
"04/29/2014 22:03:00.880","4904.000000"

The command completed successfully.
inside if
samba:"(PDH-CSV 4.0)","\\SBATTAGI-LAP\Processor(_Total)\% Processor Time"
samba:"04/29/2014 22:03:06.560","9.519420"
samba:xiting, please wait...
samba:The command completed successfully.

siva:"(PDH-CSV 4.0)","\\SBATTAGI-LAP\Memory\Available MBytes"
siva:"04/29/2014 22:03:08.030","4900.000000"
siva:xiting, please wait...
siva:The command completed successfully.
siva:
Terminate batch job (Y/N)? y

C:\>


Please help me to resolve this issue.
I need to skip the first,last line in the ouput and also the extra line of output "xiting, please wait..."

Thanks
Sambasiva

Re: typeperf commands in batch script issue

Posted: 29 Apr 2014 23:12
by Magialisk
Quick and dirty as is my usual style, but I believe this does what you're looking for. Just tweak the file names I created to suit your liking.

Code: Select all

@echo off
setlocal enabledelayedexpansion
:: Get input value and define default interval if none provided
set time_interval=%~1
IF "%time_interval%"=="" set time_interval=5
:: Filenames to log to
set cpu_csv=CPU.csv
set mem_csv=MEM.csv
:: Flags for first typeperf calls
set firstCPU=true
set firstMEM=true

:logMemCpuUsage
set line=0
for /f "tokens=*" %%a in ('typeperf "\Processor(_Total)\%% Processor Time" -SC 1 -y') do (
   set /a line+=1
   IF !line!==1 IF !firstCPU!==true (
      echo samba:%%a>>%cpu_csv%
      set firstCPU=false
   )
   IF !line!==2 echo samba:%%a>>%cpu_csv%
)
set line=0
for /f "tokens=*" %%a in ('typeperf "\Memory\Available MBytes" -SC 1 -y') do (
   set /a line+=1
   IF !line!==1 IF !firstMEM!==true (
      echo siva:%%a>>%mem_csv%
      set firstMEM=false
   )
   IF !line!==2 echo siva:%%a>>%mem_csv%
)
timeout /t %time_interval% /nobreak >nul
goto :logMemCpuUsage
I ran it for a few seconds to test it, and obtained the following output:

Code: Select all

C:\Users\Marc\Desktop>test 1
^CTerminate batch job (Y/N)? y

C:\Users\Marc\Desktop>type CPU.csv
samba:"(PDH-CSV 4.0)","\\WORKSTATION\Processor(_Total)\% Processor Time"
samba:"04/29/2014 22:03:52.044","2.504947"
samba:"04/29/2014 22:03:55.430","8.835798"
samba:"04/29/2014 22:03:58.393","6.014776"
samba:"04/29/2014 22:04:01.449","0.945036"
samba:"04/29/2014 22:04:04.407","3.771123"

C:\Users\Marc\Desktop>type MEM.csv
siva:"(PDH-CSV 4.0)","\\WORKSTATION\Memory\Available MBytes"
siva:"04/29/2014 22:03:53.340","1487.000000"
siva:"04/29/2014 22:03:56.710","1487.000000"
siva:"04/29/2014 22:03:59.666","1486.000000"
siva:"04/29/2014 22:04:02.726","1488.000000"

C:\Users\Marc\Desktop>
Note that the program can't actually get results every second, as it seems that a single execution of 'typeperf' takes one second, and you're running it twice. So assuming a zero input value for timeout, you're still only going to get data every other second. With my timeout input of "1" it corresponds to data every 3rd second, etc. I'm not sure if that's an issue for you, since you didn't specify how frequently you're actually trying to log data. You might want to do a 'set /a time_interval=%~1 -2' or similar (with additional error checking) to get a more precise correlation between input value and collection rate.

Re: typeperf commands in batch script issue

Posted: 30 Apr 2014 02:51
by sambasiva
Thanks a lot Magialisk.

That's the output I am looking for.

-SS