From 4db645d42f3df582bb2ea7580d4549147e8e5149 Mon Sep 17 00:00:00 2001 From: Tony Murray Date: Wed, 22 May 2019 11:16:40 -0500 Subject: [PATCH] Update weathermap to work with modern LibreNMS --- Weathermap.php | 38 ++++++------ config.inc.php | 2 +- data-pick.php | 123 +++++++++++++++----------------------- editor.php | 30 ++++------ lib/editor.inc.php | 143 ++++++++++++++++++++++----------------------- map-poller.php | 24 ++++---- 6 files changed, 161 insertions(+), 199 deletions(-) diff --git a/Weathermap.php b/Weathermap.php index 430c390d..bd87d231 100644 --- a/Weathermap.php +++ b/Weathermap.php @@ -1,35 +1,39 @@ '; - $count = 0; - foreach ($files as $file=>$data) { - $nicefile = htmlspecialchars($file); - $submenu .= '
  • '.htmlspecialchars($data['title']).'
  • '; - $count ++; + //Create submenu + $submenu = ' '; + $submenu .= ' '; //Display it if not empty if ($count > 0) { - echo(''); + echo(''); } else { //Create menu without submenu - echo('
  • '.get_class().'
  • '); + echo('
  • ' . self::$name . '
  • '); } - } + } } -?> diff --git a/config.inc.php b/config.inc.php index 0dfc2ac2..d2773150 100644 --- a/config.inc.php +++ b/config.inc.php @@ -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 = ''; diff --git a/data-pick.php b/data-pick.php index 3d9726dd..b995691d 100755 --- a/data-pick.php +++ b/data-pick.php @@ -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', @@ -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)) @@ -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)); // ****************************************** @@ -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']; ?> @@ -107,7 +96,7 @@ function update_source_step2(graphid) self.close(); } - window.onload = update_source_step2(); + window.onload = update_source_step2(); @@ -268,8 +257,6 @@ function applyDSFilterChange(objForm) { =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 @@ -290,30 +276,25 @@ function applyDSFilterChange(objForm) { 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']); ?>

    Pick a data source:

    0) { +if($hosts->isNotEmpty()) { print 'Host: '; print ''; @@ -518,15 +487,15 @@ function update_source_step1(graphid,name) print '
    '; print '
    '; - + $errormessage = ""; if ($configerror!='') { $errormessage .= $configerror.'

    '; } - + if ( !$librenms_found && !$ignore_librenms) { //$errormessage .= '$cacti_base is not set correctly. Cacti integration will be disabled in the editor.'; //$errormessage .= "$librenms_found and $ignore_librenms"; - //if ($config_loaded != 1) { - //$errormessage .= " You might need to copy editor-config.php-dist to editor-config.php and edit it."; + //if ($config_loaded != 1) { + //$errormessage .= " You might need to copy editor-config.php-dist to editor-config.php and edit it."; //} } - + if ($errormessage != '') { print '

    '.htmlspecialchars($errormessage).'
    '; } @@ -216,7 +215,7 @@ function show_editor_startpage() print '
    NOTE: This editor is not finished! There are many features of '; print 'Weathermap that you will be missing out on if you choose to use the editor only.'; print 'These include: curves, node offsets, font definitions, colour changing, per-node/per-link settings and image uploading. You CAN use the editor without damaging these features if you added them by hand, however.

    '; - + print 'Do you want to:

    '; print 'Create A New Map:
    '; if($action == 'newmap') { @@ -243,7 +242,7 @@ function show_editor_startpage() while (false !== ($file = readdir($dh))) { $realfile=$mapdir . DIRECTORY_SEPARATOR . $file; $note = ""; - + // skip directories, unreadable files, .files and anything that doesn't come through the sanitiser unchanged if ( (is_file($realfile)) && (is_readable($realfile)) && (!preg_match("/^\./",$file) ) && ( wm_editor_sanitize_conffile($file) == $file ) ) { if (!is_writable($realfile)) { @@ -254,12 +253,12 @@ function show_editor_startpage() if ($fd) { while (!feof($fd)) { $buffer=fgets($fd, 4096); - - if (preg_match("/^\s*TITLE\s+(.*)/i", $buffer, $matches)) { - $title= wm_editor_sanitize_string($matches[1]); + + if (preg_match("/^\s*TITLE\s+(.*)/i", $buffer, $matches)) { + $title= wm_editor_sanitize_string($matches[1]); } } - + fclose ($fd); $titles[$file] = $title; $notes[$file] = $note; @@ -269,17 +268,17 @@ function show_editor_startpage() } closedir ($dh); - } else { - $errorstring = "Can't open mapdir to read."; + } else { + $errorstring = "Can't open mapdir to read."; } - + ksort($titles); - - if ($n == 0) { - $errorstring = "No files in mapdir"; + + if ($n == 0) { + $errorstring = "No files in mapdir"; } - } else { - $errorstring = "NO DIRECTORY named $mapdir"; + } else { + $errorstring = "NO DIRECTORY named $mapdir"; } print 'OR
    Create A New Map as a copy of an existing map:
    '; @@ -289,7 +288,7 @@ function show_editor_startpage() print ''; print ''; print ''; print ''; print ''; @@ -306,7 +305,7 @@ function show_editor_startpage() print 'Open An Existing Map (looking in ' . htmlspecialchars($mapdir) . '):