rrd script for UPS

Post Reply
d2d
Posts: 5
Joined: Fri Dec 10, 2021 12:36 pm

Hello. I would very much like to add voltage / shutdown / charge graphs from UPS sensors to the statistics panel. In the panel, they are generated using RRD, this is the first time I encountered it :shock: . Perhaps someone has already implemented this or it is not difficult for him to adapt ready-made scripts from the Internet. :roll:
like this https://mn3m.info/posts/configure-ups-m ... on-debian/
or this http://linuxoid.in/RRD:_%D0%A1%D0%B1%D0 ... %D0%B7_NUT
Thanks.

BTW. I have used a lot of docker containers, and for each one panel has generated net graphs. I think there could be some filter for network intefaces and its stats.
d2d
Posts: 5
Joined: Fri Dec 10, 2021 12:36 pm

Made this for myself, but if anyone need it.
It will produce such a chart(s)
It will be overwritten during myVesta update!
Image

dir: /usr/local/vesta/bin
modify file:v-update-sys-rrd
add in "# Updating system stats" section

Code: Select all

    $BIN/v-update-sys-rrd-nut $period >/dev/null 2>&1
modify file:v-list-sys-rrd
add in appropriate place in json_list() function

Code: Select all

            if [ "$type" = 'nut' ]; then
                title="UPS '$rrd'"
            fi
Change section # Definng rrd charts

Code: Select all

rrd_types="nut la mem net"
create new file:v-update-sys-rrd-nut

Code: Select all


#!/bin/bash
# info: update NUT rrd
# options: PERIOD
#
# The function is for updating Network UPS Tools rrd database and graphic.


#----------------------------------------------------------#
#                    Variable&Function                     #
#----------------------------------------------------------#

# Argument definition
period=${1-daily}

# Includes
source $VESTA/func/main.sh
source $VESTA/conf/vesta.conf

#----------------------------------------------------------#
#                       Action                             #
#----------------------------------------------------------#

# Switching on time period
case $period in
    daily)   start='-1d'; end='now'; grid='MINUTE:30:HOUR:1:HOUR:4:0:%H:%M';;
    weekly)  start='-7d'; end='now'; grid='HOUR:8:DAY:1:DAY:1:0:%a %d';;
    monthly) start='-1m'; end='now'; grid='WEEK:1:WEEK:1:WEEK:1:0:%b %d';;
    yearly)  start='-1y'; end='now'; grid='MONTH:1:YEAR:1:MONTH:2:2419200:%b';;
    *) exit $E_RRD ;;
esac

# Checking directory
if [ ! -d "$RRD/nut" ]; then
    mkdir $RRD/nut
fi

# Parsing UPS list
ups_list=$(upsc -l 2>/dev/null)

for ups in $ups_list; do
    # Checking database
    if [ ! -e "$RRD/nut/$ups.rrd" ]; then
        # Adding database
        rrdtool create "$RRD/nut/$ups.rrd" --step $RRD_STEP \
            DS:inputv:GAUGE:600:U:U \
            DS:outputv:GAUGE:600:U:U \
            DS:charge:GAUGE:600:U:U \
            DS:load:GAUGE:600:U:U \
            RRA:AVERAGE:0.5:1:600 \
            RRA:AVERAGE:0.5:6:700 \
            RRA:AVERAGE:0.5:24:775 \
            RRA:AVERAGE:0.5:288:797 \
            RRA:MAX:0.5:1:600 \
            RRA:MAX:0.5:6:700 \
            RRA:MAX:0.5:24:775 \
            RRA:MAX:0.5:288:797
    fi

    # Parsing device stats
    if [ "$period" = 'daily' ]; then
        rrdtool update "$RRD/nut/$ups.rrd" $(/bin/upsc "$ups" | gawk '
        /battery.charge:/{charge=int($2)}
        /input.voltage:/{inputv=int($2)}
        /output.voltage:/{outputv=int($2)}
        /ups.load:/{load=int($2)}
        END {print "N:"inputv":"outputv":"charge":"load}
        ')
    fi

    # Updating rrd graph
    rrdtool graph $RRD/nut/$period-$ups.png \
        --imgformat PNG \
        --height="150" \
        --width="670" \
        --start "$start" \
        --end "$end" \
        --vertical-label "Voltage & percent" \
        --x-grid "$grid" \
        DEF:inputv="$RRD/nut/$ups.rrd":inputv:AVERAGE \
        DEF:outputv="$RRD/nut/$ups.rrd":outputv:AVERAGE \
        DEF:charge="$RRD/nut/$ups.rrd":charge:AVERAGE \
        DEF:load="$RRD/nut/$ups.rrd":load:AVERAGE \
        LINE1:inputv#0000FF:'Input V ' \
        GPRINT:inputv:'LAST: Current\:''%8.0lf'  \
        GPRINT:inputv:'MIN: Min\:''%8.0lf'  \
        GPRINT:inputv:'MAX: Max\:''%8.0lf\j'  \
        LINE2:outputv#00FF00:'Output V' \
        GPRINT:outputv:'LAST: Current\:''%8.0lf'  \
        GPRINT:outputv:'MIN: Min\:''%8.0lf'  \
        GPRINT:outputv:'MAX: Max\:''%8.0lf\j'\
        LINE3:charge#FF0000:'Charge %' \
        GPRINT:charge:'LAST: Current\:''%8.0lf'  \
        GPRINT:charge:'MIN: Min\:''%8.0lf'  \
        GPRINT:charge:'MAX: Max\:''%8.0lf\j'  \
        LINE4:load#FFFF00:'Load %  ' \
        GPRINT:load:'LAST: Current\:''%8.0lf'  \
        GPRINT:load:'MIN: Min\:''%8.0lf'  \
        GPRINT:load:'MAX: Max\:''%8.0lf\j'\
        -c "BACK#ffffff" \
        -c "SHADEA#ffffff" \
        -c "SHADEB#ffffff" \
        -c "FONT#555555" \
        -c "CANVAS#302c2d" \
        -c "GRID#666666" \
        -c "MGRID#AAAAAA" \
        -c "FRAME#302c2d" \
        -c "ARROW#FFFFFF" \
        COMMENT:'\r'; result=$?
    if [ "$result" -ne 0 ]; then
        exit $E_RRD
    fi
done


#----------------------------------------------------------#
#                       Vesta                              #
#----------------------------------------------------------#

exit
Last edited by d2d on Tue Jan 11, 2022 5:42 am, edited 3 times in total.
User avatar
myVesta
Site Admin
Posts: 949
Joined: Fri Jun 19, 2020 9:59 am
Has thanked: 9 times
Been thanked: 6 times

Thank you very much, I moved this topic to myVesta Knowledge base :)
Post Reply