Search code examples
powershellwindows-server-2012freeswitchpbx

Script for monitoring CPU


I need to create some kind of script/runnable file for monitoring my Freeswitch PBX on Windows Server 2012 that:

  1. checks a number of calls each ~5s and then writes in into a file,
  2. checks % of CPU usage at that point and also writes it (into second column).

For the first part, I figured out how to check for actual number of calls flowing through:

fs_cli.exe -x "show calls count" > testlog.txt

but I have to do this manually and it always overwrites the previous one. I need the script to do this automatically every 5s until I stop the script.


Solution

  • fs_cli.exe -x "show calls count" >> testlog.txt
    

    (notice the additional >) will append text to the file instead of overwriting the file

    You can write a loop using this kind of code in PS:

    #never-ending loop, condition is always true
    while($true) {
    
        #run a command (fs_cli.exe -x "show calls count" >> testlog.txt)
        #or maybe several
        date
        (Get-WmiObject Win32_Processor).LoadPercentage >> c:\cpu_usage.txt
    
        #sleep for 5 seconds
        Start-Sleep -Seconds 5
    }