You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sip-tester/tools/monitor/snmparser

162 lines
4.1 KiB

#!/usr/bin/php
<?php
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
//
// Author : Juan Antonio Alvarez <jualvarez@gmail.com>
// Ignacio Martin
//
// Definitions
$SEPARATOR = "\t";
$EMPTY = "NA";
// MAIN LOOP //
foreach ($argv as $filenum => $file) {
if ($filenum != 0) {
$container[$filenum] = read_snmpwalk_dump_nodups($file);
}
}
if (is_array($container) && $container[1] != NULL)
print_csv ($container, $SEPARATOR);
function read_snmpwalk_dump($file)
{
$i = $j = 0;
if (!($lines = file ($file)))
echo "ERROR: No se pudo abrir el archivo\n";
foreach ($lines as $line) {
$pos = strpos($line, "::");
$root = substr($line, 0, $pos);
$tmp_str = substr($line, $pos+2);
$pos = strpos ($tmp_str, " = ");
$leaf = substr($tmp_str, 0, $pos);
$tmp_str = substr($tmp_str, $pos+3);
$pos = strpos ($tmp_str, ": ");
$type = substr ($tmp_str, 0, $pos);
$value = substr ($tmp_str, $pos+2);
//para que sea leible por SPSS
$leaf = str_replace(".","_",$leaf);
//Sacamos el \n del final
$value = substr ($value, 0, strpos($value, "\n"));
//Agregamos " cuando no están cerradas
$pos=strpos($value,"\"");
if($pos!==false){
if ($pos == 0)
if (!strpos(substr($value, 1),"\""))
$value =sprintf("%s\"",$value);
}
//echo "root: $root leaf: $leaf type: $type value: $value\n";
if($j == 0) {
$root1 = $root;
$leaf1 = $leaf;
}
/* Typecasting de valores
* no se si tiene sentido hacerlo...
switch ($type) {
case "INTEGER":
} */
if ($i==0)
$leaves[$j] = $leaf;
if ($j!=0 && $root == $root1 && $leaf == $leaf1)
$i++;
$container[$leaf][$i] = $value;
// Si es un counter, agregar también las diferencias.
if(strripos($type, "counter") !== false) {
// Tenemos un counter! empezar las diferencias si no es el primer paso
// Rellenamos el primero con 0 para no tener quilombo
$dif_leaf = sprintf("difs_%s", $leaf);
if($i > 0)
$container[$dif_leaf][$i] = $container[$leaf][$i] - $container[$leaf][$i-1];
elseif ($i == 0)
$container[$dif_leaf][$i] = $EMPTY;
}
$j++;
}
fprintf (STDERR,"Archivo: %s. Se procesaron %d registros, se encontraron %d repeticiones.\n",$file,$j,$i+1);
return $container;
}
function read_snmpwalk_dump_nodups($file)
{
$container = read_snmpwalk_dump($file);
foreach ($container as $leaf => $data) {
$is_first = true;
foreach ($data as $value) {
if($is_first)
$old_value = $value;
if($value != $old_value && $changes[$leaf] !== true) {
$changes[$leaf] = true;
$dif_container[$leaf] = $container[$leaf];
}
$old_value = $value;
$is_first = false;
}
}
return $dif_container;
}
function print_csv($container, $separator)
{
foreach ($container as $data_array) {
$col = 0;
foreach ($data_array as $key => $table) {
$lineas[0][$col]=$key;
foreach ($table as $index => $value){
//echo "$key :: $index => $value\n";
$lineas[$index+1][$col] = $value;
}
$col++;
}
foreach ($lineas as $line_num => $linea) {
$last_col = 0;
if ($line_num != 0)
echo "\n";
foreach ($linea as $col_num => $col_data) {
if($col_num == 0)
echo "$col_data";
elseif ($col_num == $last_col + 1)
echo "$separator$col_data";
else {
for ($i = $last_col;$i<$col_num;$i++){
echo "$separator$EMPTY";
}
}
$last_col = $col_num;
}
//$csv_line = implode ($separator,$linea);
//echo "$csv_line\n";
}
}
//var_dump($lineas);
}
?>