-
Notifications
You must be signed in to change notification settings - Fork 1
/
rssi.php
47 lines (40 loc) · 1.2 KB
/
rssi.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
<?php
$hostname = $_GET['host'];
$community = 'hamwan';
$oidrssi = '1.3.6.1.4.1.14988.1.1.1.1.1.4.2';
$oidrssiac = '1.3.6.1.4.1.14988.1.1.1.1.1.4.3';
$output = array(
'host' => $hostname,
'rssi' => NULL,
);
function snmpint($snmpstr) {
return (int) explode(' ', $snmpstr)[1];
}
function calculate_median($arr) {
$count = count($arr); //total numbers in array
$middleval = floor(($count-1)/2); // find the middle value, or the lowest middle value
if($count % 2) { // odd number, middle is the median
$median = $arr[$middleval];
} else { // even number, calculate avg of 2 medians
$low = $arr[$middleval];
$high = $arr[$middleval+1];
$median = (($low+$high)/2);
}
return $median;
}
// try each oid
foreach (array($oidrssiac, $oidrssi) as $oid) {
$rssi = snmpget($hostname, $community, $oid);
if ($rssi) {
// smoothing
$avg[0] = snmpint($rssi);
for ($i=1; $i<3; $i++) {
usleep(200000);
$avg[$i] = snmpint(snmpget($hostname, $community, $oid));
}
$output['rssi'] = calculate_median($avg);
break;
}
}
header('Content-Type: application/json');
echo json_encode($output);