--- /dev/null
+#!/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()