Esempio n. 1
0
 /**
  * Returns array of MAC=>Signal data for some MikroTik/UBNT device
  * 
  * @param string $ip
  * @param string $community
  * @return array
  */
 function deviceQuery($ip, $community)
 {
     $oid = '.1.3.6.1.4.1.14988.1.1.1.2.1.3';
     $mask_mac = false;
     $ubnt_shift = 0;
     $result = array();
     $rawsnmp = array();
     $snmp = new SNMPHelper();
     $snmp->setBackground(false);
     $snmp->setMode('native');
     $tmpSnmp = $snmp->walk($ip, $community, $oid, false);
     // Returned string '.1.3.6.1.4.1.14988.1.1.1.2.1.3 = '
     // in AirOS 5.6 and newer
     if ($tmpSnmp === "{$oid} = ") {
         $oid = '.1.3.6.1.4.1.41112.1.4.7.1.3.1';
         $tmpSnmp = $snmp->walk($ip, $community, $oid, false);
         $ubnt_shift = 1;
     }
     if (!empty($tmpSnmp) and $tmpSnmp !== "{$oid} = ") {
         $explodeData = explodeRows($tmpSnmp);
         if (!empty($explodeData)) {
             foreach ($explodeData as $io => $each) {
                 $explodeRow = explode(' = ', $each);
                 if (isset($explodeRow[1])) {
                     $rawsnmp[$explodeRow[0]] = $explodeRow[1];
                 }
             }
         }
     }
     if (!empty($rawsnmp)) {
         if (is_array($rawsnmp)) {
             foreach ($rawsnmp as $indexOID => $rssi) {
                 $oidarray = explode(".", $indexOID);
                 $end_num = sizeof($oidarray) + $ubnt_shift;
                 $mac = '';
                 for ($counter = 2; $counter < 8; $counter++) {
                     $temp = sprintf('%02x', $oidarray[$end_num - $counter]);
                     if ($counter < 5 && $mask_mac) {
                         $mac = ":xx{$mac}";
                     } else {
                         if ($counter == 7) {
                             $mac = "{$temp}{$mac}";
                         } else {
                             $mac = ":{$temp}.{$mac}";
                         }
                     }
                 }
                 $mac = str_replace('.', '', $mac);
                 $mac = trim($mac);
                 $rssi = str_replace('INTEGER:', '', $rssi);
                 $rssi = trim($rssi);
                 $result[$mac] = $rssi;
             }
         }
     }
     return $result;
 }
Esempio n. 2
0
/**
 * Show data for some device
 * 
 * @param   $ip device ip
 * @param   $community snmp community
 * @param   $alltemplates all of snmp templates
 * @param   $quiet  no output
 * 
 * @return  void
 */
