Skip to content

Instantly share code, notes, and snippets.

@lozzd
Created October 11, 2012 09:09
Show Gist options
  • Save lozzd/3871166 to your computer and use it in GitHub Desktop.
Save lozzd/3871166 to your computer and use it in GitHub Desktop.
FITB support for PHP Weathermap
<?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