new: [send] add cron hourly for disk_usage and load_average_max
#5
Closed
bgallet
wants to merge 2 commits from bgallet:send_ntfy
into send_ntfy
pull from: bgallet:send_ntfy
merge into: Myceliandre:send_ntfy
Myceliandre:master
Myceliandre:rc1
Myceliandre:send_ntfy
6 changed files with 215 additions and 1 deletions
-
48bin/myc-install
-
56bin/myc-update
-
45bin/send
-
3etc/cron.d/alerting
-
38etc/cron.hourly/disk_usage
-
26etc/cron.hourly/load_average_max
@ -0,0 +1,45 @@ |
|||
#!/bin/bash |
|||
|
|||
## Send a notification with NTFY and check if the config file is complete |
|||
|
|||
if [[ "$UID" == "0" ]]; then |
|||
NTFY_CONFIG_FILE="/etc/ntfy/ntfy.conf" |
|||
else |
|||
NTFY_CONFIG_FILE="$HOME/.config/ntfy/ntfy.conf" |
|||
fi |
|||
|
|||
SERVER="https://ntfy.0k.io/" |
|||
|
|||
if ! [ -e "$NTFY_CONFIG_FILE" ]; then |
|||
mkdir -p "${NTFY_CONFIG_FILE%/*}" |
|||
## default option to change if needed |
|||
echo "SERVER=$SERVER" > "$NTFY_CONFIG_FILE" |
|||
## else if $NTFY_CONFIG_FILE exist but SERVER is not defined |
|||
elif ! grep -q "^SERVER=" "$NTFY_CONFIG_FILE"; then |
|||
echo "SERVER=$SERVER" >> "$NTFY_CONFIG_FILE" |
|||
fi |
|||
|
|||
source "$NTFY_CONFIG_FILE" |
|||
|
|||
for var in SERVER LOGIN PASSWORD; do |
|||
if ! [ -v "$var" ]; then |
|||
echo "Error: missing $var in $NTFY_CONFIG_FILE" |
|||
exit 1 |
|||
fi |
|||
done |
|||
|
|||
|
|||
exname=${0##*/} |
|||
usage="Usage: $exname CHANNEL MESSAGE" |
|||
|
|||
if [ "$#" -ne 2 ]; then |
|||
echo "$usage" >&2 |
|||
exit 1 |
|||
fi |
|||
|
|||
channel="$1" |
|||
message="$2" |
|||
|
|||
curl -s -u $LOGIN:$PASSWORD \ |
|||
-d "$message" "$SERVER/$channel" > /dev/null |
|||
|
@ -0,0 +1,3 @@ |
|||
SHELL=/bin/bash |
|||
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin |
|||
|
@ -0,0 +1,38 @@ |
|||
SHELL=/bin/bash |
|||
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin |
|||
|
|||
## check disk usage and send a notification if it's above 75% or 90% |
|||
|
|||
percent_usage=$(df /srv -h) |
|||
percent_usage=${percent_usage##*$'\n'} |
|||
percent_usage=${percent_usage% *} |
|||
percent_usage=${percent_usage##* } |
|||
percent_usage=${percent_usage%\%} |
|||
|
|||
LOCK_WORKING_DIR="/var/run/myc-manage" |
|||
mkdir -p "$LOCK_WORKING_DIR" |
|||
|
|||
if [ "$percent_usage" -ge "90" ]; then |
|||
if [ -e $LOCK_WORKING_DIR/disk_usage_90.lock ]; then |
|||
exit 0 |
|||
else |
|||
touch $LOCK_WORKING_DIR/disk_usage_90.lock |
|||
message="$(hostname): WARNING disk usage >=90%" |
|||
send "disk_usage" "$message" |
|||
fi |
|||
elif [ "$percent_usage" -ge "75" ]; then |
|||
if [ -e $LOCK_WORKING_DIR/disk_usage_75.lock ]; then |
|||
exit 0 |
|||
else |
|||
touch $LOCK_WORKING_DIR/disk_usage_75.lock |
|||
message="$(hostname): WARNING disk usage >=75 <90%" |
|||
send "disk_usage" "$message" |
|||
fi |
|||
else |
|||
if [ -e $LOCK_WORKING_DIR/disk_usage_75.lock ]; then |
|||
rm $LOCK_WORKING_DIR/disk_usage_75.lock |
|||
fi |
|||
if [ -e $LOCK_WORKING_DIR/disk_usage_90.lock ]; then |
|||
rm $LOCK_WORKING_DIR/disk_usage_90.lock |
|||
fi |
|||
fi |
@ -0,0 +1,26 @@ |
|||
SHELL=/bin/bash |
|||
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin |
|||
|
|||
MAX_PER_PROC=3 |
|||
|
|||
## integer only for the 5m load avg |
|||
|
|||
LOCK_WORKING_DIR="/var/run/myc-manage" |
|||
mkdir -p "$LOCK_WORKING_DIR" |
|||
|
|||
int_avg=$(while read line; do |
|||
echo "$line" |
|||
done < /proc/loadavg) |
|||
int_avg=${int_avg#* } |
|||
int_avg=${int_avg%%.*} |
|||
max=$[$MAX_PER_PROC * $(grep -c ^processor /proc/cpuinfo)] |
|||
if [ "$int_avg" -gt "$max" ]; then |
|||
if [ -e $LOCK_WORKING_DIR/load_average_max.lock ]; then |
|||
exit 0 |
|||
else |
|||
touch $LOCK_WORKING_DIR/load_average_max.lock |
|||
message="$(hostname) : WARNING - load average ($int_avg) is above max per processor : ($MAX_PER_PROC * $(grep -c ^processor /proc/cpuinfo) = $max)" |
|||
echo $message | logger -t load_average_max |
|||
send "load_average_max" "$message" |
|||
fi |
|||
fi |
Write
Preview
Loading…
Cancel
Save
Reference in new issue