implemented job metrics
This commit is contained in:
parent
503e9b69a1
commit
1c11148374
5 changed files with 115 additions and 25 deletions
60
files/monitor-cronjob.py
Executable file
60
files/monitor-cronjob.py
Executable file
|
|
@ -0,0 +1,60 @@
|
|||
#!/bin/python3
|
||||
import time
|
||||
import sys
|
||||
import re
|
||||
import os
|
||||
import croniter
|
||||
import subprocess
|
||||
|
||||
name = sys.argv[1]
|
||||
cron_expr = sys.argv[2]
|
||||
cmd = sys.argv[3]
|
||||
exporterdir = '/var/lib/prometheus/node-exporter'
|
||||
|
||||
label_name = name.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n')
|
||||
file_name = re.sub('[^a-zA-Z0-9]', '_', name)
|
||||
|
||||
start_ts = time.time()
|
||||
result = subprocess.run(cmd, shell=True)
|
||||
end_ts = time.time()
|
||||
|
||||
next_ts = croniter.croniter(cron_expr).get_next()
|
||||
|
||||
jobstats = f'''# Generated by monitor-cronjob.py
|
||||
|
||||
# HELP cron_job_last_run_seconds Unix timestamp of last run
|
||||
# TYPE cron_job_last_run_seconds counter
|
||||
cron_job_last_run_seconds{{name="{ label_name }"}} { start_ts }
|
||||
|
||||
# HELP cron_job_exit_code Exit code of last run
|
||||
# TYPE cron_job_exit_code gauge
|
||||
cron_job_exit_code{{name="{ label_name }"}} { result.returncode }
|
||||
|
||||
# HELP cron_job_duration_seconds Duration of last run
|
||||
# TYPE cron_job_duration_seconds gauge
|
||||
cron_job_duration_seconds{{name="{ label_name }"}} { end_ts - start_ts }
|
||||
|
||||
# HELP cron_job_next_run_seconds Unix timestamp of next run
|
||||
# TYPE cron_job_next_run_seconds counter
|
||||
cron_job_next_run_seconds{{name="{ label_name }"}} { next_ts }
|
||||
'''
|
||||
|
||||
successstats = f'''# Generated by monitor-cronjob.py
|
||||
|
||||
# HELP cron_job_last_success_seconds Unix timestamp of last successful run
|
||||
# TYPE cron_job_last_success_seconds counter
|
||||
cron_job_last_success_seconds{{name="{ label_name }"}} { start_ts }
|
||||
'''
|
||||
|
||||
tmpsuffix = '.%d'%(os.getpid())
|
||||
|
||||
path = os.path.join(exporterdir, f'cron_job_generic_{ file_name }.prom')
|
||||
with open(path + tmpsuffix, 'w') as f:
|
||||
f.write(jobstats)
|
||||
os.rename(path + tmpsuffix, path)
|
||||
|
||||
if result.returncode == 0:
|
||||
path = os.path.join(exporterdir, f'cron_job_success_{ file_name }.prom')
|
||||
with open(path + tmpsuffix, 'w') as f:
|
||||
f.write(successstats)
|
||||
os.rename(path + tmpsuffix, path)
|
||||
Loading…
Add table
Add a link
Reference in a new issue