diff options
author | Renato Botelho <renato@netgate.com> | 2015-09-18 16:27:09 -0300 |
---|---|---|
committer | Renato Botelho <renato@netgate.com> | 2015-09-18 16:27:09 -0300 |
commit | f70b256ed5d68d70b8fb22ac2e64b2797488394c (patch) | |
tree | 98dc7010371ed519b37b91b6e50605de0d3ec8f4 /usr | |
parent | e29de78b04b01a4c7955d09a800df67bd7237c3c (diff) | |
parent | 26ab9c1a1c41a57cd1267300a2148b8588adc0e4 (diff) | |
download | pfsense-f70b256ed5d68d70b8fb22ac2e64b2797488394c.zip pfsense-f70b256ed5d68d70b8fb22ac2e64b2797488394c.tar.gz |
Merge pull request #1842 from phil-davis/GW-Widget-Monitor-IP-2-2
Diffstat (limited to 'usr')
-rw-r--r-- | usr/local/www/includes/functions.inc.php | 197 | ||||
-rw-r--r-- | usr/local/www/widgets/widgets/gateways.widget.php | 113 |
2 files changed, 224 insertions, 86 deletions
diff --git a/usr/local/www/includes/functions.inc.php b/usr/local/www/includes/functions.inc.php index dfc2115..2c48543 100644 --- a/usr/local/www/includes/functions.inc.php +++ b/usr/local/www/includes/functions.inc.php @@ -5,7 +5,7 @@ Copyright (C) 2013-2015 Electric Sheep Fencing, LP */ -if(Connection_Aborted()) { +if (Connection_Aborted()) { exit; } @@ -32,42 +32,76 @@ function get_stats() { } function get_gatewaystats() { + global $config; + if (isset($config["widgets"]["gateways_widget"]["display_type"])) { + $display_type = $config["widgets"]["gateways_widget"]["display_type"]; + } else { + $display_type = "gw_ip"; + } + $a_gateways = return_gateways_array(); $gateways_status = array(); $gateways_status = return_gateways_status(true); $data = ""; $isfirst = true; - foreach($a_gateways as $gname => $gw) { - if(!$isfirst) + foreach ($a_gateways as $gname => $gw) { + if (!$isfirst) { $data .= ","; + } $isfirst = false; $data .= $gw['name'] . ","; + + $monitor_address = ""; + $monitor_address_disp = ""; + if ($display_type == "monitor_ip" || $display_type == "both_ip") { + $monitor_address = $gw['monitor']; + if ($monitor_address != "" && $display_type == "both_ip") { + $monitor_address_disp = " (" . $monitor_address . ")"; + } else { + $monitor_address_disp = $monitor_address; + } + } + if ($gateways_status[$gname]) { - $data .= "<b>" . lookup_gateway_ip_by_name($gname) . "</b>,"; + if ($display_type == "gw_ip" || $display_type == "both_ip" || ($display_type == "monitor_ip" && $monitor_address == "")) { + $if_gw = lookup_gateway_ip_by_name($gname); + } else { + $if_gw = ""; + } + if ($monitor_address == $if_gw) { + $monitor_address_disp = ""; + } + + $data .= "<b>" . $if_gw . $monitor_address_disp . "</b>,"; $gws = $gateways_status[$gname]; - switch(strtolower($gws['status'])) { - case "none": - $online = "Online"; - $bgcolor = "#90EE90"; // lightgreen - break; - case "down": - $online = "Offline"; - $bgcolor = "#F08080"; // lightcoral - break; - case "delay": - $online = "Latency"; - $bgcolor = "#F0E68C"; // khaki - break; - case "loss": - $online = "Packetloss"; - $bgcolor = "#F0E68C"; // khaki - break; - default: - $online = "Pending"; - break; + switch (strtolower($gws['status'])) { + case "none": + $online = "Online"; + $bgcolor = "#90EE90"; // lightgreen + break; + case "down": + $online = "Offline"; + $bgcolor = "#F08080"; // lightcoral + break; + case "delay": + $online = "Latency"; + $bgcolor = "#F0E68C"; // khaki + break; + case "loss": + $online = "Packetloss"; + $bgcolor = "#F0E68C"; // khaki + break; + default: + $online = "Pending"; + break; } } else { - $data .= "~,"; + if ($display_type == "gw_ip" || $display_type == "both_ip" || ($display_type == "monitor_ip" && $monitor_address == "")) { + $if_gw = "~"; + } else { + $if_gw = ""; + } + $data .= $if_gw . $monitor_address_disp . ","; $gws['delay'] = "~"; $gws['loss'] = "~"; $online = "Unknown"; @@ -82,8 +116,9 @@ function get_gatewaystats() { function get_uptime() { $uptime = get_uptime_sec(); - if(intval($uptime) == 0) + if (intval($uptime) == 0) { return; + } $updays = (int)($uptime / 86400); $uptime %= 86400; @@ -94,19 +129,23 @@ function get_uptime() { $upsecs = (int)($uptime); $uptimestr = ""; - if ($updays > 1) + if ($updays > 1) { $uptimestr .= "$updays Days "; - else if ($updays > 0) + } else if ($updays > 0) { $uptimestr .= "1 Day "; + } - if ($uphours > 1) + if ($uphours > 1) { $hours = "s"; + } - if ($upmins > 1) + if ($upmins > 1) { $minutes = "s"; + } - if ($upmins > 1) + if ($upmins > 1) { $seconds = "s"; + } $uptimestr .= sprintf("%02d Hour$hours %02d Minute$minutes %02d Second$seconds", $uphours, $upmins, $upsecs); return $uptimestr; @@ -124,8 +163,9 @@ function cpu_usage() { $totalEnd = array_sum($cpuTicks2); // Something wrapped ?!?! - if ($totalEnd <= $totalStart) + if ($totalEnd <= $totalStart) { return 0; + } // Calculate total cycles used $totalUsed = ($totalEnd - $totalStart) - ($cpuTicks2['idle'] - $cpuTicks['idle']); @@ -139,23 +179,27 @@ function cpu_usage() { function get_pfstate($percent=false) { global $config; $matches = ""; - if (isset($config['system']['maximumstates']) and $config['system']['maximumstates'] > 0) + if (isset($config['system']['maximumstates']) and $config['system']['maximumstates'] > 0) { $maxstates="{$config['system']['maximumstates']}"; - else + } else { $maxstates=pfsense_default_state_size(); + } $curentries = `/sbin/pfctl -si |grep current`; if (preg_match("/([0-9]+)/", $curentries, $matches)) { $curentries = $matches[1]; } - if (!is_numeric($curentries)) + if (!is_numeric($curentries)) { $curentries = 0; - if ($percent) - if (intval($maxstates) > 0) + } + if ($percent) { + if (intval($maxstates) > 0) { return round(($curentries / $maxstates) * 100, 0); - else + } else { return "NA"; - else + } + } else { return $curentries . "/" . $maxstates; + } } function has_temp() { @@ -171,20 +215,23 @@ function get_hwtype() { function get_mbuf($percent=false) { $mbufs_output=trim(`/usr/bin/netstat -mb | /usr/bin/grep "mbuf clusters in use" | /usr/bin/awk '{ print $1 }'`); - list( $mbufs_current, $mbufs_cache, $mbufs_total, $mbufs_max ) = explode( "/", $mbufs_output); - if ($percent) - if ($mbufs_max > 0) + list($mbufs_current, $mbufs_cache, $mbufs_total, $mbufs_max) = explode("/", $mbufs_output); + if ($percent) { + if ($mbufs_max > 0) { return round(($mbufs_total / $mbufs_max) * 100, 0); - else + } else { return "NA"; - else + } + } else { return "{$mbufs_total}/{$mbufs_max}"; + } } function get_temp() { $temp_out = get_single_sysctl("dev.cpu.0.temperature"); - if ($temp_out == "") + if ($temp_out == "") { $temp_out = get_single_sysctl("hw.acpi.thermal.tz0.temperature"); + } // Remove 'C' from the end return rtrim($temp_out, 'C'); @@ -221,11 +268,12 @@ function disk_usage($slice = '/') { function swap_usage() { exec("/usr/sbin/swapinfo", $swap_info); $swap_used = ""; - foreach ($swap_info as $line) + foreach ($swap_info as $line) { if (preg_match('/(\d+)%$/', $line, $matches)) { $swap_used = $matches[1]; break; } + } return $swap_used; } @@ -238,8 +286,9 @@ function mem_usage() { $freeMem = get_single_sysctl("vm.stats.vm.v_free_count"); $usedMem = $totalMem - ($inactiveMem + $cachedMem + $freeMem); $memUsage = round(($usedMem * 100) / $totalMem, 0); - } else + } else { $memUsage = "NA"; + } return $memUsage; } @@ -257,8 +306,9 @@ function get_cpufreq() { $maxfreq = $maxfreq[0]; $curfreq = ""; $curfreq = get_single_sysctl('dev.cpu.0.freq'); - if (($curfreq > 0) && ($curfreq != $maxfreq)) + if (($curfreq > 0) && ($curfreq != $maxfreq)) { $out = "Current: {$curfreq} MHz, Max: {$maxfreq} MHz"; + } return $out; } @@ -295,24 +345,24 @@ function get_interfacestats() { $new_data = ""; //build data arrays - foreach ($ifdescrs as $ifdescr => $ifname){ + foreach ($ifdescrs as $ifdescr => $ifname) { $ifinfo = get_interface_info($ifdescr); - $new_data .= "{$ifinfo['inpkts']},"; - $new_data .= "{$ifinfo['outpkts']},"; - $new_data .= format_bytes($ifinfo['inbytes']) . ","; - $new_data .= format_bytes($ifinfo['outbytes']) . ","; - if (isset($ifinfo['inerrs'])){ - $new_data .= "{$ifinfo['inerrs']},"; - $new_data .= "{$ifinfo['outerrs']},"; - } - else{ - $new_data .= "0,"; - $new_data .= "0,"; - } - if (isset($ifinfo['collisions'])) - $new_data .= htmlspecialchars($ifinfo['collisions']) . ","; - else - $new_data .= "0,"; + $new_data .= "{$ifinfo['inpkts']},"; + $new_data .= "{$ifinfo['outpkts']},"; + $new_data .= format_bytes($ifinfo['inbytes']) . ","; + $new_data .= format_bytes($ifinfo['outbytes']) . ","; + if (isset($ifinfo['inerrs'])) { + $new_data .= "{$ifinfo['inerrs']},"; + $new_data .= "{$ifinfo['outerrs']},"; + } else { + $new_data .= "0,"; + $new_data .= "0,"; + } + if (isset($ifinfo['collisions'])) { + $new_data .= htmlspecialchars($ifinfo['collisions']) . ","; + } else { + $new_data .= "0,"; + } }//end for return $new_data; @@ -325,25 +375,28 @@ function get_interfacestatus() { //build interface list for widget use $ifdescrs = get_configured_interface_with_descr(); - foreach ($ifdescrs as $ifdescr => $ifname){ + foreach ($ifdescrs as $ifdescr => $ifname) { $ifinfo = get_interface_info($ifdescr); $data .= $ifname . "^"; - if($ifinfo['status'] == "up" || $ifinfo['status'] == "associated") { + if ($ifinfo['status'] == "up" || $ifinfo['status'] == "associated") { $data .= "up"; - }else if ($ifinfo['status'] == "no carrier") { + } else if ($ifinfo['status'] == "no carrier") { $data .= "down"; - }else if ($ifinfo['status'] == "down") { + } else if ($ifinfo['status'] == "down") { $data .= "block"; } $data .= "^"; - if ($ifinfo['ipaddr']) + if ($ifinfo['ipaddr']) { $data .= "<strong>" . htmlspecialchars($ifinfo['ipaddr']) . "</strong>"; + } $data .= "^"; - if ($ifinfo['ipaddrv6']) + if ($ifinfo['ipaddrv6']) { $data .= "<strong>" . htmlspecialchars($ifinfo['ipaddrv6']) . "</strong>"; + } $data .= "^"; - if ($ifinfo['status'] != "down") + if ($ifinfo['status'] != "down") { $data .= htmlspecialchars($ifinfo['media']); + } $data .= "~"; diff --git a/usr/local/www/widgets/widgets/gateways.widget.php b/usr/local/www/widgets/widgets/gateways.widget.php index 4f34191..26a6f4d 100644 --- a/usr/local/www/widgets/widgets/gateways.widget.php +++ b/usr/local/www/widgets/widgets/gateways.widget.php @@ -37,6 +37,24 @@ require_once("pfsense-utils.inc"); require_once("functions.inc"); require_once("/usr/local/www/widgets/include/gateways.inc"); +if ($_POST) { + if (!is_array($config["widgets"]["gateways_widget"])) { + $config["widgets"]["gateways_widget"] = array(); + } + if (isset($_POST["display_type"])) { + $config["widgets"]["gateways_widget"]["display_type"] = $_POST["display_type"]; + } + write_config("Updated gateways widget settings via dashboard."); + header("Location: /"); + exit(0); +} + +if (isset($config["widgets"]["gateways_widget"]["display_type"])) { + $display_type = $config["widgets"]["gateways_widget"]["display_type"]; +} else { + $display_type = "gw_ip"; +} + $a_gateways = return_gateways_array(); $gateways_status = array(); $gateways_status = return_gateways_status(true); @@ -45,6 +63,49 @@ $counter = 1; ?> +<input type="hidden" id="gateways-config" name="gateways-config" value="" /> + +<div id="gateways-settings" class="widgetconfigdiv" style="display:none;"> +<form action="/widgets/widgets/gateways.widget.php" method="post" name="gateways_widget_iform" id="gateways_widget_iform"> + Display: + <?php + $display_type_gw_ip="checked=\"checked\""; + $display_type_monitor_ip=""; + $display_type_both_ip=""; + if (isset($config["widgets"]["gateways_widget"]["display_type"])) { + $selected_radio = $config["widgets"]["gateways_widget"]["display_type"]; + if ($selected_radio == "gw_ip") { + $display_type_gw_ip = "checked=\"checked\""; + $display_type_monitor_ip=""; + $display_type_both_ip=""; + } else if ($selected_radio == "monitor_ip") { + $display_type_gw_ip = ""; + $display_type_monitor_ip="checked=\"checked\""; + $display_type_both_ip=""; + } else if ($selected_radio == "both_ip") { + $display_type_gw_ip = ""; + $display_type_monitor_ip=""; + $display_type_both_ip="checked=\"checked\""; + } + } + ?> + <input name="display_type" class="radio" type="radio" id="display_type_gw_ip" value="gw_ip" <?php echo $display_type_gw_ip; ?> onchange="updateGatewayDisplays();" /> <span>Gateway IP</span> + <input name="display_type" class="radio" type="radio" id="display_type_monitor_ip" value="monitor_ip" <?php echo $display_type_monitor_ip; ?> onchange="updateGatewayDisplays();" /> <span>Monitor IP</span> + <input name="display_type" class="radio" type="radio" id="display_type_both_ip" value="both_ip" <?php echo $display_type_both_ip; ?> onchange="updateGatewayDisplays();" /> <span>Both</span> + <br /><br /> + <input id="submit_settings" name="submit_settings" type="submit" onclick="return updatePref();" class="formbtn" value="Save Settings" /> +</form> +</div> + +<script type="text/javascript"> +//<![CDATA[ + d = document; + selectIntLink = "gateways-configure"; + textlink = d.getElementById(selectIntLink); + textlink.style.display = "inline"; +//]]> +</script> + <table bgcolor="#990000" width="100%" border="0" cellspacing="0" cellpadding="0" summary="gateway status"> <tr> <td class="listhdrr" id="gatewayname" align="center">Name</td> @@ -63,17 +124,41 @@ $counter = 1; <td colspan="3" class="listr ellipsis" align="center"> <div id="gateway<?php echo $counter; ?>" style="display:inline"><b> <?php + $monitor_address = ""; + $monitor_address_disp = ""; + if ($display_type == "monitor_ip" || $display_type == "both_ip") { + $monitor_address = $gateway['monitor']; + if ($monitor_address != "" && $display_type == "both_ip") { + $monitor_address_disp = " (" . $monitor_address . ")"; + } else { + $monitor_address_disp = $monitor_address; + } + } $if_gw = ''; - if (is_ipaddr($gateway['gateway'])) - $if_gw = htmlspecialchars($gateway['gateway']); - else { - if($gateway['ipprotocol'] == "inet") - $if_gw = htmlspecialchars(get_interface_gateway($gateway['friendlyiface'])); - if($gateway['ipprotocol'] == "inet6") - $if_gw = htmlspecialchars(get_interface_gateway_v6($gateway['friendlyiface'])); + // If the user asked to display Gateway IP or both IPs, or asked for just monitor IP but the monitor IP is blank + // then find the gateway IP (which is also the monitor IP if the monitor IP was not explicitly set). + if ($display_type == "gw_ip" || $display_type == "both_ip" || ($display_type == "monitor_ip" && $monitor_address == "")) { + if (is_ipaddr($gateway['gateway'])) { + $if_gw = htmlspecialchars($gateway['gateway']); + } else { + if ($gateway['ipprotocol'] == "inet") { + $if_gw = htmlspecialchars(get_interface_gateway($gateway['friendlyiface'])); + } + if ($gateway['ipprotocol'] == "inet6") { + $if_gw = htmlspecialchars(get_interface_gateway_v6($gateway['friendlyiface'])); + } + } + if ($if_gw == "") { + $if_gw = "~"; + } + } + if ($monitor_address == $if_gw) { + $monitor_address_disp = ""; } - echo ($if_gw == '' ? '~' : $if_gw); + echo $if_gw . $monitor_address_disp; unset ($if_gw); + unset ($monitor_address); + unset ($monitor_address_disp); $counter++; ?> </b></div> @@ -82,19 +167,21 @@ $counter = 1; <tr> <td class="listr ellipsis" align="center" id="gateway<?php echo $counter; ?>"> <?php - if ($gateways_status[$gname]) + if ($gateways_status[$gname]) { echo htmlspecialchars($gateways_status[$gname]['delay']); - else + } else { echo gettext("Pending"); + } ?> <?php $counter++; ?> </td> <td class="listr ellipsis" align="center" id="gateway<?php echo $counter; ?>"> <?php - if ($gateways_status[$gname]) + if ($gateways_status[$gname]) { echo htmlspecialchars($gateways_status[$gname]['loss']); - else + } else { echo gettext("Pending"); + } ?> <?php $counter++; ?> </td> @@ -138,5 +225,3 @@ $counter = 1; </tr> <?php } // foreach ?> </table> - - |