commit fdcd9ed4428ed2997fed933d07424f47810f554f from: mischa date: Tue May 14 18:25:49 2024 UTC adding options and default values, debug option commit - bfcf5fd779b074f17f2b4653855d8906382a89c6 commit + fdcd9ed4428ed2997fed933d07424f47810f554f blob - 74c8320ea39101d5c08c2c0ed70938d128ed0a4b blob + 85360d291613d0dbea7f939e9222d5f81b7dccc8 --- cpu.sh +++ cpu.sh @@ -13,49 +13,65 @@ # 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 +# +LOAD=90 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};; + d) {DEBUG}=1;; + h) usage;; + *) usage;; + esac +done + HOSTNAME=$(hostname) TIMESTAMP=$(date +%s) -OUT=$(ps -uU _vmd | awk -v var="${THRESHOLD}" '$12 !~ /\// && $3 > var {print $12}') -if [ -z "$OUT" ]; then - echo "No VM over the threshold overload" +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; fi for VM in ${OUT}; do - echo -n "${VM}" + if [ -n "${DEBUG}" ]; then echo -n "${VM}"; fi 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 [ -z "${DEBUG}" ]; then + echo -n " - ${FIRSTSEEN}" + echo -n ", $((${TIMESTAMP} - ${FIRSTSEEN}))" + echo -n ", ${LASTSEEN}" + echo -n ", $((${TIMESTAMP} - ${LASTSEEN}))" + fi + 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 + echo "${VM} over ${LOAD}% CPU load for ${GRACE} hours" | mail -s "${HOSTNAME} - ${VM} over ${LOAD}% CPU load for ${GRACE} hours" -r mischa@high5.nl mischa@high5.nl rm -rf /tmp/${VM} fi fi echo else - echo " - firstseen" + if [ -n "${DEBUG} ]; then echo " - firstseen"; fi echo ${TIMESTAMP} > /tmp/${VM} echo ${TIMESTAMP} >> /tmp/${VM} @@ -66,7 +82,7 @@ done SEEN=$(ls -1 /tmp/vm* | awk -F'/' '{print $3}') for s in ${SEEN[@]}; do if [[ "${OUT[*]}" != *"${s}"* ]]; then - echo "${s} - remove from tracking list" + if [ -n "${DEBUG}" ]; then echo "${s} - remove from tracking list"; fi rm -rf /tmp/${s} fi done