Diferencia entre revisiones de «Servidor de gráficas»
De Guifi.net - Wiki Hispano
(→Como trabaja el cliente SNPServices) |
(→Como trabaja el cliente SNPServices) |
||
| Línea 24: | Línea 24: | ||
= Como trabaja el cliente SNPServices = | = Como trabaja el cliente SNPServices = | ||
El cliente SNPServices tiene varios passos a realizar para poder recopilar datos y hacer un escaneo en las direcciones cercanas. | El cliente SNPServices tiene varios passos a realizar para poder recopilar datos y hacer un escaneo en las direcciones cercanas. | ||
| + | |||
| + | La lista de nodos a buscar se hace mediante cnml2mrtgcsv.php | ||
| + | Cuando tiene esta lista hace un escaneo a los nodos | ||
| + | La información que ha de buscar en los nodos se hace mediante el archivo mrtgcsv2mrtgcfg.php | ||
| + | |||
| + | <?php | ||
| + | |||
| + | if (file_exists("/var/www/guifi/snpservices/common/config.php")) { | ||
| + | include_once("/var/www/guifi/snpservices/common/config.php"); | ||
| + | } else { | ||
| + | include_once("/var/www/guifi/snpservices/common/config.php.template"); | ||
| + | } | ||
| + | |||
| + | $now = time(); | ||
| + | $mlast= @fopen("/tmp/last_mrtg", "r"); | ||
| + | if ($mlast) | ||
| + | $last = fgets($mlast); | ||
| + | else | ||
| + | $last = 0; | ||
| + | print "Last: ".date('Y/m/d H:i:s',((int)$last)+(60*60))."\n"; | ||
| + | print "Now: ".date('Y/m/d H:i:s',(int)$now)."\n"; | ||
| + | print "ServerId: ".$SNPGraphServerId."\n"; | ||
| + | # | ||
| + | # Looks if the files has been refreshed at least between 60 an 90 mins ago | ||
| + | # range 60..90 depends on $SNPGraphServerId mod 30 | ||
| + | # if is still fresh, does not even looks to the server to check if it has changed | ||
| + | # | ||
| + | $secs = $SNPGraphServerId % 285; | ||
| + | $mins = $SNPGraphServerId % 30; | ||
| + | $nanos = $SNPGraphServerId % 10; | ||
| + | if (($last) and ($now < ($last + ((60 + $mins) * 60)))) { | ||
| + | fclose($mlast); | ||
| + | echo "Still fresh.\n"; | ||
| + | exit(); | ||
| + | } | ||
| + | |||
| + | # | ||
| + | # Local file is not fresh, so looks to the server and cheks if has changed | ||
| + | # | ||
| + | $hlastnow = @fopen($SNPDataServer_url."/guifi/cnml", "r") or die('Error reading changes\n'); | ||
| + | $last_now = fgets($hlastnow); | ||
| + | fclose($hlastnow); | ||
| + | $hlast= @fopen("/tmp/last_update.mrtg", "r"); | ||
| + | if (($hlast) and ($last_now == fgets($hlast))) { | ||
| + | fclose($hlast); | ||
| + | echo "No changes.\n"; | ||
| + | $hlast= @fopen("/tmp/last_mrtg", "w+") or die('Error!'); | ||
| + | fwrite($hlast,$now); | ||
| + | fclose($hlast); | ||
| + | exit(); | ||
| + | } | ||
| + | } | ||
| + | print "Sever CNML dated as: ".date('Y/m/d H:i:s',$last_now)."\n"; | ||
| + | |||
| + | # | ||
| + | # Server CNML has changed, so going to call the server for the new file | ||
| + | # Befoge calling, sleep $SNPGrahServerId mod 285 (4 min, 45 segs) to spread across that | ||
| + | # timeslot. | ||
| + | # | ||
| + | |||
| + | print "Waiting for ".$secs.".".$nanos." seconds\n"; | ||
| + | time_nanosleep($secs,($nanos * 10000000)); | ||
| + | print date('Y/m/d H:i:s')."\n"; | ||
| + | |||
| + | $hf = @fopen($MRTGConfigSource,"r") or die('Error reading MRTG csv input\n"'); | ||
| + | $cf = @fopen('../data/mrtg.cfg','w+'); | ||
| + | |||
| + | fputs($cf,sprintf($rrdtool_header,$rrdimg_path,$rrdimg_path,$rrddb_path,$rrddb_path)); | ||
| + | |||
| + | while ( $buffer = fgets($hf, 4096) ) { | ||
| + | $node_line_array = explode(",",$buffer); | ||
| + | $line = $node_line_array[count($node_line_array) - 1]; | ||
| + | if ( substr($buffer,0,1) == '#' || $line == "Planned\n" || $line == "Dropped\n" || $line == "Building\n" || $line == "Reserved\n" ) | ||
| + | continue; | ||
| + | |||
| + | $buffer = str_replace("\n","",$buffer); | ||
| + | $dev=explode(',',$buffer); | ||
| + | if ( count($dev) != 5 ) { | ||
| + | $dev[0] = $dev[0]; | ||
| + | $dev[1] = $dev[1]; | ||
| + | $dev[2] = $dev[2]; | ||
| + | $dev[3] = 'eth0;'; | ||
| + | $dev[4] = $dev[3]; | ||
| + | } | ||
| + | fputs($cf,sprintf($mrtg_ping_template, | ||
| + | $dev[0], | ||
| + | $dev[1], | ||
| + | $dev[0], | ||
| + | $dev[1], | ||
| + | $dev[1], | ||
| + | $dev[2], | ||
| + | $dev[0], | ||
| + | $dev[2], | ||
| + | $dev[0], | ||
| + | $dev[0], | ||
| + | $dev[0], | ||
| + | $dev[0], | ||
| + | $dev[0], | ||
| + | $dev[0], | ||
| + | $dev[0], | ||
| + | $dev[0]) | ||
| + | ); | ||
| + | $t = explode('|',$dev[3]); | ||
| + | |||
| + | foreach ($t as $k=>$r) { | ||
| + | // is the snmp Index given?? | ||
| + | if (is_numeric($r)) { | ||
| + | $rn = $dev[0].'-'.$r; | ||
| + | $trap = $r; | ||
| + | $wn = 'wLan'; | ||
| + | } // end if numeric snmp Index | ||
| + | else { | ||
| + | $rn = $dev[0].'-'.$k; | ||
| + | // snmp is given by interface name | ||
| + | $d = explode(';',$r); | ||
| + | $wn = $d[1]; | ||
| + | $trap = '\\'.$d[0]; | ||
| + | } | ||
| + | fputs($cf,sprintf($mrtg_traffic_template."\n", | ||
| + | $rn, | ||
| + | $trap, | ||
| + | $dev[2], | ||
| + | $rn, | ||
| + | $dev[2], | ||
| + | $dev[1], | ||
| + | $rn, $rn, | ||
| + | $wn, | ||
| + | $dev[1], | ||
| + | $rn, | ||
| + | $wn, | ||
| + | $dev[1], | ||
| + | $dev[1], | ||
| + | $wn) | ||
| + | ); | ||
| + | |||
| + | } // foreach interface | ||
| + | } | ||
| + | fclose($hf); | ||
| + | fclose($cf); | ||
| + | |||
| + | $hlast= @fopen("/tmp/last_update.mrtg", "w+") or die('Error!'); | ||
| + | fwrite($hlast,$last_now); | ||
| + | fclose($hlast); | ||
| + | $hlast= @fopen("/tmp/last_mrtg", "w+") or die('Error!'); | ||
| + | fwrite($hlast,$now); | ||
| + | fclose($hlast); | ||
| + | |||
| + | ?> | ||
| + | |||
| + | |||
| + | |||
| + | este archivo es | ||
<?php | <?php | ||
Revisión de 11:43 29 oct 2012
Definicion
El servidor de gráficas se baas en el servicio de GUIFI llamado SNPServices. El servidor SNPServices, y para ser mas exactos cliente SNPServices es un servidor linux, con php, apache, mrtg y rrdtool, no necesita nada mas. Le llamamos cliente porque el servidor Master SNPServices es un servidor que nos enviará informacion a nuestro servidor cliente SNPServices. Aqui tienes un esquema que te hará ver claro esta definicion.
El cliente SNPServices consulta al Master snpservices la lista de nodos a monitorear, con php se parsea la lista y formatea scripts mrtg y los ejecuta (pings y trafico) para generar un rrd y con sonsultas http, se ejecuta un php que convierte los .rrd en imagenes y las muestra.
El servidor WEB de guifi.net conoce la url del servidor de graficas de la zona de un nodo, y cuando visitas un nodo en la web se muestran las graficas/imagenes remotas que hay en los servidores SNPServices cliente.
Manuales
- Servidor de gráficas 1 (Debian/Ubuntu) método apt + configuración virtualhost apache
- Servidor de gráficas 2 (Debian/Ubuntu) método dpkg
- Servidor de gráficas 3 (Debian/Ubuntu) extendido
Como trabaja el cliente SNPServices
El cliente SNPServices tiene varios passos a realizar para poder recopilar datos y hacer un escaneo en las direcciones cercanas.
La lista de nodos a buscar se hace mediante cnml2mrtgcsv.php Cuando tiene esta lista hace un escaneo a los nodos La información que ha de buscar en los nodos se hace mediante el archivo mrtgcsv2mrtgcfg.php
<?php
if (file_exists("/var/www/guifi/snpservices/common/config.php")) {
include_once("/var/www/guifi/snpservices/common/config.php");
} else {
include_once("/var/www/guifi/snpservices/common/config.php.template");
}
$now = time(); $mlast= @fopen("/tmp/last_mrtg", "r"); if ($mlast)
$last = fgets($mlast);
else
$last = 0;
print "Last: ".date('Y/m/d H:i:s',((int)$last)+(60*60))."\n"; print "Now: ".date('Y/m/d H:i:s',(int)$now)."\n"; print "ServerId: ".$SNPGraphServerId."\n";
- Looks if the files has been refreshed at least between 60 an 90 mins ago
- range 60..90 depends on $SNPGraphServerId mod 30
- if is still fresh, does not even looks to the server to check if it has changed
$secs = $SNPGraphServerId % 285; $mins = $SNPGraphServerId % 30; $nanos = $SNPGraphServerId % 10; if (($last) and ($now < ($last + ((60 + $mins) * 60)))) {
fclose($mlast); echo "Still fresh.\n"; exit();
}
- Local file is not fresh, so looks to the server and cheks if has changed
$hlastnow = @fopen($SNPDataServer_url."/guifi/cnml", "r") or die('Error reading changes\n'); $last_now = fgets($hlastnow); fclose($hlastnow); $hlast= @fopen("/tmp/last_update.mrtg", "r"); if (($hlast) and ($last_now == fgets($hlast))) {
fclose($hlast);
echo "No changes.\n";
$hlast= @fopen("/tmp/last_mrtg", "w+") or die('Error!');
fwrite($hlast,$now);
fclose($hlast);
exit();
} } print "Sever CNML dated as: ".date('Y/m/d H:i:s',$last_now)."\n";
- Server CNML has changed, so going to call the server for the new file
- Befoge calling, sleep $SNPGrahServerId mod 285 (4 min, 45 segs) to spread across that
- timeslot.
print "Waiting for ".$secs.".".$nanos." seconds\n"; time_nanosleep($secs,($nanos * 10000000)); print date('Y/m/d H:i:s')."\n";
$hf = @fopen($MRTGConfigSource,"r") or die('Error reading MRTG csv input\n"'); $cf = @fopen('../data/mrtg.cfg','w+');
fputs($cf,sprintf($rrdtool_header,$rrdimg_path,$rrdimg_path,$rrddb_path,$rrddb_path));
while ( $buffer = fgets($hf, 4096) ) {
$node_line_array = explode(",",$buffer);
$line = $node_line_array[count($node_line_array) - 1];
if ( substr($buffer,0,1) == '#' || $line == "Planned\n" || $line == "Dropped\n" || $line == "Building\n" || $line == "Reserved\n" )
continue;
$buffer = str_replace("\n","",$buffer);
$dev=explode(',',$buffer);
if ( count($dev) != 5 ) {
$dev[0] = $dev[0];
$dev[1] = $dev[1];
$dev[2] = $dev[2];
$dev[3] = 'eth0;';
$dev[4] = $dev[3];
}
fputs($cf,sprintf($mrtg_ping_template,
$dev[0],
$dev[1],
$dev[0],
$dev[1],
$dev[1],
$dev[2],
$dev[0],
$dev[2],
$dev[0],
$dev[0],
$dev[0],
$dev[0],
$dev[0],
$dev[0],
$dev[0],
$dev[0])
);
$t = explode('|',$dev[3]);
foreach ($t as $k=>$r) {
// is the snmp Index given??
if (is_numeric($r)) {
$rn = $dev[0].'-'.$r;
$trap = $r;
$wn = 'wLan';
} // end if numeric snmp Index
else {
$rn = $dev[0].'-'.$k;
// snmp is given by interface name
$d = explode(';',$r);
$wn = $d[1];
$trap = '\\'.$d[0];
}
fputs($cf,sprintf($mrtg_traffic_template."\n",
$rn,
$trap,
$dev[2],
$rn,
$dev[2],
$dev[1],
$rn, $rn,
$wn,
$dev[1],
$rn,
$wn,
$dev[1],
$dev[1],
$wn)
);
} // foreach interface
} fclose($hf); fclose($cf);
$hlast= @fopen("/tmp/last_update.mrtg", "w+") or die('Error!'); fwrite($hlast,$last_now); fclose($hlast); $hlast= @fopen("/tmp/last_mrtg", "w+") or die('Error!'); fwrite($hlast,$now); fclose($hlast);
?>
este archivo es <?php
$rootZone = 52771;
- 3671;
$minX = 999; $minY = 999; $maxX = -999; $maxY = -999;
$members = array();
$hlastnow = @fopen("http://guifi.net/guifi/refresh/maps", "r") or die('Error reading changes\n');
$last_now = fgets($hlastnow);
fclose($hlastnow);
$hlast= @fopen("/tmp/last_update.cnml", "r");
if (($hlast) and ($last_now == fgets($hlast))) {
fclose($hlast);
echo "No changes.\n";
exit();
}
echo "Getting CNML file\n";
$hcnml = @fopen("http://guifi.net/guifi/cnml/".$rootZone."/detail", "r");
$wcnml = @fopen("guifi.cnml", "w+");
while (!feof($hcnml)) {
$buffer = fgets($hcnml, 4096);
fwrite($wcnml,$buffer);
}
fclose($hcnml);
fclose($wcnml);
$hlast= @fopen("/tmp/last_update.cnml", "w+") or die('Error!');
fwrite($hlast,$last_now);
fclose($hlast);
?>
PMF
- Regenerar los ficheros rrd
- tener en cuenta que al connectar un dispositivo por cable ponerlo en Lan o en la ubicacion que sea, sino puede que no grafique correctamente.