spacecruft-galmon/scripts/galmonmonmon

98 lines
3.4 KiB
Python
Executable File

#!/usr/bin/env python3
"""
galmonmonmon
Monitor galmon ground station status
Copyright (C) 2022, Jeff Moe
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
# Usage:
# galmonmonmon --id=[Ground Station ID] --sec=[Alert max in seconds]
# Example:
# Alert if station ID 192 is down for more than 1 hour (3600 seconds).
# ./galmonmonmon --id=192 --sec=3600
import argparse
import os
import pandas as pd
from datetime import datetime
# Live URL
observer_url=('https://galmon.eu/observers.json')
# Cached file
#observer_url=('data/observers.json')
def get_observer(observer_id):
observers = pd.read_json(observer_url, orient='records')
for x in range(len(observers.id)):
if observers['id'][x] == observer_id:
observer_owner=(observers['owner'][x])
observer_last_seen=(observers['last-seen'][x])
return observer_owner, observer_last_seen
def get_status(observer_last_seen):
timestamp = datetime.fromtimestamp(observer_last_seen)
unix_time = get_unix_time()
diff_time = ( unix_time - timestamp ).total_seconds()
return(diff_time)
def get_unix_time():
now = datetime.now()
ts = now.timestamp()
unix_time=datetime.fromtimestamp(ts)
return unix_time
def parse_cli():
parser = argparse.ArgumentParser(description='Monitor galmon observation ground station.')
parser.add_argument('--id', type=int, required=True,
help='Galmon observation ground station ID')
parser.add_argument('--sec', type=int, required=True,
help='Alert max in seconds')
args = parser.parse_args()
observer_id = args.id
alert_sec = args.sec
return observer_id, alert_sec
def show_status(observer_id, observer_owner, observer_sec, alert_sec):
float_time=float(alert_sec)
if observer_sec > float_time:
observer_alert(observer_id, observer_owner, observer_sec)
else:
observer_ok(observer_id, observer_owner, observer_sec)
def observer_alert(observer_id, observer_owner, observer_sec):
print('ALERT: ', end='')
print('Station ', observer_id, ', ', observer_owner, end='', sep='')
print(', last seen', '{:.0f}'.format(observer_sec), 'seconds ago.')
# Put a shell command you want to run here, perhaps.
#os.system('echo OMG down | mail -s"Galmon alert" foo@barmail')
#os.system('/usr/local/bin/my-chat-alert-script.sh')
def observer_ok(observer_id, observer_owner, observer_sec):
print('OK: ', end='')
print('Station ', observer_id, ', ', observer_owner, end='', sep='')
print(', last seen', '{:.0f}'.format(observer_sec), 'seconds ago.')
def main():
observer_id, alert_sec=parse_cli()
observer_owner, observer_last_seen=get_observer(observer_id);
observer_sec=get_status(observer_last_seen)
show_status(observer_id, observer_owner, observer_sec, alert_sec)
if __name__ == "__main__":
main();