Observium_CE/html/pages/graphs.inc.php

490 lines
17 KiB
PHP

<?php
/**
* Observium
*
* This file is part of Observium.
*
* @package observium
* @subpackage web
* @copyright (C) 2006-2013 Adam Armstrong, (C) 2013-2023 Observium Limited
*
*/
unset($vars['page']);
// Setup here
//if (isset($_SESSION['widescreen']))
//{
// $graph_width=1700;
// $thumb_width=180;
//} else {
$graph_width = 1158;
$thumb_width = 150;
//}
// Timestamps from Graphs page form, convert to common from/to unixtime
if (isset($vars['timestamp_from']) && preg_match(OBS_PATTERN_TIMESTAMP, $vars['timestamp_from'])) {
$vars['from'] = strtotime($vars['timestamp_from']);
unset($vars['timestamp_from']);
}
if (isset($vars['timestamp_to']) && preg_match(OBS_PATTERN_TIMESTAMP, $vars['timestamp_to'])) {
$vars['to'] = strtotime($vars['timestamp_to']);
unset($vars['timestamp_to']);
}
// Validate rrdtool compatible time string and set to now/day if it's not valid
if (preg_match(OBS_PATTERN_RRDTIME, $vars['to'])) {
$to = $vars['to'];
} // else { $to = $config['time']['now']; }
if (preg_match(OBS_PATTERN_RRDTIME, $vars['from'])) {
$from = $vars['from'];
} // else { $from = $config['time']['day']; }
preg_match(OBS_PATTERN_GRAPH_TYPE, $vars['type'], $graphtype);
print_debug_vars($graphtype, 1);
$type = $graphtype['type'];
$subtype = $graphtype['subtype'];
if (is_numeric($vars['device'])) {
$device = device_by_id_cache($vars['device']);
} elseif (!empty($vars['device'])) {
$device = device_by_name($vars['device']);
}
if (is_file($config['html_dir'] . "/includes/graphs/" . $type . "/auth.inc.php")) {
include($config['html_dir'] . "/includes/graphs/" . $type . "/auth.inc.php");
}
if (!$auth) {
print_error_permission();
return;
}
// If we have a group_id, assume id was generated by auth.inc.php and unset it. It breaks graph url generation otherwise.
if (isset($vars['group_id'], $vars['id'])) {
unset($vars['id']);
}
// Print the device header
if (isset($device) && is_array($device)) {
$args = [];
if ($type === 'device') {
// device have own panel, hide on xl
$args['div-class'] = 'hidden-xl';
}
print_device_header($device, $args);
}
// Generate navbar with subtypes
$graph_array = $vars;
$graph_array['height'] = "60";
$graph_array['width'] = $thumb_width;
// Clear collectd argument forcing axis rendering
if (isset($graph_array['draw_all'])) {
unset($graph_array['draw_all']);
}
//$graph_array['legend'] = "no";
//$graph_array['to'] = $config['time']['now'];
$navbar = ['brand' => "Graph", 'class' => "navbar-narrow"];
switch ($type) {
case 'device':
case 'sensor':
case 'cefswitching':
case 'munin':
$navbar['options']['graph'] = ['text' => nicecase($type) . ' (' . $subtype . ')',
'url' => generate_url($vars, ['type' => $type . "_" . $subtype, 'page' => "graphs"])];
break;
default:
# Load our list of available graphtypes for this object
/// FIXME not all of these are going to be valid
/// This is terrible. --mike
/// The future solution is to keep a 'registry' of which graphtypes apply to which entities and devices.
/// I'm not quite sure if this is going to be too slow. --adama 2013-11-11
$types = [];
if ($handle = opendir($config['html_dir'] . "/includes/graphs/" . $type . "/")) {
while (FALSE !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file != "auth.inc.php" && $file != "graph.inc.php" && $file != ".auth.swp" && strstr($file, ".inc.php")) {
$types[] = str_replace(".inc.php", "", $file);
}
}
closedir($handle);
}
foreach ($title_array as $key => $element) {
$navbar['options'][$key] = $element;
}
$navbar['options']['graph'] = ['text' => 'Graph'];
sort($types);
foreach ($types as $avail_type) {
if ($subtype == $avail_type) {
$navbar['options']['graph']['suboptions'][$avail_type]['class'] = 'active';
$navbar['options']['graph']['text'] .= ' (' . $avail_type . ')';
}
$navbar['options']['graph']['suboptions'][$avail_type]['text'] = nicecase($avail_type);
$navbar['options']['graph']['suboptions'][$avail_type]['url'] = generate_url($vars, ['type' => $type . "_" . $avail_type, 'page' => "graphs"]);
}
}
// Adding to the dashboard
if ($_SESSION['userlevel'] > 7) {
$dashboards = dbFetchRows("SELECT * FROM `dashboards`");
// FIXME - widget_exists() dashboard_exists(), widget_permitted(), dashboard_permitted(), etc.
// FIXME - convert this to ajax call, maybe make the code usable on other pages too
$valid = ['id', 'group_id', 'idb', 'idc', 'device', 'c_plugin', 'c_plugin_instance', 'c_type', 'c_type_instance'];
$add_array = ['type' => $vars['type']];
if (isset($vars['period']) && is_numeric($vars['period'])) {
$add_array['period'] = $vars['period'];
}
foreach ($vars as $var => $value) {
if (in_array($var, $valid)) {
$add_array[$var] = $value;
}
}
$dash_add_key = 'widget_graph_' . $vars['dash_add'] . '_' . var_encode($add_array); // prevent clone widget graphs
if (isset($vars['dash_add']) && empty($_SESSION[$dash_add_key]) && dashboard_exists($vars['dash_add'])) {
$widget_id = dbInsert(['dash_id' => $vars['dash_add'],
'widget_config' => safe_json_encode($add_array),
'widget_type' => 'graph',
'x' => 0, 'y' => 99, 'width' => 3, 'height' => 2], 'dash_widgets');
print_message('Graph widget added to dashboard.', 'info');
unset($vars['dash_add']);
session_set_var($dash_add_key, 1);
}
if (isset($vars['dash_add_widget'])) {
dbUpdate(['widget_config' => json_encode($add_array)], 'dash_widgets', '`widget_id` = ?', [$vars['dash_add_widget']]);
if (dbAffectedRows() == 1) {
print_message("Widget updated.", 'info');
}
unset($vars['dash_add_widget']);
}
$navbar['options_right']['export']['text'] = "Export Data";
foreach ($config['graph_formats'] as $format => $entry) {
$export_array = $graph_array;
$export_array['format'] = $format;
$navbar['options_right']['export']['suboptions'][$format]['text'] = $entry['descr'];
$navbar['options_right']['export']['suboptions'][$format]['url'] = generate_graph_url($export_array);
$navbar['options_right']['export']['suboptions'][$format]['link_opts'] = 'target="_blank"';
}
if (!safe_empty($dashboards)) {
$navbar['options_right']['dash']['text'] = "Add to Dashboard";
foreach ($dashboards as $dash) {
$navbar['options_right']['dash']['suboptions'][$dash['dash_id']]['text'] = "Add to " . $dash['dash_name'];
$navbar['options_right']['dash']['suboptions'][$dash['dash_id']]['url'] = generate_url($vars, ['page' => "graphs", 'dash_add' => $dash['dash_id']]);
/* Disable adding to specific widgets, the menu doesn't expand.
$widgets = dbFetchRows("SELECT * FROM `dash_widgets` WHERE `dash_id` = ? AND widget_type = 'graph' AND `widget_config` = ?", array($dash['dash_id'], '[]'));
foreach($widgets as $widget)
{
$navbar['options_right']['dash']['suboptions'][$dash['dash_id']]['entries'][$widget['widget_id']]['text'] = "Add to Widget #".$widget['widget_id']."";
$navbar['options_right']['dash']['suboptions'][$dash['dash_id']]['entries'][$widget['widget_id']]['url'] = generate_url($vars, array('page' => "graphs", 'dash_add_widget' => $widget['widget_id']));
}
*/
}
}
}
print_navbar($navbar);
// Start form for the custom range.
echo generate_box_open([ 'box-style' => 'padding-bottom: 5px;' ]);
$thumb_array = [
'sixhour' => '6 Hours',
'day' => '24 Hours',
'twoday' => '48 Hours',
'week' => 'One Week',
//'twoweek' => 'Two Weeks',
'month' => 'One Month',
'threemonth' => 'Three Months',
'year' => 'One Year',
'threeyear' => 'Three Years'
];
$periods = [
'21600' => '6 Hours',
'86400' => '1 Day',
'172800' => '2 Days',
'604800' => 'One Week',
//'1209600' => 'Two Weeks',
'2628000' => 'One Month',
'7884000' => 'Three Months',
'31536000' => 'One Year',
'94608000' => 'Three Years'
];
echo('<table style="width: 100%; background: transparent;"><tr>');
foreach ($periods as $period => $text) {
$graph_array['period'] = $period;
$graph_array['loading'] = 'lazy';
// We need to remove the from and to variables, as these graphs are built using the period instead
$remove_vars = ['from', 'to'];
foreach ($remove_vars as $remove_var) {
if (isset($graph_array[$remove_var])) {
unset($graph_array[$remove_var]);
}
}
$link_array = $graph_array;
$link_array['page'] = "graphs";
$link = generate_url($link_array);
echo '<td style="text-align: center;">';
echo '<span class="device-head">' . $text . '</span><br />';
echo '<a href="' . $link . '">';
echo generate_graph_tag($graph_array);
echo '</a>';
echo '</td>';
}
unset($graph_array['loading']);
echo '</tr></table>';
$graph_array = $vars;
$graph_array['height'] = "300";
$graph_array['width'] = $graph_width;
echo generate_box_close();
$form_vars = $vars;
unset($form_vars['from'], $form_vars['to'], $form_vars['period']);
$form = [
'type' => 'rows',
'space' => '5px',
'submit_by_key' => TRUE,
'url' => 'graphs' . generate_url($form_vars)
];
if (is_numeric($vars['from']) && $vars['from'] < 0) {
$text_from = time() + $vars['from'];
} elseif (is_numeric($vars['from'])) {
$text_from = date('Y-m-d H:i:s', $vars['from']);
}
if (is_numeric($vars['to']) && $vars['to'] < 0) {
$text_to = time() + $vars['to'];
} elseif ($vars['to'] === 'now' || $vars['to'] === "NOW") {
$text_to = time();
} elseif (is_numeric($vars['to'])) {
$text_to = date('Y-m-d H:i:s', $vars['to']);
}
if ($vars['to'] === 'now' || $vars['to'] === "NOW") {
$text_to = date('Y-m-d H:i:s');
} elseif (is_numeric($vars['to'])) {
$text_to = date('Y-m-d H:i:s', $vars['to']);
} else {
$text_to = $vars['to'];
}
if (isset($vars['period']) && (!isset($vars['from'], $vars['to']))) {
$text_to = date('Y-m-d H:i:s');
$text_from = date('Y-m-d H:i:s', time() - $vars['period']);
}
// Datetime Field
$form['row'][0]['timestamp'] = [
'type' => 'datetime',
'grid' => 10,
'grid_xs' => 10,
//'width' => '70%',
//'div_class' => 'text-nowrap col-sm-push-0', // Too hard, will fix later
//'div_class' => 'col-lg-10 col-md-10 col-sm-10 col-xs-10',
'presets' => TRUE,
'min' => '2007-04-03 16:06:59', // Hehe, who will guess what this date/time means? --mike
// First commit! Though Observium was already 7 months old by that point. --adama
'max' => date('Y-m-d 23:59:59'), // Today
'from' => $text_from,
'to' => $text_to
];
$search_grid = 2;
if ($type === "port") {
if ($subtype === "bits") {
$speed_list = ['auto' => 'Autoscale', 'speed' => 'Interface Speed (' . format_bps($port['ifSpeed'], 4, 4) . ')'];
foreach ($config['graphs']['ports_scale_list'] as $entry) {
$speed = (int)unit_string_to_numeric($entry, 1000);
$speed_list[$entry] = format_bps($speed, 4, 4);
}
$form['row'][0]['scale'] = [
'type' => 'select', // Type
'name' => 'Scale', // Displayed title for item
'grid' => 2,
'width' => '100%',
'value' => $vars['scale'] ?? $config['graphs']['ports_scale_default'],
'values' => $speed_list
];
//reduce timestamp element grid sizes
$form['row'][0]['timestamp']['grid'] -= 2;
}
if (in_array($subtype, [ 'bits', 'percent', 'upkts', 'pktsize' ])) {
$form['row'][0]['style'] = [
'type' => 'select',
'name' => 'Graph style',
'grid' => 2,
'width' => '100%',
'value' => $vars['style'] ?? $config['graphs']['style'],
'values' => [ 'default' => 'Default', 'mrtg' => 'MRTG' ]
];
//reduce timestamp element grid sizes
$form['row'][0]['timestamp']['grid'] -= 1;
unset($form['row'][0]['timestamp']['grid_xs']);
$search_grid = 1;
}
}
// Update button
$form['row'][0]['update'] = [
'type' => 'submit',
//'name' => 'Search',
//'icon' => 'icon-search',
//'div_class' => 'col-lg-2 col-md-2 col-sm-2 col-xs-2',
'grid' => $search_grid,
'grid_xs' => $search_grid > 1 ? $search_grid : 12,
'right' => TRUE
];
print_form($form);
unset($form, $speed_list, $speed, $search_grid);
// Run the graph to get data array out of it
$vars = array_merge($vars, $graph_array);
$vars['command_only'] = 1;
include($config['html_dir'] . "/includes/graphs/graph.inc.php");
unset($vars['command_only']);
// Print options navbar
$navbar = [];
$navbar['brand'] = "Options";
$navbar['class'] = "navbar-narrow";
$navbar['options']['legend'] = [ 'text' => 'Show Legend', 'inverse' => TRUE ];
$navbar['options']['title'] = [ 'text' => 'Show Title' ];
$navbar['options']['force_autoscale'] = [ 'text' => 'Force Autoscale' ];
$navbar['options']['previous'] = [ 'text' => 'Graph Previous' ];
if (in_array('95th', (array)$graph_return['valid_options'])) {
$navbar['options']['95th'] = [ 'text' => '95th %ile', 'inverse' => TRUE ];
}
if (in_array('trend', (array)$graph_return['valid_options'])) {
$navbar['options']['trend'] = [ 'text' => 'Graph Trend' ];
}
//$navbar['options']['max'] = array('text' => 'Graph Maximum');
if (in_array('inverse', (array)$graph_return['valid_options'])) {
$navbar['options']['inverse'] = [ 'text' => 'Invert Graph' ];
}
if (in_array('line_graph', (array)$graph_return['valid_options'])) {
$navbar['options']['line_graph'] = [ 'text' => 'Line Graph' ];
}
$navbar['options_right']['showcommand'] = ['text' => 'RRD Command'];
foreach ([ 'options' => $navbar['options'], 'options_right' => $navbar['options_right'] ] as $side => $options) {
foreach ($options as $option => $array) {
if ($array['inverse']) {
if (isset($vars[$option]) && get_var_false($vars[$option])) {
$navbar[$side][$option]['url'] = generate_url($vars, ['page' => "graphs", $option => NULL]);
} else {
$navbar[$side][$option]['url'] = generate_url($vars, ['page' => "graphs", $option => 'no']);
$navbar[$side][$option]['class'] .= " active";
}
} else {
if (get_var_true($vars[$option])) {
$navbar[$side][$option]['url'] = generate_url($vars, ['page' => "graphs", $option => NULL]);
$navbar[$side][$option]['class'] .= " active";
} else {
$navbar[$side][$option]['url'] = generate_url($vars, ['page' => "graphs", $option => 'yes']);
}
}
}
}
$navbar['options_right']['graph_link'] = ['text' => 'Link to Graph', 'url' => generate_graph_url($graph_array), 'link_opts' => 'target="_blank"'];
print_navbar($navbar);
unset($navbar);
/// End options navbar
echo generate_graph_js_state($graph_array);
echo generate_box_open();
$graph_array['fetchpriority'] = 'high'; // Load the main graph first
echo generate_graph_tag($graph_array);
echo generate_box_close();
if (!empty($graph_return['descr'])) {
echo generate_box_open(['title' => 'Description', 'padding' => TRUE]);
echo($graph_return['descr']);
echo generate_box_close();
}
//r($graph_return);
if (isset($vars['showcommand'])) {
echo generate_box_open(['title' => 'Performance & Output', 'padding' => TRUE]);
echo("RRDTool Output: " . escape_html($graph_return['output']) . "<br />
RRDtool Runtime: " . number_format($graph_return['runtime'], 3) . "s |
Total time: " . number_format($graph_return['total'], 3) . "s");
echo generate_box_close();
echo generate_box_open(['title' => 'RRDTool Command', 'padding' => TRUE]);
echo escape_html($graph_return['command']);
echo generate_box_close();
echo generate_box_open(['title' => 'RRDTool Files Used', 'padding' => TRUE]);
if (is_array($graph_return['rrds'])) {
foreach ($graph_return['rrds'] as $key => $val) {
if (is_array($val)) {
echo "$key [";
echo "[" . implode("] [", $val) . "]";
echo "]<br />";
} else {
echo "$val <br />";
}
}
} else {
echo "No RRD information returned. This may be because the graph module doesn't yet return this data. <br />";
}
echo generate_box_close();
}
// EOF