Skip to content

Instantly share code, notes, and snippets.

@dadatuputi
Last active January 25, 2021 06:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dadatuputi/0ab8eecf05e6c49dd52b38ee3632c428 to your computer and use it in GitHub Desktop.
Save dadatuputi/0ab8eecf05e6c49dd52b38ee3632c428 to your computer and use it in GitHub Desktop.
Elixxir Monitor
#! /usr/bin/env python3
"""elixxir_monitor.py: Monitors an elixxir node log for signs of activity and alerts when enough time elapses without activity"""
import time, subprocess, select, time, logging, signal, pushover
# Wait this long to send alert - default 5 minutes
alert_time = 60*5
logfile = "/opt/xxnetwork/node-logs/node.log"
search_string = '"took\|Updating"'
# Setup Pushover
po = pushover.Client("PUSHOVER CLIENT API TOKEN", api_token="PUSHOVER APP API TOKEN")
# Setup logging
logging.basicConfig(format='%(asctime)s %(levelname)s:%(message)s', level=logging.INFO)
# Alarm handler - this runs when the timeout has been reached
last_log = time.time()
def handler(signum, stack):
_t_elapsed = time.time() - last_log
_msg = "Alert - Elixxir node hasn't participated in a round for {} seconds".format(int(_t_elapsed))
logging.error(_msg)
po.send_message(_msg, title="Elixxir Alert")
signal.alarm(alert_time)
# Set up alarm to trigger if we don't receive an alert in time
signal.signal(signal.SIGALRM, handler)
# Spawns a process to tail the log and grep for the search string
proc = subprocess.Popen('tail -F {} | grep --line-buffered {}'.format(logfile, search_string),
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
try:
# Wait for output from the process until it exits
while not proc.poll():
signal.alarm(alert_time)
_l = proc.stdout.readline().decode('utf-8').strip()
logging.debug("Log read: {}".format(_l))
last_log = time.time()
finally:
po.send_message("Elixxir monitor stopped")
@redindian
Copy link

Thanks for sharing! Make sure you have python-pushover installed: pip install python-pushover

@dadatuputi
Copy link
Author

Thanks for sharing! Make sure you have python-pushover installed: pip install python-pushover

Yes, thanks for that. the pushover package doesn't work.

@redindian
Copy link

redindian commented Jan 25, 2021

It's quite easy to run this as a service, just follow this guide and use this service file. Make sure the user running it (for example root) also has python-pushover installed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment