scriptbox/sysstat_webhook/stat_discord.sh

94 lines
1.8 KiB
Bash
Raw Permalink Normal View History

2023-10-31 13:13:37 +00:00
#!/bin/bash
while getopts 'w:crdnt:f:o:' flag
2023-10-31 13:13:37 +00:00
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
t) MESSAGE_TEXT=${OPTARG};; # Add some text to your webhook message!
2023-10-31 13:13:37 +00:00
f) datafile=${OPTARG};; # where your data belongs!
o) SYSSTAT_OPTIONS=${OPTARG};; # Sysstat options
2023-10-31 13:13:37 +00:00
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
2023-11-02 06:36:52 +00:00
/usr/bin/sadf -g -O skipempty,packed -- $1 ${EXPANSION[@]} > $POSTSVG
2023-10-31 13:13:37 +00:00
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": "'$MESSAGE_TEXT'"}' \
2023-10-31 13:13:37 +00:00
-F "file1=@$POSTFILE" \
$webhook_url
}
if [ ! -n "${webhook_url}" ]; then
echo "Fatal : no webhook_url (-w) suppiled"
exit 1
fi
if [ "$MESSAGE_TEXT" == "" ]; then
MESSAGE_TEXT=$DATETIME
fi
EXPANSION=()
if [ ! "$SYSSTAT_OPTIONS" == "" ]; then
2023-11-02 06:12:11 +00:00
EXPANSION+=("$SYSSTAT_OPTIONS")
fi
2023-10-31 13:13:37 +00:00
if [ -f "$datafile" ]; then
EXPANSION+=("$datafile")
2023-10-31 13:13:37 +00:00
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