diff --git a/monitoring.py b/monitoring.py index 70220101..2d6e280c 100644 --- a/monitoring.py +++ b/monitoring.py @@ -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 diff --git a/tests/test_monitoring.py b/tests/test_monitoring.py new file mode 100644 index 00000000..87475fcf --- /dev/null +++ b/tests/test_monitoring.py @@ -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"]