From: Guillaume Pellerin Date: Wed, 13 Mar 2024 09:49:42 +0000 (+0100) Subject: add nagios server check X-Git-Tag: 3.0~5^2 X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=58e73db27af102697dcefd1973def964bf71d7f9;p=telecaster-server.git add nagios server check --- diff --git a/bin/monitoring/check_telecaster.py b/bin/monitoring/check_telecaster.py new file mode 100755 index 0000000..ddc383d --- /dev/null +++ b/bin/monitoring/check_telecaster.py @@ -0,0 +1,95 @@ +#!/usr/bin/python3 + +import os, sys, psutil + + +class TelecasterCheck: + """Nagios compatible Telecaster server check""" + + record_paths = ['/home/telecaster/trash', '/home/telecaster/monitor'] + formats = ['mp3', 'webm'] + daemons = [ + {'proc': 'jackd', 'args': ''}, + {'proc': 'Xtigervnc', 'args': ''}, + {'proc': 'gst-launch-1.0', 'args': 'lamemp3enc'}, + {'proc': 'gst-launch-1.0', 'args': 'vp8enc'}, + {'proc': 'deefuzzer', 'args': 'telecaster_mp3_monitor'}, + {'proc': 'deefuzzer', 'args': 'telecaster_webm_monitor'}, + ] + + log_path = '/var/log/telecaster/' + + OK_STATE = 0 + WARNING_STATE = 1 + CRITICAL_STATE = 2 + + def __init__(self): + self.message = "" + self.is_up = True + self.is_writing = True + + def get_pid(self, name, args=None): + """Get a process pid filtered by arguments and uid""" + for proc in psutil.process_iter(): + if proc.cmdline(): + if name == proc.name(): + if args: + if args in proc.cmdline()[1:]: + return proc.pid + else: + return proc.pid + return None + + def get_dir_size(self, path='.'): + """https://note.nkmk.me/en/python-os-path-getsize/""" + total = 0 + with os.scandir(path) as it: + for entry in it: + if entry.is_file(): + total += entry.stat().st_size + elif entry.is_dir(): + total += self.get_dir_size(entry.path) + return total + + def check_daemons(self): + for daemon in self.daemons: + if not self.get_pid(daemon['proc'], args=daemon['args']): + self.is_up = False + self.message += daemon['proc'] + " " + daemon['args'] + " is OFF" + "\n" + + def check_writing(self): + for record_path in self.record_paths: + if os.path.exists(record_path): + for format in self.formats: + size = self.get_dir_size(record_path + os.sep + format) + log = self.log_path + os.sep + format + '.log' + if not os.path.exists(log): + f = open(log, 'w') + f.write(str(size)) + f.close() + else: + f = open(log) + previous_size = f.read() + f.close() + if previous_size: + previous_size = int(previous_size) + if size == previous_size: + self.is_writing = False + self.message += format + " monitor is not writing\n" + f = open(log, 'w') + f.write(str(size)) + f.close() + + def run(self): + self.check_daemons() + self.check_writing() + if not self.is_up or not self.is_writing: + print(self.message) + sys.exit(self.CRITICAL_STATE) + else: + sys.exit(self.OK_STATE) + + +if __name__ == "__main__": + check = TelecasterCheck() + check.run()