Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Weathermap to work with modern LibreNMS #60

Merged
merged 2 commits into from
May 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions Weathermap.php
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
<?php

namespace LibreNMS\Plugins;

include_once 'lib/editor.inc.php';

class Weathermap {
class Weathermap
{
public static $name = 'Weathermap';

public static function menu()
{

public static function menu() {
//Include config
include_once 'config.inc.php';
include 'config.inc.php';

//Parse config files
//Parse config files
$files = list_weathermaps($mapdir);

//Create submenu
$submenu = ' <ul class="dropdown-menu scrollable-menu">';
$count = 0;
foreach ($files as $file=>$data) {
$nicefile = htmlspecialchars($file);
$submenu .= ' <li><a href="/plugins/'.get_class().'/'.htmlspecialchars($data['page']).'"><i class="fa fa-map fa-fw fa-lg" aria-hidden="true"></i> '.htmlspecialchars($data['title']).'</a></li>';
$count ++;
//Create submenu
$submenu = ' <ul class="dropdown-menu scrollable-menu">';
$count = 0;
foreach ($files as $file => $data) {
$submenu .= ' <li><a href="/plugins/' . self::$name . '/' . htmlspecialchars($data['page']) . '"><i class="fa fa-map fa-fw fa-lg" aria-hidden="true"></i> ' . htmlspecialchars($data['title']) . '</a></li>';
$count++;
}
$submenu .= ' </ul>';
$submenu .= ' </ul>';

//Display it if not empty
if ($count > 0) {
echo('<li class="dropdown-submenu"><a href="plugin/p='.get_class().'">'.get_class().'</a>');
echo('<li class="dropdown-submenu"><a href="plugin/p=' . self::$name . '">' . self::$name . '</a>');
echo $submenu;
echo ('</li>');
echo('</li>');
} else {
//Create menu without submenu
echo('<li><a href="plugin/p='.get_class().'">'.get_class().'</a></li>');
echo('<li><a href="plugin/p=' . self::$name . '">' . self::$name . '</a></li>');
}
}
}
}
?>
2 changes: 1 addition & 1 deletion config.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
$conf_dir = 'configs/';
$mapdir=dirname(__FILE__)."/". $conf_dir;

$librenms_base = '../../../';
$librenms_base = realpath(dirname(__FILE__) . '/../../../');
$librenms_url = '/';
$ignore_librenms=FALSE;
$configerror = '';
Expand Down
123 changes: 46 additions & 77 deletions data-pick.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

// ******************************************
// sensible defaults
$mapdir='configs';
$ignore_librenms=FALSE;
$config['base_url'] = '/';
include 'config.inc.php';
$whats_installed = '';

$librenms_base = '../../../';

$weathermap_config = array (
'show_interfaces' => 'all',
Expand Down Expand Up @@ -38,15 +35,16 @@
*/
/* Load Weathermap config defaults, see file for description. */

$init_modules = array('web', 'auth');
require realpath(__DIR__ . '/../../..') . '/includes/init.php';
$init_modules = ['web', 'auth'];
require $librenms_base . '/includes/init.php';

if (empty($_SESSION['authenticated']) || !isset($_SESSION['authenticated'])) {
if (!Auth::check()) {
header('Location: /');
exit;
}

chdir('plugins/Weathermap');
$librenms_found = TRUE;
chdir($librenms_base . '/plugins/Weathermap');
$librenms_found = true;

/* Validate configuration, see defaults.inc.php for explaination */
if (in_array ($config['plugins']['Weathermap']['sort_if_by'], $valid_sort_if_by))
Expand All @@ -57,8 +55,6 @@
elseif (validate_device_id ($config['plugins']['Weathermap']['show_interfaces']))
$weathermap_config['show_interfaces'] = $config['plugins']['Weathermap']['show_interfaces'];

$link = mysqli_connect($config['db_host'],$config['db_user'],$config['db_pass'],$config['db_name'],$config['db_port'])
or die('Could not connect: ' . mysqli_error($link));

// ******************************************

Expand All @@ -76,13 +72,6 @@ function js_escape($str)
{
$dataid = intval($_REQUEST['dataid']);

//$SQL_graphid = sprintf("SELECT graph_templates_item.local_graph_id, title_cache FROM graph_templates_item,graph_templates_graph,data_template_rrd where graph_templates_graph.local_graph_id = graph_templates_item.local_graph_id and task_item_id=data_template_rrd.id and local_data_id=%d LIMIT 1;",$dataid);

//mysql_selectdb($config['db_name'],$link) or die('Could not select database: '.mysql_error());

//$result = mysql_query($SQL_graphid) or die('Query failed: ' . mysql_error());
//$line = mysql_fetch_array($result, MYSQL_ASSOC);
//$graphid = $line['local_graph_id'];

?>
<html>
Expand All @@ -107,7 +96,7 @@ function update_source_step2(graphid)
self.close();
}

window.onload = update_source_step2(<?php echo $graphid ?>);
window.onload = update_source_step2(<?php echo $dataid ?>);

</script>
</head>
Expand Down Expand Up @@ -268,8 +257,6 @@ function applyDSFilterChange(objForm) {
<body>
<?php

//$SQL_picklist = "select data_local.host_id, data_template_data.local_data_id, data_template_data.name_cache, data_template_data.active, data_template_data.data_source_path from data_local,data_template_data,data_input,data_template where data_local.id=data_template_data.local_data_id and data_input.id=data_template_data.data_input_id and data_local.data_template_id=data_template.id ";

$host_id = $weathermap_config['show_interfaces'];

$overlib = true;
Expand All @@ -282,38 +269,32 @@ function applyDSFilterChange(objForm) {
if (isset ($_REQUEST['host_id']) and !empty ($_REQUEST['host_id']))
{
$host_id = intval ($_REQUEST['host_id']);
//if($host_id>=0) $SQL_picklist .= " and data_local.host_id=$host_id ";
}

/* If the editor gave us the links source node name, try to find the device_id
* so we can present the user with the interfaces of this particular device. */
if (isset ($_REQUEST['node1']) and !empty ($_REQUEST['node1']))
{
$node1 = strtolower ($_REQUEST['node1']);
$node1_id = dbFetchCell ("SELECT device_id FROM devices where hostname like ?", array ("%$node1%"));
$node1_id = \App\Models\Device::where('hostname', 'like', "%$node1%")->value('device_id');
if ($node1_id)
$host_id = $node1_id;
}

//$SQL_picklist .= " order by name_cache;";


// Link query
$result = mysqli_query($link,"SELECT device_id,hostname FROM devices ORDER BY hostname");
//$hosts = mysql_fetch_assoc($result);
//$result = mysql_query($SQL_picklist);
$hosts = 1;
$hosts = \App\Models\Device::orderBy('hostname')->get(['device_id', 'hostname']);
?>

<h3>Pick a data source:</h3>

<form name="mini">
<?php
if(sizeof($hosts) > 0) {
if($hosts->isNotEmpty()) {
print 'Host: <select name="host_id" onChange="applyDSFilterChange(document.mini)">';

print '<option '.($host_id==-1 ? 'SELECTED' : '' ).' value="-1">Any</option>';
print '<option '.($host_id==0 ? 'SELECTED' : '' ).' value="0">None</option>';
while ($host = mysqli_fetch_assoc($result))
foreach ($hosts as $host)
{
print '<option ';
if($host_id==$host['device_id']) print " SELECTED ";
Expand All @@ -334,43 +315,35 @@ function applyDSFilterChange(objForm) {
*/
$result = Null;
if ($host_id != 0) {
$query = "SELECT devices.device_id,hostname,ports.port_id,ports.ifAlias,ports.ifIndex,ports.ifDescr,ports.deleted FROM devices LEFT JOIN ports ON devices.device_id=ports.device_id WHERE ports.disabled=0";

/* ...of specific host/device? */
if($host_id > 0) {
$query .= " AND devices.device_id='$host_id'";
}

/* ...in specific order? */
$query .= " ORDER BY hostname,ports." . $weathermap_config['sort_if_by'];
$result = mysqli_query($link,$query);
$devices = \App\Models\Device::when($host_id > 0, function ($query) use ($host_id) {
$query->where('device_id', $host_id);
})
->with(['ports' => function ($query) use ($weathermap_config) {
$query->orderBy($weathermap_config['sort_if_by']);
}])
->orderBy('hostname')
->get();
}

$i=0;
if( mysqli_num_rows($result) > 0 )
{
while ($queryrows = mysqli_fetch_assoc($result)) {
echo "<li class=\"row".($i%2)."\">";
$key = $queryrows['device_id']."','".$queryrows['hostname']."','".$queryrows['port_id']."','".addslashes($queryrows['ifAlias'])."','".addslashes($queryrows['ifDescr'])."','".$queryrows['ifIndex'];
// Indicated if port is marked deleted
$deleted = $queryrows['deleted'] ? " (D)" : "";
echo "<a href=\"#\" onclick=\"update_source_step1('$key')\">". $queryrows['hostname'] . "/" . $queryrows['ifDescr'] . " Desc:" . $queryrows['ifAlias'] . "$deleted</a>";
echo "</li>\n";
$i++;
}
}
else
{
if ($devices->isNotEmpty()) {
foreach ($devices as $device) {
if (!is_null($device->ports)) {
foreach ($device->ports as $port) {
echo "<li class=\"row" . ($i % 2) . "\">";
$key = $device->device_id . "','" . $device->hostname . "','" . $port->port_id . "','" . addslashes($port->ifAlias) . "','" . addslashes($port->ifDescr) . "','" . (int)$port->ifIndex;

echo "<a href=\"#\" onclick=\"update_source_step1('$key')\">" . $device->displayName() . "/$port->ifDescr Desc: $port->ifAlias</a>";
echo "</li>\n";
}
$i++;
}

}
} else {
print "<li>No results...</li>";
}

// Free resultset
//mysql_free_result($result);

// Closing connection
//mysql_close($link);

?>
</ul>
</div>
Expand All @@ -382,8 +355,7 @@ function applyDSFilterChange(objForm) {
if(isset($_REQUEST['command']) && $_REQUEST["command"]=='node_step1')
{
$host_id = -1;
$SQL_picklist = "SELECT `device_id` AS `id`,`hostname` AS `name` FROM devices ORDER BY hostname";
//$SQL_picklist = "SELECT 1,2,'Test','Y','/dsad'";


$overlib = true;
$aggregate = false;
Expand All @@ -395,12 +367,9 @@ function applyDSFilterChange(objForm) {
if(isset($_REQUEST['host_id']))
{
$host_id = intval($_REQUEST['host_id']);
//if($host_id>=0) $SQL_picklist .= " and graph_local.host_id=$host_id ";
}
//$SQL_picklist .= " order by title_cache";

$query = mysqli_query($link,"SELECT id,hostname AS name FROM `devices` ORDER BY hostname");
$hosts = mysqli_fetch_assoc($query);

$hosts = \App\Models\Device::orderBy('hostname')->get(['device_id AS id', 'hostname AS name']);

?>
<html>
Expand Down Expand Up @@ -500,7 +469,7 @@ function update_source_step1(graphid,name)

<form name="mini">
<?php
if(sizeof($hosts) > 0) {
if($hosts->isNotEmpty()) {
print 'Host: <select name="host_id" onChange="applyDSFilterChange(document.mini)">';

print '<option '.($host_id==-1 ? 'SELECTED' : '' ).' value="-1">Any</option>';
Expand All @@ -518,15 +487,15 @@ function update_source_step1(graphid,name)
print '<input id="overlib" name="overlib" type="checkbox" value="yes" '.($overlib ? 'CHECKED' : '' ).'> <label for="overlib">Set both OVERLIBGRAPH and INFOURL.</label><br />';

print '</form><div class="listcontainer"><ul id="dslist">';
$result = mysqli_query($link,$SQL_picklist);
if( mysqli_num_rows($result) > 0)

if($hosts->isNotEmpty())
{
$i=0;
while($queryrows = mysqli_fetch_assoc($result)) {
foreach($hosts as $host) {
echo "<li class=\"row".($i%2)."\">";
$key = $queryrows['id'];
$name = $queryrows['name'];
echo "<a href=\"#\" onclick=\"update_source_step1('$key','$name')\">". $queryrows['name'] . "</a>";
$key = $host['id'];
$name = $host['name'];
echo "<a href=\"#\" onclick=\"update_source_step1('$key','$name')\">". $host['name'] . "</a>";
echo "</li>\n";
$i++;
}
Expand Down
30 changes: 12 additions & 18 deletions editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,20 @@
}

// check if the goalposts have moved
if( is_dir($librenms_base) && file_exists($librenms_base."/config.php") )
{
// include the cacti-config, so we know about the database
chdir('../../');
$init_modules = array('web', 'auth');
require realpath(__DIR__ . '/../../..') . '/includes/init.php';
if (empty($_SESSION['authenticated']) || !isset($_SESSION['authenticated']))
{
header('Location: /');
}
chdir('plugins/Weathermap');
$librenms_found = TRUE;
}
else
{
$librenms_found = FALSE;
$librenms_found = false;
if (is_dir($librenms_base) && file_exists($librenms_base . "/.env")) {
// Boot LibreNMS
$init_modules = ['web', 'auth'];
require $librenms_base . '/includes/init.php';
if (!Auth::check()) {
header('Location: /');
exit;
}
chdir($librenms_base . '/html/plugins/Weathermap');
$librenms_found = true;
}

if(! is_writable($mapdir))
{
if(! is_writable($mapdir)) {
$configerror = "The map config directory is not writable by the web server user. You will not be able to edit any files until this is corrected. [WMEDIT01]";
}

Expand Down
Loading