From d526848ac99ca11b3bdbe48222cc9a8c9c71a36f Mon Sep 17 00:00:00 2001 From: theenoro Date: Mon, 10 May 2021 08:15:50 +0200 Subject: [PATCH] a1 - test scripts --- .gitignore | 2 ++ config/config.example.yaml | 17 ++++++++++ helper/parse_yaml.sh | 19 +++++++++++ index.js | 12 +++++++ runner.sh | 7 ++++ scripts/DHCP.py | 26 ++++++++++++++ scripts/FILESYSTEM.py | 32 ++++++++++++++++++ scripts/lib/__init__.py | 0 .../lib/__pycache__/__init__.cpython-37.pyc | Bin 0 -> 161 bytes scripts/lib/__pycache__/bcolor.cpython-37.pyc | Bin 0 -> 487 bytes scripts/lib/bcolor.py | 11 ++++++ scripts/notify.rb | 18 ++++++++++ 12 files changed, 144 insertions(+) create mode 100644 .gitignore create mode 100644 config/config.example.yaml create mode 100644 helper/parse_yaml.sh create mode 100644 index.js create mode 100644 runner.sh create mode 100644 scripts/DHCP.py create mode 100644 scripts/FILESYSTEM.py create mode 100644 scripts/lib/__init__.py create mode 100644 scripts/lib/__pycache__/__init__.cpython-37.pyc create mode 100644 scripts/lib/__pycache__/bcolor.cpython-37.pyc create mode 100644 scripts/lib/bcolor.py create mode 100644 scripts/notify.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1115446 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/config/config.yaml +/test.sh \ No newline at end of file diff --git a/config/config.example.yaml b/config/config.example.yaml new file mode 100644 index 0000000..7ba2ad9 --- /dev/null +++ b/config/config.example.yaml @@ -0,0 +1,17 @@ +global: + debug: yes + discord: + webhookurl: https://discord.com/api/webhooks/ + network: + interface: eth0 + dhcp: yes + filesystem: + - hdd: / + max: 20 + warn: 15 + - hdd: /mnt/hdd2 + max: 3570 + warn: 3000 + - hdd: /mnt/hdd3 + warn: 18000 + max: 20000 diff --git a/helper/parse_yaml.sh b/helper/parse_yaml.sh new file mode 100644 index 0000000..65cb6fd --- /dev/null +++ b/helper/parse_yaml.sh @@ -0,0 +1,19 @@ +#!/bin/sh +function parse_yaml { + local prefix=$2 + local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034') + sed -ne "s|^\($s\):|\1|" \ + -e "s|^\($s\)\($w\)$s:$s[\"']\(.*\)[\"']$s\$|\1$fs\2$fs\3|p" \ + -e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 | + awk -F$fs '{ + indent = length($1)/2; + vname[indent] = $2; + for (i in vname) {if (i > indent) {delete vname[i]}} + if (length($3) > 0) { + vn=""; for (i=0; i{ + console.log(req); + console.dir(req.body.monit.services[0]); +}); + +app.listen(8081); diff --git a/runner.sh b/runner.sh new file mode 100644 index 0000000..19da6e6 --- /dev/null +++ b/runner.sh @@ -0,0 +1,7 @@ +#!/bin/sh +source ./helper/parse_yaml.sh + +py=/usr/bin/python3 + + +${py} ./scripts/DHCP.py ${CONF_global_network_interface} ${CONF_global_network_dhcp} \ No newline at end of file diff --git a/scripts/DHCP.py b/scripts/DHCP.py new file mode 100644 index 0000000..8e1d60c --- /dev/null +++ b/scripts/DHCP.py @@ -0,0 +1,26 @@ +#!/usr/bin/python3 + +import sys +import re +import subprocess + +from lib import bcolor + +bcolors = bcolor.bcolors + +regex = r"inet ((([0-9]{1,3}.){4})(\/){0,1}([0-9]{0,2}))" + +INTERFACE=sys.argv[1] +DHCP=sys.argv[2] +RET="" +INTERFACE_DUMP = subprocess.run(["ip", "a" ,"show","dev",INTERFACE],stdout=subprocess.PIPE, text=True) +matches = re.search(regex, INTERFACE_DUMP.stdout) + +if DHCP == "yes": + if matches: + print(bcolors.BOLD + bcolors.OKCYAN+"[CONFIG - NETWORK]"+bcolors.ENDC+" found IP address on "+INTERFACE+".") + else: + print(bcolors.BOLD + bcolors.FAIL+"[CONFIG - NETWORK]"+bcolors.ENDC+" got no IP address, run dhclient on "+INTERFACE+"!") +else: + print(bcolors.BOLD + bcolors.WARNING+"[CONFIG - NETWORK]"+bcolors.ENDC+" no DHCP active in configuration!") + diff --git a/scripts/FILESYSTEM.py b/scripts/FILESYSTEM.py new file mode 100644 index 0000000..08acc44 --- /dev/null +++ b/scripts/FILESYSTEM.py @@ -0,0 +1,32 @@ +import sys +import re +import subprocess +import yaml +import shutil +from lib import bcolor + +bcolors = bcolor.bcolors + + +with open('./config/config.yaml') as f: + + data = yaml.load(f, Loader=yaml.FullLoader) + hhds = data["global"]["filesystem"] + for hddNum in range(0, len(hhds)): + total, used, free = shutil.disk_usage(hhds[hddNum]["hdd"]) + hdd_total = (total // (2**30)) + hdd_used = (used // (2**30)) + hdd_free = (free // (2**30)) + print(bcolors.BOLD + bcolors.HEADER+"⸢-------------------------------------------------------------------------"+bcolors.ENDC+"") + print(bcolors.BOLD + bcolors.HEADER+"⸠ [FILESYSTEM CHECK]"+bcolors.ENDC+" Filesystem: %s " % hhds[hddNum]["hdd"]) + if hhds[hddNum]["warn"] < hdd_used: + print(bcolors.BOLD + bcolors.FAIL+"⸠ [FILESYSTEM CHECK]"+bcolors.ENDC+" Free: %d GiB" % hdd_free) + print(bcolors.BOLD + bcolors.FAIL+"⸠ [FILESYSTEM CHECK]"+bcolors.ENDC+" Used: %d GiB" % hdd_used) + print(bcolors.BOLD + bcolors.FAIL+"⸤ [FILESYSTEM CHECK]"+bcolors.ENDC+" Total: %d GiB" % hdd_total) + else: + print(bcolors.BOLD + bcolors.OKGREEN+"⸠ [FILESYSTEM CHECK]"+bcolors.ENDC+" Free: %d GiB" % hdd_free) + print(bcolors.BOLD + bcolors.OKGREEN+"⸠ [FILESYSTEM CHECK]"+bcolors.ENDC+" Used: %d GiB" % hdd_used) + print(bcolors.BOLD + bcolors.OKGREEN+"⸤ [FILESYSTEM CHECK]"+bcolors.ENDC+" Total: %d GiB" % hdd_total) + + + \ No newline at end of file diff --git a/scripts/lib/__init__.py b/scripts/lib/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/scripts/lib/__pycache__/__init__.cpython-37.pyc b/scripts/lib/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..d384015b9d7f43dc6af0350931cc5f3839aa9e94 GIT binary patch literal 161 zcmZ?b<>g`kf>xdx2_X70h=2h`Aj1KOi&=m~3PUi1CZpdADmiLmRh9io1d3il3$dWm#!O}T$EV=lqrVNIhjfN@$s2J a`S^Ifg34PQHo5sJr8%i~AUi$-F#`Z&7%5W# literal 0 HcmV?d00001 diff --git a/scripts/lib/__pycache__/bcolor.cpython-37.pyc b/scripts/lib/__pycache__/bcolor.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..f5945a3f1fb3f35e07bc61492f97f7b729d33ee1 GIT binary patch literal 487 zcmZ9IOHaZ;5XZL<`T{Oq`2-xWsAyth3>XJZ+aX75dZ3NN>wqPPx{SbZyzf!MW zy%{f_+;)8=&SvKK-<`~4XIka5ftbCOo^L)FU$e`PNlY}ECwnY(5k*vZV*V%sqT(1; zC6~Sbn!r?(c~WP=Lm*HDA{9WQBFI#N0+m6b1yHF18dX838W>cEA~jgAVye#F$dBS= zSEx)+kAoZyjuVbUjw6nPU?*3m^*nOj2ynHOYJ`Ndz#}9z2niy(^)p=}i}(HNt(|1^L42JE?5rPCJ!bL#G4!1hZ17kL?!} zYi!m$eD=&m6nLh;nt9 'application/json'}) +request.body = { + "content" => "[#{ENV['MONIT_HOST']}] #{ENV['MONIT_SERVICE']} - #{ENV['MONIT_DESCRIPTION']}" +}.to_json +response = http.request(request) +puts response.body \ No newline at end of file