Issues (2963)

includes/polling/availability.inc.php (2 issues)

1
<?php
2
3
use LibreNMS\Config;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Config. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
4
use LibreNMS\RRD\RrdDefinition;
5
6
$os->enableGraph('availability');
7
8
$col = dbFetchColumn('SELECT duration FROM availability WHERE device_id = ?', [$device['device_id']]);
9
foreach (Config::get('graphing.availability') as $duration) {
10
    if (! in_array($duration, $col)) {
11
        $data = ['device_id' => $device['device_id'],
12
            'duration' => $duration, ];
13
        dbInsert($data, 'availability');
14
    }
15
}
16
17
echo 'Availability: ' . PHP_EOL;
18
19
foreach (dbFetchRows('SELECT * FROM availability WHERE device_id = ?', [$device['device_id']]) as $row) {
20
    //delete not more interested availabilities
21
    if (! in_array($row['duration'], Config::get('graphing.availability'))) {
0 ignored issues
show
It seems like LibreNMS\Config::get('graphing.availability') can also be of type null; however, parameter $haystack of in_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

21
    if (! in_array($row['duration'], /** @scrutinizer ignore-type */ Config::get('graphing.availability'))) {
Loading history...
22
        dbDelete('availability', 'availability_id=?', [$row['availability_id']]);
23
        continue;
24
    }
25
26
    $avail = \LibreNMS\Device\Availability::availability($device, $row['duration']);
27
    $human_time = \LibreNMS\Util\Time::humanTime($row['duration']);
28
29
    $rrd_name = ['availability', $row['duration']];
30
    $rrd_def = RrdDefinition::make()
31
        ->addDataset('availability', 'GAUGE', 0);
32
33
    $fields = [
34
        'availability' => $avail,
35
    ];
36
37
    $tags = ['name' => $row['duration'], 'rrd_def' => $rrd_def, 'rrd_name' => $rrd_name];
38
    data_update($device, 'availability', $tags, $fields);
39
40
    dbUpdate(['availability_perc' => $avail], 'availability', '`availability_id` = ?', [$row['availability_id']]);
41
42
    echo $human_time . ' : ' . $avail . '%' . PHP_EOL;
43
}
44
unset($duration);
45