Issues (2963)

LibreNMS/Modules/Mpls.php (3 issues)

1
<?php
2
/**
3
 * Mpls.php
4
 *
5
 * -Description-
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 *
20
 * @link       https://www.librenms.org
21
 *
22
 * @copyright  2019 Vitali Kari
23
 * @copyright  2019 Tony Murray
24
 * @author     Vitali Kari <[email protected]>
25
 * @author     Tony Murray <[email protected]>
26
 */
27
28
namespace LibreNMS\Modules;
29
30
use App\Observers\ModuleModelObserver;
31
use LibreNMS\DB\SyncsModels;
32
use LibreNMS\Interfaces\Discovery\MplsDiscovery;
33
use LibreNMS\Interfaces\Module;
34
use LibreNMS\Interfaces\Polling\MplsPolling;
35
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...
36
37
class Mpls implements Module
38
{
39
    use SyncsModels;
0 ignored issues
show
The trait LibreNMS\DB\SyncsModels requires the property $keyBy which is not provided by LibreNMS\Modules\Mpls.
Loading history...
40
41
    /**
42
     * Discover this module. Heavier processes can be run here
43
     * Run infrequently (default 4 times a day)
44
     *
45
     * @param  OS  $os
46
     */
47
    public function discover(OS $os)
48
    {
49
        if ($os instanceof MplsDiscovery) {
50
            echo "\nMPLS LSPs: ";
51
            ModuleModelObserver::observe('\App\Models\MplsLsp');
52
            $lsps = $this->syncModels($os->getDevice(), 'mplsLsps', $os->discoverMplsLsps());
53
54
            echo "\nMPLS LSP Paths: ";
55
            ModuleModelObserver::observe('\App\Models\MplsLspPath');
56
            $paths = $this->syncModels($os->getDevice(), 'mplsLspPaths', $os->discoverMplsPaths($lsps));
57
58
            echo "\nMPLS SDPs: ";
59
            ModuleModelObserver::observe('\App\Models\MplsSdp');
60
            $sdps = $this->syncModels($os->getDevice(), 'mplsSdps', $os->discoverMplsSdps());
61
62
            echo "\nMPLS Services: ";
63
            ModuleModelObserver::observe('\App\Models\MplsService');
64
            $svcs = $this->syncModels($os->getDevice(), 'mplsServices', $os->discoverMplsServices());
65
66
            echo "\nMPLS SAPs: ";
67
            ModuleModelObserver::observe('\App\Models\MplsSap');
68
            $this->syncModels($os->getDevice(), 'mplsSaps', $os->discoverMplsSaps($svcs));
69
70
            echo "\nMPLS SDP Bindings: ";
71
            ModuleModelObserver::observe('\App\Models\MplsSdpBind');
72
            $this->syncModels($os->getDevice(), 'mplsSdpBinds', $os->discoverMplsSdpBinds($sdps, $svcs));
73
74
            echo "\nMPLS Tunnel Active Routing Hops: ";
75
            ModuleModelObserver::observe('\App\Models\MplsTunnelArHop');
76
            $this->syncModels($os->getDevice(), 'mplsTunnelArHops', $os->discoverMplsTunnelArHops($paths));
77
78
            echo "\nMPLS Tunnel Constrained Shortest Path First Hops: ";
79
            ModuleModelObserver::observe('\App\Models\MplsTunnelCHop');
80
            $this->syncModels($os->getDevice(), 'mplsTunnelCHops', $os->discoverMplsTunnelCHops($paths));
81
82
            echo PHP_EOL;
83
        }
84
    }
85
86
    /**
87
     * Poll data for this module and update the DB / RRD.
88
     * Try to keep this efficient and only run if discovery has indicated there is a reason to run.
89
     * Run frequently (default every 5 minutes)
90
     *
91
     * @param  OS  $os
92
     */
93
    public function poll(OS $os)
94
    {
95
        if ($os instanceof MplsPolling) {
96
            $device = $os->getDevice();
97
98
            if ($device->mplsLsps()->exists()) {
99
                echo "\nMPLS LSPs: ";
100
                ModuleModelObserver::observe('\App\Models\MplsLsp');
101
                $lsps = $this->syncModels($device, 'mplsLsps', $os->pollMplsLsps());
102
            }
103
104
            if ($device->mplsLspPaths()->exists()) {
105
                echo "\nMPLS LSP Paths: ";
106
                ModuleModelObserver::observe('\App\Models\MplsLspPath');
107
                $paths = $this->syncModels($device, 'mplsLspPaths', $os->pollMplsPaths($lsps));
108
            }
109
110
            if ($device->mplsSdps()->exists()) {
111
                echo "\nMPLS SDPs: ";
112
                ModuleModelObserver::observe('\App\Models\MplsSdp');
113
                $sdps = $this->syncModels($device, 'mplsSdps', $os->pollMplsSdps());
114
            }
115
116
            if ($device->mplsServices()->exists()) {
117
                echo "\nMPLS Services: ";
118
                ModuleModelObserver::observe('\App\Models\MplsService');
119
                $svcs = $this->syncModels($device, 'mplsServices', $os->pollMplsServices());
120
            }
121
122
            if ($device->mplsSaps()->exists()) {
123
                echo "\nMPLS SAPs: ";
124
                ModuleModelObserver::observe('\App\Models\MplsSap');
125
                $this->syncModels($device, 'mplsSaps', $os->pollMplsSaps($svcs));
126
            }
127
128
            if ($device->mplsSdpBinds()->exists()) {
129
                echo "\nMPLS SDP Bindings: ";
130
                ModuleModelObserver::observe('\App\Models\MplsSdpBind');
131
                $this->syncModels($device, 'mplsSdpBinds', $os->pollMplsSdpBinds($sdps, $svcs));
132
            }
133
134
            if ($device->mplsTunnelArHops()->exists()) {
135
                echo "\nMPLS Tunnel Active Routing Hops: ";
136
                ModuleModelObserver::observe('\App\Models\MplsTunnelArHop');
137
                $this->syncModels($device, 'mplsTunnelArHops', $os->pollMplsTunnelArHops($paths));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $paths does not seem to be defined for all execution paths leading up to this point.
Loading history...
138
            }
139
140
            if ($device->mplsTunnelCHops()->exists()) {
141
                echo "\nMPLS Tunnel Constrained Shortest Path First Hops: ";
142
                ModuleModelObserver::observe('\App\Models\MplsTunnelCHop');
143
                $this->syncModels($device, 'mplsTunnelCHops', $os->pollMplsTunnelCHops($paths));
144
            }
145
146
            echo PHP_EOL;
147
        }
148
    }
149
150
    /**
151
     * Remove all DB data for this module.
152
     * This will be run when the module is disabled.
153
     *
154
     * @param  OS  $os
155
     */
156
    public function cleanup(OS $os)
157
    {
158
        $os->getDevice()->mplsLsps()->delete();
159
        $os->getDevice()->mplsLspPaths()->delete();
160
        $os->getDevice()->mplsSdps()->delete();
161
        $os->getDevice()->mplsServices()->delete();
162
        $os->getDevice()->mplsSaps()->delete();
163
        $os->getDevice()->mplsSdpBinds()->delete();
164
        $os->getDevice()->mplsTunnelArHops()->delete();
165
        $os->getDevice()->mplsTunnelCHops()->delete();
166
    }
167
}
168