Issues (2963)

LibreNMS/Modules/OS.php (4 issues)

1
<?php
2
/**
3
 * OS.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  2020 Tony Murray
23
 * @author     Tony Murray <[email protected]>
24
 */
25
26
namespace LibreNMS\Modules;
27
28
use App\Models\Location;
29
use LibreNMS\Interfaces\Module;
30
use LibreNMS\Interfaces\Polling\OSPolling;
31
use LibreNMS\Util\Url;
32
33
class OS implements Module
34
{
35
    public function discover(\LibreNMS\OS $os)
36
    {
37
        $this->updateLocation($os);
38
        $this->sysContact($os);
39
40
        // null out values in case they aren't filled.
41
        $os->getDevice()->fill([
42
            'hardware' => null,
43
            'version' => null,
44
            'features' => null,
45
            'serial' => null,
46
            'icon' => null,
47
        ]);
48
49
        $os->discoverOS($os->getDevice());
50
        $this->handleChanges($os);
51
    }
52
53
    public function poll(\LibreNMS\OS $os)
54
    {
55
        $deviceModel = $os->getDevice(); /** @var \App\Models\Device $deviceModel */
56
        if ($os instanceof OSPolling) {
57
            $os->pollOS();
58
        } else {
59
            // legacy poller files
60
            global $graphs, $device;
61
            $location = null;
62
63
            if (is_file(base_path('/includes/polling/os/' . $device['os'] . '.inc.php'))) {
64
                // OS Specific
65
                include base_path('/includes/polling/os/' . $device['os'] . '.inc.php');
66
            } elseif ($device['os_group'] && is_file(base_path('/includes/polling/os/' . $device['os_group'] . '.inc.php'))) {
67
                // OS Group Specific
68
                include base_path('/includes/polling/os/' . $device['os_group'] . '.inc.php');
69
            } else {
70
                echo "Generic :(\n";
71
            }
72
73
            // handle legacy variables, sometimes they are false
74
            $deviceModel->version = ($version ?? $deviceModel->version) ?: null;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $version seems to never exist and therefore isset should always be false.
Loading history...
75
            $deviceModel->hardware = ($hardware ?? $deviceModel->hardware) ?: null;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $hardware seems to never exist and therefore isset should always be false.
Loading history...
76
            $deviceModel->features = ($features ?? $deviceModel->features) ?: null;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $features seems to never exist and therefore isset should always be false.
Loading history...
77
            $deviceModel->serial = ($serial ?? $deviceModel->serial) ?: null;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $serial seems to never exist and therefore isset should always be false.
Loading history...
78
79
            if (! empty($location)) { // legacy support, remove when no longer needed
80
                $deviceModel->setLocation($location);
81
                optional($deviceModel->location)->save();
82
            }
83
        }
84
85
        $this->handleChanges($os);
86
    }
87
88
    public function cleanup(\LibreNMS\OS $os)
89
    {
90
        // no cleanup needed?
91
    }
92
93
    private function handleChanges(\LibreNMS\OS $os)
94
    {
95
        $device = $os->getDevice();
96
97
        $device->icon = basename(Url::findOsImage($device->os, $device->features, null, 'images/os/'));
98
99
        echo trans('device.attributes.location') . ': ' . optional($device->location)->display() . PHP_EOL;
100
        foreach (['hardware', 'version', 'features', 'serial'] as $attribute) {
101
            echo \App\Observers\DeviceObserver::attributeChangedMessage($attribute, $device->$attribute, $device->getOriginal($attribute)) . PHP_EOL;
102
        }
103
104
        $device->save();
105
    }
106
107
    private function updateLocation(\LibreNMS\OS $os)
108
    {
109
        $device = $os->getDevice();
110
        $new_location = $device->override_sysLocation ? new Location() : $os->fetchLocation(); // fetch location data from device
111
        $device->setLocation($new_location, true); // set location and lookup coordinates if needed
112
        optional($device->location)->save();
113
    }
114
115
    private function sysContact(\LibreNMS\OS $os)
116
    {
117
        $device = $os->getDevice();
118
        $device->sysContact = snmp_get($os->getDeviceArray(), 'sysContact.0', '-Ovq', 'SNMPv2-MIB');
119
        $device->sysContact = str_replace(['', '"', '\n', 'not set'], '', $device->sysContact);
120
        if (empty($device->sysContact)) {
121
            $device->sysContact = null;
122
        }
123
    }
124
}
125