How do i monitor CPU Usage on a Linux Agent Managed Server through Powershell?

João Silva 21 Reputation points
2021-07-22T12:44:31.833+00:00

Hello, i'm trying to build a dashboard with some performance counters without using the graphs but instead using the powershell grid script widget. But i can't seem to find any information regarding out to get the % cpu utilization on the linux machines through powershell? I understand Get-SCXAgents exists but it only shows me information relative to the agent itself? And Get-Counter only works on windows machines from what i read..

Any idea on how i could accomplish this?

Thanks in advance,
Joao

Operations Manager
Operations Manager
A family of System Center products that provide infrastructure monitoring, help ensure the predictable performance and availability of vital applications, and offer comprehensive monitoring for datacenters and cloud, both private and public.
1,493 questions
0 comments No comments
{count} votes

Accepted answer
  1. AlexZhu-MSFT 5,956 Reputation points Microsoft Vendor
    2021-07-23T08:20:34.4+00:00

    Hi,

    As for as I know, it's not so easy to get CPU usage of Linux computers to show the data in the dashboard. Strong PowerShell skill and Linux knowledge are required. Here's some thoughts shared, just for your reference.

    If we have SSH client installed in the Management Server (Note: Server 2019 is required to install SSH client) and have configured passwordless ssh connection as mentioned below, we may create a PowerShell Grid Widget, enter the script (just an example, need to be completed) to see if it works.

    How to Setup Passwordless SSH Connect from Windows to Linux
    Note: This is not from Microsoft, just for your reference

    For writing a complete script, we may refer the article below.
    Operations Manager Dashboard Script Widgets

    $i=1  
    foreach ($computer in $computers)  
    {  
        $cpu_usage_linux = ssh root@10.1.1.250 "top -bn 2 -d 0.01 | grep '^%Cpu' | tail -n 1 | gawk '{print `$2+`$4+`$6}'"  
        $dataObject=$ScriptContext.CreateFromObject/CreateInstance.......  
        $dataObject["Index"]=$i  
        $dataObject["CPU"]=$cpu_usage_linux  
        $ScriptContext.ReturnCollection.Add($dataObject)  
        $i++  
    }  
    

    add a widget

    117327-scom-dashboard-powershell-linux-01.png

    enter the script (only the main structure, need to be complete in real environment)

    117422-scom-dashboard-powershell-linux-02.png

    test the command in PS directly

    117366-scom-dashboard-powershell-linux-03.png

    Alex
    If the response is helpful, please click "Accept Answer" and upvote it.

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. João Silva 21 Reputation points
    2021-07-26T13:59:43.003+00:00

    I was able to do this thankfully.

    On another note, do you know of a way to get the values for the Bytes Sent and Received per second over the network, or on the interface? I was only able to find a reliable way to get the total bytes sent and received in the entire time of that interface and not the last value.

    How could i approach this? Thank you very much,

    Joao

    0 comments No comments

  2. AlexZhu-MSFT 5,956 Reputation points Microsoft Vendor
    2021-07-27T08:32:16.047+00:00

    Hi,

    Thank you very much for getting back. I'm not an export of Linux shell scripting. For this request, we may consider performing the task via the shell script. Thoughts:
    1, write a shell script that get the data we desired
    2, using ssh to run the script and return the output
    3, store the returned output to $dataObject as above

    For a quick test, I've put the above command in the test.bash to see if it works

    vi ./test.sh  
      
    #!/bin/bash  
    top -bn 2 -d 0.01 | grep '^%Cpu' | tail -n 1 | gawk '{print $2+$4+$6}'  
    

    118139-scom-dashboard-powershell-linux-04.png

    Alex
    If the response is helpful, please click "Accept Answer" and upvote it.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.