Issues (2963)

LibreNMS/Modules/Slas.php (1 issue)

1
<?php
2
/**
3
 * SLA.php
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17
 *
18
 * @link       https://www.librenms.org
19
 */
20
21
namespace LibreNMS\Modules;
22
23
use App\Models\Sla;
24
use App\Observers\ModuleModelObserver;
25
use LibreNMS\DB\SyncsModels;
26
use LibreNMS\Interfaces\Discovery\SlaDiscovery;
27
use LibreNMS\Interfaces\Module;
28
use LibreNMS\Interfaces\Polling\SlaPolling;
29
use LibreNMS\OS;
0 ignored issues
show
This use statement conflicts with another class in this namespace, LibreNMS\Modules\OS. 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...
30
31
class Slas implements Module
32
{
33
    use SyncsModels;
34
35
    /**
36
     * Discover this module. Heavier processes can be run here
37
     * Run infrequently (default 4 times a day)
38
     *
39
     * @param  OS  $os
40
     */
41
    public function discover(OS $os)
42
    {
43
        if ($os instanceof SlaDiscovery) {
44
            $slas = $os->discoverSlas();
45
            ModuleModelObserver::observe(Sla::class);
46
            $this->syncModels($os->getDevice(), 'slas', $slas);
47
        }
48
    }
49
50
    /**
51
     * Poll data for this module and update the DB / RRD.
52
     * Try to keep this efficient and only run if discovery has indicated there is a reason to run.
53
     * Run frequently (default every 5 minutes)
54
     *
55
     * @param  OS  $os
56
     */
57
    public function poll(OS $os)
58
    {
59
        if ($os instanceof SlaPolling) {
60
            // Gather our SLA's from the DB.
61
            $slas = $os->getDevice()->slas()
62
                ->where('deleted', 0)->get();
63
64
            if ($slas->isNotEmpty()) {
65
                // We have SLA's, lets go!!!
66
                $os->pollSlas($slas);
67
                $os->getDevice()->slas()->saveMany($slas);
68
            }
69
        }
70
    }
71
72
    /**
73
     * Remove all DB data for this module.
74
     * This will be run when the module is disabled.
75
     *
76
     * @param  OS  $os
77
     */
78
    public function cleanup(OS $os)
79
    {
80
        $os->getDevice()->slas()->delete();
81
    }
82
}
83