Skip to content

Commit

Permalink
Update ProcessStats query by using API instead of parsing ps command.
Browse files Browse the repository at this point in the history
  • Loading branch information
FengPan-Frank committed Feb 29, 2024
1 parent 970e7b3 commit 3fca792
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
1 change: 1 addition & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ stages:
- script: |
set -xe
sudo pip3 install enum34
sudo pip install psutil
sudo pip3 install swsssdk-2.0.1-py3-none-any.whl
sudo pip3 install sonic_py_common-1.0-py3-none-any.whl
sudo pip3 install sonic_yang_mgmt-1.0-py3-none-any.whl
Expand Down
31 changes: 21 additions & 10 deletions scripts/procdockerstatsd
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Daemon which periodically gathers process and docker statistics and pushes the d
'''

import os
import psutil
import re
import subprocess
import sys
Expand Down Expand Up @@ -136,18 +137,28 @@ class ProcDockerStats(daemon_base.DaemonBase):
return True

def update_processstats_command(self):
cmd0 = ["ps", "-eo", "uid,pid,ppid,%mem,%cpu,stime,tty,time,cmd", "--sort", "-%cpu"]
cmd1 = ["head", "-1024"]
exitcode, data = getstatusoutput_noshell_pipe(cmd0, cmd1)
if any(exitcode):
cmd = ' | '.join([' '.join(cmd0), ' '.join(cmd1)])
self.log_error("Error running command '{}'".format(cmd))
data = None
processdata = self.format_process_cmd_output(data)
value = ""
processdata = []
for process in psutil.process_iter(['pid', 'ppid', 'memory_percent', 'cpu_percent', 'create_time', 'cmdline']):
try:
uid = process.uids().real
pid = process.pid
ppid = process.ppid()
mem = process.memory_percent()
cpu = process.cpu_percent()
stime = process.create_time()
tty = process.terminal()
time = process.cpu_times().user + process.cpu_times().system
cmd = ' '.join(process.cmdline())

row = {'PID': pid, 'UID': uid, 'PPID': ppid, '%CPU': cpu, '%MEM': mem, 'STIME': stime, 'TT': tty, 'TIME': time, 'CMD': cmd}
processdata.append(row)
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass

# wipe out all data before updating with new values
self.state_db.delete_all_by_pattern('STATE_DB', 'PROCESS_STATS|*')
for row in processdata[0:]:

for row in processdata:
cid = row.get('PID')
if cid:
value = 'PROCESS_STATS|{}'.format(cid)
Expand Down
17 changes: 15 additions & 2 deletions tests/procdockerstatsd_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
import os
import psutil
import pytest
from unittest.mock import call, patch
from swsscommon import swsscommon
Expand Down Expand Up @@ -53,8 +54,20 @@ def test_run_command(self):
def test_update_processstats_command(self):
expected_calls = [call(["ps", "-eo", "uid,pid,ppid,%mem,%cpu,stime,tty,time,cmd", "--sort", "-%cpu"], ["head", "-1024"])]
pdstatsd = procdockerstatsd.ProcDockerStats(procdockerstatsd.SYSLOG_IDENTIFIER)
with patch("procdockerstatsd.getstatusoutput_noshell_pipe", return_value=([0, 0], 'output')) as mock_cmd:
pdstatsd.update_processstats_command()

# Patch the 'psutil.process_iter' function to return a list of mock processes
with patch("psutil.process_iter") as mock_process_iter:
mock_processes = [
psutil.Process(pid=123, name='test_process1', status='running', cmdline=['command']),
psutil.Process(pid=456, name='test_process2', status='running', cmdline=['command2']),
]
mock_process_iter.return_value = mock_processes

# Patch the 'procdockerstatsd.getstatusoutput_noshell_pipe' function
with patch("procdockerstatsd.getstatusoutput_noshell_pipe", return_value=([0, 0], 'output')) as mock_cmd:
pdstatsd.update_processstats_command()

# Perform assertions
mock_cmd.assert_has_calls(expected_calls)

@patch('procdockerstatsd.getstatusoutput_noshell_pipe', return_value=([0, 0], ''))
Expand Down

0 comments on commit 3fca792

Please sign in to comment.