📜 ⬆️ ⬇️

Monitoring server resources running * nix with RRDtool



Good day, dear% user%! Today I will tell how to raise monitoring of iron and system in real time using a set of utilities - RRDtool. Our goal is to collect and graphically display real-time data on the system operation: CPU load, memory status, network interface load, as well as CPU temperature over various periods of time.

A bit of theory. RRDtool is a set of utilities for working with ring databases. Such databases are specifically designed for storing time-varying data sequences (network traffic, CPU load) - just what we need. In addition, we will use the collectd daemon - this is a small daemon that collects system resource statistics over time periods - i.e. and creates the necessary ring databases on the basis of which with the help of RRDtool we can build graphs. Before installation, let us assume that you have already installed and configured a web server, you will need it to view graphs in the browser. We proceed to the installation and configuration.
')
All manipulations were performed on a freshly installed Debian 7 wheezy:

root@kd-ast:/etc/collectd# uname -a
Linux kd-ast 3.2.0-4-686-pae #1 SMP Debian 3.2.46-1 i686 GNU/Linux


To draw graphs, you need to install RRDtool:

apt-get install rrdtool

To collect statistics on the use of system resources
instead of self-written scripts, use the “Collectd” daemon:

apt-get install collectd

Run:

cd /usr/sbin/
./collectd onestart


After downloading, we will see in /var/lib/collectd/router.local, where “router.local” is the name of your server.
Now you can go to the script itself, which will generate graphics:

 #!/bin/sh ### CPU /usr/bin/rrdtool graph /var/www/monitor/cpu0.png \ #  RRDtoll      -e now \ -s 'end - 6 hours' \ -S 60 \ --title "CPU USAGE: AMD Athlon(tm) II X3 455 Processor" \ #CPU --vertical-label "Percents" \ --imgformat PNG \ --slope-mode \ --lower-limit 0 \ --upper-limit 100 \ --rigid \ -E \ -i \ --color SHADEA#FFFFFF \ --color SHADEB#FFFFFF \ --color BACK#CCCCCC \ -w 600 \ -h 150 \ --interlaced \ --font DEFAULT:8:/usr/share/fonts/truetype/ttf-dejavu/DejaVuSansMono.ttf \ DEF:a=/var/lib/collectd/rrd/kd-ast/cpu-0/cpu-idle.rrd:value:MAX \ #    DEF:b=/var/lib/collectd/rrd/kd-ast/cpu-0/cpu-system.rrd:value:MAX \ DEF:c=/var/lib/collectd/rrd/kd-ast/cpu-0/cpu-user.rrd:value:MAX \ LINE2:b#2cc320: \ AREA:b#54eb48:System \ LINE2:c#e7ad4a: \ AREA:c#ebd648:User #LINE1:a#CCCCCC:Idle \ ###eth0 /usr/bin/rrdtool graph /var/www/monitor/network0.png \ -e now \ -s 'end - 6 hours' \ -S 60 \ --title 'Traffic on ext_if: eth0 (local) (100Mb/s)' \ ##   --vertical-label 'Mbyte\s' \ --imgformat PNG \ --slope-mode \ --lower-limit 0 \ --upper-limit 20000000 \ #   - max --rigid \ -E \ -i \ --color SHADEA#FFFFFF \ --color SHADEB#FFFFFF \ --color BACK#CCCCCC \ -w 600 \ -h 150 \ --interlaced \ --font DEFAULT:8:/usr/share/fonts/truetype/ttf-dejavu/DejaVuSansMono.ttf \ DEF:a=/var/lib/collectd/rrd/kd-ast/interface-eth0/if_octets.rrd:tx:MAX \ DEF:b=/var/lib/collectd/rrd/kd-ast/interface-eth0/if_octets.rrd:rx:MAX \ DEF:c=/var/lib/collectd/rrd/kd-ast/interface-eth0/if_errors.rrd:tx:MAX \ AREA:a#4169E1:Tx \ LINE2:b#2cc320: \ AREA:b#54eb48:Rx \ LINE1:c#FF0000:Errors ### RAM /usr/bin/rrdtool graph /var/www/monitor/memory.png \ -e now \ -s 'end - 6 hours' \ -S 60 \ --title 'MEMORY USAGE: 2Gb' \ # RAM --vertical-label 'Mbyte' \ --imgformat PNG \ --slope-mode \ --lower-limit 0 \ --upper-limit 2000000000 \ #   --rigid \ -E \ -i \ --color SHADEA#FFFFFF \ --color SHADEB#FFFFFF \ --color BACK#CCCCCC \ -w 600 \ -h 150 \ --interlaced \ --font DEFAULT:8:/usr/share/fonts/truetype/ttf-dejavu/DejaVuSansMono.ttf \ DEF:a=/var/lib/collectd/rrd/kd-ast/memory/memory-buffered.rrd:value:MAX \ #     DEF:b=/var/lib/collectd/rrd/kd-ast/memory/memory-cached.rrd:value:MAX \ DEF:c=/var/lib/collectd/rrd/kd-ast/memory/memory-free.rrd:value:MAX \ DEF:d=/var/lib/collectd/rrd/kd-ast/memory/memory-used.rrd:value:MAX \ LINE1:a#6959CD:buffered \ AREA:b#00FF00:cache \ LINE2:c#006400: \ AREA:c#00CD66:free \ AREA:d#FF1493:used \ 


