-
Notifications
You must be signed in to change notification settings - Fork 1
/
snmputils.php
88 lines (80 loc) · 2.9 KB
/
snmputils.php
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
<?php
class snmp_querier {
private $ip;
private $community;
private $type;
private $version;
function __construct($ip, $community, $version) {
$this->ip = $ip;
$this->community = $community;
$this->version = $version;
}
function walk($oid) {
// we're using realwalk so that we can build iterators with map_oids(), see:
// walk [0] => eth0
// realwalk [IF-MIB::ifDescr.2] => eth0
if($this->version == "1") {
$snmp_result = snmprealwalk($this->ip, $this->community, $oid);
} else if($this->version == "2c") {
$snmp_result = snmp2_real_walk($this->ip, $this->community, $oid);
}
$result = $this->clear_snmp_string($snmp_result);
return $result;
}
function get($oid) {
if($this->version == "1") {
$snmp_result = snmpget($this->ip, $this->community, $oid);
} else if($this->version == "2c") {
$snmp_result = snmp2_get($this->ip, $this->community, $oid);
}
$result = $this->clear_snmp_string($snmp_result);
return $result;
}
function get_oids($query) {
// this function builds iterators for snmp gets starting from a walk, e.g.
// IF-MIB::ifDescr.1 and IF-MIB::ifDescr.3 --> [0] => 1, [1] => 3
foreach($query as $oid=>$value) {
$oid_split = explode(".", $oid);
$oidlist[] = $oid_split[count($oid_split)-1];
}
return $oidlist;
}
function map_oids($query) {
// this function maps the name to the last OID digit, e.g.:
// [IF-MIB::ifDescr.1] => eth0, [IF-MIB::ifDescr.3] => eth1
// [eth0] => 1, [eth1] => 3
// just like get_oids() this turns useful for iterating over items
// we're swapping keys with their values and doing some cleanup..
$oidmap = array_flip($query);
foreach($oidmap as $item=>&$oid) {
$oid_split = explode(".", $oid);
$oid = $oid_split[count($oid_split)-1];
}
return $oidmap;
}
function get_uptime_from_timeticks($str) {
$re = "/Timeticks: \\((.*)\\) (.*)\\./";
preg_match_all($re, $str, $matches);
return $matches[2][0];
}
function clear_snmp_string($data) {
// clean this all up (filter?)
if(is_array($data)) {
foreach($data as $key=>&$val) {
$val = str_replace('STRING: ', '', $val);
$val = str_replace('Counter32: ', '', $val);
$val = str_replace('"', '', $val);
$val = trim($val);
}
}
else {
$data = str_replace('STRING: ', '', $data);
$data = str_replace('INTEGER: ', '', $data);
$data = str_replace('Counter32: ', '', $data);
$data = str_replace('"', '', $data);
$data = trim($data);
}
return $data;
}
}
?>