Passed
Push — master ( 2f9657...00b148 )
by Tony
08:56
created

Mpls::cleanup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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 <http://www.gnu.org/licenses/>.
19
 *
20
 * @package    LibreNMS
21
 * @link       http://librenms.org
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 LibreNMS\DB\SyncsModels;
31
use LibreNMS\Interfaces\Discovery\MplsDiscovery;
32
use LibreNMS\Interfaces\Module;
33
use LibreNMS\Interfaces\Polling\MplsPolling;
34
use LibreNMS\OS;
35
use LibreNMS\Util\ModuleModelObserver;
36
37
class Mpls implements Module
38
{
39
    use SyncsModels;
0 ignored issues
show
Bug introduced by
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->getDeviceModel(), 'mplsLsps', $os->discoverMplsLsps());
53
54
            echo "\nMPLS LSP Paths: ";
55
            ModuleModelObserver::observe('\App\Models\MplsLspPath');
56
            $this->syncModels($os->getDeviceModel(), 'mplsLspPaths', $os->discoverMplsPaths($lsps));
57
58
            echo PHP_EOL;
59
        }
60
    }
61
62
    /**
63
     * Poll data for this module and update the DB / RRD.
64
     * Try to keep this efficient and only run if discovery has indicated there is a reason to run.
65
     * Run frequently (default every 5 minutes)
66
     *
67
     * @param OS $os
68
     */
69
    public function poll(OS $os)
70
    {
71
        if ($os instanceof MplsPolling) {
72
            echo "\nMPLS LSPs: ";
73
            ModuleModelObserver::observe('\App\Models\MplsLsp');
74
            $lsps = $this->syncModels($os->getDeviceModel(), 'mplsLsps', $os->pollMplsLsps());
75
76
            echo "\nMPLS LSP Paths: ";
77
            ModuleModelObserver::observe('\App\Models\MplsLspPath');
78
            $this->syncModels($os->getDeviceModel(), 'mplsLspPaths', $os->pollMplsPaths($lsps));
79
80
            echo PHP_EOL;
81
        }
82
    }
83
84
    /**
85
     * Remove all DB data for this module.
86
     * This will be run when the module is disabled.
87
     *
88
     * @param OS $os
89
     */
90
    public function cleanup(OS $os)
91
    {
92
        $os->getDeviceModel()->mplsLsps()->delete();
93
        $os->getDeviceModel()->mplsLspPaths()->delete();
94
    }
95
}
96