Categories: MiscTools

Gnuplot vmstat

Gnuplot vmstat

After you installed following packages:

apt-get install gnuplot ttf-bitstream-vera sharutils

you can run collect-data script:

DIR='/var/tmp'
PREFIX='vmstat'
HOSTNAME=`hostname`
SLEEPTIME='60'
vmstat -n -S M $SLEEPTIME | while read line; do LOGFILE=" ${DIR}/${PREFIX}_${HOSTNAME}_`date '+%Y-%m-%d'`.log"; echo "`date '+%T ' `$line" >> $LOGFILE; done &

Crontabize the plotting script:

#!/bin/bash

DIR='/var/tmp'
PREFIX='vmstat'
HOSTNAME=`hostname`
MAX_MEMORY=`free -m | awk 'NR==2 {print $2}' | head`
SCALE=`echo "scale=0;$MAX_MEMORY/10" | bc`

if [ -n "$1" ]
    then
        DATE=`date '+%Y-%m-%d' -r $1`
        DATA="$1"
    else
        DATE=`date --date="-1 day" '+%Y-%m-%d'`
        DATA="${DIR}/${PREFIX}_${HOSTNAME}_${DATE}.log"
fi
DATA_PLOT=`dirname $DATA`/`basename $DATA .log`.png

gnuplot << EOF
set terminal png size 1920,1200 font '/usr/share/fonts/truetype/ttf-bitstream-vera/Vera.ttf' 9
set lmargin 10
set tmargin 3
set rmargin 5
set bmargin 4
set border 3
set grid
set output "${DATA_PLOT}"
set xdata time
set timefmt "%H:%M:%S"
set format x "%H:%M:%S"
set xlabel "Time"
set ylabel "Value"
set multiplot layout 2, 2 title "${HOSTNAME} - ${DATE}"
#
set size 0.5, 0.5;
set origin 0, 0;
set title "Memory"
set yrange [ 0 : $MAX_MEMORY ]
set ytics 0,$SCALE
plot "${DATA}" using 1:4 ti "swap" w l, "${DATA}" using 1:5 ti "free" w l, "${DATA}" using 1:6 ti "buff" w l, "${DATA}" using 1:7 ti "cache" w l
#
set size 0.5, 0.5;
set origin 0.5, 0;
set title "IO"
set autoscale xy
set yrange [ 0 : 4000 ]
set ytics 0,1000
set mytics 2
plot "${DATA}" using 1:10 ti "in" w l, "${DATA}" using 1:11 ti "out" w l
#
set size 0.5, 0.5;
set origin 0, 0.5;
set title "System"
set yrange [ 0 : 5000 ]
set ytics 0,1000
set mytics 2
plot "${DATA}" using 1:12 ti "in" w l, "${DATA}" using 1:13 ti "cs" w l
#
set size 0.5, 0.5;
set origin 0.5, 0.5;
set title "Cpu"
set yrange [ 0 : 100 ]
set ytics 0,10
set mytics 2
plot "${DATA}" using 1:14 ti "user" w l, "${DATA}" using 1:15 ti "system" w l, "${DATA}" using 1:16 ti "idle" w l, "${DATA}" using 1:17 ti "wait" w l
unset multiplot
EOF

rm -f $TEMPFILE

exit 0

configure your report program and run it:

#!/bin/bash

TEMPFILE=`mktemp -p /tmp/`
trap "rm -f ${TEMPFILE}; exit 1" INT

HOSTNAME=`hostname`
DATE=`date --date="-1 day" '+%Y-%m-%d'`

DIR='/var/tmp'
PREFIX='vmstat'
DATA="${DIR}/${PREFIX}_${HOSTNAME}_${DATE}.log"
DATA_PLOT=${DIR}/`basename $DATA .log`.png
IP=`/sbin/ifconfig | grep -C 1 eth|grep 'inet '|awk {'print $2'}|cut -d":" -f2`

MAILING_LIST='santi@*****.**'
LOG='1'
MAIL='1'
MOVE='1'
RETENTION='+30'
DEST_DIR='/var/www/blabla/doc_root/vmstat/'

##########
RED='\033[31;1m'
GREEN='\033[32;1m'
BOLD='\033[1m'
DEFAULT='\033[0m'
##########

if [ "$MAIL" -eq '1' ]
        then
        SUBJECT="Report vmstat $HOSTNAME"
        MESSAGE="$DATE\n\nMacchina: $HOSTNAME - $IP\n"
        echo "$MESSAGE" >> "$TEMPFILE"
        echo "Email inviata a: ${BOLD}${MAILING_LIST}${DEFAULT}"
        if [ "$LOG" -eq '1' ]
                then
                    ( cat "$TEMPFILE"; uuencode "${DATA}" "${DATA}"; uuencode "${DATA_PLOT}" "${DATA_PLOT}"; ) | mailx -s "$SUBJECT" "$MAILING_LIST"
                else
                    ( cat "$TEMPFILE"; uuencode "${DATA_PLOT}" "${DATA_PLOT}"; ) | mailx -s "$SUBJECT" "$MAILING_LIST"
        fi
fi

if [ "${MOVE}" -eq '1' ]
        then
            mv ${DATA_PLOT} ${DEST_DIR}
fi

[ "$LOG" -eq '1' ] && echo "Logging is ${GREEN}ON${DEFAULT}" || echo "Logging is ${RED}OFF${DEFAULT}"
[ "$MAIL" -eq '1' ] && echo "Mailing is ${GREEN}ON${DEFAULT}" || echo "Mailing is ${RED}OFF${DEFAULT}"
[ "$MOVE" -eq '1' ] && echo "Moving log is ${GREEN}ON${DEFAULT}" || echo "Moving log is ${RED}OFF${DEFAULT}"

find $DEST_DIR -mtime $RETENTION -iname '*.png' -exec rm -f {} \;

rm -f $TEMPFILE

exit 0

Actually, those scripts are running on this machine, so you can see results below:

Santi Strati

Share
Published by
Santi Strati

Recent Posts

OPENWRT e DD-WRT – Alternativa open e free a Cisco IOS

OPENWRT e DD-WRT - Alternativa open e free a Cisco IOS Scarica il file PDF

4 anni ago

EasyList Italy + EasyList AdBlock

EasyList Italy+EasyList Add these following lines to adblock.sources.gz (located on /etc/adblock/): [code] "reg_it": { "url":…

4 anni ago

WGET auto resume script for dropping connection

[code light="true" language="bash"] LINK='https://software-download.microsoft.com/db/Win10_2004_Italian_x64.iso' SLEEPTIME='120' while true do if wget -c ${LINK}; then echo 'Download…

5 anni ago

Voting Machine Smartmatic A4-210…

... come riconvertirlo ad uso didattico, ludico, ufficio o punto chiosco. Come molti di voi…

5 anni ago

Retropie es_input.cfg Logitech Wireless Gamepad F710 and Keyboard

[code light="true" language="xml"] <?xml version="1.0"?> <inputList> <inputConfig type="joystick" deviceName="Logitech Gamepad F710" deviceGUID="030000006d0400001fc2000005030000"> <input name="a" type="button"…

5 anni ago