Navigation :

Guides informatiques - Retour

 Image tiret PC

  
Le 26-06-2007 14:51
par ludovic
(1693 lectures)
Bande Passante utilisée pour chaque Protocole


Introduction


Il faut tout d'abord installer IPtTraf qui va récupérer le nom des protocoles de l'interface ainsi que le nombre de données.

IPtraf est exécuté pendant X minutes, puis il va stocker, ce qu'il aura détecté dans cet intervalle de temps, les données dans un fichier texte. Il vaut mieux une durée de plusieurs minutes, plutôt qu'une seule, car il se peut qu'IPtraf ne détecte pas assez de protocole et de trafic sur une courte durée.

Ce fichier texte est parsé et des graphes RRDtool sont créés.

apt-get install iptraf rrdtool

chown -R www-data:votre_user /var/log/iptraf
chmod -R 774 /var/log/iptraf


iptraf
Configure :
Service Name > on // affiche le nom "80 -> www"
// quittez IPtraf

mkdir /var/www/stats/iptraf // stockage du nécessaire pour les logs, page php, etc
chown -R www-data:votre_user /var/www/stats/iptraf


Récupération des logs -- Exécution pendant 4 minute toutes les 5 minutes


Ce script sera exécuté en tâche CRON toutes les 5 minutes.
IPtraf est exécuté pendant 4 minutes, on fait donc attendre le script .sh pendant 4 minutes 30 pour être certain qu'IPtraf est récupéré des données puis créé le fichier de log qu'il faut.
Ensuite, on déplace le fichier de log récupéré d'IPtraf dans notre dossier web.
Puis on exécute le script PHP qui va créé les fichiers RRD à partir des logs.

nano /var/www/stats/iptraf/recup_log_iptraf.pl

#!/bin/sh

# logue pendant 4 min le traffic par port
/usr/sbin/iptraf -s eth0 -t 4 -B;
sleep 280;

# copie du log d'iptraf vers le rep WEB de nagios
/bin/mv /var/log/iptraf/tcp_udp_services-eth0.log /var/www/stats/iptraf;

# MAJ BDD RRD et génération des graphiques
/usr/bin/php /var/www/stats/iptraf/log_to_rrd.php
exit 0;

chmod +x ./recup_log_iptraf.pl //rend le script exécutable


Création des graphiques en .png dans le répertoire "$chemin_rrd_png"


Lors de son exécution, ce script construit les graphiques avec RRDTool.

nano /var/www/stats/iptraf/log_to_rrd.php
#!/bin/sh

<?php
function nettoyage($texte)
{
$texte = str_replace(":", "",$texte);
$texte = str_replace(";", "",$texte);
$texte = str_replace(",", "",$texte);
$texte = str_replace("packets", "",$texte);
$texte = str_replace("bytes", "",$texte);
$texte = str_replace("total", "",$texte);
$texte = str_replace("incoming", "",$texte);
$texte = str_replace("outgoing", "",$texte);
$texte = str_replace("kbits/s", "",$texte);
$texte = str_replace(" ", " ",$texte);
$texte = str_replace(" ", " ",$texte);
return $texte;
}
function bytes2koctets($int) // 8 bits = 1 bytes = 1 octet
{
$koctets = $int / 1024; // octets -> Kilo octets
return round($koctets, 0); // arrondi à 0 chiffre après la virgule
}
function suppr_anti($texte)
{
$texte = str_replace("/", "_",$texte);
return $texte;
}

$i=0;
$chemin_log_eth = '/var/www/stats/iptraf/tcp_udp_services-eth0.log';
$chemin_rrd_png = '/var/www/stats/iptraf/';

