Informations :
Dates
- Publish : : Wednesday 08 may 2024
- Modification : Friday 17 may 2024
- 563 views
Share :
Ci-desous les scripts qui me permettent de générer mes graphiques MRTG.
Si vous voulez vous essayer pour comparer.
Ci-dessous un exemple de mon fichier /etc/mrtg.cfg :
Script avec 66 lignes
001# Title correspond au titre de la page html générée.002#Title[string]: Titre du graph003#PageTop[string]: <H1>MRTG</H1>004# Target correspond à ce que nous voulons interroger.005#Target[string]: 1:Public@127.0.0.1006#MaxBytes[string]: 16000007 008WriteExpires: Yes009 010#######################011# Global Settings012RunAsDaemon: yes013 014# Répertoire de destination des graphes, des fichiers de données (log,old), des pages html.015WorkDir:/var/pro/mrtg/router/016 017# On peut choisir le chemin où doit se trouver les fichiers html, png, log.018#Imagedir: /var/pro/mrtg/router/019Htmldir:/var/pro/mrtg/router/020Logdir:/var/pro/mrtg/router/021# Options générales022# growright, noinfo, transparent, unknaszero, bits, nopercent, growright, gauge023# défilement de droite à gauche, fond des graphes transparent, en cas de problème de poling le graphe tombe à zéro sinon MRTG prend la dernière valeur connnue et trace une ligne continue tant que MRTG n'arrive plus interroger l?équipement.024Options[_]:growright,transparent,unknaszero,noinfo,nobanner025 026# Cela permet de supprimer les graphes voulus (y=yearly m=monthly w=weekly d=daily).027Suppress[_]: y028 029#######################030# Options correspond aux options spécifiques du graphe et annule la ou les options générales spécifiées lors de la création du fichier de configuration.031#Options[string]: growleft, nobanner, noinfo032#Options[string]: nobanner, noinfo033 034# Normalement MRTG ajuste automatiquement la valeur maximale du graphe pour avoir la meilleure échelle possible.035# Cette option permet en fait d'annuler cet automatisme et d'avoir un graphe fixe.036#Unscaled[string]: dwym037 038# Fixe la valeur maximale des graphes, cette valeur est en octets.039#MaxBytes[string]: 1250000040 041#AbsMax[string]: 1250000042 043# Si vous voulez monter plus de 4 lignes par graphique.044#Ytics[string]: 6045 046# Cela permet d'ajuster les graphes, c'est à dire cela consiste à multiplier le graphe par une valeur.047#YScale[string]: 1.25048#XScale[string]: 1.5049 050# Permet de mettre une légende explicite à l'axe des y.051#YLegend[string]: Packets /s052 053# Cela consiste à définir la taille du graphe en pixel de 20 à 600 par axe.054#YSize[string]: 150055#XSize[string]: 600056 057#Legend1[string]: IN Packets /s058#Legend2[string]: OUT Packets /s059#LegendI[string]: IN P/s:060#LegendO[string]: OUT P/s:061 062# Légende des valeurs des graphes.063#ShortLegend[string]: P/s064 065# Aprés j'ai ajouté les scripts ".cfg" (en double) pour générer l'index.066# /usr/bin/indexmaker --output /var/pro/mrtg/router/index.html /etc/mrtg.cfg
La charge CPU :
Le fichier de configuration MRTG /etc/mrtg/mrtg.cpu.cfg :
Script avec 19 lignes
001# --------------------------------------------------------------002# url : http://nicolas.agius.pagesperso-orange.fr/linux/code/mrtg.html003WorkDir:/var/pro/mrtg/router/004Target[cpu]: `/etc/mrtg/cpu.pl`005MaxBytes1[cpu]: 100006MaxBytes2[cpu]: 100007AbsMax[cpu]: 1000008Options[cpu]: nopercent,growright,gauge,unknaszero,nobanner, integer009#Options[cpu]: dorelpercent, growright, nobanner, noinfo, transparent010Unscaled[cpu]: dwmy011YLegend[cpu]: % Charge CPU012ShortLegend[cpu]: %013Legend2[cpu]: LoadAverage014Legend1[cpu]: Charge CPU totale015LegendI[cpu]: Charge CPU :016LegendO[cpu]: LoadAverage:017Title[cpu]: Charge CPU018PageTop[cpu]: <h1>Charge CPU</h1>019# --------------------------------------------------------------
Le script /etc/mrtg/cpu.pl :
Script avec 118 lignes
001#!/usr/bin/perl002# mrtg-cpu.pl003# v2.0 - Nicolas AGIUS - 01/2007004#005###################################################################################006## Sonde MRTG pour la charge processeur007#008# Copyright (C) 2006 Nicolas AGIUS <nicolas_agius@yahoo.fr>009#010# This program is free software; you can redistribute it and/or011# modify it under the terms of the GNU General Public License012# as published by the Free Software Foundation; either version 2013# of the License, or any later version.014#015# This program is distributed in the hope that it will be useful,016# but WITHOUT ANY WARRANTY; without even the implied warranty of017# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the018# GNU General Public License for more details.019#020# You should have received a copy of the GNU General Public License021# along with this program; if not, write to the Free Software022# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.023#024###################################################################################025 026use POSIX;027 028# /proc/stat report CPU usage in jiffies.029#030# CPU times in second = jiffies/HZ and031# HZ is at 100 on my system as reported032# by the sysconf() C funtion :033#034#// Begin C code035##include <unistd.h>036##include <stdio.h>037#038#int main()039#{040# printf("HZ=%i\n",sysconf(_SC_CLK_TCK));041#}042#// EOF043 044$HZ=100;045 046# Syntax047(scalar(@ARGV) <= 1) or die "Usage : $0 [noinfo]\n";048 049### CPU% ###050 051$stat_file="/var/lib/mrtg-cpu.stat";052 053#Initialisation054$oldtime=0;055$oldvalue=0;056$time=time();057do $stat_file if(-x $stat_file);058 059open(STAT,"</proc/stat") or die "Cannot open stat file";060<STAT> =~ /^cpu\s+(\d+)\s+(\d+)\s+(\d+)/;061close(STAT);062 063# Get only total064$value = $1+$2+$3;065 066# Save old param067open(OLDSTAT,">$stat_file") or die "Cannot open or create $stat_file";068print(OLDSTAT "\$oldtime=\"$time\";\n");069print(OLDSTAT "\$oldvalue=\"$value\";\n");070close(OLDSTAT);071chmod(755,$stat_file);072 073#Compute CPU% for last period074$cpu=ceil((($value-$oldvalue)/$HZ)*100/($time-$oldtime));075 076### END-CPU% ###077 078print "$cpu\n";079 080### LOADAVG ###081 082open(LOADAVG,"</proc/loadavg") or die("Cannot open /proc/loadavg");083<LOADAVG> =~ /^\S+\s(\S+)/;084close LOADAVG;085$loadavg = $1*100;086 087### END-LOADAVG ###088 089print "$loadavg\n";090 091### INFO ###092if($ARGV[0] ne "noinfo")093{094use Sys::Hostname;095 096### UPTIME ###097 098open(UPTIME,"</proc/uptime") or die("Cannot open /proc/uptime");099<UPTIME> =~ /^(\d+)/;100close UPTIME;101 102# I hope perl will optimise computing ;-)103$d = int( $1 / 86400 );104$h = int(( $1 % 86400 ) / 3600 );105$m = int((($1 % 86400 ) % 3600 ) / 60);106 107# Ha ha, ca c est beau ;-) Faudrait le mettre sur variable implicite !108$uptime = $d>0?"$d ".(($d>1)?"jours ":"jour "):"";109$uptime .= sprintf("%02d:%02d",$h,$m);110 111### END-UPTIME ###112 113print "$uptime\n";114print hostname()."\n";115}116### END-INFO ###117 118exit(0);
Traffic eth0 :
Le fichier de configuration MRTG /etc/mrtg/mrtg.packets-in-out.cfg :
Script avec 18 lignes
001#----------------eth0---------------002WorkDir:/var/pro/mrtg/router/003Target[eth0]: `/bin/sh /etc/mrtg/network_mrtg.eth0.pl.sh`;004Options[eth0]: dorelpercent, growright, nobanner, noinfo, transparent005MaxBytes[eth0]: 2000000006AbsMax[eth0]: 10000000007kilo[eth0]: 1024008kMG[eth0]: K,M,G009YLegend[eth0]: Bytes per second010ShortLegend[eth0]: B/s011Legend1[eth0]: Incoming Traffic in Bytes per second012Legend2[eth0]: Outgoing Traffic in Bytes per second013LegendI[eth0]: In:014LegendO[eth0]: Out:015Title[eth0]: Traffic Analysis for : eth0016PageTop[eth0]:<h1>Traffic Analysis for : <code class="code">eth0</code></h1>017#Unscaled[eth0]: dwmy018#WithPeak[eth0]: my
Le script /etc/mrtg/network_mrtg.eth0.pl.sh :
Script avec 3 lignes
001#!/bin/sh002 003/usr/bin/perl -e '@a=split(/[:\s]+/,qx(grep eth0 /proc/net/dev));printf "%.0f\n%.0f\n1\neth0 traffic\n",$a[2],$a[10];';
Apache (serveur Web) :
Traffic (bytes) :
Le script /etc/mrtg/apache2.pl :
Script avec 25 lignes
001#!/usr/bin/perl002# can return hits or bytes (counters)003@res = `lynx -dump http://localhost:80/server-status`;004foreach $res (@res) {005if ($res =~ /Server uptime: (.*)$/) { $up = $1; last } else { next }006if ($res =~ /Server at/) { $server = $res; last } else { next }007}008 009@res = `lynx -dump http://localhost:80/server-status?auto`;010 011foreach $res (@res) {012if ($res =~ /Total Accesses: (\d+)/) { $d1 = $1; next }013if ($res =~ /Total kBytes: (\d+)/) { $d2 = $1 * 1024; next }014}015$d1 = int($d1);016$d2 = int($d2);017if ($ARGV[0] eq "hits") {018print "$d1\n";019print "$d1\n";020} elsif ($ARGV[0] eq "bytes") {021print "$d2\n";022print "$d2\n";023}024print "$up\n";025print "$server";
Il faut bien entendu activer le module Apache "/etc/apache2/mods-enabled/status.conf"
Le fichier de configuration MRTG /etc/mrtg/mrtg.apache2-traffic.cfg :
Script avec 17 lignes
001#---------Apache2 Traffic-----------------------002# https://www.xenetis.org/installation_monitoring_mrtg_snmp_debian.html003WorkDir:/var/pro/mrtg/router/004Target[apache2_traffic]: `/etc/mrtg/apache2.pl bytes`005Options[apache2_traffic]: nopercent, growright, noinfo, nobanner, noi006PageTop[apache2_traffic]: <h1>Traffic Apache</h1>007MaxBytes[apache2_traffic]:16000008AbsMax[apache2_traffic]:20000009kMG[apache2_traffic]: K,M,G010YLegend[apache2_traffic]: octets/s011ShortLegend[apache2_traffic]: o/s012LegendO[apache2_traffic]: Traffic Apache:013Legend2[apache2_traffic]: Traffic Apache014Title[apache2_traffic]: Traffic du serveur Apache015WithPeak[apache2_traffic]: wmy016Legend4[apache2_traffic]: Traffic max Apache017#------------End Apache2 Traffic------------------
Traffic (hits) :
Le fichier de configuration MRTG /etc/mrtg/mrtg.apache2-hits.cfg :
Script avec 14 lignes
001#---------Apache2 hits-----------------------002WorkDir:/var/pro/mrtg/router/003Target[apache2_hits]: `/etc/mrtg/apache2.pl hits`004Options[apache2_hits]: perhour, nopercent, growright, noinfo, nobanner, noi005PageTop[apache2_hits]: <h1>Hits Apache</h1>006MaxBytes[apache2_hits]: 1000000007YLegend[apache2_hits]: hits/minute008ShortLegend[apache2_hits]: hits/min009LegendO[apache2_hits]: Hits:010Legend2[apache2_hits]: Hits per minute011Legend4[apache2_hits]: Hits per minute max012Title[apache2_hits]: Hits Apache013WithPeak[apache2_hits]: wmy014#------------End Apache2 Hits------------------
MySQL (Bases de données)
Requêtes MySQL sur DB1
Le script /etc/mrtg/mysql-stats.sh :
Script avec 24 lignes
001#!/bin/bash002# https://www.webmaster-hub.com/topic/17728-activit%C3%A9-mysql-sur-les-graphs-mrtg/003 004# need a user and password? add it here then.005mysqlcmd="/usr/bin/mysqladmin -h db1.lan -u user_read_stats --password=ton_super_mot_de_passe_super_long_impossible_a_memoriser extended-status";006 007# dont edit below this line unless you know what you're doing.008# -----------------------------------------------------------009 010function get_data {011data=`$mysqlcmd`;012questions=`echo "$data"|grep 'Questions'|awk -F " " '{print $4}'`;013uptime=`echo "$data"|grep 'Uptime '|awk -F " " '{print $4}'`;014cache=`echo "$data"|grep 'Qcache_hits'|awk -F " " '{print $4}'`;015}016 017function output_data {018echo "$questions";019echo "$cache";020echo "`expr $uptime / 86400` jours";021echo "MySQL Server DB1";022}023get_data;024output_data
Le fichier de configuration MRTG /etc/mrtg/mrtg.mysql.cfg :
Script avec 14 lignes
001#----------- Mysql Questions---------------------002WorkDir:/var/pro/mrtg/router/003Target[db1_mysql]: `/etc/mrtg/mysql-stats.sh`004PageTop[db1_mysql]: <h1>Requêtes MySQL sur DB1</h1>005Options[db1_mysql]: growright, nopercent006MaxBytes[db1_mysql]: 100007Title[db1_mysql]: Requêtes MySQL sur DB1008YLegend[db1_mysql]: Requêtes009ShortLegend[db1_mysql]: q/s010Legend1[db1_mysql]: Requêtes exécutées011Legend2[db1_mysql]: Cache hits012LegendI[db1_mysql]: Questions:013LegendO[db1_mysql]: Cache hits:014#----------- Mysql Questions ---------------------
MYSQL queries count
Le fichier de configuration MRTG /etc/mrtg/mrtg.mysql-query.cfg :
Script avec 14 lignes
001#----------- Mysql Query ---------------------002WorkDir:/var/pro/mrtg/router/003Target[mysql_queries]: `/usr/bin/mysqladmin -h db1.lan -u user_read_stats --password=ton_super_mot_de_passe_super_long_impossible_a_memoriser ver|awk '/Qu/{q=$4;s=$7}/Up/{u=$0}/Se/{v=$3}END{print q"n"s"n"u"nMYSQL version "v}'|sed 's/Uptime:[[:space:]]+//'`004Options[mysql_queries]: nopercent, growright, integer, perminute, pngdate005MaxBytes[mysql_queries]: 250000006Title[mysql_queries]: MYSQL queries count007PageTop[mysql_queries]: <h1>MYSQL queries count</h1>008ShortLegend[mysql_queries]: q/m009YLegend[mysql_queries]: Count010LegendI[mysql_queries]: queries011LegendO[mysql_queries]: slow queries012Legend1[mysql_queries]: queries013Legend2[mysql_queries]: slow queries014#----------- Mysql Query ---------------------
Bytes MySQL sur DB1
Le script /etc/mrtg/mysql-stats-bytes.sh :
Script avec 24 lignes
001#!/bin/bash002# https://www.webmaster-hub.com/topic/17728-activit%C3%A9-mysql-sur-les-graphs-mrtg/003 004# need a user and password? add it here then.005mysqlcmd="/usr/bin/mysqladmin -h db1.lan -u user_read_stats --password=ton_super_mot_de_passe_super_long_impossible_a_memoriser extended-status";006 007# dont edit below this line unless you know what you're doing.008# -----------------------------------------------------------009 010function get_data {011data=`$mysqlcmd`;012Bytes_received=`echo "$data"|grep 'Bytes_received'|awk -F " " '{print $4}'`;013uptime=`echo "$data"|grep 'Uptime '|awk -F " " '{print $4}'`;014Bytes_sent=`echo "$data"|grep 'Bytes_sent'|awk -F " " '{print $4}'`;015}016 017function output_data {018echo "$Bytes_received";019echo "$Bytes_sent";020echo "`expr $uptime / 86400` jours";021echo "MySQL Server DB1";022}023get_data;024output_data
Le fichier de configuration MRTG /etc/mrtg/mrtg.mysql-bytes.cfg :
Script avec 19 lignes
001#--------------- MySQL Bytes -----------------002WorkDir:/var/pro/mrtg/router/003Target[db1_bytes_mysql]: `/etc/mrtg/mysql-stats-bytes.sh`004PageTop[db1_bytes_mysql]: <h1>Bytes MySQL sur DB1</h1>005Options[db1_bytes_mysql]: bits, growright, nopercent, nobanner006#Options[db1_bytes_mysql]: growright, nopercent, noinfo, nobanner, noi007#MaxBytes[db1_bytes_mysql]: 12500000008MaxBytes[db1_bytes_mysql]: 10000000000009#MaxBytes[db1_bytes_mysql]: 3433771833010kMG[db1_bytes_mysql]: K,M,G011#kilo[db1_bytes_mysql]: 1024012Title[db1_bytes_mysql]: Bytes MySQL sur DB1013YLegend[db1_bytes_mysql]: Bytes per second014ShortLegend[db1_bytes_mysql]: B/s015Legend1[db1_bytes_mysql]: Bytes received016Legend2[db1_bytes_mysql]: Bytes sent017LegendI[db1_bytes_mysql]: Bytes received:018LegendO[db1_bytes_mysql]: Bytes sent:019#--------------- MySQL Bytes -----------------
Bien sûr pour générer les graphiques il faut ajouter le refresh toutes les 5 minutes aux tâches planifiées, par exemple :
Script avec 12 lignes
001$ crontab - l002 003# MRTG004*/5 * * * * /usr/bin/indexmaker --output /var/pro/mrtg/router/index.html /etc/mrtg.cfg --nolegend 2>/dev/null 2>&1005# ;)006*/5 * * * * /usr/bin/env LANG=C /usr/bin/mrtg /etc/mrtg/mrtg.cpu.cfg 2>/dev/null 2>&1007*/5 * * * * /usr/bin/env LANG=C /usr/bin/mrtg /etc/mrtg/mrtg.packets-in-out.cfg 2>/dev/null 2>&1008*/5 * * * * /usr/bin/env LANG=C /usr/bin/mrtg /etc/mrtg/mrtg.apache2-traffic.cfg 2>/dev/null 2>&1009*/5 * * * * /usr/bin/env LANG=C /usr/bin/mrtg /etc/mrtg/mrtg.apache2-hits.cfg 2>/dev/null 2>&1010*/5 * * * * /usr/bin/env LANG=C /usr/bin/mrtg /etc/mrtg/mrtg.mysql.cfg 2>/dev/null 2>&1011*/5 * * * * /usr/bin/env LANG=C /usr/bin/mrtg /etc/mrtg/mrtg.mysql-query.cfg 2>/dev/null 2>&1012*/5 * * * * /usr/bin/env LANG=C /usr/bin/mrtg /etc/mrtg/mrtg.mysql-bytes.cfg 2>/dev/null 2>&1
C'est tout ;)
Romain.
Liens utiles :
- Multi Router Traffic Grapher (MRTG) (Ref examples)
- Nicolas Agius : MRTG
- Webmaster Hub (Dan) : Activité mySQL sur les graphs mrtg
- pteuz Wiki : MRTG (Latence, Connexions TCP, nb users sys/ftp, Seuils d'alerte..) 😇
- L'Internet Rapide et Permanent : SNMP (Simple Network Management Protocol)









