File: snmparser.svn-base

package info (click to toggle)
sip-tester 2.0.1-1.2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,164 kB
  • ctags: 1,187
  • sloc: cpp: 12,331; ansic: 1,984; sh: 229; makefile: 184
file content (161 lines) | stat: -rw-r--r-- 4,207 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/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);
}

?>