From 1de2ccafd93bcba2600426b373b2eee1352831a2 Mon Sep 17 00:00:00 2001 From: minoplhy Date: Tue, 31 Oct 2023 20:13:37 +0700 Subject: [PATCH] sysstat_webhook : init --- sysstat_webhook/README.md | 6 +++ sysstat_webhook/stat_discord.sh | 83 +++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 sysstat_webhook/README.md create mode 100644 sysstat_webhook/stat_discord.sh diff --git a/sysstat_webhook/README.md b/sysstat_webhook/README.md new file mode 100644 index 0000000..73938f0 --- /dev/null +++ b/sysstat_webhook/README.md @@ -0,0 +1,6 @@ +# Sysstat_Webhook + +currrently, WIP. But yeah, Discord sucks! They don't rendering SVG. +``` +sudo apt install -y librsvg2-bin +``` \ No newline at end of file diff --git a/sysstat_webhook/stat_discord.sh b/sysstat_webhook/stat_discord.sh new file mode 100644 index 0000000..eb19986 --- /dev/null +++ b/sysstat_webhook/stat_discord.sh @@ -0,0 +1,83 @@ +#!/bin/bash + +while getopts 'w:crdnf:' flag +do + case "${flag}" in + w) webhook_url=${OPTARG};; # Discord Webhook URL + c) CPU="True";; # CPU + r) RAM="True";; # RAM + d) DISK_IO="True";; # DISK I/O + n) NETWORK="True";; # Network + f) datafile=${OPTARG};; # where your data belongs! + esac +done + +MAKE_DIR=$(mktemp -d) +DATETIME=$(date +"%Y-%m-%d") + +function cpu { + graph_gen "-u" "cpu" +} + +function ram { + graph_gen "-r" "ram" +} + +function diskIO { + graph_gen "-b" "diskio" +} + +function network { + graph_gen "-n DEV" "network" +} + +function graph_gen { + POSTSVG=$MAKE_DIR/sysstat_"$2"_data.svg + POSTPNG=$MAKE_DIR/sysstat_"$2"_data.png + + /usr/bin/sadf -g $EXPANSION -O oneday,skipempty -- $1 > $POSTSVG + svg_to_png $POSTSVG $POSTPNG + Process $POSTPNG +} + +function Process { + POSTFILE=$1 + Discord_hooks +} + +function svg_to_png { + /usr/bin/rsvg-convert $1 -o $2 +} +function Discord_hooks { + curl \ + -F 'payload_json={"content": "'$DATETIME'"}' \ + -F "file1=@$POSTFILE" \ + $webhook_url +} + +if [ ! -n "${webhook_url}" ]; then + echo "Fatal : no webhook_url (-w) suppiled" + exit 1 +fi + +if [ -f "$datafile" ]; then + EXPANSION="-f $datafile" +fi + +if [ "$CPU" == "True" ]; then + cpu +fi + +if [ "$RAM" == "True" ]; then + ram +fi + +if [ "$DISK_IO" == "True" ]; then + diskIO +fi + +if [ "$NETWORK" == "True" ]; then + network +fi + +rm -rf $MAKE_DIR \ No newline at end of file