rrdtool/cpu_load.sh

61 lines
2.1 KiB
Bash
Executable File

#!/bin/sh
#
# Copyright (c) 2019-2021 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.
#
test -n "$1" || exit 1
HOST="$1"
COMMUNITY="public"
RRDFILES="/home/mischa/rrd"
IMAGES="/var/www/rrdtool/obsda.ms"
WATERMARK="OpenBSD Amsterdam - https://obsda.ms"
RRDTOOL="/usr/local/bin/rrdtool"
CPUINFO="/tmp/${HOST}-cpu.txt"
UPTIME=$(cat /tmp/${HOST}-uptime.txt)
VERSION=$(cat /tmp/${HOST}-version.txt)
NOW=$(date "+%Y-%m-%d %H:%M:%S %Z" | sed 's/:/\\:/g')
if ! test -f "${RRDFILES}/${HOST}-cpu.rrd"
then
echo "Creating ${RRDFILES}/${HOST}-cpu.rrd"
${RRDTOOL} create ${RRDFILES}/${HOST}-cpu.rrd \
--step 300 \
DS:ds0:GAUGE:600:U:U \
RRA:MAX:0.5:1:20000
fi
snmp walk -c ${COMMUNITY} ${HOST} hrProcessorLoad | awk '{print $4}' > ${CPUINFO}
CORES=$(grep -cv "^0$" ${CPUINFO})
CPU_LOAD_SUM=$(awk '{sum += $1} END {print sum}' ${CPUINFO})
CPU_LOAD=$(echo "scale=2; ${CPU_LOAD_SUM}/${CORES}" | bc -l)
${RRDTOOL} update ${RRDFILES}/${HOST}-cpu.rrd N:${CPU_LOAD}
${RRDTOOL} graph ${IMAGES}/${HOST}-cpu.png \
--start -43200 \
--title "${HOST} - ${VERSION} - CPU" \
--vertical-label "% CPU Used" \
--watermark "${WATERMARK}" \
--color BACK#ffffff \
--border 0 \
DEF:CPU=${RRDFILES}/${HOST}-cpu.rrd:ds0:AVERAGE \
AREA:CPU#FFCC00 \
LINE2:CPU#CC0033:"CPU" \
GPRINT:CPU:MAX:"Max\:%2.2lf %s" \
GPRINT:CPU:AVERAGE:"Average\:%2.2lf %s" \
GPRINT:CPU:LAST:" Current\:%2.2lf %s\n" \
COMMENT:"\\n" \
COMMENT:" SUM CPU Load / Active Cores = % CPU Used\n" \
COMMENT:" Up for ${UPTIME} at ${NOW}"