Skip to content

Commit 4db645d

Browse files
committed
Update weathermap to work with modern LibreNMS
1 parent 924bdd2 commit 4db645d

File tree

6 files changed

+161
-199
lines changed

6 files changed

+161
-199
lines changed

Weathermap.php

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,39 @@
11
<?php
22

3+
namespace LibreNMS\Plugins;
4+
35
include_once 'lib/editor.inc.php';
46

5-
class Weathermap {
7+
class Weathermap
8+
{
9+
public static $name = 'Weathermap';
10+
11+
public static function menu()
12+
{
613

7-
public static function menu() {
814
//Include config
9-
include_once 'config.inc.php';
15+
include 'config.inc.php';
1016

11-
//Parse config files
17+
//Parse config files
1218
$files = list_weathermaps($mapdir);
1319

14-
//Create submenu
15-
$submenu = ' <ul class="dropdown-menu scrollable-menu">';
16-
$count = 0;
17-
foreach ($files as $file=>$data) {
18-
$nicefile = htmlspecialchars($file);
19-
$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>';
20-
$count ++;
20+
//Create submenu
21+
$submenu = ' <ul class="dropdown-menu scrollable-menu">';
22+
$count = 0;
23+
foreach ($files as $file => $data) {
24+
$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>';
25+
$count++;
2126
}
22-
$submenu .= ' </ul>';
27+
$submenu .= ' </ul>';
2328

2429
//Display it if not empty
2530
if ($count > 0) {
26-
echo('<li class="dropdown-submenu"><a href="plugin/p='.get_class().'">'.get_class().'</a>');
31+
echo('<li class="dropdown-submenu"><a href="plugin/p=' . self::$name . '">' . self::$name . '</a>');
2732
echo $submenu;
28-
echo ('</li>');
33+
echo('</li>');
2934
} else {
3035
//Create menu without submenu
31-
echo('<li><a href="plugin/p='.get_class().'">'.get_class().'</a></li>');
36+
echo('<li><a href="plugin/p=' . self::$name . '">' . self::$name . '</a></li>');
3237
}
33-
}
38+
}
3439
}
35-
?>

config.inc.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
$conf_dir = 'configs/';
66
$mapdir=dirname(__FILE__)."/". $conf_dir;
77

8-
$librenms_base = '../../../';
8+
$librenms_base = realpath(dirname(__FILE__) . '/../../../');
99
$librenms_url = '/';
1010
$ignore_librenms=FALSE;
1111
$configerror = '';

data-pick.php

Lines changed: 46 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22

33
// ******************************************
44
// sensible defaults
5-
$mapdir='configs';
6-
$ignore_librenms=FALSE;
7-
$config['base_url'] = '/';
5+
include 'config.inc.php';
86
$whats_installed = '';
97

10-
$librenms_base = '../../../';
118

129
$weathermap_config = array (
1310
'show_interfaces' => 'all',
@@ -38,15 +35,16 @@
3835
*/
3936
/* Load Weathermap config defaults, see file for description. */
4037

41-
$init_modules = array('web', 'auth');
42-
require realpath(__DIR__ . '/../../..') . '/includes/init.php';
38+
$init_modules = ['web', 'auth'];
39+
require $librenms_base . '/includes/init.php';
4340

44-
if (empty($_SESSION['authenticated']) || !isset($_SESSION['authenticated'])) {
41+
if (!Auth::check()) {
4542
header('Location: /');
43+
exit;
4644
}
4745

48-
chdir('plugins/Weathermap');
49-
$librenms_found = TRUE;
46+
chdir($librenms_base . '/plugins/Weathermap');
47+
$librenms_found = true;
5048

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

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

6359
// ******************************************
6460

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

79-
//$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);
80-
81-
//mysql_selectdb($config['db_name'],$link) or die('Could not select database: '.mysql_error());
82-
83-
//$result = mysql_query($SQL_graphid) or die('Query failed: ' . mysql_error());
84-
//$line = mysql_fetch_array($result, MYSQL_ASSOC);
85-
//$graphid = $line['local_graph_id'];
8675

8776
?>
8877
<html>
@@ -107,7 +96,7 @@ function update_source_step2(graphid)
10796
self.close();
10897
}
10998

110-
window.onload = update_source_step2(<?php echo $graphid ?>);
99+
window.onload = update_source_step2(<?php echo $dataid ?>);
111100

112101
</script>
113102
</head>
@@ -268,8 +257,6 @@ function applyDSFilterChange(objForm) {
268257
<body>
269258
<?php
270259

271-
//$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 ";
272-
273260
$host_id = $weathermap_config['show_interfaces'];
274261

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

288274
/* If the editor gave us the links source node name, try to find the device_id
289275
* so we can present the user with the interfaces of this particular device. */
290276
if (isset ($_REQUEST['node1']) and !empty ($_REQUEST['node1']))
291277
{
292278
$node1 = strtolower ($_REQUEST['node1']);
293-
$node1_id = dbFetchCell ("SELECT device_id FROM devices where hostname like ?", array ("%$node1%"));
279+
$node1_id = \App\Models\Device::where('hostname', 'like', "%$node1%")->value('device_id');
294280
if ($node1_id)
295281
$host_id = $node1_id;
296282
}
297-
298-
//$SQL_picklist .= " order by name_cache;";
299-
283+
300284
// Link query
301-
$result = mysqli_query($link,"SELECT device_id,hostname FROM devices ORDER BY hostname");
302-
//$hosts = mysql_fetch_assoc($result);
303-
//$result = mysql_query($SQL_picklist);
304-
$hosts = 1;
285+
$hosts = \App\Models\Device::orderBy('hostname')->get(['device_id', 'hostname']);
305286
?>
306287

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

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

314295
print '<option '.($host_id==-1 ? 'SELECTED' : '' ).' value="-1">Any</option>';
315296
print '<option '.($host_id==0 ? 'SELECTED' : '' ).' value="0">None</option>';
316-
while ($host = mysqli_fetch_assoc($result))
297+
foreach ($hosts as $host)
317298
{
318299
print '<option ';
319300
if($host_id==$host['device_id']) print " SELECTED ";
@@ -334,43 +315,35 @@ function applyDSFilterChange(objForm) {
334315
*/
335316
$result = Null;
336317
if ($host_id != 0) {
337-
$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";
338-
339-
/* ...of specific host/device? */
340-
if($host_id > 0) {
341-
$query .= " AND devices.device_id='$host_id'";
342-
}
343-
344-
/* ...in specific order? */
345-
$query .= " ORDER BY hostname,ports." . $weathermap_config['sort_if_by'];
346-
$result = mysqli_query($link,$query);
318+
$devices = \App\Models\Device::when($host_id > 0, function ($query) use ($host_id) {
319+
$query->where('device_id', $host_id);
320+
})
321+
->with(['ports' => function ($query) use ($weathermap_config) {
322+
$query->orderBy($weathermap_config['sort_if_by']);
323+
}])
324+
->orderBy('hostname')
325+
->get();
347326
}
348327

349328
$i=0;
350-
if( mysqli_num_rows($result) > 0 )
351-
{
352-
while ($queryrows = mysqli_fetch_assoc($result)) {
353-
echo "<li class=\"row".($i%2)."\">";
354-
$key = $queryrows['device_id']."','".$queryrows['hostname']."','".$queryrows['port_id']."','".addslashes($queryrows['ifAlias'])."','".addslashes($queryrows['ifDescr'])."','".$queryrows['ifIndex'];
355-
// Indicated if port is marked deleted
356-
$deleted = $queryrows['deleted'] ? " (D)" : "";
357-
echo "<a href=\"#\" onclick=\"update_source_step1('$key')\">". $queryrows['hostname'] . "/" . $queryrows['ifDescr'] . " Desc:" . $queryrows['ifAlias'] . "$deleted</a>";
358-
echo "</li>\n";
359-
360-
$i++;
361-
}
362-
}
363-
else
364-
{
329+
if ($devices->isNotEmpty()) {
330+
foreach ($devices as $device) {
331+
if (!is_null($device->ports)) {
332+
foreach ($device->ports as $port) {
333+
echo "<li class=\"row" . ($i % 2) . "\">";
334+
$key = $device->device_id . "','" . $device->hostname . "','" . $port->port_id . "','" . addslashes($port->ifAlias) . "','" . addslashes($port->ifDescr) . "','" . (int)$port->ifIndex;
335+
336+
echo "<a href=\"#\" onclick=\"update_source_step1('$key')\">" . $device->displayName() . "/$port->ifDescr Desc: $port->ifAlias</a>";
337+
echo "</li>\n";
338+
}
339+
$i++;
340+
}
341+
342+
}
343+
} else {
365344
print "<li>No results...</li>";
366345
}
367346

368-
// Free resultset
369-
//mysql_free_result($result);
370-
371-
// Closing connection
372-
//mysql_close($link);
373-
374347
?>
375348
</ul>
376349
</div>
@@ -382,8 +355,7 @@ function applyDSFilterChange(objForm) {
382355
if(isset($_REQUEST['command']) && $_REQUEST["command"]=='node_step1')
383356
{
384357
$host_id = -1;
385-
$SQL_picklist = "SELECT `device_id` AS `id`,`hostname` AS `name` FROM devices ORDER BY hostname";
386-
//$SQL_picklist = "SELECT 1,2,'Test','Y','/dsad'";
358+
387359

388360
$overlib = true;
389361
$aggregate = false;
@@ -395,12 +367,9 @@ function applyDSFilterChange(objForm) {
395367
if(isset($_REQUEST['host_id']))
396368
{
397369
$host_id = intval($_REQUEST['host_id']);
398-
//if($host_id>=0) $SQL_picklist .= " and graph_local.host_id=$host_id ";
399370
}
400-
//$SQL_picklist .= " order by title_cache";
401-
402-
$query = mysqli_query($link,"SELECT id,hostname AS name FROM `devices` ORDER BY hostname");
403-
$hosts = mysqli_fetch_assoc($query);
371+
372+
$hosts = \App\Models\Device::orderBy('hostname')->get(['device_id AS id', 'hostname AS name']);
404373

405374
?>
406375
<html>
@@ -500,7 +469,7 @@ function update_source_step1(graphid,name)
500469

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

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

520489
print '</form><div class="listcontainer"><ul id="dslist">';
521-
$result = mysqli_query($link,$SQL_picklist);
522-
if( mysqli_num_rows($result) > 0)
490+
491+
if($hosts->isNotEmpty())
523492
{
524493
$i=0;
525-
while($queryrows = mysqli_fetch_assoc($result)) {
494+
foreach($hosts as $host) {
526495
echo "<li class=\"row".($i%2)."\">";
527-
$key = $queryrows['id'];
528-
$name = $queryrows['name'];
529-
echo "<a href=\"#\" onclick=\"update_source_step1('$key','$name')\">". $queryrows['name'] . "</a>";
496+
$key = $host['id'];
497+
$name = $host['name'];
498+
echo "<a href=\"#\" onclick=\"update_source_step1('$key','$name')\">". $host['name'] . "</a>";
530499
echo "</li>\n";
531500
$i++;
532501
}

editor.php

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,20 @@
3030
}
3131

3232
// check if the goalposts have moved
33-
if( is_dir($librenms_base) && file_exists($librenms_base."/config.php") )
34-
{
35-
// include the cacti-config, so we know about the database
36-
chdir('../../');
37-
$init_modules = array('web', 'auth');
38-
require realpath(__DIR__ . '/../../..') . '/includes/init.php';
39-
if (empty($_SESSION['authenticated']) || !isset($_SESSION['authenticated']))
40-
{
41-
header('Location: /');
42-
}
43-
chdir('plugins/Weathermap');
44-
$librenms_found = TRUE;
45-
}
46-
else
47-
{
48-
$librenms_found = FALSE;
33+
$librenms_found = false;
34+
if (is_dir($librenms_base) && file_exists($librenms_base . "/config.php")) {
35+
// Boot LibreNMS
36+
$init_modules = ['web', 'auth'];
37+
require $librenms_base . '/includes/init.php';
38+
if (!Auth::check()) {
39+
header('Location: /');
40+
exit;
41+
}
42+
chdir($librenms_base . '/html/plugins/Weathermap');
43+
$librenms_found = true;
4944
}
5045

51-
if(! is_writable($mapdir))
52-
{
46+
if(! is_writable($mapdir)) {
5347
$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]";
5448
}
5549

0 commit comments

Comments
 (0)