Fix: Track processes that start with the same chars independently (smtp vs stmpd) (#974)

* Fix: Track processes that start with the same chars independently (smtp vs stmpd)

* Format

* Added more test

Co-authored-by: Adrià Casajús <adria.casajus@proton.ch>
This commit is contained in:
Adrià Casajús 2022-05-12 12:37:19 +02:00 committed by GitHub
parent 79f6b2235e
commit 2660c96fa7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 2 deletions

View File

@ -34,6 +34,7 @@ def log_postfix_metrics():
proc_counts = get_num_procs(["smtp", "smtpd", "bounce", "cleanup"])
for proc_name in proc_counts:
LOG.d(f"Process count {proc_counts}")
newrelic.agent.record_custom_metric(
f"Custom/process_{proc_name}_count", proc_counts[proc_name]
)
@ -50,16 +51,20 @@ def get_num_procs(proc_names: List[str]) -> Dict[str, int]:
.communicate()[0]
.decode("utf-8")
)
return _process_ps_output(proc_names, data)
def _process_ps_output(proc_names: List[str], data: str) -> Dict[str, int]:
proc_counts = {proc_name: 0 for proc_name in proc_names}
lines = data.split("\n")
for line in lines:
entry = [field for field in line.split() if field.strip()]
entry = [field for field in line.strip().split() if field.strip()]
if len(entry) < 5:
continue
if entry[4][0] == "[":
continue
for proc_name in proc_names:
if entry[4].find(proc_name) == 0:
if entry[4] == proc_name:
proc_counts[proc_name] += 1
return proc_counts

17
tests/test_monitoring.py Normal file
View File

@ -0,0 +1,17 @@
from monitoring import _process_ps_output
def test_monitoring_proc_count():
data = """
PID TTY STAT TIME COMMAND
1432 ? S< 0:00 [loop44]
3438 ? Ssl 0:00 /bin/sh arg
3440 ? Sl 0:00 /bin/cron args
3440 ? Sl 0:00 smtp arg
3448 ? Sl 0:00 smtpd arg
3441 ? Sl 0:00 other smtpd arg
"""
result = _process_ps_output(["smtp", "smtpd", "cron"], data)
assert 1 == result["smtp"]
assert 1 == result["smtpd"]
assert 0 == result["cron"]