deploy.pl/cpu.sh

88 lines
2.6 KiB
Bash
Raw Permalink Normal View History

2024-05-14 18:20:00 +02:00
#!/bin/sh
#
# Copyright (c) 2024 Mischa Peters <mischa @ openbsd.amsterdam>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
LOAD=90
2024-05-14 18:20:00 +02:00
TTL=10
GRACE=6
usage() {
echo "usage: ${0##*/} [-l load (90)] [-t ttl (10)] [-g grace (6)] [-d]" 1>&2
exit
}
while getopts l:t:g:dh arg; do
case ${arg} in
l) LOAD=${OPTARG};;
t) TTL=${OPTARG};;
g) GRACE=${OPTARG};;
2024-05-14 20:26:42 +02:00
d) DEBUG=1;;
h) usage;;
*) usage;;
esac
done
2024-05-14 18:20:00 +02:00
HOSTNAME=$(hostname)
TIMESTAMP=$(date +%s)
OUT=$(ps -uU _vmd | awk -v var="${LOAD}" '$12 !~ /\// && $3 > var {print $12}')
if [ -z "${OUT}" ]; then
if [ -n "${DEBUG}" ]; then echo "No VM over the threshold overload"; fi
rm -rf /tmp/vm*
exit 0;
2024-05-14 18:20:00 +02:00
fi
for VM in ${OUT}; do
if [ -n "${DEBUG}" ]; then echo -n "${VM}"; fi
2024-05-14 18:20:00 +02:00
if [ ! -f /home/mischa/vms/${VM}.exclude ]; then
if [ -f /tmp/${VM} ]; then
FIRSTSEEN=$(awk 'NR==1' /tmp/${VM})
LASTSEEN=$(awk 'NR==2' /tmp/${VM})
2024-05-14 20:37:48 +02:00
if [ -n "${DEBUG}" ]; then
echo -n " - ${FIRSTSEEN}"
echo -n ", $((${TIMESTAMP} - ${FIRSTSEEN}))"
echo -n ", ${LASTSEEN}"
2024-05-14 20:39:11 +02:00
echo ", $((${TIMESTAMP} - ${LASTSEEN}))"
fi
2024-05-14 18:20:00 +02:00
if [[ $(( ${TIMESTAMP} - ${LASTSEEN} )) -gt $(( 60 * ${TTL} )) ]]; then
echo ${FIRSTSEEN} > /tmp/${VM}
echo ${TIMESTAMP} >> /tmp/${VM}
fi
if [[ $(( ${TIMESTAMP} - ${FIRSTSEEN} )) -gt $(( 60 * 60 * ${GRACE} )) ]]; then
if [[ $(( ${TIMESTAMP} - ${LASTSEEN} )) -gt $(( 60 * ${TTL} )) ]]; then
/home/mischa/bin/notify.pl cpu ${VM}
2024-05-15 20:53:09 +02:00
echo "${VM} over ${LOAD}% CPU load for ${GRACE} hours" | mail -s "${HOSTNAME} - ${VM} over ${LOAD}% CPU load for ${GRACE} hours" -r root@obsda.ms cpu@obsda.ms
2024-05-14 18:20:00 +02:00
rm -rf /tmp/${VM}
fi
fi
else
2024-05-14 20:31:59 +02:00
if [ -n "${DEBUG}" ]; then echo " - firstseen"; fi
2024-05-14 18:20:00 +02:00
echo ${TIMESTAMP} > /tmp/${VM}
echo ${TIMESTAMP} >> /tmp/${VM}
fi
fi
done
if ls /tmp/vm* 1> /dev/null 2>&1; then
SEEN=$(ls -1 /tmp/vm* | awk -F'/' '{print $3}')
for s in ${SEEN[@]}; do
if [[ "${OUT[*]}" != *"${s}"* ]]; then
if [ -n "${DEBUG}" ]; then echo "${s} - remove from tracking list"; fi
rm -rf /tmp/${s}
fi
done
fi