forked from scotepi/snmp-interfacegraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snmpGet.php
125 lines (93 loc) · 3.94 KB
/
snmpGet.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
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
<?php
session_start();
/**
Script by Scott Peters, @scotepi
Version 1.3
1.4:
- Removed Speed being part of the math, it had nothing to do with the change over time
- Fixed interfaces without a name, they will show as Interface iface
- Added an option hash to invert in & out, just add ,true to the end of the hash string
1.3:
- The forward and back buttons will now work for changing graphs & pages
1.2:
- Fixed overflow: none from hiding the host & interface list on small frames
- Disabled the last console.log
- Added another example host
1.1:
- Added support for location.search on index.htm by using
* ?hostname=host&name=My Host
* ?hostname=host&interface=4&name=My Interface
1.0:
- Inital Release
**/
// SNMPv2 Community String
$community = 'public';
$snmpVersion = SNMP::VERSION_2C; // SNMP::VERSION_2C or SNMP::VERSION_1, only tested with 2C
// 'hostname' => 'Friendly Name',
$hosts = array(
'gateway' => 'Main Gateway',
'switch' => 'Main Switch',
);
// Use ?clear=true to clear the session data for testing
if (isset($_GET['clear'])) session_unset();
if (isset($_REQUEST['hostname'])) {
$hostname = $_REQUEST['hostname'];
$snmp = new snmp($snmpVersion, $hostname, $community);
$snmp->oid_output_format = SNMP_OID_OUTPUT_FULL;
$snmp->valueretrieval = SNMP_VALUE_PLAIN;
if (isset($_REQUEST['interface'])) {
$interface = $_REQUEST['interface'];
// Old Values
$prev = $_SESSION[$hostname][$interface]['data'];
// Current Values
$time = time();
$ifInOctets = $snmp->get('.1.3.6.1.2.1.2.2.1.10.' . $interface);
$ifOutOctets = $snmp->get('.1.3.6.1.2.1.2.2.1.16.' . $interface);
// Over Time
$timeDiff = $time - $prev['time'];
$inDiff = $ifInOctets - $prev['ifInOctets'];
$outDiff = $ifOutOctets - $prev['ifOutOctets'];
$data = array(
'ifInOctets' => $ifInOctets,
'ifOutOctets' => $ifOutOctets,
'time' => $time,
'timeDiff' => $timeDiff,
'inDiff' => $inDiff,
'outDiff' => $outDiff,
);
if ($inDiff != 0 and $outDiff != 0) {
$_SESSION[$hostname][$interface]['data'] = $data;
}
@header("Content-Type: application/json; charset=UTF-8");
echo json_encode($_SESSION[$hostname][$interface]);
die();
} else {
$interfaceNames = $snmp->walk('.iso.3.6.1.2.1.31.1.1.1', true);
if (!isset($_SESSION[$hostname])) $_SESSION[$hostname] = array();
$interfaces = array();
foreach ($interfaceNames as $mibs=>$value) {
list($mib, $if) = explode('.', $mibs);
switch ($mib) {
case 1: $_SESSION[$hostname][$if]['interface'] = $value; break;
case 18: $_SESSION[$hostname][$if]['name'] = ($value) ? $value : "Interface " . $_SESSION[$hostname][$if]['interface']; break;
}
$_SESSION[$hostname][$if]['host'] = $hosts[$hostname];
$_SESSION[$hostname][$if]['data'] = array(
'ifInOctets' => 0,
'ifOutOctets' => 0,
'time' => time(),
);
if (isset($_SESSION[$hostname][$if]['name']) and !empty($_SESSION[$hostname][$if]['name'])) {
$interfaces[$if] = $_SESSION[$hostname][$if]['interface'] . ': ' . $_SESSION[$hostname][$if]['name'];
}
}
@header("Content-Type: application/json; charset=UTF-8");
echo json_encode($interfaces);
die();
}
$snmp->close();
} else {
@header("Content-Type: application/json; charset=UTF-8");
echo json_encode($hosts);
die();
}