From 35f2394f5fd0bce421ff617137c425e4d88fcea1 Mon Sep 17 00:00:00 2001 From: Yax <1949284+kianby@users.noreply.github.com> Date: Mon, 1 Feb 2021 19:25:00 +0100 Subject: [PATCH] monitoring --- check_git.sh | 9 +++++++++ monitor.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ params.json | 3 ++- 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100755 check_git.sh create mode 100755 monitor.py diff --git a/check_git.sh b/check_git.sh new file mode 100755 index 0000000..f11dbcf --- /dev/null +++ b/check_git.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +if git diff-index --quiet HEAD --; then + # no change + exit 0 +else + # change + exit 1 +fi \ No newline at end of file diff --git a/monitor.py b/monitor.py new file mode 100755 index 0000000..37059f1 --- /dev/null +++ b/monitor.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import sys +import os +import requests +import time +import json + +def fread(filename): + """Read file and close the file.""" + with open(filename, "r") as f: + return f.read() + +def get_nb_of_comments(): + req_url = params["stacosys_url"] + "/comments/count" + query_params = dict( + token=params["stacosys_token"] + ) + resp = requests.get(url=req_url, params=query_params) + return 0 if not resp.ok else int(resp.json()["count"]) + +def exit_program(): + sys.exit(0) + +# Default parameters. +params = { + "stacosys_token": "", + "stacosys_url": "", + "external_check": "", +} + +# If params.json exists, load it. +if os.path.isfile("params.json"): + params.update(json.loads(fread("params.json"))) + +external_check_cmd = params["external_check"] +initial_count = get_nb_of_comments() +print(f"Comments = {initial_count}") +while True: + # check number of comments every 60 seconds + for _ in range(15): + time.sleep(60) + if initial_count != get_nb_of_comments(): + exit_program() + # check if git repo changed every 15 minutes + if external_check_cmd and os.system(external_check_cmd): + exit_program() diff --git a/params.json b/params.json index 1341014..8f56a91 100644 --- a/params.json +++ b/params.json @@ -4,5 +4,6 @@ "author": "Yax", "site_url": "https://blogduyax.madyanne.fr", "stacosys_token": "9fb3fc042c572cb831005fd16186126765140fa2bd9bb2d4a28e47a9457dc26c", - "stacosys_url": "http://stacosys:8100" + "stacosys_url": "http://stacosys:8100", + "external_check": "./check_git.sh" }