$fichier = fopen($chemin_log_eth, "r");
$nbre_ligne = count(file($chemin_log_eth)); // nbre de ligne
for($i=0;$i<4;$i++)
{
$temp = fgets($fichier, 4096); // lecture du contenu de la ligne
}
while (!feof($fichier)) { //on parcourt le fichier

$ligne = fgets($fichier, 4096); // lecture du contenu de la ligne

if(strlen($ligne) > 1 && ($i < ($nbre_ligne-3 ))) // suppression retour à la ligne et suppr 3 lignes du bas
{
list($prot_nom, $nbre_paquet, $nbre_byte, $bp_kbits, $inc_nbre_paquet, $inc_nbre_byte, $inc_bp_kbits, $out_nbre_paquet, $out_nbre_byte, $out_bp_kbits) = split (" ", nettoyage($ligne));

$nom_bdd_rrd = $chemin_rrd_png.suppr_anti($prot_nom).'.rrd';
if (file_exists($nom_bdd_rrd)) {

// mise à jour des données
// rrdtool update TCP_www.rrd N:1:1
//echo exec('rrdtool update '.$nom_bdd_rrd.' -t '.suppr_anti($prot_nom).'_in N:'.round($inc_nbre_byte,0).'');
//echo exec('rrdtool update '.$nom_bdd_rrd.' -t '.suppr_anti($prot_nom).'_out N:'.round($out_nbre_byte,0).'');
echo exec('rrdtool update '.$nom_bdd_rrd.' N:'.round($out_bp_kbits,0).':'.round($inc_bp_kbits,0).'');


// création des images PNG
echo exec('rrdtool graph '.$chemin_rrd_png.suppr_anti($prot_nom).'.png -s -1days -e -1min \
--title " Débit '.suppr_anti($prot_nom).' " \
--vertical-label " kbits/s " \
DEF:input='.$nom_bdd_rrd.':'.suppr_anti($prot_nom).'_in:AVERAGE \
DEF:output='.$nom_bdd_rrd.':'.suppr_anti($prot_nom).'_out:AVERAGE \
AREA:input#FF0000:"Input en kbits/s" \
LINE1:output#0000FF:Output');

} else { // on cré la base si le RRD n'existe pas
echo exec('rrdtool create '.$chemin_rrd_png.suppr_anti($prot_nom).'.rrd \
DS:'.suppr_anti($prot_nom).'_out:GAUGE:600:U:U \
DS:'.suppr_anti($prot_nom).'_in:GAUGE:600:U:12500000 \
RRA:AVERAGE:0.5:1:600 \
RRA:AVERAGE:0.5:6:700 \
RRA:AVERAGE:0.5:24:775 \
RRA:AVERAGE:0.5:288:797 \
RRA:MAX:0.5:1:600 RRA:MAX:0.5:6:700 \
RRA:MAX:0.5:24:775 \
RRA:MAX:0.5:288:797');
}
}
$i++;
}
fclose($fichier);
?>
chmod +x ./log_to_rrd.php


Affichage des ko transférés par protocole


Cette page affiche, sous forme de tableau, la bande passante en ko/sec suivant chaque protocole.

nano /var/www/stats/iptraf/iptraf.php

<html>
<head><title>Utilisation BP par Service/Port pendant 4 min.</title></head>
<body>
<?php
function nettoyage($texte)
{
$texte = str_replace(":", "",$texte);
$texte = str_replace(";", "",$texte);
$texte = str_replace(",", "",$texte);
$texte = str_replace("packets", "",$texte);
$texte = str_replace("bytes", "",$texte);
$texte = str_replace("total", "",$texte);
$texte = str_replace("incoming", "",$texte);
$texte = str_replace("outgoing", "",$texte);
$texte = str_replace("kbits/s", "",$texte);
$texte = str_replace(" ", " ",$texte);
$texte = str_replace(" ", " ",$texte);
return $texte;
}

function bytes2koctets($int) // 8 bits = 1 bytes = 1 octet
{
$koctets = $int / 1024; // octets -> Kilo octets
return round($koctets, 0); // arrondi à 0 chiffre après la virgule
}

