Table des matières
Installation d'un systeme de monitoring MRTG (Multi Router Traffic Grapher) sous debian
Ce tutoriel a pour but d'installer l'outil de monitoring MRTG. Cet outil permet de générer des graphiques en fonction de données SNMP, ou de scripts shell.
Installation de base SNMP et MRTG :
Installer snmpd :
apt-get install snmpd
Editer le fichier de configuration du serveur snmp afin de le rendre utilisable par MRTG :
vi /etc/snmp/snmpd.conf
décommenter cette ligne :
com2sec readonly default public
Redémarrer snmpd :
/etc/init.d/snmpd restart
Installer MRTG :
apt-get install mrtg
Créer le dossier /etc/mrtg qui contiendra les scripts spécifiques :
mkdir /etc/mrtg
Créer l'index.html MRTG afin de visionner le 1er graphique :
indexmaker --output /var/www/mrtg/index.html /etc/mrtg.cfg --columns=2 --nolegend
Création des scripts :
Ajouter le script ping /etc/mrtg/ping :
#!/bin/sh P=`ping -c3 -q 217.12.3.11|cut -d" " -f4` MIN=`echo $P|cut -d"/" -f1` MAX=`echo $P|cut -d"/" -f2` echo $MAX echo $MIN
Ajouter le script mem /etc/mrtg/mem :
#!/bin/sh USED=`free -b|grep cache:|cut -d ":" -f2|cut -c1-11` FREE=`free -b|grep cache:|cut -d ":" -f2|cut -c12-22` echo $FREE echo $USED
Pour utiliser le script CPU, il faut installer le package sysstat (commande sar) :
apt-get install sysstat
Activer le processus sysstat en remplaçant FALSE par TRUE dans le fichier /etc/default/sysstat :
# ENABLED="false" ENABLED="true"
Ajouter le script cpu /etc/mrtg/cpu1 :
#!/usr/bin/perl @line = `/usr/bin/sar | grep all | /usr/bin/tail -n 2 | /usr/bin/head -n 1 | /bin/sed 's/\ \ */ /g'`; @data = split(/ /, @line[0]); if (@data[2] eq "") { printf "0\n"; }else { printf ("%3.0f\n", @data[2] + 0.5); } printf ("%3.0f\n", (@data[3])+(@data[2])+(@data[4]+0.5)); $uptime = `/usr/bin/uptime | sed 's/\ \ */ /g'`; @uptime = split(/,/, $uptime); @uptime = split(/up/, @uptime[0]); $server = `/bin/uname -n`; printf "@uptime[1]\n"; printf $server;
Ajouter le script cpu /etc/mrtg/cpu2 :
#!/usr/bin/perl #Affichage de la consommation cpu pour MRTG my $cpu =`cat /proc/stat | grep "cpu "`; $cpu=~ /cpu (.*) (.*) (.*) (.*)/; my $conso = $1 + $2 + $3; $conso = int($conso); print "$conso\n"; print "$conso\n";
Pour installer le monitoring Apache 2, il faut décommenter la partie server-status dans la configuration d'Apache2 pour avoir accès aux status serveur :
vi /etc/apache2/apache2.conf
Verifier la présence de ses lignes, les ajouter si besoin :
<Location /server-status> SetHandler server-status Order deny,allow Deny from all Allow from 127.0.0.1 </Location> ExtendedStatus On
sauvegarder le ficher et redémarrer apache2 :
/etc/init.d/apache2 restart
Ajouter le script apache2 /etc/mrtg/apache2 :
#!/usr/bin/perl # can return hits or bytes (counters) @res = `lynx -dump http://localhost:80/server-status`; foreach $res (@res) { if ($res =~ /Server uptime: (.*)$/) { $up = $1; last } else { next } if ($res =~ /Server at/) { $server = $res; last } else { next } } @res = `lynx -dump http://localhost:80/server-status?auto`; foreach $res (@res) { if ($res =~ /Total Accesses: (\d+)/) { $d1 = $1; next } if ($res =~ /Total kBytes: (\d+)/) { $d2 = $1 * 1024; next } } $d1 = int($d1); $d2 = int($d2); if ($ARGV[0] eq "hits") { print "$d1\n"; print "$d1\n"; } elsif ($ARGV[0] eq "bytes") { print "$d2\n"; print "$d2\n"; } print "$up\n"; print "$server";
Pour permettre l'exécution, on corrige les droits des scripts créés :
chmod 700 /etc/mrtg/*
Génération des stats :
Voila, maintenant que les scripts sont prêt, il faut faire les appels dans le fichier de conf :)
Ajouter la configuration du script ping à /etc/mrtg.cfg :
## Ping ## Target[ping_yahoo]: `/etc/mrtg/ping` Options[ping_yahoo]: nopercent,growright,gauge,noinfo, nobanner MaxBytes[ping_yahoo]: 10000 AbsMax[ping_yahoo]: 10000 YLegend[ping_yahoo]: Latence ShortLegend[ping_yahoo]: ms Legend1[ping_yahoo]: Latence max en ms Legend2[ping_yahoo]: Latence min en ms LegendI[ping_yahoo]: Latence Max: LegendO[ping_yahoo]: Latence Min: Title[ping_yahoo]: Ping sur yahoo.fr PageTop[ping_yahoo]: <h1>Ping</h1> WithPeak[ping_yahoo]:wmy Legend4[ping_yahoo]: Max de la latence min Legend3[ping_yahoo]: Max de la latence max ## Memory ## Target[mem]: `/etc/mrtg/mem` Options[mem]: nopercent,growright,gauge,noinfo, nobanner Unscaled[mem]:dwmy MaxBytes[mem]: 261025792 Kilo[mem]:1024 YLegend[mem]: RAM ShortLegend[mem]: o Legend1[mem]: Mémoire libre Legend2[mem]: Mémoire utilisée LegendI[mem]: Mém. Libre: LegendO[mem]: Mém. Utilisée: Title[mem]: Mémoire PageTop[mem]: <h1>Mémoire</h1> WithPeak[mem]:wmy Legend3[mem]: Mémoire libre max Legend4[mem]: Mémoire utilisée max ## CPU 1 ## Target[cpu]: `/etc/mrtg/cpu1` Options[cpu]: nopercent,growright,gauge,noinfo, nobanner #Unscaled[cpu]:dwmy MaxBytes[cpu]: 100 YLegend[cpu]: % CPU ShortLegend[cpu]: % CPU Legend1[cpu]: % CPU Utilisateur Legend2[cpu]: % CPU Utilisateur + Système LegendI[cpu]: Utilisateur: LegendO[cpu]: Total: Title[cpu]: CPU PageTop[cpu]: <h1>Consommation du CPU1</h1> WithPeak[cpu]:wmy Legend3[cpu]: Max % CPU Utilisateur Legend4[cpu]: Max % CPU Utilisateur + Système ## CPU 2 ## Target[cpu2]: `/etc/mrtg/cpu2` Options[cpu2]: noinfo, nopercent, growright, nobanner, noi Title[cpu2]: Suivi de la consommation du CPU2 MaxBytes[cpu2]: 9999999999 YLegend[cpu2]: CPU2 ShortLegend[cpu2]: % LegendO[cpu2]: CPU2 Legend2[cpu2]: CPU2 PageTop[cpu2]: <H1>Consommation du CPU2</H1> WithPeak[cpu2]:wmy Legend3[cpu2]: Max % CPU Utilisateur Legend4[cpu2]: Max % CPU Utilisateur + Système ## Apache2 hits ## Target[apache2_hits]: `/etc/mrtg/apache2 hits` Options[apache2_hits]: perhour, nopercent, growright, noinfo, nobanner, noi PageTop[apache2_hits]: <h1>Hits Apache2</h1> MaxBytes[apache2_hits]: 1000000 YLegend[apache2_hits]: hits/heure ShortLegend[apache2_hits]: par heure LegendO[apache2_hits]: Hits: Legend2[apache2_hits]: Hits horaires Legend4[apache2_hits]: Hits Horaires max Title[apache2_hits]: Hits horaires du serveur Apache WithPeak[apache2_hits]: wmy ## Apache2 Traffic ## Target[apache2_traffic]: `/etc/mrtg/apache2 bytes` Options[apache2_traffic]: nopercent, growright, noinfo, nobanner, noi PageTop[apache2_traffic]: <h1>Traffic Apache</h1> MaxBytes[apache2_traffic]:16000 AbsMax[apache2_traffic]:20000 YLegend[apache2_traffic]: octets/s ShortLegend[apache2_traffic]: o/s LegendO[apache2_traffic]: Traffic Apache: Legend2[apache2_traffic]: Traffic Apache Title[apache2_traffic]: Traffic du serveur Apache WithPeak[apache2_traffic]: wmy Legend4[apache2_traffic]: Traffic max Apache
Et pour finir, générez l'index.html MRTG :
indexmaker --columns=2 --output /var/www/mrtg/index.html /etc/mrtg.cfg