MRTG and custom graphs

tsiou

Verified User
Joined
Sep 15, 2006
Messages
213
Location
Larisa, Greece
Hi, i wanted to monitor some extra things for my server and thanks to many people i managed to watch my server's load, mem etc in mrtg.
Some scripts are as is and some created or modified.
If /usr/local/etc/mrtg doesn't exist or you keep mrtg files somewhere else replace with your folder.

I'll post my conf for now which includes df, load, mem and swap info.
Anyone willing to write more can post it to share it with us..

first edit /etc/inetd.conf and add:

load stream tcp nowait nobody /usr/local/etc/mrtg/scripts/loadaverage.sh loadaverage.sh
df stream tcp nowait nobody /usr/local/etc/mrtg/scripts/df.sh df.sh
swap stream tcp nowait nobody /usr/local/etc/mrtg/scripts/swapinfo.sh swapinfo.sh
mem stream tcp nowait nobody /usr/local/etc/mrtg/scripts/meminfo.sh meminfo.sh

then edit /etc/services and add:

load 9054/tcp #MRTG CUSTOM
df 9055/tcp #MRTG CUSTOM
swap 9056/tcp #MRTG CUSTOM
mem 9057/tcp #MRTG CUSTOM

then
cd /usr/local/etc/mrtg
mkdir scripts