$i=0;
$chemin_log = '/usr/local/nagios/share/iptraf/tcp_udp_services-eth0.log';

$fichier = fopen($chemin_log, "r");
$nbre_ligne = count(file($chemin_log)); // nbre de ligne

for($i=0;$i<4;$i++) // suppression des 3 premières lignes
{
$temp = fgets($fichier, 4096); // lecture du contenu de la ligne
}
while (!feof($fichier)) { //on parcourt le fichier

$ligne = fgets($fichier, 4096); // lecture du contenu de la ligne

if(strlen($ligne) > 1 && ($i < ($nbre_ligne-3 ))) // suppression retour à la ligne
{
list($prot_nom, $nbre_paquet, $nbre_byte, $bp_kbits, $inc_nbre_paquet, $inc_nbre_byte, $inc_bp_kbits, $out_nbre_paquet, $out_nbre_byte, $out_bp_kbits) = split (" ", nettoyage($ligne));

$page .= ' <tr>
<td align="center" colspan="3" style="color:white; background-color:#A0D0FF;"><b>'.$prot_nom.'</b></td>
</tr>
<tr>
<td align="center">Incoming : </td>
<!-- <td>'.$inc_nbre_paquet.' paquets</td> -->
<td style="padding-right:5px;">'.bytes2koctets($inc_nbre_byte).' ko</td>
<td style="padding-right:5px;"><b>'.$inc_bp_kbits.'</b> kbits/s</td>
</tr>
<tr>
<td align="center">Outcoming : </td>
<!-- <td>'.$out_nbre_paquet.' paquets</td> -->
<td style="padding-right:5px;">'.bytes2koctets($out_nbre_byte).' ko</td>
<td style="padding-right:5px;"><b>'.$out_bp_kbits.'</b> kbits/s</td>
</tr>
<tr>
<td align="center" style="border-top:2px solid #A0D0FF;">Total : </td>
<!-- <td>'.$nbre_paquet.' paquets</td> -->
<td style="padding-right:5px; border-top:2px solid #A0D0FF;">'.bytes2koctets($nbre_byte).' ko</td>
<td style="padding-right:5px; border-top:2px solid #A0D0FF;">'.$bp_kbits.' kbits/s</td>
</tr>';
}
$i++;
}
fclose($fichier);
echo '<table style="border:1px solid black; width:700px; border-collapse:collapse; text-align:right;" align="center">';
echo $page;
echo '</table>';
?>
</body>
</html>

chmod +x ./iptraf.php


Affichage des graphiques


Cette page affiche les graphiques en .png créés précédemment.

nano /var/www/stats/iptraf/accueil.html

<html>
<head>
<title>Graphiques BP par protocole</title>
<meta http-equiv="Refresh" content="90" />
</head>
<body>

<p align="center">

<img src="UDP_snmp.png" /><br />
<img src="TCP_ssh.png" /><br />
<img src="TCP_www.png" /><br />
<img src="UDP_domain.png" /><br />
<img src="UDP_netbios-dg.png" /><br />
<img src="UDP_netbios-ns.png" />

</p>
</body>
</html>


Conclusion


Comme toujours, si vous modifiez les scripts ou trouvez de nouvelles fonctionnalités, n'hésitez pas à me le dire.

Je n'ai pas trouvé d'autres moyens pour afficher la bande passante utilisée pour chaque protocole, donc j'espère qu'il pourra vous aider.

TOP 5 Configurer et ouvrir les ports de sa FreeBox Configurer sa NeufBox version 4 Installation de Nagios et Oréon : monitoring de parc informatique Comment overclocker son processeur Intel Article sur le montage de son PC.      
Connectés en ligne : 1 -- © Copyright PC Futé 2003-2008 -- Design par Cyferus
Page exécutée en 0.539 seconde.
Mes autres sites : Serveur dédié RPS - LesPotes

Sites partenaires : benchmark