示例#1
0
function mikrotik_devices()
{
    global $config, $colors, $item_rows;
    /* ================= input validation ================= */
    input_validate_input_number(get_request_var_request('status'));
    input_validate_input_number(get_request_var_request('page'));
    input_validate_input_number(get_request_var_request('rows'));
    /* ==================================================== */
    /* clean up sort_column */
    if (isset($_REQUEST['sort_column'])) {
        $_REQUEST['sort_column'] = sanitize_search_string(get_request_var_request('sort_column'));
    }
    /* clean up sort_direction */
    if (isset($_REQUEST['sort_direction'])) {
        $_REQUEST['sort_direction'] = sanitize_search_string(get_request_var_request('sort_direction'));
    }
    /* clean up filter string */
    if (isset($_REQUEST['filter'])) {
        $_REQUEST['filter'] = sanitize_search_string(get_request_var_request('filter'));
    }
    if (isset($_REQUEST['reset'])) {
        kill_session_var('sess_mikrotik_device_sort_column');
        kill_session_var('sess_mikrotik_device_sort_direction');
        kill_session_var('sess_mikrotik_device_status');
        kill_session_var('sess_mikrotik_device_filter');
        kill_session_var('sess_default_rows');
        kill_session_var('sess_mikrotik_device_current_page');
    } elseif (isset($_REQUEST['clear'])) {
        kill_session_var('sess_mikrotik_device_sort_column');
        kill_session_var('sess_mikrotik_device_sort_direction');
        kill_session_var('sess_mikrotik_device_status');
        kill_session_var('sess_mikrotik_device_filter');
        kill_session_var('sess_default_rows');
        kill_session_var('sess_mikrotik_device_current_page');
        unset($_REQUEST['sort_column']);
        unset($_REQUEST['sort_direction']);
        unset($_REQUEST['status']);
        unset($_REQUEST['filter']);
        unset($_REQUEST['rows']);
        unset($_REQUEST['page']);
    } else {
        /* if any of the settings changed, reset the page number */
        $changed = false;
        $changed += mikrotik_check_changed('status', 'sess_mikrotik_device_status');
        $changed += mikrotik_check_changed('filter', 'sess_mikrotik_device_filter');
        $changed += mikrotik_check_changed('rows', 'sess_default_rows');
        if ($changed) {
            $_REQUEST['page'] = '1';
        }
    }
    load_current_session_value('page', 'sess_mikrotik_device_current_page', '1');
    load_current_session_value('rows', 'sess_default_rows', read_config_option('num_rows_table'));
    load_current_session_value('sort_column', 'sess_mikrotik_device_sort_column', 'description');
    load_current_session_value('sort_direction', 'sess_mikrotik_device_sort_direction', 'ASC');
    load_current_session_value('status', 'sess_mikrotik_device_status', '-1');
    load_current_session_value('filter', 'sess_mikrotik_device_filter', '');
    ?>
	<script type='text/javascript'>
	function applyFilter() {
		strURL  = '?action=devices';
		strURL += '&status='   + $('#status').val();
		strURL += '&filter='   + $('#filter').val();
		strURL += '&rows='     + $('#rows').val();
		strURL += '&header=false';
		loadPageNoHeader(strURL);
	}

	function clearFilter() {
		strURL  = '?action=devices&clear=&header=false';
		loadPageNoHeader(strURL);
	}

	$(function() {
		$('#form_devices').submit(function(event) {
			event.preventDefault();
			applyFilter();
		});
	});
	</script>
	<?php 
    html_start_box('<strong>Device Filter</strong>', '100%', $colors['header'], '3', 'center', '');
    ?>
	<tr class='even noprint'>
		<td>
		<form id='form_devices' action='mikrotik.php?action=devices'>
			<table class='filterTable'>
				<tr>
					<td>
						Search
					</td>
					<td>
						<input id='filter' type='text' size='25' value='<?php 
    print get_request_var_request('filter');
    ?>
'>
					</td>
					<td>
						Status
					</td>
					<td>
						<select id='status' onChange='applyFilter()'>
							<option value='-1'<?php 
    if (get_request_var_request('type') == '-1') {
        ?>
 selected<?php 
    }
    ?>
>All</option>
							<?php 
    $statuses = db_fetch_assoc('SELECT DISTINCT status
								FROM host
								INNER JOIN plugin_mikrotik_system
								ON host.id=plugin_mikrotik_system.host_id');
    $statuses = array_merge($statuses, array('-2' => array('status' => '-2')));
    if (sizeof($statuses)) {
        foreach ($statuses as $s) {
            switch ($s['status']) {
                case '0':
                    $status = 'Unknown';
                    break;
                case '1':
                    $status = 'Down';
                    break;
                case '2':
                    $status = 'Recovering';
                    break;
                case '3':
                    $status = 'Up';
                    break;
                case '-2':
                    $status = 'Disabled';
                    break;
            }
            echo "<option value='" . $s['status'] . "' " . (get_request_var_request('status') == $s['status'] ? 'selected' : '') . '>' . $status . '</option>';
        }
    }
    ?>
						</select>
					</td>
					<td>
						Devices
					</td>
					<td>
						<select id='rows' onChange='applyFilter()'>
							<option value='-1'<?php 
    if (get_request_var_request('rows') == '-1') {
        ?>
 selected<?php 
    }
    ?>
>Default</option>
							<?php 
    if (sizeof($item_rows)) {
        foreach ($item_rows as $key => $name) {
            echo "<option value='" . $key . "' " . (get_request_var_request('rows') == $key ? 'selected' : '') . '>' . $name . '</option>';
        }
    }
    ?>
						</select>
					</td>
					<td>
						<input id='refresh' type='button' onClick='applyFilter()' value='Go'>
					</td>
					<td>
						<input id='clear' type='button' onClick='clearFilter()' value='Clear'>
					</td>
				</tr>
			</table>
			<input type='hidden' id='page' name='page' value='<?php 
    print $_REQUEST['page'];
    ?>
'>
			</form>
		</td>
	</tr>
	<?php 
    html_end_box();
    html_start_box('', '100%', $colors['header'], '3', 'center', '');
    if ($_REQUEST['rows'] == '-1') {
        $num_rows = read_config_option('num_rows_table');
    } else {
        $num_rows = get_request_var_request('rows');
    }
    $limit = ' LIMIT ' . $num_rows * (get_request_var_request('page') - 1) . ',' . $num_rows;
    $sql_where = '';
    if ($_REQUEST['status'] != '-1') {
        $sql_where .= (strlen($sql_where) ? ' AND' : 'WHERE') . ' hrs.host_status=' . $_REQUEST['status'];
    }
    $sql_join = '';
    if ($_REQUEST['filter'] != '') {
        $sql_where .= (strlen($sql_where) ? ' AND' : 'WHERE') . " h.description LIKE '%" . $_REQUEST['filter'] . "%' OR\n\t\t\th.hostname LIKE '%" . $_REQUEST['filter'] . "%'";
    }
    $sql = "SELECT hrs.*, h.hostname, h.description, h.disabled, trees.trees, queues.queues\n\t\tFROM plugin_mikrotik_system AS hrs\n\t\tINNER JOIN host AS h \n\t\tON h.id=hrs.host_id\n\t\tLEFT JOIN (SELECT host_id AS hid, count(*) AS trees FROM plugin_mikrotik_trees GROUP BY host_id) AS trees\n\t\tON trees.hid=hrs.host_id\n\t\tLEFT JOIN (SELECT host_id AS hid, count(*) AS queues FROM plugin_mikrotik_queues GROUP BY host_id) AS queues\n\t\tON queues.hid=hrs.host_id\n\t\t{$sql_join}\n\t\t{$sql_where}\n\t\tORDER BY " . get_request_var_request("sort_column") . " " . get_request_var_request("sort_direction") . " " . $limit;
    //echo $sql;
    $rows = db_fetch_assoc($sql);
    $total_rows = db_fetch_cell("SELECT COUNT(*)\n\t\tFROM plugin_mikrotik_system AS hrs\n\t\tINNER JOIN host AS h\n\t\tON h.id=hrs.host_id\n\t\t{$sql_join}\n\t\t{$sql_where}");
    $nav = html_nav_bar('mikrotik.php?action=devices', MAX_DISPLAY_PAGES, get_request_var_request('page'), $num_rows, $total_rows, 15, 'Devices', 'page', 'main');
    print $nav;
    $display_text = array('nosort' => array('display' => 'Actions', 'sort' => 'ASC', 'align' => 'left'), 'description' => array('display' => 'Name', 'sort' => 'ASC', 'align' => 'left'), 'host_status' => array('display' => 'Status', 'sort' => 'DESC', 'align' => 'center'), 'firmwareVersion' => array('display' => 'FW Ver', 'sort' => 'DESC', 'align' => 'right'), 'licVersion' => array('display' => 'Lic Ver', 'sort' => 'DESC', 'align' => 'right'), 'uptime' => array('display' => 'Uptime(d:h:m)', 'sort' => 'DESC', 'align' => 'right'), 'trees' => array('display' => 'Trees', 'sort' => 'DESC', 'align' => 'right'), 'users' => array('display' => 'Users', 'sort' => 'DESC', 'align' => 'right'), 'cpuPercent' => array('display' => 'CPU %', 'sort' => 'DESC', 'align' => 'right'), 'numCpus' => array('display' => 'CPUs', 'sort' => 'DESC', 'align' => 'right'), 'processes' => array('display' => 'Processes', 'sort' => 'DESC', 'align' => 'right'), 'memSize' => array('display' => 'Total Mem', 'sort' => 'DESC', 'align' => 'right'), 'memUsed' => array('display' => 'Used Mem', 'sort' => 'DESC', 'align' => 'right'), 'diskSize' => array('display' => 'Total Disk', 'sort' => 'DESC', 'align' => 'right'), 'diskUsed' => array('display' => 'Used Disk', 'sort' => 'DESC', 'align' => 'right'));
    html_header_sort($display_text, get_request_var_request('sort_column'), get_request_var_request('sort_direction'), 'false');
    /* set some defaults */
    $url = $config['url_path'] . 'plugins/mikrotik/mikrotik.php';
    $users = $config['url_path'] . 'plugins/mikrotik/images/view_users.gif';
    $usersn = $config['url_path'] . 'plugins/mikrotik/images/view_users_none.gif';
    $host = $config['url_path'] . 'plugins/mikrotik/images/view_hosts.gif';
    $trees = $config['url_path'] . 'plugins/mikrotik/images/view_trees.gif';
    $treesn = $config['url_path'] . 'plugins/mikrotik/images/view_trees_none.gif';
    $queues = $config['url_path'] . 'plugins/mikrotik/images/view_queues.gif';
    $queuesn = $config['url_path'] . 'plugins/mikrotik/images/view_queues_none.gif';
    $interfaces = $config['url_path'] . 'plugins/mikrotik/images/view_interfaces.gif';
    $dashboard = $config['url_path'] . 'plugins/mikrotik/images/view_dashboard.gif';
    $graphs = $config['url_path'] . 'plugins/mikrotik/images/view_graphs.gif';
    $nographs = $config['url_path'] . 'plugins/mikrotik/images/view_graphs_disabled.gif';
    $hcpudq = read_config_option('mikrotik_dq_host_cpu');
    if (sizeof($rows)) {
        foreach ($rows as $row) {
            $days = intval($row['uptime'] / (60 * 60 * 24 * 100));
            $remainder = $row['uptime'] % (60 * 60 * 24 * 100);
            $hours = intval($remainder / (60 * 60 * 100));
            $remainder = $remainder % (60 * 60 * 100);
            $minutes = intval($remainder / (60 * 100));
            $found = db_fetch_cell('SELECT COUNT(*) FROM graph_local WHERE host_id=' . $row['host_id']);
            form_alternate_row();
            echo "<td style='white-space:nowrap;min-width:115px;text-align:left;'>";
            //echo "<a style='padding:1px;' href='" . htmlspecialchars("$url?action=dashboard&reset=1&device=" . $row["host_id"]) . "'><img src='$dashboard' title='View Dashboard' align='absmiddle' border='0'></a>";
            if ($row['users'] > 0) {
                echo "<a class='hyperLink' href='" . htmlspecialchars("{$url}?action=users&reset=1&device=" . $row['host_id']) . "'><img src='{$users}' title='View Users' align='absmiddle' border='0' alt=''></a>";
            } else {
                echo "<img style='border:0px;padding:3px;' src='{$usersn}' title='No Users Found' align='absmiddle' alt=''>";
            }
            if ($row['queues'] > 0) {
                echo "<a class='hyperLink' href='" . htmlspecialchars("{$url}?action=queues&reset=1&device=" . $row['host_id']) . "'><img src='{$queues}' title='View Simple Queue' align='absmiddle' border='0' alt=''></a>";
            } else {
                echo "<img style='border:0px;padding:3px;' src='{$queuesn}' title='No Simple Queues Found' align='absmiddle' alt=''>";
            }
            if ($row['trees'] > 0) {
                echo "<a class='hyperLink' href='" . htmlspecialchars("{$url}?action=trees&reset=1&device=" . $row['host_id']) . "'><img src='{$trees}' title='View Queue Trees' align='absmiddle' border='0' alt=''></a>";
            } else {
                echo "<img style='border:0px;padding:3px;' src='{$treesn}' title='No Queue Trees Found' align='absmiddle' alt=''>";
            }
            echo "<a class='hyperLink' href='" . htmlspecialchars("{$url}?action=interfaces&reset=1&device=" . $row['host_id']) . "'><img src='{$interfaces}' title='View Interfaces' align='absmiddle' border='0' alt=''></a>";
            if ($found) {
                echo "<a class='hyperLink' href='" . htmlspecialchars("{$url}?action=graphs&reset=1&host_id=" . $row['host_id'] . "&style=selective&graph_add=&graph_list=&graph_template_id=0&filter=") . "'><img  src='{$graphs}' title='View Graphs' align='absmiddle' border='0' alt=''></a>";
            } else {
                echo "<img src='{$nographs}' title='No Graphs Defined' align='absmiddle' border='0'>";
            }
            $graph_cpu = mikrotik_get_graph_url($hcpudq, $row['host_id'], '', $row['numCpus'], false);
            $graph_cpup = mikrotik_get_graph_template_url(mikrotik_template_by_hash('7df474393f58bae8e8d6b85f10efad71'), $row['host_id'], round($row['cpuPercent'], 2), false);
            $graph_users = mikrotik_get_graph_template_url(mikrotik_template_by_hash('99e37ff13139f586d257ba9a637d7340'), $row['host_id'], empty($rows['users']) ? '-' : $row['users'], false);
            $graph_aproc = mikrotik_get_graph_template_url(mikrotik_template_by_hash('e797d967db24fd86341a8aa8c60fa9e0'), $row['host_id'], $row['host_status'] < 2 ? 'N/A' : $row['processes'], false);
            $graph_disk = mikrotik_get_graph_template_url(mikrotik_template_by_hash('0ece13b90785aa04d1f554a093685948'), $row['host_id'], $row['host_status'] < 2 ? 'N/A' : round($row['diskUsed'], 2), false);
            $graph_mem = mikrotik_get_graph_template_url(mikrotik_template_by_hash('4396ae857c4f9bc5ed1f26b5361e42d9'), $row['host_id'], $row['host_status'] < 2 ? 'N/A' : round($row['memUsed'], 2), false);
            $graph_upt = mikrotik_get_graph_template_url(mikrotik_template_by_hash('7d8dc3050621a2cb937cac3895bc5d5b'), $row['host_id'], $row['host_status'] < 2 ? 'N/A' : mikrotik_format_uptime($days, $hours, $minutes), false);
            if (api_plugin_user_realm_auth('host.php')) {
                $host_url = "<a class='hyperLink' href='" . htmlspecialchars($config['url_path'] . 'host.php?action=edit&id=' . $row['host_id']) . "' title='Edit Hosts'>" . $row['hostname'] . '</a>';
            } else {
                $host_url = $row['hostname'];
            }
            echo '</td>';
            echo "<td style='text-align:left;white-space:nowrap;'><strong>" . (strlen($_REQUEST['filter']) ? preg_replace('/(' . preg_quote(get_request_var_request('filter'), '/') . ')/i', "<span class='filteredValue'>\\1</span>", $row['description'] . '</strong> [' . $host_url . ']') : $row['description'] . '</strong> [' . $host_url . ']') . '</td>';
            echo "<td style='text-align:center;'>" . get_colored_device_status($row['disabled'] == 'on' ? true : false, $row['host_status']) . '</td>';
            echo "<td style='text-align:right;'>" . $row['firmwareVersion'] . '</td>';
            echo "<td style='text-align:right;'>" . $row['licVersion'] . '</td>';
            echo "<td style='text-align:right;'>" . $graph_upt . '</td>';
            echo "<td style='text-align:right;'>" . (!empty($row['trees']) ? $row['trees'] : '-') . '</td>';
            echo "<td style='text-align:right;'>" . $graph_users . '</td>';
            echo "<td style='text-align:right;'>" . ($row['host_status'] < 2 ? 'N/A' : $graph_cpup) . '</td>';
            echo "<td style='text-align:right;'>" . ($row['host_status'] < 2 ? 'N/A' : $graph_cpu) . '</td>';
            echo "<td style='text-align:right;'>" . $graph_aproc . '</td>';
            echo "<td style='text-align:right;'>" . mikrotik_memory($row['memSize']) . '</td>';
            echo "<td style='text-align:right;'>" . $graph_mem . ' %</td>';
            echo "<td style='text-align:right;'>" . mikrotik_memory($row['diskSize']) . '</td>';
            echo "<td style='text-align:right;'>" . $graph_disk . ' %</td>';
            form_end_row();
        }
        print $nav;
    } else {
        print '<tr><td colspan="5"><em>No Devices Found</em></td></tr>';
    }
    html_end_box();
}
示例#2
0
function mikrotik_devices()
{
    global $config, $colors, $item_rows;
    /* ================= input validation ================= */
    input_validate_input_number(get_request_var_request("status"));
    input_validate_input_number(get_request_var_request("page"));
    input_validate_input_number(get_request_var_request("rows"));
    /* ==================================================== */
    /* clean up sort_column */
    if (isset($_REQUEST["sort_column"])) {
        $_REQUEST["sort_column"] = sanitize_search_string(get_request_var("sort_column"));
    }
    /* clean up sort_direction */
    if (isset($_REQUEST["sort_direction"])) {
        $_REQUEST["sort_direction"] = sanitize_search_string(get_request_var("sort_direction"));
    }
    /* clean up filter string */
    if (isset($_REQUEST["filter"])) {
        $_REQUEST["filter"] = sanitize_search_string(get_request_var("filter"));
    }
    if (isset($_REQUEST["reset"])) {
        kill_session_var("sess_mikrotik_device_sort_column");
        kill_session_var("sess_mikrotik_device_sort_direction");
        kill_session_var("sess_mikrotik_device_status");
        kill_session_var("sess_mikrotik_device_filter");
        kill_session_var("sess_mikrotik_device_rows");
        kill_session_var("sess_mikrotik_device_current_page");
    } elseif (isset($_REQUEST["clear"])) {
        kill_session_var("sess_mikrotik_device_sort_column");
        kill_session_var("sess_mikrotik_device_sort_direction");
        kill_session_var("sess_mikrotik_device_status");
        kill_session_var("sess_mikrotik_device_filter");
        kill_session_var("sess_mikrotik_device_rows");
        kill_session_var("sess_mikrotik_device_current_page");
        unset($_REQUEST["sort_column"]);
        unset($_REQUEST["sort_direction"]);
        unset($_REQUEST["status"]);
        unset($_REQUEST["filter"]);
        unset($_REQUEST["rows"]);
        unset($_REQUEST["page"]);
    } else {
        /* if any of the settings changed, reset the page number */
        $changed = false;
        $changed += mikrotik_check_changed("status", "sess_mikrotik_device_status");
        $changed += mikrotik_check_changed("fitler", "sess_mikrotik_device_filter");
        $changed += mikrotik_check_changed("rows", "sess_mikrotik_device_rows");
        if ($changed) {
            $_REQUEST["page"] = "1";
        }
    }
    load_current_session_value("page", "sess_mikrotik_device_current_page", "1");
    load_current_session_value("rows", "sess_mikrotik_device_rows", "-1");
    load_current_session_value("sort_column", "sess_mikrotik_device_sort_column", "description");
    load_current_session_value("sort_direction", "sess_mikrotik_device_sort_direction", "ASC");
    load_current_session_value("status", "sess_mikrotik_device_status", "-1");
    load_current_session_value("filter", "sess_mikrotik_device_filter", "");
    ?>
	<script type="text/javascript">
	<!--
	function applyHostFilter(objForm) {
		strURL = '?action=devices';
		strURL = strURL + '&status='   + objForm.status.value;
		strURL = strURL + '&filter='   + objForm.filter.value;
		strURL = strURL + '&rows='     + objForm.rows.value;
		document.location = strURL;
	}

	function clearHosts() {
		strURL = '?action=devices&clear=';
		document.location = strURL;
	}
	-->
	</script>
	<?php 
    html_start_box("<strong>Device Filter</strong>", "100%", $colors["header"], "3", "center", "");
    ?>
	<tr bgcolor="#<?php 
    print $colors["panel"];
    ?>
">
		<td>
			<form name="devices" action="mikrotik.php?action=devices">
			<table cellpadding="0" cellspacing="0">
				<tr>
					<td nowrap style='white-space: nowrap;' width="60">
						&nbsp;Status:&nbsp;
					</td>
					<td width="1">
						<select name="status" onChange="applyHostFilter(document.devices)">
							<option value="-1"<?php 
    if (get_request_var_request("type") == "-1") {
        ?>
 selected<?php 
    }
    ?>
>All</option>
							<?php 
    $statuses = db_fetch_assoc("SELECT DISTINCT status\n\t\t\t\t\t\t\t\tFROM host\n\t\t\t\t\t\t\t\tINNER JOIN plugin_mikrotik_system\n\t\t\t\t\t\t\t\tON host.id=plugin_mikrotik_system.host_id");
    $statuses = array_merge($statuses, array("-2" => array("status" => "-2")));
    if (sizeof($statuses)) {
        foreach ($statuses as $s) {
            switch ($s["status"]) {
                case "0":
                    $status = "Unknown";
                    break;
                case "1":
                    $status = "Down";
                    break;
                case "2":
                    $status = "Recovering";
                    break;
                case "3":
                    $status = "Up";
                    break;
                case "-2":
                    $status = "Disabled";
                    break;
            }
            echo "<option value='" . $s["status"] . "' " . (get_request_var_request("status") == $s["status"] ? "selected" : "") . ">" . $status . "</option>";
        }
    }
    ?>
						</select>
					</td>
					<td nowrap style='white-space: nowrap;' width="60">
						&nbsp;Rows:&nbsp;
					</td>
					<td width="1">
						<select name="rows" onChange="applyHostFilter(document.devices)">
							<option value="-1"<?php 
    if (get_request_var_request("rows") == "-1") {
        ?>
 selected<?php 
    }
    ?>
>Default</option>
							<?php 
    if (sizeof($item_rows)) {
        foreach ($item_rows as $key => $name) {
            echo "<option value='" . $key . "' " . (get_request_var_request("rows") == $key ? "selected" : "") . ">" . $name . "</option>";
        }
    }
    ?>
						</select>
					</td>
					<td nowrap style='white-space: nowrap;' width="60">
						&nbsp;Search:&nbsp;
					</td>
					<td>
						<input type='text' size='40' name='filter' value='<?php 
    print get_request_var_request("filter");
    ?>
'>
					</td>
					<td nowrap>
						<input type="button" onClick="applyHostFilter(document.devices)" value="Go" border="0">
					</td>
					<td nowrap>
						<input type="button" onClick="clearHosts()" value="Clear" name="clear" border="0">
					</td>
				</tr>
			</table>
			<input type='hidden' name='action' value='devices'>
			</form>
		</td>
	</tr>
	<?php 
    html_end_box();
    html_start_box("", "100%", $colors["header"], "3", "center", "");
    if ($_REQUEST["rows"] == "-1") {
        $num_rows = read_config_option("num_rows_device");
    } else {
        $num_rows = get_request_var_request("rows");
    }
    $limit = " LIMIT " . $num_rows * (get_request_var_request("page") - 1) . "," . $num_rows;
    $sql_where = "";
    if ($_REQUEST["status"] != "-1") {
        $sql_where .= (strlen($sql_where) ? " AND" : "WHERE") . " hrs.host_status=" . $_REQUEST["status"];
    }
    $sql_join = "";
    if ($_REQUEST["filter"] != "") {
        $sql_where .= (strlen($sql_where) ? " AND" : "WHERE") . " host.description LIKE '%" . $_REQUEST["filter"] . "%' OR\n\t\t\thost.hostname LIKE '%" . $_REQUEST["filter"] . "%'";
    }
    $sql = "SELECT hrs.*, host.hostname, host.description, host.disabled\n\t\tFROM plugin_mikrotik_system AS hrs\n\t\tINNER JOIN host ON host.id=hrs.host_id\n\t\t{$sql_join}\n\t\t{$sql_where}\n\t\tORDER BY " . get_request_var_request("sort_column") . " " . get_request_var_request("sort_direction") . " " . $limit;
    //echo $sql;
    $rows = db_fetch_assoc($sql);
    $total_rows = db_fetch_cell("SELECT COUNT(*)\n\t\tFROM plugin_mikrotik_system AS hrs\n\t\tINNER JOIN host ON host.id=hrs.host_id\n\t\t{$sql_join}\n\t\t{$sql_where}");
    if ($total_rows > 0) {
        /* generate page list */
        $url_page_select = get_page_list(get_request_var_request("page"), MAX_DISPLAY_PAGES, $num_rows, $total_rows, "mikrotik.php" . "?action=devices");
        $nav = "<tr bgcolor='#" . $colors["header"] . "'>\n\t\t\t<td colspan='16'>\n\t\t\t\t<table width='100%' cellspacing='0' cellpadding='0' border='0'>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td align='left' class='textHeaderDark'>\n\t\t\t\t\t\t\t<strong>&lt;&lt; ";
        if (get_request_var_request("page") > 1) {
            $nav .= "<a class='linkOverDark' href='" . htmlspecialchars("mikrotik.php" . "?action=devices&page=" . (get_request_var_request("page") - 1)) . "'>";
        }
        $nav .= "Previous";
        if (get_request_var_request("page") > 1) {
            $nav .= "</a>";
        }
        $nav .= "</strong>\n\t\t\t\t\t\t</td>\n\n\t\t\t\t\t\t<td align='center' class='textHeaderDark'>\n\t\t\t\t\t\t\tShowing Rows " . ($num_rows * (get_request_var_request("page") - 1) + 1) . " to " . ($total_rows < $num_rows || $total_rows < $num_rows * get_request_var_request("page") ? $total_rows : $num_rows * get_request_var_request("page")) . " of {$total_rows} [{$url_page_select}]\n\t\t\t\t\t\t</td>\n\n\t\t\t\t\t\t<td align='right' class='textHeaderDark'>\n\t\t\t\t\t\t\t<strong>";
        if (get_request_var_request("page") * $num_rows < $total_rows) {
            $nav .= "<a class='linkOverDark' href='" . htmlspecialchars("mikrotik.php" . "?action=devices&page=" . (get_request_var_request("page") + 1)) . "'>";
        }
        $nav .= "Next";
        if (get_request_var_request("page") * $num_rows < $total_rows) {
            $nav .= "</a>";
        }
        $nav .= " &gt;&gt;</strong>\n\t\t\t\t\t\t</td>\n\n\t\t\t\t\t</tr>\n\t\t\t\t</table>\n\t\t\t</td>\n\t\t</tr>\n";
    } else {
        $nav = "<tr bgcolor='#" . $colors["header"] . "'>\n\t\t\t<td colspan='16'>\n\t\t\t\t<table width='100%' cellspacing='0' cellpadding='0' border='0'>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td align='center' class='textHeaderDark'>\n\t\t\t\t\t\t\tNo Rows Found\n\t\t\t\t\t\t</td>\n\n\t\t\t\t\t</tr>\n\t\t\t\t</table>\n\t\t\t</td>\n\t\t</tr>\n";
    }
    print $nav;
    $display_text = array("nosort" => array("Actions", array("ASC", "left")), "description" => array("Name", array("ASC", "left")), "host_status" => array("Status", array("DESC", "right")), "uptime" => array("Uptime(d:h:m)", array("DESC", "right")), "users" => array("Users", array("DESC", "right")), "cpuPercent" => array("CPU %", array("DESC", "right")), "numCpus" => array("CPUs", array("DESC", "right")), "processes" => array("Processes", array("DESC", "right")), "memSize" => array("Total Mem", array("DESC", "right")), "memUsed" => array("Used Mem", array("DESC", "right")), "diskSize" => array("Total Disk", array("DESC", "right")), "diskUsed" => array("Used Disk", array("DESC", "right")));
    mikrotik_header_sort($display_text, get_request_var_request("sort_column"), get_request_var_request("sort_direction"), "action=devices");
    /* set some defaults */
    $url = $config["url_path"] . "plugins/mikrotik/mikrotik.php";
    $users = $config["url_path"] . "plugins/mikrotik/images/view_users.gif";
    $host = $config["url_path"] . "plugins/mikrotik/images/view_hosts.gif";
    $trees = $config["url_path"] . "plugins/mikrotik/images/view_trees.gif";
    $dashboard = $config["url_path"] . "plugins/mikrotik/images/view_dashboard.gif";
    $graphs = $config["url_path"] . "plugins/mikrotik/images/view_graphs.gif";
    $nographs = $config["url_path"] . "plugins/mikrotik/images/view_graphs_disabled.gif";
    $hcpudq = read_config_option("mikrotik_dq_host_cpu");
    $i = 0;
    if (sizeof($rows)) {
        foreach ($rows as $row) {
            $days = intval($row["uptime"] / (60 * 60 * 24 * 100));
            $remainder = $row["uptime"] % (60 * 60 * 24 * 100);
            $hours = intval($remainder / (60 * 60 * 100));
            $remainder = $remainder % (60 * 60 * 100);
            $minutes = intval($remainder / (60 * 100));
            $found = db_fetch_cell("SELECT COUNT(*) FROM graph_local WHERE host_id=" . $row["host_id"]);
            form_alternate_row_color($colors["alternate"], $colors["light"], $i);
            $i++;
            echo "<td width='100' align='left'>";
            //echo "<a style='padding:1px;' href='" . htmlspecialchars("$url?action=dashboard&reset=1&device=" . $row["host_id"]) . "'><img src='$dashboard' title='View Dashboard' align='absmiddle' border='0'></a>";
            echo "<a style='padding:1px;' href='" . htmlspecialchars("{$url}?action=users&reset=1&device=" . $row["host_id"]) . "'><img src='{$users}' title='View Users' align='absmiddle' border='0'></a>";
            echo "<a style='padding:1px;' href='" . htmlspecialchars("{$url}?action=trees&reset=1&device=" . $row["host_id"]) . "'><img src='{$trees}' title='View Queue Trees' align='absmiddle' border='0'></a>";
            if ($found) {
                echo "<a style='padding:1px;' href='" . htmlspecialchars("{$url}?action=graphs&reset=1&host=" . $row["host_id"] . "&style=selective&graph_add=&graph_list=&graph_template_id=0&filter=") . "'><img  src='{$graphs}' title='View Graphs' align='absmiddle' border='0'></a>";
            } else {
                echo "<img src='{$nographs}' title='No Graphs Defined' align='absmiddle' border='0'>";
            }
            $graph_cpu = mikrotik_get_graph_url($hcpudq, $row["host_id"], "", $row["numCpus"], false);
            $graph_cpup = mikrotik_get_graph_template_url(read_config_option("mikrotik_gt_cpu"), $row["host_id"], round($row["cpuPercent"], 2), false);
            $graph_users = mikrotik_get_graph_template_url(read_config_option("mikrotik_gt_users"), $row["host_id"], $row["users"], false);
            $graph_aproc = mikrotik_get_graph_template_url(read_config_option("mikrotik_gt_processes"), $row["host_id"], $row["host_status"] < 2 ? "N/A" : $row["processes"], false);
            $graph_disk = mikrotik_get_graph_template_url(read_config_option("mikrotik_gt_disk"), $row["host_id"], $row["host_status"] < 2 ? "N/A" : round($row["diskUsed"], 2), false);
            $graph_mem = mikrotik_get_graph_template_url(read_config_option("mikrotik_gt_memory"), $row["host_id"], $row["host_status"] < 2 ? "N/A" : round($row["memUsed"], 2), false);
            $graph_upt = mikrotik_get_graph_template_url(read_config_option("mikrotik_gt_uptime"), $row["host_id"], $row["host_status"] < 2 ? "N/A" : mikrotik_format_uptime($days, $hours, $minutes), false);
            if (api_plugin_user_realm_auth('host.php')) {
                $host_url = "<a href='" . htmlspecialchars($config["url_path"] . "host.php?action=edit&id=" . $row["host_id"]) . "' title='Edit Hosts'>" . $row["hostname"] . "</a>";
            } else {
                $host_url = $row["hostname"];
            }
            echo "</td>";
            echo "<td style='white-space:nowrap;' align='left' width='200'><strong>" . $row["description"] . "</strong> [" . $host_url . "]" . "</td>";
            echo "<td style='white-space:nowrap;' align='right'>" . get_colored_device_status($row["disabled"] == "on" ? true : false, $row["host_status"]) . "</td>";
            echo "<td style='white-space:nowrap;' align='right'>" . $graph_upt . "</td>";
            echo "<td style='white-space:nowrap;' align='right'>" . $graph_users . "</td>";
            echo "<td style='white-space:nowrap;' align='right'>" . ($row["host_status"] < 2 ? "N/A" : $graph_cpup) . "</td>";
            echo "<td style='white-space:nowrap;' align='right'>" . ($row["host_status"] < 2 ? "N/A" : $graph_cpu) . "</td>";
            echo "<td style='white-space:nowrap;' align='right'>" . $graph_aproc . "</td>";
            echo "<td style='white-space:nowrap;' align='right'>" . mikrotik_memory($row["memSize"]) . "</td>";
            echo "<td style='white-space:nowrap;' align='right'>" . $graph_mem . " %</td>";
            echo "<td style='white-space:nowrap;' align='right'>" . mikrotik_memory($row["diskSize"]) . "</td>";
            echo "<td style='white-space:nowrap;' align='right'>" . $graph_disk . " %</td>";
        }
        echo "</tr>";
        print $nav;
    } else {
        print "<tr><td><em>No Devices Found</em></td></tr>";
    }
    html_end_box();
}