Add it to cron, to run every minute:

nano /etc/crontab

*/1 * * * * root /home/sysbes/Scripts/RRDtool/mon.sh > /dev/null 2>&1

Create index.php with the following contents:

 <html> <head> <meta charset='koi8-r'> <title>NOC</title> </head> <body bgcolor='#8FBC8F'> <br> <center> <h2><font face='Arial'>  : kd-ast</font></h2> <br><br> <center> <img src='cpu0.png'> </center><br> <center> </h2> <img src='network0.png'> </center><br> <center> </h2> <img src='memory.png'> </center><br> </body> </html> 


We can watch the resulting graphics by opening in the browser: _/monitor

To determine the temperature, we will use lm-sensors - a set of drivers and a utility that allows you to monitor the temperature, voltage, fan speed in your system. It should be remembered that the set of sensors is individual for your system, so some features may not be available. You can also use mbmon.

apt-get install lm-sensors

To identify sensors:

sensors-detect

The script will ask you some questions - do not be intimidated.

We try to determine:

sensors

If the temperature is incorrect or not determined, you may have to load the core modules, it helped me:

modprobe it87

and again sensors

If the temperature is removed, we add the it87 module to the autoload correctly:

nano /etc/modules

it87


sensors

Next, as you know, you need to write a script that will take the values,
draw through rrdtool, and for the day, week, month, year ... Then we add it
in cron and he will perform his manipulations.

The script was as follows:

 #!/bin/bash RRDTOOL=/usr/bin/rrdtool #  RRDtool DATABASE=/var/lib/collectd/rrd/kd-ast/space-temperature.rrd #     PERIOD=60 SENSOR=` /usr/bin/sensors | grep temp3 | awk '{ print $2-0}'` #    IMAGE_PATH=/var/www/monitor #    TITLE_TEXT='CPU TEMP' UNDER_TEXT='TEMP' TEMP_MIN=20 TEMP_MAX=90 if ! [ -f $DATABASE ] then $RRDTOOL create $DATABASE -s $PERIOD DS:temperature:GAUGE:600:10:80 \ RRA:AVERAGE:0.5:1:576 \ RRA:AVERAGE:0.5:6:672 \ RRA:AVERAGE:0.5:24:732 \ RRA:AVERAGE:0.5:144:1460 fi $RRDTOOL update $DATABASE N:$SENSOR function DRAW_GRAPHIC { NOW_HOUR=`date +%H` NOW_MIN=`date +%M` NOW_SEC=`date +%S` case $2 in day) TIME_TEXT="Last 24 hours" ;; week) TIME_TEXT="Last week" ;; month) TIME_TEXT="Last mounth" ;; year) TIME_TEXT="Last year" ;; esac $RRDTOOL graph $IMAGE_PATH/$1 \ -s -1$2 \ -e now \ -a PNG \ -v 'C' \ -t "$TITLE_TEXT [$HOSTNAME] - $TIME_TEXT" \ -l $TEMP_MIN \ -u $TEMP_MAX \ -r \ -E \ -i \ -R light \ --zoom 1.0 \ -w 600 \ -h 150 \ DEF:temperature=$DATABASE:temperature:AVERAGE \ LINE2:temperature#33cc33:"$UNDER_TEXT" \ GPRINT:temperature:MIN:'MIN\:%2.lf' \ GPRINT:temperature:MAX:'MAX\:%2.lf' \ GPRINT:temperature:AVERAGE:'AVG\:%4.1lf' \ GPRINT:temperature:LAST:'NOW\:%2.lf \n' \ COMMENT:"TIME \: $NOW_HOUR\:$NOW_MIN\:$NOW_SEC \n" } DRAW_GRAPHIC 'temp_d.png' 'day' DRAW_GRAPHIC 'temp_w.png' 'week' DRAW_GRAPHIC 'temp_m.png' 'month' DRAW_GRAPHIC 'temp_y.png' 'year' 


Add the script to crontab, to run every minute:

*/1 * * * * root /home/sysbes/temp.sh > /dev/null 2>&1

An approximate view of the statistics table is:

 <html> <head> <meta charset='koi8-r'> <title> RRD   :  -</title> </head> <body bgcolor='#ccddcc'> <h2> <font face='Arial'>   @  - </font> </h2> <img src='png/temp_d.png'> <br> <img src='png/temp_w.png'> <br> <img src='png/temp_m.png'> <br> <img src='png/temp_y.png'> </body> </html> 


As a result, we obtain:

 <html> <head> <meta charset='koi8-r'> <title>kd-ast</title> </head> <body bgcolor='#8FBC8F'> <br> <center> <h2><font face='Arial'>Real Time Load kd-ast:</font></h2> <center> <img src='cpu0.png'> </center><br> <center> </h2> <img src='network0.png'> </center><br> <center> </h2> <img src='memory.png'> </center><br> <center> <h2><font face='Arial'>CPU TEMP kd-ast:</font></h2> <img src='temp_d.png'> <br> <img src='temp_w.png'> <br> <img src='temp_m.png'> <br> <img src='temp_y.png'> </body> </html> 


On request _/monitor _/monitor can view statistics in the browser.

Source: https://habr.com/ru/post/186942/


All Articles