Issues (2963)

includes/html/table/sensors-common.php (1 issue)

1
<?php
2
/*
3
 * LibreNMS
4
 *
5
 * This program is free software: you can redistribute it and/or modify it
6
 * under the terms of the GNU General Public License as published by the
7
 * Free Software Foundation, either version 3 of the License, or (at your
8
 * option) any later version.  Please see LICENSE.txt at the top level of
9
 * the source code distribution for details.
10
 *
11
 * @package    LibreNMS
12
 * @subpackage webui
13
 * @link       https://www.librenms.org
14
 * @copyright  2018 LibreNMS
15
 * @author     LibreNMS Contributors
16
*/
17
18
use LibreNMS\Config;
19
20
$graph_type = $vars['graph_type'];
21
$unit = $vars['unit'];
22
$class = $vars['class'];
23
24
$sql = " FROM `$table` AS S, `devices` AS D";
25
26
$sql .= ' WHERE S.sensor_class=? AND S.device_id = D.device_id ';
27
$param[] = $vars['class'];
28
29
if (! Auth::user()->hasGlobalRead()) {
30
    $device_ids = Permissions::devicesForUser()->toArray() ?: [0];
31
    $sql .= ' AND `D`.`device_id` IN ' . dbGenPlaceholders(count($device_ids));
32
    $param = array_merge($param, $device_ids);
33
}
34
35
if (isset($searchPhrase) && ! empty($searchPhrase)) {
36
    $sql .= ' AND (`D`.`hostname` LIKE ? OR `sensor_descr` LIKE ? OR `sensor_current` LIKE ?)';
37
    $param[] = "%$searchPhrase%";
38
    $param[] = "%$searchPhrase%";
39
    $param[] = "%$searchPhrase";
40
}
41
42
$count_sql = "SELECT COUNT(`sensor_id`) $sql";
43
44
$count = dbFetchCell($count_sql, $param);
45
if (empty($count)) {
46
    $count = 0;
47
}
48
49
if (! isset($sort) || empty($sort)) {
50
    $sort = '`D`.`hostname`, `S`.`sensor_descr`';
51
}
52
53
$sql .= " ORDER BY $sort";
54
55
if (isset($current)) {
56
    $limit_low = (($current * $rowCount) - ($rowCount));
57
    $limit_high = $rowCount;
58
}
59
60
if ($rowCount != -1) {
61
    $sql .= " LIMIT $limit_low,$limit_high";
62
}
63
64
$sql = "SELECT * $sql";
65
66
foreach (dbFetchRows($sql, $param) as $sensor) {
67
    $alert = '';
68
    if (! isset($sensor['sensor_current'])) {
69
        $sensor['sensor_current'] = 'NaN';
70
    } elseif ((! is_null($sensor['sensor_limit']) && $sensor['sensor_current'] >= $sensor['sensor_limit']) ||
71
        (! is_null($sensor['sensor_limit_low']) && $sensor['sensor_current'] <= $sensor['sensor_limit_low'])
72
    ) {
73
        $alert = '<i class="fa fa-flag fa-lg" style="color:red" aria-hidden="true"></i>';
74
    }
75
76
    // FIXME - make this "four graphs in popup" a function/include and "small graph" a function.
77
    // FIXME - So now we need to clean this up and move it into a function. Isn't it just "print-graphrow"?
78
    // FIXME - DUPLICATED IN device/overview/sensors
79
    $graph_colour = str_replace('#', '', $row_colour);
80
81
    $graph_array = [];
82
    $graph_array['height'] = '100';
83
    $graph_array['width'] = '210';
84
    $graph_array['to'] = Config::get('time.now');
85
    $graph_array['id'] = $sensor['sensor_id'];
86
    $graph_array['type'] = $graph_type;
87
    $graph_array['from'] = Config::get('time.day');
88
    $graph_array['legend'] = 'no';
89
90
    $link_array = $graph_array;
91
    $link_array['page'] = 'graphs';
92
    unset($link_array['height'], $link_array['width'], $link_array['legend']);
93
    $link_graph = \LibreNMS\Util\Url::generate($link_array);
94
95
    $link = \LibreNMS\Util\Url::generate(['page' => 'device', 'device' => $sensor['device_id'], 'tab' => $group, 'metric' => $sensor['sensor_class']]);
96
97
    $overlib_content = '<div style="width: 580px;"><span class="overlib-text">' . $sensor['hostname'] . ' - ' . $sensor['sensor_descr'] . '</span>';
98
    $even = true;
99
    foreach (['day', 'week', 'month', 'year'] as $period) {
100
        $graph_array['from'] = Config::get("time.$period");
101
        if ($even) {
102
            $overlib_content .= '<br>';
103
        }
104
        $overlib_content .= str_replace('"', "\\'", \LibreNMS\Util\Url::graphTag($graph_array));
105
        $even = ! $even;
0 ignored issues
show
The condition $even is always true.
Loading history...
106
    }
107
108
    $overlib_content .= '</div>';
109
110
    $graph_array['width'] = 80;
111
    $graph_array['height'] = 20;
112
    $graph_array['bg'] = 'ffffff00';
113
    // the 00 at the end makes the area transparent.
114
    $graph_array['from'] = Config::get('time.day');
115
    $sensor_minigraph = \LibreNMS\Util\Url::graphTag($graph_array);
116
117
    $sensor['sensor_descr'] = substr($sensor['sensor_descr'], 0, 48);
118
119
    $sensor_current = $graph_type == 'sensor_state' ? get_state_label($sensor) : get_sensor_label_color($sensor, $translations);
120
    $response[] = [
121
        'hostname'         => generate_device_link($sensor),
122
        'sensor_descr'     => \LibreNMS\Util\Url::overlibLink($link, $sensor['sensor_descr'], $overlib_content),
123
        'graph'            => \LibreNMS\Util\Url::overlibLink($link_graph, $sensor_minigraph, $overlib_content),
124
        'alert'            => $alert,
125
        'sensor_current'   => $sensor_current,
126
        'sensor_limit_low' => is_null($sensor['sensor_limit_low']) ? '-' :
127
            '<span class=\'label label-default\'>' . trim(\LibreNMS\Util\Number::formatSi($sensor['sensor_limit_low'], 2, 3, '') . $unit) . '</span>',
128
        'sensor_limit'     => is_null($sensor['sensor_limit']) ? '-' :
129
            '<span class=\'label label-default\'>' . trim(\LibreNMS\Util\Number::formatSi($sensor['sensor_limit'], 2, 3, '') . $unit) . '</span>',
130
    ];
131
132
    if ($vars['view'] == 'graphs') {
133
        $daily_graph = 'graph.php?id=' . $sensor['sensor_id'] . '&amp;type=' . $graph_type . '&amp;from=' . Config::get('time.day') . '&amp;to=' . Config::get('time.now') . '&amp;width=211&amp;height=100';
134
        $daily_url = 'graph.php?id=' . $sensor['sensor_id'] . '&amp;type=' . $graph_type . '&amp;from=' . Config::get('time.day') . '&amp;to=' . Config::get('time.now') . '&amp;width=400&amp;height=150';
135
136
        $weekly_graph = 'graph.php?id=' . $sensor['sensor_id'] . '&amp;type=' . $graph_type . '&amp;from=' . Config::get('time.week') . '&amp;to=' . Config::get('time.now') . '&amp;width=211&amp;height=100';
137
        $weekly_url = 'graph.php?id=' . $sensor['sensor_id'] . '&amp;type=' . $graph_type . '&amp;from=' . Config::get('time.week') . '&amp;to=' . Config::get('time.now') . '&amp;width=400&amp;height=150';
138
139
        $monthly_graph = 'graph.php?id=' . $sensor['sensor_id'] . '&amp;type=' . $graph_type . '&amp;from=' . Config::get('time.month') . '&amp;to=' . Config::get('time.now') . '&amp;width=211&amp;height=100';
140
        $monthly_url = 'graph.php?id=' . $sensor['sensor_id'] . '&amp;type=' . $graph_type . '&amp;from=' . Config::get('time.month') . '&amp;to=' . Config::get('time.now') . '&amp;width=400&amp;height=150';
141
142
        $yearly_graph = 'graph.php?id=' . $sensor['sensor_id'] . '&amp;type=' . $graph_type . '&amp;from=' . Config::get('time.year') . '&amp;to=' . Config::get('time.now') . '&amp;width=211&amp;height=100';
143
        $yearly_url = 'graph.php?id=' . $sensor['sensor_id'] . '&amp;type=' . $graph_type . '&amp;from=' . Config::get('time.year') . '&amp;to=' . Config::get('time.now') . '&amp;width=400&amp;height=150';
144
145
        $response[] = [
146
            'hostname'       => "<a onmouseover=\"return overlib('<img src=\'$daily_url\'>', LEFT);\" onmouseout=\"return nd();\">
147
            <img src='$daily_graph' border=0></a> ",
148
            'sensor_descr'   => "<a onmouseover=\"return overlib('<img src=\'$weekly_url\'>', LEFT);\" onmouseout=\"return nd();\">
149
            <img src='$weekly_graph' border=0></a> ",
150
            'graph'          => "<a onmouseover=\"return overlib('<img src=\'$monthly_url\'>', LEFT);\" onmouseout=\"return nd();\">
151
            <img src='$monthly_graph' border=0></a>",
152
            'alert'          => '',
153
            'sensor_current' => "<a onmouseover=\"return overlib('<img src=\'$yearly_url\'>', LEFT);\" onmouseout=\"return nd();\">
154
            <img src='$yearly_graph' border=0></a>",
155
            'sensor_range'   => '',
156
        ];
157
    } //end if
158
}//end foreach
159
160
$output = [
161
    'current'  => $current,
162
    'rowCount' => $rowCount,
163
    'rows'     => $response,
164
    'total'    => $count,
165
];
166
echo json_encode($output, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
167