#!/usr/bin/env python3 # -*- coding: utf-8 -*- # from subprocess import run import getopt, sys """ The program assumes there is a rule in the OUTPUT chain creatd by the following command: ip6tables -A OUTPUT -p tcp --dest srv01.libre.space or iptables -A OUTPUT -p tcp --dest srv01.libre.space depending on yout netwprk setup The default setting is for IPv6 Author: Bent Bagger, OZ6BL. (c) 2021. """ status_file_name = '/root/bin/status.file' def usage(): print("""Usage: check_satnogs_polls [-h | --help] Options: -h, --help There are no options but '-h' or '--help' :-( """) def init(): try: opts, args = getopt.getopt(sys.argv[1:], "h", "help") except getopt.GetoptError as err: # print help information and exit: print(err) # will print something like "option -a not recognized" usage() sys.exit(2) for o, a in opts: if o in ("-h", "--help"): usage() sys.exit() else: assert False, "unhandled option" #print("crit: %d, warn: %d, period: %d" % (critical, warning, period)) def get_status(): status_file = open(status_file_name, mode = 'r') status = status_file.readline() status_file.close() return status def set_status(status): status_file = open(status_file_name, mode = 'w') status_file.write(status) status_file.close() return init() # get command line arguments # Get the OUTPUT chain try: text = run(['/usr/sbin/ip6tables', '-xvL', 'OUTPUT'], capture_output=True, check=True) except Exception as X: print(f'CRITICAL - Exception: {X}') sys.exit(2) finally: pass # Decode to a string with lines separated by new line lines = text.stdout.decode().split('\n') # Find the line for the SatNOGS server my_line = '' for line in lines: if 'libre.space' in line: my_line =line if my_line != '': count = my_line.split()[0] # no. packets seen else: print("Rule for the SatNOGS server not found\nDid you forget to add it to your firewall?") sys.exit(1) # Get the previous reading try: prev_count = get_status() #logger('got status') except IOError: prev_count = '0' # Save current reading set_status(count) if int(prev_count) < int(count): # Good - some datagrams have been sent print(f"OK: {count} datagrams seen so far") sys.exit(0) else: # No change since last probe print("CRITICAL: No change") sys.exit(2)