Skip to content

Commit

Permalink
include io counters and cpu times
Browse files Browse the repository at this point in the history
  • Loading branch information
cofiem committed Apr 23, 2024
1 parent 9bbbe90 commit f5da1ed
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/server_monitor_agent/service/server/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,26 @@ class ProcessResult(agent_model.OpResult):
user: typing.Optional[str] = None
pid: typing.Optional[int] = None
cpu_percent: typing.Optional[float] = None
"""gauge of percent of total system cpu usage since last call"""
mem_percent: typing.Optional[float] = None
"""gauge of percent of total system memory used by process rss"""
vms: typing.Optional[int] = None
"""gauge of 'Virtual Memory Size', the total amount of virtual memory used by a process"""
rss: typing.Optional[int] = None
"""gauge of 'Resident Set Size', the non-swapped physical memory used by a process"""
cmdline: typing.Optional[str] = None
io_read_ops_count: typing.Optional[int] = None
"""counter (cumulative) of number of process I/O read operations"""
io_write_ops_count: typing.Optional[int] = None
"""counter (cumulative) of number of process I/O write operations"""
io_read_bytes_count: typing.Optional[int] = None
"""counter (cumulative) of number of process I/O bytes read"""
io_write_bytes_count: typing.Optional[int] = None
"""counter (cumulative) of number of process I/O bytes written"""
cpu_time_count: typing.Optional[float] = None
"""counter (cumulative) of cpu time used by system and user"""
cpu_usable_count: typing.Optional[int] = None
"""gauge of number of cpus process can use"""


@beartype.beartype
Expand Down
29 changes: 29 additions & 0 deletions src/server_monitor_agent/service/server/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ def cpu_usage(interval: float = 2.0) -> float:
def processes() -> typing.List[server_model.ProcessResult]:
"""Get a list of the local processes."""

# Note: Linux doesn't seem to expose easily usable per-process network stats.
# Maybe `sudo lsof -niTCP` combined with `iftop`?
# See https://github.com/giampaolo/psutil/issues/1900

result = []
attrs = {
"pid": "PID",
Expand All @@ -154,6 +158,8 @@ def processes() -> typing.List[server_model.ProcessResult]:
"cpu_percent": "%CPU",
"memory_info": ["VSZ", "RSS"],
"username": "USER",
"io_counters": "",
"cpu_times": "",
}
count = 0
for p in psutil.process_iter(attrs=list(attrs.keys()), ad_value=None):
Expand All @@ -168,9 +174,15 @@ def processes() -> typing.List[server_model.ProcessResult]:
vms = info.get("memory_info").vms if info.get("memory_info") else 0
rss = info.get("memory_info").rss if info.get("memory_info") else 0

# ignore a process that is using no memory
if (vms + rss) < 1:
continue

# note that memory percent can be 0 if there is no previous information
mem_raw = info.get("memory_percent")
mem_p = round(mem_raw, 1) if mem_raw is not None else None

# note that cpu percent can be 0 if there is no previous information
cpu_raw = info.get("cpu_percent")
cpu_p = round(cpu_raw, 1) if cpu_raw is not None else None

Expand All @@ -179,6 +191,17 @@ def processes() -> typing.List[server_model.ProcessResult]:
else:
cmdline = info.get("name")

io_counters = info.get("io_counters")
io_read_ops_count = io_counters.read_count
io_write_ops_count = io_counters.write_count
io_read_bytes_count = io_counters.read_bytes
io_write_bytes_count = io_counters.write_bytes

cpu_times = info.get("cpu_times")
cpu_time_count = cpu_times.user + cpu_times.system

cpu_usable_count = len(p.cpu_affinity())

result.append(
server_model.ProcessResult(
exit_code=0,
Expand All @@ -189,6 +212,12 @@ def processes() -> typing.List[server_model.ProcessResult]:
vms=vms,
rss=rss,
cmdline=cmdline,
io_read_ops_count=io_read_ops_count,
io_write_ops_count=io_write_ops_count,
io_read_bytes_count=io_read_bytes_count,
io_write_bytes_count=io_write_bytes_count,
cpu_time_count=cpu_time_count,
cpu_usable_count=cpu_usable_count,
)
)

Expand Down

0 comments on commit f5da1ed

Please sign in to comment.