create df.sh and paste (change /usr/local/bin/bash to your system's bash):

----------------------------------------------------
#!/usr/local/bin/bash

a=`df |grep var |grep -v named|awk '{print $3}'`
b=`df |grep home |awk '{print $3}'`

echo $a
echo $b
uptime | sed 's:^.* up \(.*\), [0-9][0-9]* users.*$:\1:'
uname -n
-----------------------------------------------------

edit loadaverage.sh and paste

-----------------------------------------------
#!/bin/sh

# first the load 5 and 15 min avg
# multiply * 100 to avoid floats
# it helps if mrtg "period" is a multiple of 5 mins
uptime | sed -e 's/^.*average.*: \(.*\)$/\1/' -e 's/ //g' |
awk -F, '{ printf("%.0f\n",$2*100); printf("%.0f\n",$3*100) }'
# the uptime
uptime | sed 's:^.* up \(.*\), [0-9][0-9]* users.*$:\1:'
# my name
uname -n
-----------------------------------------------------

edit meminfo.sh and paste

----------------------------------
#!/usr/local/bin/bash
a=`top |grep Mem|awk '{print $2}'`
a=$(echo $a | rev);
a=$(echo ${a:1} | rev);
a=`expr $a \* 1000`
b=`top |grep Mem|awk '{print $4}'`
b=$(echo $b | rev);
b=$(echo ${b:1} | rev);
b=`expr $b \* 1000`

echo "$a";
echo "$b";
uptime | sed 's:^.* up \(.*\), [0-9][0-9]* users.*$:\1:'
uname -n
--------------------------------------------

edit swapinfo.sh and paste

-----------------------------------------
#!/usr/local/bin/bash
a=`top |grep Swap|awk '{print $4}'`
a=$(echo $a | rev);
a=$(echo ${a:1} | rev);
a=`expr $a \* 1000`
b=`top |grep Swap|awk '{print $6}'`
b=$(echo $b | rev);
b=$(echo ${b:1} | rev);
b=`expr $b \* 1000`

echo $a;
echo $b;
uptime | sed 's:^.* up \(.*\), [0-9][0-9]* users.*$:\1:'
uname -n
--------------------------------------------

now do a
chmod 755 * to make them executable

edit mrtg.cfg and paste :

-------------------------------------------------------
####################################################################################################
### LOAD

Target[127.0.0.1.load]: `/usr/bin/nc 127.0.0.1 9054`
Xsize[127.0.0.1.load]: 400
Ysize[127.0.0.1.load]: 200
Ytics[127.0.0.1.load]: 10
MaxBytes[127.0.0.1.load]: 15000
PageTop[127.0.0.1.load]: <H1> Load Average </H1>
Background[127.0.0.1.load]: #738AA6
Options[127.0.0.1.load]: gauge, nopercent
YLegend[127.0.0.1.load]: Load (10E-2)
ShortLegend[127.0.0.1.load]: (10E-2) 
Legend1[127.0.0.1.load]: Load Average over last 5 minutes 
Legend2[127.0.0.1.load]: Load Average over last 15 minutes 
Legend3[127.0.0.1.load]: Average over last 5 minutes
Legend4[127.0.0.1.load]: Average over last 15 minutes
LegendO[127.0.0.1.load]: 15 minute stagger
Title[127.0.0.1.load]: System load


####################################################################################################
### DF

Target[127.0.0.1.disk]: `/usr/bin/nc localhost 9055`
Xsize[127.0.0.1.disk]: 400
Ysize[127.0.0.1.disk]: 200
Ytics[127.0.0.1.disk]: 10
Title[127.0.0.1.disk]: Disk Space Used
Background[127.0.0.1.disk]: #738AA6
Unscaled[127.0.0.1.disk]: dwym
MaxBytes[127.0.0.1.disk]: 193497660
PageTop[127.0.0.1.disk]: <H1>Disk Space</H1>
kmg[127.0.0.1.disk]: KB,MB,GB
LegendI[127.0.0.1.disk]: var Disk Space
LegendO[127.0.0.1.disk]: home Disk Space
Legend1[127.0.0.1.disk]: var Disk Space
Legend2[127.0.0.1.disk]: homeDisk Space
YLegend[127.0.0.1.disk]: Megabytes
ShortLegend[127.0.0.1.disk]:  
Options[127.0.0.1.disk]: gauge, nopercent

####################################################################################################
### SWAP

Target[127.0.0.1.swap]: `/usr/bin/nc localhost 9056`
Xsize[127.0.0.1.swap]: 400
Ysize[127.0.0.1.swap]: 200
Ytics[127.0.0.1.swap]: 10
MaxBytes[127.0.0.1.swap]: 4999856
Unscaled[127.0.0.1.swap]: dwym
YLegend[127.0.0.1.swap]: Swap In Use
kmg[127.0.0.1.swap]: kB,MB
kilo[127.0.0.1.swap]: 1024
LegendI[127.0.0.1.swap]: Swap Usage: 
LegendO[127.0.0.1.swap]: Swap Total: 
Legend1[127.0.0.1.swap]: Swap Usage:
Legend2[127.0.0.1.swap]: Swap Total
Options[127.0.0.1.swap]: gauge, nopercent
ShortLegend[127.0.0.1.swap]: 
Title[127.0.0.1.swap]: Swap Usage
PageTop[127.0.0.1.swap]: <H1>Swap Usage</H1>


####################################################################################################
### MEM

Target[127.0.0.1.mem]: `/usr/bin/nc localhost 9057`
Xsize[127.0.0.1.mem]: 400
Ysize[127.0.0.1.mem]: 200
Ytics[127.0.0.1.mem]: 10
MaxBytes[127.0.0.1.mem]: 3999856
Unscaled[127.0.0.1.mem]: dwym
YLegend[127.0.0.1.mem]: Mem In Use
kmg[127.0.0.1.mem]: kB,MB
kilo[127.0.0.1.mem]: 1024
LegendI[127.0.0.1.mem]: Mem Active: 
LegendO[127.0.0.1.mem]: Mem Inactive: 
Legend1[127.0.0.1.mem]: Mem Active:
Legend2[127.0.0.1.mem]: Mem Inactive
Options[127.0.0.1.mem]: gauge, nopercent
ShortLegend[127.0.0.1.mem]: 
Title[127.0.0.1.mem]: Mem Usage
PageTop[127.0.0.1.mem]: <H1>Mem Usage</H1>
--------------------------------------------------------------

run :
indexmaker /usr/local/etc/mrtg/mrtg.cfg /var/www/html/mrtg/index.html
ps auxw |grep inetd
kill -HUP yourinetdprocesshere

and you're done


replace with your folder names...

The way to write any script is :

add a line with a custom service in inetd.conf, add it in /etc/services
make a script with 4 outputs, 2 for numbers for that we want to meter and the rest 2 are always :

uptime | sed 's:^.* up \(.*\), [0-9][0-9]* users.*$:\1:'
uname -n

the uptime and the server name
and finally add a section in mrtg replacing the names (load here) and ports (9054 here), legend names and maxbytes (15000 here) with our script's needs.

Target[127.0.0.1.load]: `/usr/bin/nc 127.0.0.1 9054`
Xsize[127.0.0.1.load]: 400
Ysize[127.0.0.1.load]: 200
Ytics[127.0.0.1.load]: 10
MaxBytes[127.0.0.1.load]: 15000
PageTop[127.0.0.1.load]: <H1> Load Average </H1>
Background[127.0.0.1.load]: #738AA6
Options[127.0.0.1.load]: gauge, nopercent
YLegend[127.0.0.1.load]: Load (10E-2)
ShortLegend[127.0.0.1.load]: (10E-2) 
Legend1[127.0.0.1.load]: Load Average over last 5 minutes 
Legend2[127.0.0.1.load]: Load Average over last 15 minutes 
Legend3[127.0.0.1.load]: Average over last 5 minutes
Legend4[127.0.0.1.load]: Average over last 15 minutes
LegendO[127.0.0.1.load]: 15 minute stagger
Title[127.0.0.1.load]: System load
 
Last edited:
Why are you binding them to ports? Why not just access them locally? Im confused?
 
Why are you binding them to ports? Why not just access them locally? Im confused?

Because some may want at a time to collect mrtg info from many servers by just changing localhost to a servers ip and opening the right ports..
 
Back
Top