From 8e5cf43167c6be421a2c621ed0516f5ca4e3e38d Mon Sep 17 00:00:00 2001 From: jbruce Date: Sat, 21 May 2022 13:54:54 -0400 Subject: [PATCH] created watcher script, changed api endpt to /api/stats and get instead of post --- kiln-controller.py | 10 ++++++++- watcher.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100755 watcher.py diff --git a/kiln-controller.py b/kiln-controller.py index 137c408..e2660b8 100755 --- a/kiln-controller.py +++ b/kiln-controller.py @@ -47,10 +47,18 @@ ovenWatcher = OvenWatcher(oven) def index(): return bottle.redirect('/picoreflow/index.html') +@app.get('/api/stats') +def handle_api(): + log.info("/api/stats command received") + if hasattr(oven,'pid'): + if hasattr(oven.pid,'pidstats'): + return json.dumps(oven.pid.pidstats) + + @app.post('/api') def handle_api(): log.info("/api is alive") - log.info(bottle.request.json) + # run a kiln schedule if bottle.request.json['cmd'] == 'run': diff --git a/watcher.py b/watcher.py new file mode 100755 index 0000000..a4d3f44 --- /dev/null +++ b/watcher.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python +import requests +import json +import time +import datetime + +# this monitors your kiln stats every N seconds +# if X checks fail, an alert is sent to a slack channel +# configure an incoming web hook on the slack channel +# set slack_hook_url to that + +def get_stats(): + try: + r = requests.get(kiln_url) + return r.json() + except: + return {} + +def send_alert(msg): + try: + r = requests.post(slack_hook_url, json={'text': msg }) + except: + pass + +if __name__ == "__main__": + + kiln_url = "http://0.0.0.0:8081/api/stats" + slack_hook_url = "you must set this" + + bad_check_limit = 6 + bad_checks = 0 + temp_error_limit = 10 + sleepfor = 10 + + while(True): + stats = get_stats() + + if 'time' not in stats: + bad_checks = bad_checks + 1 + print("no data") + if 'err' in stats: + if abs(stats['err']) > temp_error_limit: + bad_checks = bad_checks + 1 + print ("temp out of whack") + if bad_checks >= bad_check_limit: + print("ERR sending alert") + msg = "error kiln needs help. %s" % stats + send_alert(msg) + bad_checks = 0 + else: + print("OK %s" % datetime.datetime.now()) + + time.sleep(sleepfor) +