Skip to content

Instantly share code, notes, and snippets.

@lozzd
Created October 11, 2012 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lozzd/3871216 to your computer and use it in GitHub Desktop.
Save lozzd/3871216 to your computer and use it in GitHub Desktop.
FITB support for PHP Weathermap
diff -upN weathermap_clean/editor.js weathermap/editor.js
--- weathermap_clean/editor.js 2010-03-04 10:45:19.000000000 +0000
+++ weathermap/editor.js 2011-10-11 11:49:15.000000000 +0000
@@ -127,6 +127,7 @@ function attach_click_events()
jQuery('.wm_cancel').click(cancel_op);
jQuery('#link_cactipick').click(cactipicker).attr("href","#");
+ jQuery('#link_fitbpick').click(fitbpicker).attr("href","#");
jQuery('#node_cactipick').click(nodecactipicker).attr("href","#");
jQuery('#xycapture').mouseover(function(event) {coord_capture(event);});
@@ -256,6 +257,24 @@ function cactipicker()
newWindow.location = "cacti-pick.php?command=link_step1";
}
+function fitbpicker()
+ {
+ // make sure it isn't already opened
+ if (!newWindow || newWindow.closed)
+ {
+ newWindow = window.open("", "fitbpicker", "scrollbars=1,status=1,height=400,width=400,resizable=1");
+ }
+
+ else if (newWindow.focus)
+ {
+ // window is already open and focusable, so bring it to the front
+ newWindow.focus();
+ }
+
+ newWindow.location = "fitb-pick.php?command=link_step1";
+ }
+
+
function nodecactipicker()
{
@@ -729,4 +748,4 @@ function attach_help_events()
// add an onblur/onfocus handler to all the visible <input> items
jQuery("input").focus(help_handler).blur(help_handler);
-}
\ No newline at end of file
+}
diff -upN weathermap_clean/editor.php weathermap/editor.php
--- weathermap_clean/editor.php 2010-03-08 21:14:22.000000000 +0000
+++ weathermap/editor.php 2012-09-10 11:59:47.000000000 +0000
@@ -4,7 +4,7 @@ require_once 'editor.inc.php';
require_once 'Weathermap.class.php';
// so that you can't have the editor active, and not know about it.
-$ENABLED=false;
+$ENABLED=true;
if(! $ENABLED)
{
@@ -29,7 +29,7 @@ $grid_snap_value = 0; // set non-zero to
if( isset($_COOKIE['wmeditor']))
{
- $parts = split(":",$_COOKIE['wmeditor']);
+ $parts = preg_split("/:/",$_COOKIE['wmeditor']);
if( (isset($parts[0])) && (intval($parts[0]) == 1) ) { $use_overlay = TRUE; }
if( (isset($parts[1])) && (intval($parts[1]) == 1) ) { $use_relative_overlay = TRUE; }
@@ -1094,7 +1094,9 @@ else
<tr>
<th>Data Source</th>
<td><input id="link_target" name="link_target" type="text" /> <span class="cactilink"><a id="link_cactipick">[Pick
- from Cacti]</a></span></td>
+ from Cacti]</a></span>
+ <span class="fitblink"><a id="link_fitbpick">[Pick from FITB]</a></span>
+ </td>
</tr>
<tr>
<th>Link Width</th>
diff -upN weathermap_clean/fitb-pick.php weathermap/fitb-pick.php
--- weathermap_clean/fitb-pick.php 1970-01-01 00:00:00.000000000 +0000
+++ weathermap/fitb-pick.php 2011-10-11 14:14:29.000000000 +0000
@@ -0,0 +1,199 @@
+<?php
+
+$mapdir='configs';
+
+# FITB Settings
+$fitb_base = '/var/www/fitb';
+$fitb_url = 'http://fitb.yourdomain.com/';
+$rra_path = '/var/lib/fitb/rrds/';
+
+# The FITB database details
+$database_hostname = "localhost";
+$database_username = "fitbuser";
+$database_password = "fitbpassword";
+$database_default = "fitb";
+
+$config['base_url'] = $fitb_url;
+$config['rra_path'] = $rra_path;
+
+
+
+// Shouldn't need to touch anything below here
+// ******************************************
+
+function js_escape($str)
+{
+ $str = str_replace('\\', '\\\\', $str);
+ $str = str_replace("'", "\\\'", $str);
+
+ $str = "'".$str."'";
+
+ return($str);
+}
+
+# Link step one. This is the main entry point for Weathermap. This was a two step process in
+# Cacti, but with FITB I've rewritten it to be one step.
+
+if(isset($_REQUEST['command']) && $_REQUEST["command"]=='link_step1')
+{
+?>
+<html>
+<head>
+ <script type="text/javascript" src="editor-resources/jquery-latest.min.js"></script>
+ <script type="text/javascript">
+
+ function filterlist(previous)
+ {
+ var filterstring = $('input#filterstring').val();
+
+ if(filterstring=='')
+ {
+ $('ul#dslist > li').show();
+ return;
+ }
+
+ if(filterstring!=previous)
+ {
+ $('ul#dslist > li').hide();
+ $("ul#dslist > li:contains('" + filterstring + "')").show();
+ }
+ }
+
+ $(document).ready( function() {
+ $('span.filter').keyup(function() {
+ var previous = $('input#filterstring').val();
+ setTimeout(function () {filterlist(previous)}, 500);
+ }).show();
+ });
+
+ function update_source_step1(safename,host,name)
+ {
+ var rra_path = <?php echo js_escape($config['rra_path']); ?>;
+
+ opener.document.forms["frmMain"].link_target.value = rra_path + host + '/' + host + '-' + safename + '_bits.rrd';
+ opener.document.forms["frmMain"].link_infourl.value = '<?php echo $fitb_url ?>viewgraph.php?host=' + host + '&rrdname=' + host + '-' + safename + '&type=bits';
+ opener.document.forms["frmMain"].link_hover.value = '<?php echo $fitb_url ?>graph.php?host=' + host + '&rrdname=' + host + '-' + safename + '&type=bits';
+ self.close();
+
+ }
+ function applyDSFilterChange(objForm) {
+ strURL = '?host_id=' + objForm.host_id.value;
+ strURL = strURL + '&command=link_step1';
+ strURL = strURL + "&overlib=1";
+ document.location = strURL;
+ }
+
+ </script>
+<style type="text/css">
+body { font-family: sans-serif; font-size: 10pt; }
+ul { list-style: none; margin: 0; padding: 0; }
+ul { border: 1px solid black; }
+ul li.row0 { background: #ddd;}
+ul li.row1 { background: #ccc;}
+ul li { border-bottom: 1px solid #aaa; border-top: 1px solid #eee; padding: 2px;}
+ul li a { text-decoration: none; color: black; }
+</style>
+<title>Pick a data source</title>
+</head>
+<body>
+<?php
+
+ $SQL_picklist = "SELECT host, name, alias, safename FROM ports WHERE graphtype='bits' ";
+
+ $host_id = -1;
+
+ $overlib = true;
+ $aggregate = false;
+
+ if(isset($_REQUEST['aggregate'])) $aggregate = ( $_REQUEST['aggregate']==0 ? false : true);
+ if(isset($_REQUEST['overlib'])) $overlib= ( $_REQUEST['overlib']==0 ? false : true);
+
+
+ if(isset($_REQUEST['host_id']))
+ {
+ $host_id = $_REQUEST['host_id'];
+ if($host_id>=0) $SQL_picklist .= " AND host='$host_id' ";
+ }
+
+ $SQL_picklist .= " ORDER BY lastpoll DESC";
+
+ connectToDB();
+
+ $hosts_result = mysql_query("SELECT DISTINCT host FROM ports");
+ while ($hosts[] = mysql_fetch_array($hosts_result))
+?>
+
+<h3>Pick a data source:</h3>
+
+<form name="mini">
+<?php
+if(sizeof($hosts) > 0) {
+ print 'Host: <select name="host_id" onChange="applyDSFilterChange(document.mini)">';
+
+ print '<option '.($host_id==-1 ? 'SELECTED' : '' ).' value="-1">Any</option>';
+ foreach ($hosts as $host)
+ {
+ print '<option ';
+ if($host_id==$host['host']) print " SELECTED ";
+ print 'value="'.$host['host'].'">'.$host['host'].'</option>';
+ }
+ print '</select><br />';
+}
+
+ print '<span class="filter" style="display: none;">Filter: <input id="filterstring" name="filterstring" size="20"> (case-sensitive)<br /></span>';
+ print '<input id="overlib" name="overlib" type="hidden" value="yes" '.($overlib ? 'CHECKED' : '' ).'> <br />';
+
+ print '</form><div class="listcontainer"><ul id="dslist">';
+
+ $queryrows_result = mysql_query($SQL_picklist);
+ while ($queryrows[] = mysql_fetch_array($queryrows_result, MYSQL_ASSOC))
+
+ $i=0;
+ if( is_array($queryrows) && count($queryrows) > 1 )
+ {
+ foreach ($queryrows as $line) {
+ echo "<li class=\"row".($i%2)."\">";
+ $key = $line['safename']."','".$line['host']. "','" . $line['name'];
+ echo "<a href=\"#\" onclick=\"update_source_step1('$key')\">". $line['host'] . " - " . $line['name'] . " (" . $line['alias'] . ")</a>";
+ echo "</li>\n";
+
+ $i++;
+ }
+ }
+ else
+ {
+ print "<li>No results...</li>";
+ }
+
+
+?>
+</ul>
+</div>
+</body>
+</html>
+<?php
+} // This is the end of the link step
+
+# Note the lack of a node step 1. This is because we have no reason for a node to have a graph
+# picked from FITB.
+
+# Helper function to connect to the DB
+
+function connectToDB() {
+ global $database_hostname, $database_username, $database_password, $database_default;
+ if(function_exists('mysql_connect')) {
+ if(!mysql_connect($database_hostname, $database_username, $database_password)) {
+ return false;
+ }
+ if(!mysql_select_db($database_default)) {
+ return false;
+ }
+ return true;
+ } else {
+ echo "MySQL support is required";
+ return false;
+ }
+}
+
+// vim:ts=4:sw=4:
+?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment