Issues (2963)

includes/html/table/storage.inc.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\Util\Number;
19
20
$graph_type = 'storage_usage';
21
22
$where = 1;
23
$param = [];
24
25
$sql = ' FROM `storage` AS `S` LEFT JOIN `devices` AS `D` ON `S`.`device_id` = `D`.`device_id`';
26
27
if (! Auth::user()->hasGlobalRead()) {
28
    $device_ids = Permissions::devicesForUser()->toArray() ?: [0];
0 ignored issues
show
The method devicesForUser() does not exist on App\Facades\Permissions. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
    $device_ids = Permissions::/** @scrutinizer ignore-call */ devicesForUser()->toArray() ?: [0];
Loading history...
29
    $where .= ' AND `S`.`device_id` IN ' . dbGenPlaceholders(count($device_ids));
30
    $param = array_merge($param, $device_ids);
31
}
32
33
$sql .= " WHERE $where";
34
35
if (isset($searchPhrase) && ! empty($searchPhrase)) {
36
    $sql .= ' AND (`hostname` LIKE ? OR `storage_descr` LIKE ?)';
37
    $param[] = "%$searchPhrase%";
38
    $param[] = "%$searchPhrase%";
39
}
40
41
$count_sql = "SELECT COUNT(`storage_id`) $sql";
42
43
$count = dbFetchCell($count_sql, $param);
44
if (empty($count)) {
45
    $count = 0;
46
}
47
48
if (! isset($sort) || empty($sort)) {
49
    $sort = '`D`.`hostname`, `S`.`storage_descr`';
50
}
51
52
$sql .= " ORDER BY $sort";
53
54
if (isset($current)) {
55
    $limit_low = (($current * $rowCount) - ($rowCount));
56
    $limit_high = $rowCount;
57
}
58
59
if ($rowCount != -1) {
60
    $sql .= " LIMIT $limit_low,$limit_high";
61
}
62
63
$sql = "SELECT * $sql";
64
65
foreach (dbFetchRows($sql, $param) as $drive) {
66
    $perc = round($drive['storage_perc']);
67
    $total = Number::formatBi($drive['storage_size']);
68
    $free = Number::formatBi($drive['storage_free']);
69
    $used = Number::formatBi($drive['storage_used']);
70
71
    $graph_array['type'] = $graph_type;
72
    $graph_array['id'] = $drive['storage_id'];
73
    $graph_array['from'] = \LibreNMS\Config::get('time.day');
74
    $graph_array['to'] = \LibreNMS\Config::get('time.now');
75
    $graph_array['height'] = '20';
76
    $graph_array['width'] = '80';
77
    $graph_array_zoom = $graph_array;
78
    $graph_array_zoom['height'] = '150';
79
    $graph_array_zoom['width'] = '400';
80
    $link = 'graphs/id=' . $graph_array['id'] . '/type=' . $graph_array['type'] . '/from=' . $graph_array['from'] . '/to=' . $graph_array['to'] . '/';
81
    $mini_graph = \LibreNMS\Util\Url::overlibLink($link, \LibreNMS\Util\Url::graphTag($graph_array), \LibreNMS\Util\Url::graphTag($graph_array_zoom));
82
    $background = \LibreNMS\Util\Colors::percentage($perc, $drive['storage_perc_warn']);
83
    $bar_link = \LibreNMS\Util\Url::overlibLink($link, print_percentage_bar(400, 20, $perc, "$used / $total", 'ffffff', $background['left'], $free, 'ffffff', $background['right']), \LibreNMS\Util\Url::graphTag($graph_array_zoom));
84
85
    $response[] = [
86
        'hostname'      => generate_device_link($drive),
87
        'storage_descr' => $drive['storage_descr'],
88
        'graph'         => $mini_graph,
89
        'storage_used'  => $bar_link,
90
        'storage_perc'  => $perc . '%',
91
    ];
92
    if ($vars['view'] == 'graphs') {
93
        $graph_array['height'] = '100';
94
        $graph_array['width'] = '216';
95
        $graph_array['to'] = \LibreNMS\Config::get('time.now');
96
        $graph_array['id'] = $drive['storage_id'];
97
        $graph_array['type'] = $graph_type;
98
99
        $return_data = true;
100
        include 'includes/html/print-graphrow.inc.php';
101
        unset($return_data);
102
        $response[] = [
103
            'hostname'      => $graph_data[0],
104
            'storage_descr' => $graph_data[1],
105
            'graph'         => $graph_data[2],
106
            'storage_used'  => $graph_data[3],
107
            'storage_perc'  => '',
108
        ];
109
    } //end if
110
}//end foreach
111
112
$output = [
113
    'current'  => $current,
114
    'rowCount' => $rowCount,
115
    'rows'     => $response,
116
    'total'    => $count,
117
];
118
echo json_encode($output, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
119