From 84a2a28e532a9fc20a98b267dd3f3b8e34821955 Mon Sep 17 00:00:00 2001 From: Renato Botelho Date: Wed, 24 Jul 2013 15:19:35 -0300 Subject: Code improvements: - Convert commands to an associative array to make code more readable - Keep command headers to make it easy to understand data - Add filter to IP --- config/openbgpd/openbgpd_status.php | 46 +++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 20 deletions(-) (limited to 'config/openbgpd') diff --git a/config/openbgpd/openbgpd_status.php b/config/openbgpd/openbgpd_status.php index aaa37d26..42b77574 100644 --- a/config/openbgpd/openbgpd_status.php +++ b/config/openbgpd/openbgpd_status.php @@ -34,17 +34,17 @@ $commands = array(); defCmdT("summary", "OpenBGPD Summary", "/usr/local/sbin/bgpctl show summary"); defCmdT("interfaces", "OpenBGPD Interfaces", "/usr/local/sbin/bgpctl show interfaces"); -defCmdT("routing", "OpenBGPD Routing", "/usr/local/sbin/bgpctl show rib | /usr/bin/sed '1,4d'", true); -defCmdT("forwarding", "OpenBGPD Forwarding", "/usr/local/sbin/bgpctl show fib | /usr/bin/sed '1,5d'", true); +defCmdT("routing", "OpenBGPD Routing", "/usr/local/sbin/bgpctl show rib", true, 4); +defCmdT("forwarding", "OpenBGPD Forwarding", "/usr/local/sbin/bgpctl show fib", true, 5); defCmdT("network", "OpenBGPD Network", "/usr/local/sbin/bgpctl show network"); defCmdT("nexthops", "OpenBGPD Nexthops", "/usr/local/sbin/bgpctl show nexthop"); -defCmdT("ip", "OpenBGPD IP", "/usr/local/sbin/bgpctl show ip bgp"); +defCmdT("ip", "OpenBGPD IP", "/usr/local/sbin/bgpctl show ip bgp", true, 4); defCmdT("neighbors", "OpenBGPD Neighbors", "/usr/local/sbin/bgpctl show neighbor"); if (isset($_REQUEST['isAjax'])) { if (isset($_REQUEST['cmd']) && isset($commands[$_REQUEST['cmd']])) { echo "{$_REQUEST['cmd']}\n"; - echo htmlspecialchars_decode(doCmdT($commands[$_REQUEST['cmd']][1], $_REQUEST['limit'], $_REQUEST['filter'])); + echo htmlspecialchars_decode(doCmdT($commands[$_REQUEST['cmd']]['command'], $_REQUEST['limit'], $_REQUEST['filter'], $_REQUEST['header_size'])); } exit; } @@ -56,12 +56,14 @@ else include("head.inc"); -function doCmdT($command, $limit = "all", $filter = "") { +function doCmdT($command, $limit = "all", $filter = "", $header_size = 0) { $grepline = ""; if (!empty($filter)) $grepline = " | /usr/bin/grep " . escapeshellarg(htmlspecialchars($filter)); - if (is_numeric($limit) && $limit > 0) + if (is_numeric($limit) && $limit > 0) { + $limit += $header_size; $headline = " | /usr/bin/head -n {$limit}"; + } $fd = popen("{$command}{$grepline}{$headline} 2>&1", "r"); $ct = 0; @@ -89,39 +91,43 @@ function countCmdT($command) { return $c; } -function showCmdT($idx, $title, $command, $has_filter) { +function showCmdT($idx, $data) { echo "

\n"; - echo " \n"; + echo " \n"; echo "\n"; - echo "\n"; + echo "\n"; $limit_default = "all"; - if ($has_filter) { + if ($data['has_filter']) { $limit_options = array("10", "50", "100", "200", "500", "1000", "all"); $limit_default = "100"; echo "\n"; + echo " of " . countCmdT($data['command']) . " items\n"; echo "\n"; } echo "\n"; echo "
" . $title . "
" . $data['title'] . "
\n"; - echo "Display \n"; foreach ($limit_options as $item) echo "\n"; - echo " of " . countCmdT($command) . " itemsFilter expression: \n"; echo "\n"; - echo "\n"; + echo "\n"; echo "
";	/* no newline after pre */
-	echo doCmdT($command, $limit_default);
+	echo doCmdT($data['command'], $limit_default, "", $data['header_size']);
 	echo "
\n"; } /* Define a command, with a title, to be executed later. */ -function defCmdT($idx, $title, $command, $has_filter = false) { +function defCmdT($idx, $title, $command, $has_filter = false, $header_size = 0) { global $commands; $title = htmlspecialchars($title,ENT_NOQUOTES); - $commands[$idx] = array($title, $command, $has_filter); + $commands[$idx] = array( + 'title' => $title, + 'command' => $command, + 'has_filter' => $has_filter, + 'header_size' => $header_size); } /* List all of the commands as an index. */ @@ -130,7 +136,7 @@ function listCmds() { echo "

This status page includes the following information:\n"; echo "

\n"; } @@ -138,7 +144,7 @@ function listCmds() { function execCmds() { global $commands; foreach ($commands as $idx => $command) - showCmdT($idx, $command[0], $command[1], $command[2]); + showCmdT($idx, $command); } ?> @@ -182,12 +188,12 @@ function execCmds() {