disconnect() public method

Disconnect from RouterOS
public disconnect ( ) : void
return void
Ejemplo n.º 1
2
function collect_pppoe_users_api(&$host)
{
    $rows = array();
    $api = new RouterosAPI();
    $api->debug = false;
    $rekey_array = array('host_id', 'name', 'index', 'userType', 'serverID', 'domain', 'bytesIn', 'bytesOut', 'packetsIn', 'packetsOut', 'curBytesIn', 'curBytesOut', 'curPacketsIn', 'curPacketsOut', 'prevBytesIn', 'prevBytesOut', 'prevPacketsIn', 'prevPacketsOut', 'present', 'last_seen');
    // Put the queues into an array
    $users = array_rekey(db_fetch_assoc_prepared("SELECT \n\t\thost_id, '0' AS `index`, '1' AS userType, '0' AS serverID, SUBSTRING(name, 7) AS name, '' AS domain,\n\t\tBytesIn AS bytesIn, BytesOut AS bytesOut, PacketsIn as packetsIn, PacketsOut AS packetsOut,\n\t\tcurBytesIn, curBytesOut, curPacketsIn, curPacketsOut, \n\t\tprevBytesIn, prevBytesOut, prevPacketsIn, prevPacketsOut, present, last_seen\n\t\tFROM plugin_mikrotik_queues \n\t\tWHERE host_id = ? \n\t\tAND name LIKE 'PPPOE-%'", array($host['id'])), 'name', $rekey_array);
    $creds = db_fetch_row_prepared('SELECT * FROM plugin_mikrotik_credentials WHERE host_id = ?', array($host['id']));
    $start = microtime(true);
    if (sizeof($creds)) {
        if ($api->connect($host['hostname'], $creds['user'], $creds['password'])) {
            $api->write('/ppp/active/getall');
            $read = $api->read(false);
            $array = $api->parseResponse($read);
            $end = microtime(true);
            $sql = array();
            cacti_log('MIKROTIK RouterOS API STATS: API Returned ' . sizeof($array) . ' PPPoe Users in ' . round($end - $start, 2) . ' seconds.', false, 'SYSTEM');
            if (sizeof($array)) {
                foreach ($array as $row) {
                    if (!isset($row['name'])) {
                        continue;
                    }
                    $name = strtoupper($row['name']);
                    if (isset($users[$name])) {
                        $user = $users[$name];
                        $user['mac'] = $row['caller-id'];
                        $user['ip'] = $row['address'];
                        $user['connectTime'] = uptimeToSeconds($row['uptime']);
                        $user['host_id'] = $host['id'];
                        $user['radius'] = $row['radius'] == 'true' ? 1 : 0;
                        $user['limitBytesIn'] = $row['limit-bytes-in'];
                        $user['limitBytesOut'] = $row['limit-bytes-out'];
                        $user['userType'] = 1;
                        $sql[] = '(' . $user['host_id'] . ',' . $user['index'] . ',' . $user['userType'] . ',' . $user['serverID'] . ',' . db_qstr($user['name']) . ',' . db_qstr($user['domain']) . ',' . db_qstr($user['mac']) . ',' . db_qstr($user['ip']) . ',' . $user['connectTime'] . ',' . $user['bytesIn'] . ',' . $user['bytesOut'] . ',' . $user['packetsIn'] . ',' . $user['packetsOut'] . ',' . $user['curBytesIn'] . ',' . $user['curBytesOut'] . ',' . $user['curPacketsIn'] . ',' . $user['curPacketsOut'] . ',' . $user['prevBytesIn'] . ',' . $user['prevBytesOut'] . ',' . $user['prevPacketsIn'] . ',' . $user['prevPacketsOut'] . ',' . $user['limitBytesIn'] . ',' . $user['limitBytesOut'] . ',' . $user['radius'] . ',' . $user['present'] . ',' . db_qstr($user['last_seen']) . ')';
                    }
                }
                if (sizeof($sql)) {
                    db_execute('INSERT INTO plugin_mikrotik_users 
						(host_id, `index`, userType, serverID, name, domain, mac, ip, connectTime, 
						bytesIn, bytesOut, packetsIn, packetsOut, 
						curBytesIn, curBytesOut, curPacketsIn, curPacketsOut, 
						prevBytesIn, prevBytesOut, prevPacketsIn, prevPacketsOut, 
						limitBytesIn, limitBytesOut, radius, present, last_seen) 
						VALUES ' . implode(', ', $sql) . '
						ON DUPLICATE KEY UPDATE connectTime=VALUES(connectTime), 
						bytesIn=VALUES(bytesIn), bytesOut=VALUES(bytesOut), 
						packetsIn=VALUES(packetsIn), packetsOut=VALUES(packetsOut), 
						curBytesIn=VALUES(curBytesIn), curBytesOut=VALUES(curBytesOut),
						curPacketsIn=VALUES(curPacketsIn), curPacketsOut=VALUES(curPacketsOut),
						prevBytesIn=VALUES(prevBytesIn), prevBytesOut=VALUES(prevBytesOut),
						prevPacketsIn=VALUES(prevPacketsIn), prevPacketsOut=VALUES(prevPacketsOut),
						limitBytesIn=VALUES(limitBytesIn), limitBytesOut=VALUES(limitBytesOut),
						radius=VALUES(radius), present=VALUES(present), last_seen=VALUES(last_seen)');
                }
            }
            $idle_users = db_fetch_assoc_prepared('SELECT name 
				FROM plugin_mikrotik_users 
				WHERE last_seen < FROM_UNIXTIME(UNIX_TIMESTAMP() - ?) 
				AND present=1 
				AND host_id = ?', array(read_config_option('mikrotik_queues_freq'), $host['id']));
            db_execute_prepared('UPDATE IGNORE plugin_mikrotik_users SET
				bytesIn=0, bytesOut=0, packetsIn=0, packetsOut=0, connectTime=0,
				curBytesIn=0, curBytesOut=0, curPacketsIn=0, curPacketsOut=0,
				prevBytesIn=0, prevBytesOut=0, prevPacketsIn=0, prevPacketsOut=0, present=0
				WHERE host_id = ? 
				AND userType = 1 
				AND last_seen < FROM_UNIXTIME(UNIX_TIMESTAMP() - ?)', array($host['id'], read_config_option('mikrotik_queues_freq')));
            $api->disconnect();
        } else {
            cacti_log('ERROR:RouterOS @ ' . $host['description'] . ' Timed Out');
        }
    }
}
Ejemplo n.º 2
1
<?php

/* Example of finding registration-table ID for specified MAC */
require '../routeros_api.class.php';
$API = new RouterosAPI();
$API->debug = true;
if ($API->connect('111.111.111.111', 'LOGIN', 'PASSWORD')) {
    $ARRAY = $API->comm("/interface/wireless/registration-table/print", array(".proplist" => ".id", "?mac-address" => "00:0E:BB:DD:FF:FF"));
    print_r($ARRAY);
    $API->disconnect();
}
Ejemplo n.º 3
0
function mikrotik_host_top()
{
    global $fields_host_edit;
    $id = get_filter_request_var('id');
    $template_id = db_fetch_cell('SELECT id FROM host_template WHERE hash="d364e2b9570f166ab33c8df8bd503887"');
    $is_tik = db_fetch_row_prepared('SELECT pmc.*, host.hostname
		FROM host 
		LEFT JOIN plugin_mikrotik_credentials AS pmc 
		ON host.id=pmc.host_id 
		WHERE host_template_id = ? AND host.id = ?', array($template_id, $id));
    if (sizeof($is_tik)) {
        $fields_host_edit += array('mikrotik_head' => array('method' => 'spacer', 'collapsible' => 'true', 'friendly_name' => __('MikroTik Credentials')), 'mikrotik_user' => array('method' => 'textbox', 'friendly_name' => __('Read Only User'), 'description' => __('Provide a read only username for the MikroTik.'), 'value' => $is_tik['user'], 'max_length' => '40', 'size' => '20'), 'mikrotik_password' => array('method' => 'textbox', 'friendly_name' => __('Password'), 'description' => __('Provide the read only username password for this MikroTik.'), 'value' => $is_tik['password'], 'max_length' => '40', 'size' => '30'));
        if ($is_tik['user'] != '') {
            include_once './plugins/mikrotik/RouterOS/routeros_api.class.php';
            $api = new RouterosAPI();
            $api->debug = false;
            if ($api->connect($is_tik['hostname'], $is_tik['user'], $is_tik['password'])) {
                $api->disconnect();
                $fields_host_edit += array('mikrotik_result' => array('method' => 'other', 'friendly_name' => __('Connection Result'), 'description' => __('Ok if Cacti can connect to the Mikrotik over its API port.'), 'value' => 'Connected Successfully'));
            } else {
                $fields_host_edit += array('mikrotik_result' => array('method' => 'other', 'friendly_name' => __('Connection Result'), 'description' => __('Ok if Cacti can connect to the Mikrotik over its API port.'), 'value' => 'Connection Failed'));
            }
        }
    }
}