function sp_SnmpPollDevice($ip, $community, $alltemplates, $deviceTemplate, $allusermacs, $alladdress, $quiet = false)
{
    global $ubillingConfig;
    if (isset($alltemplates[$deviceTemplate])) {
        $currentTemplate = $alltemplates[$deviceTemplate];
        if (!empty($currentTemplate)) {
            $deviceDescription = $currentTemplate['define']['DEVICE'];
            $deviceFdb = $currentTemplate['define']['FDB'];
            $sectionResult = '';
            $sectionName = '';
            $finalResult = '';
            $tempArray = array();
            $alterCfg = $ubillingConfig->getAlter();
            $snmp = new SNMPHelper();
            //selecting FDB processing mode
            if (isset($currentTemplate['define']['FDB_MODE'])) {
                $deviceFdbMode = $currentTemplate['define']['FDB_MODE'];
            } else {
                $deviceFdbMode = 'default';
            }
            //selecting FDB ignored port for skipping MAC-s on it
            if (isset($currentTemplate['define']['FDB_IGNORE_PORTS'])) {
                $deviceFdbIgnore = $currentTemplate['define']['FDB_IGNORE_PORTS'];
                $deviceFdbIgnore = explode(',', $deviceFdbIgnore);
                $deviceFdbIgnore = array_flip($deviceFdbIgnore);
            } else {
                $deviceFdbIgnore = array();
            }
            //parse each section of template
            foreach ($alltemplates[$deviceTemplate] as $section => $eachpoll) {
                if ($section != 'define') {
                    if (!$quiet) {
                        $finalResult .= wf_tag('div', false, 'dashboard', '');
                    }
                    $sectionName = $eachpoll['NAME'];
                    $sectionOids = explode(',', $eachpoll['OIDS']);
                    $sectionParser = $eachpoll['PARSER'];
                    $sectionResult = '';
                    //now parse each oid
                    foreach ($sectionOids as $eachOid) {
                        $eachOid = trim($eachOid);
                        $rawData = $snmp->walk($ip, $community, $eachOid, true);
                        $rawData = str_replace('"', '`', $rawData);
                        $parseCode = '$sectionResult.=' . $sectionParser . '("' . $rawData . '");';
                        eval($parseCode);
                    }
                    if (!$quiet) {
                        $finalResult .= wf_tag('div', false, 'dashtask', '') . wf_tag('strong') . __($sectionName) . wf_tag('strong', true) . '<br>';
                        $finalResult .= $sectionResult . wf_tag('div', true);
                    }
                }
            }
            $finalResult .= wf_tag('div', true);
            $finalResult .= wf_tag('div', false, '', 'style="clear:both;"');
            $finalResult .= wf_tag('div', true);
            if (!$quiet) {
                show_window('', $finalResult);
            }
            //
            //parsing data from FDB table
            //
            if ($deviceFdb == 'true') {
                $portData = array();
                $snmp->setBackground(false);
                // need to process data with system + background
                if ($deviceFdbMode == 'default') {
                    //default zyxel & cisco port table
                    $portTable = $snmp->walk($ip, $community, '.1.3.6.1.2.1.17.4.3.1.2', true);
                } else {
                    if ($deviceFdbMode == 'dlp') {
                        //custom dlink port table with VLANS
                        $portTable = $snmp->walk($ip, $community, '.1.3.6.1.2.1.17.7.1.2.2.1.2', true);
                    }
                }
                if (!empty($portTable)) {
                    if ($deviceFdbMode == 'default') {
                        //default FDB parser
                        $portData = sp_SnmpParseFDB($portTable);
                    } else {
                        if ($deviceFdbMode == 'dlp') {
                            //exotic dlink parser
                            $portData = sp_SnmpParseFdbDl($portTable);
                        }
                    }
                    //skipping some port data if FDB_IGNORE_PORTS option is set
                    if (!empty($deviceFdbIgnore)) {
                        if (!empty($portData)) {
                            foreach ($portData as $some_mac => $some_port) {
                                if (!isset($deviceFdbIgnore[$some_port])) {
                                    $tempArray[$some_mac] = $some_port;
                                }
                            }
                            $portData = $tempArray;
                        }
                    }
                    $fdbCache = serialize($portData);
                    file_put_contents('exports/' . $ip . '_fdb', $fdbCache);
                }
                //show port data User friendly :)
                if (!empty($portData)) {
                    //extracting all needed data for switchport control
                    if ($alterCfg['SWITCHPORT_IN_PROFILE']) {
                        $allswitchesArray = zb_SwitchesGetAll();
                        $allportassigndata = array();
                        $allportassigndata_q = "SELECT * from `switchportassign`;";
                        $allportassigndata_raw = simple_queryall($allportassigndata_q);
                        if (!empty($allportassigndata_raw)) {
                            foreach ($allportassigndata_raw as $iopd => $eachpad) {
                                $allportassigndata[$eachpad['login']] = $eachpad;
                            }
                        }
                    }
                    $allusermacs = array_flip($allusermacs);
                    $cells = wf_TableCell(__('User'), '30%');
                    $cells .= wf_TableCell(__('MAC'));
                    $cells .= wf_TableCell(__('Ports'));
                    $rows = wf_TableRow($cells, 'row1');
                    foreach ($portData as $eachMac => $eachPort) {
                        //user detection
                        if (isset($allusermacs[$eachMac])) {
                            $userLogin = $allusermacs[$eachMac];
                            @($useraddress = $alladdress[$userLogin]);
                            $userlink = wf_Link('?module=userprofile&username='******' ' . $useraddress, false);
                            //switch port assing form
                            if ($alterCfg['SWITCHPORT_IN_PROFILE']) {
                                $assignForm = wf_modal(web_edit_icon(__('Switch port assign')), __('Switch port assign'), web_SnmpSwitchControlForm($userLogin, $allswitchesArray, $allportassigndata, @$_GET['switchid'], $eachPort), '', '500', '250');
                                if (isset($allportassigndata[$userLogin])) {
                                    $assignForm .= wf_img('skins/arrow_right_green.png') . @$allportassigndata[$userLogin]['port'];
                                }
                            } else {
                                $assignForm = '';
                            }
                        } else {
                            $userlink = '';
                            $assignForm = '';
                        }
                        $cells = wf_TableCell($userlink . $assignForm, '', '', 'sorttable_customkey="' . $eachPort . '"');
                        $cells .= wf_TableCell($eachMac);
                        $cells .= wf_TableCell($eachPort);
                        $rows .= wf_TableRow($cells, 'row3');
                    }
                    if (!$quiet) {
                        show_window(__('FDB'), wf_TableBody($rows, '100%', '0', 'sortable'));
                    }
                }
            }
        }
    }
}