commit 5795cb8879a18ea132507f0e12de90aad26c0d99 from: mischa date: Tue May 14 16:20:00 2024 UTC add a CPU load tracker commit - 4e9862efc2926428861aaa121190794842255255 commit + 5795cb8879a18ea132507f0e12de90aad26c0d99 blob - /dev/null blob + 3add5a9214877be8877921c8fbdbf927252ee5ea (mode 755) --- /dev/null +++ cpu.sh @@ -0,0 +1,72 @@ +#!/bin/sh +# +# Copyright (c) 2024 Mischa Peters +# +# 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. +# +THRESHOLD=90 +TTL=10 +GRACE=6 +#ps -uU _vmd | awk -v var="${THRESHOLD}" '$12 !~ /\// && $3 > var {print $12,$3}' +# +HOSTNAME=$(hostname) +TIMESTAMP=$(date +%s) +SEEN=$(ls -1 /tmp/vm* | awk -F'/' '{print $3}') +OUT=$(ps -uU _vmd | awk -v var="${THRESHOLD}" '$12 !~ /\// && $3 > var {print $12}') +if [ -z "$OUT" ]; then + echo "No VM over the threshold overload" + rm -rf /tmp/vm* +fi + +for VM in ${OUT}; do + echo -n "${VM}" + 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}) + echo -n " - ${FIRSTSEEN}" + echo -n ", $((${TIMESTAMP} - ${FIRSTSEEN}))" + echo -n ", ${LASTSEEN}" + echo -n ", $((${TIMESTAMP} - ${LASTSEEN}))" + + if [[ $(( ${TIMESTAMP} - ${LASTSEEN} )) -gt $(( 60 * ${TTL} )) ]]; then + echo -n ", TTL" + echo ${FIRSTSEEN} > /tmp/${VM} + echo ${TIMESTAMP} >> /tmp/${VM} + fi + if [[ $(( ${TIMESTAMP} - ${FIRSTSEEN} )) -gt $(( 60 * 60 * ${GRACE} )) ]]; then + if [[ $(( ${TIMESTAMP} - ${LASTSEEN} )) -gt $(( 60 * ${TTL} )) ]]; then + echo -n ", GRACE DONE" + echo + echo "notify!" + /home/mischa/bin/notify.pl cpu ${VM} + echo "${VM} over ${THRESHOLD}% CPU load for ${GRACE} hours" | mail -s "${HOSTNAME} - ${VM} over ${THRESHOLD}% CPU load for ${GRACE} hours" -r mischa@high5.nl mischa@high5.nl + rm -rf /tmp/${VM} + fi + fi + echo + else + echo " - firstseen" + + echo ${TIMESTAMP} > /tmp/${VM} + echo ${TIMESTAMP} >> /tmp/${VM} + fi + fi +done + +for s in ${SEEN[@]}; do + if [[ "${OUT[*]}" != *"${s}"* ]]; then + echo "${s} - remove from tracking list" + rm -rf /tmp/${s} + fi +done