Issues (2963)

includes/polling/unix-agent.inc.php (1 issue)

1
<?php
2
3
use App\Models\Device;
4
use LibreNMS\RRD\RrdDefinition;
5
6
if ($device['os_group'] == 'unix' || $device['os'] == 'windows') {
7
    echo \LibreNMS\Config::get('project_name') . ' UNIX Agent: ';
8
9
    $agent_port = get_dev_attrib($device, 'override_Unixagent_port');
10
    if (empty($agent_port)) {
11
        $agent_port = \LibreNMS\Config::get('unix-agent.port');
12
    }
13
14
    $agent_start = microtime(true);
15
    $poller_target = \LibreNMS\Util\Rewrite::addIpv6Brackets(Device::pollerTarget($device['hostname']));
16
    $agent = fsockopen($poller_target, $agent_port, $errno, $errstr, \LibreNMS\Config::get('unix-agent.connection-timeout'));
17
18
    if (! $agent) {
0 ignored issues
show
$agent is of type resource, thus it always evaluated to false.
Loading history...
19
        echo 'Connection to UNIX agent failed on port ' . $agent_port . '.';
20
    } else {
21
        // Set stream timeout (for timeouts during agent  fetch
22
        stream_set_timeout($agent, \LibreNMS\Config::get('unix-agent.read-timeout'));
23
        $agentinfo = stream_get_meta_data($agent);
24
25
        // fetch data while not eof and not timed-out
26
        while ((! feof($agent)) && (! $agentinfo['timed_out'])) {
27
            $agent_raw .= fgets($agent, 128);
28
            $agentinfo = stream_get_meta_data($agent);
29
        }
30
31
        if ($agentinfo['timed_out']) {
32
            echo 'Connection to UNIX agent timed out during fetch on port ' . $agent_port . '.';
33
        }
34
    }
35
36
    $agent_end = microtime(true);
37
    $agent_time = round(($agent_end - $agent_start) * 1000);
38
39
    if (! empty($agent_raw)) {
40
        echo 'execution time: ' . $agent_time . 'ms';
41
42
        $tags = [
43
            'rrd_def' => RrdDefinition::make()->addDataset('time', 'GAUGE', 0),
44
        ];
45
        $fields = [
46
            'time' => $agent_time,
47
        ];
48
        data_update($device, 'agent', $tags, $fields);
49
50
        $os->enableGraph('agent');
51
52
        $agentapps = [
53
            'apache',
54
            'bind',
55
            'ceph',
56
            'mysql',
57
            'nginx',
58
            'powerdns',
59
            'powerdns-recursor',
60
            'proxmox',
61
            'rrdcached',
62
            'tinydns',
63
            'gpsd',
64
        ];
65
66
        foreach (explode('<<<', $agent_raw) as $section) {
67
            [$section, $data] = explode('>>>', $section);
68
            [$sa, $sb] = explode('-', $section, 2);
69
70
            if (in_array($section, $agentapps)) {
71
                $agent_data['app'][$section] = trim($data);
72
            }
73
74
            if (! empty($sa) && ! empty($sb)) {
75
                $agent_data[$sa][$sb] = trim($data);
76
            } else {
77
                $agent_data[$section] = trim($data);
78
            }
79
        }//end foreach
80
81
        d_echo($agent_data);
82
83
        include 'unix-agent/packages.inc.php';
84
        include 'unix-agent/munin-plugins.inc.php';
85
86
        foreach (array_keys($agent_data) as $key) {
87
            if (file_exists("includes/polling/unix-agent/$key.inc.php")) {
88
                d_echo("Including: unix-agent/$key.inc.php");
89
90
                include "unix-agent/$key.inc.php";
91
            }
92
        }
93
94
        // Unix Processes
95
        if (! empty($agent_data['ps'])) {
96
            echo 'Processes: ';
97
            dbDelete('processes', 'device_id = ?', [$device['device_id']]);
98
            $data = [];
99
            foreach (explode("\n", $agent_data['ps']) as $process) {
100
                $process = preg_replace('/\((.*),([0-9]*),([0-9]*),([0-9\:\.\-]*),([0-9]*)\)\ (.*)/', '\\1|\\2|\\3|\\4|\\5|\\6', $process);
101
                [$user, $vsz, $rss, $cputime, $pid, $command] = explode('|', $process, 6);
102
                if (! empty($command)) {
103
                    $data[] = ['device_id' => $device['device_id'], 'pid' => $pid, 'user' => $user, 'vsz' => $vsz, 'rss' => $rss, 'cputime' => $cputime, 'command' => $command];
104
                }
105
            }
106
            if (count($data) > 0) {
107
                dbBulkInsert($data, 'processes');
108
            }
109
            echo "\n";
110
        }
111
112
        // Windows Processes
113
        if (! empty($agent_data['ps:sep(9)'])) {
114
            echo 'Processes: ';
115
            dbDelete('processes', 'device_id = ?', [$device['device_id']]);
116
            $data = [];
117
            foreach (explode("\n", $agent_data['ps:sep(9)']) as $process) {
118
                $process = preg_replace('/\(([^,;]+),([0-9]*),([0-9]*),([0-9]*),([0-9]*),([0-9]*),([0-9]*),([0-9]*),([0-9]*),([0-9]*)?,?([0-9]*)\)(.*)/', '\\1|\\2|\\3|\\4|\\5|\\6|\\7|\\8|\\9|\\10|\\11|\\12', $process);
119
                [$user, $VirtualSize, $WorkingSetSize, $zero, $processId, $PageFileUsage, $UserModeTime, $KernelModeTime, $HandleCount, $ThreadCount, $uptime, $process_name] = explode('|', $process, 12);
120
                if (! empty($process_name)) {
121
                    $cputime = ($UserModeTime + $KernelModeTime) / 10000000;
122
                    $days = floor($cputime / 86400);
123
                    $hours = str_pad(floor(($cputime / 3600) % 24), 2, '0', STR_PAD_LEFT);
124
                    $minutes = str_pad(floor(($cputime / 60) % 60), 2, '0', STR_PAD_LEFT);
125
                    $seconds = str_pad(($cputime % 60), 2, '0', STR_PAD_LEFT);
126
                    $cputime = ($days > 0 ? "$days-" : '') . "$hours:$minutes:$seconds";
127
                    $data[] = ['device_id' => $device['device_id'], 'pid' => $processId, 'user' => $user, 'vsz' => $PageFileUsage + $WorkingSetSize, 'rss' => $WorkingSetSize, 'cputime' => $cputime, 'command' => $process_name];
128
                }
129
            }
130
            if (count($data) > 0) {
131
                dbBulkInsert($data, 'processes');
132
            }
133
            echo "\n";
134
        }
135
136
        foreach (array_keys($agent_data['app']) as $key) {
137
            if (file_exists("includes/polling/applications/$key.inc.php")) {
138
                d_echo("Enabling $key for " . $device['hostname'] . " if not yet enabled\n");
139
140
                if (in_array($key, $agentapps)) {
141
                    if (dbFetchCell('SELECT COUNT(*) FROM `applications` WHERE `device_id` = ? AND `app_type` = ?', [$device['device_id'], $key]) == '0') {
142
                        echo "Found new application '$key'\n";
143
                        dbInsert(['device_id' => $device['device_id'], 'app_type' => $key, 'app_status' => '', 'app_instance' => ''], 'applications');
144
                    }
145
                }
146
            }
147
        }
148
149
        // memcached
150
        if (! empty($agent_data['app']['memcached'])) {
151
            $agent_data['app']['memcached'] = unserialize($agent_data['app']['memcached']);
152
            foreach ($agent_data['app']['memcached'] as $memcached_host => $memcached_data) {
153
                if (dbFetchCell('SELECT COUNT(*) FROM `applications` WHERE `device_id` = ? AND `app_type` = ? AND `app_instance` = ?', [$device['device_id'], 'memcached', $memcached_host]) == '0') {
154
                    echo "Found new application 'Memcached' $memcached_host\n";
155
                    dbInsert(['device_id' => $device['device_id'], 'app_type' => 'memcached', 'app_status' => '', 'app_instance' => $memcached_host], 'applications');
156
                }
157
            }
158
        }
159
160
        // DRBD
161
        if (! empty($agent_data['drbd'])) {
162
            $agent_data['app']['drbd'] = [];
163
            foreach (explode("\n", $agent_data['drbd']) as $drbd_entry) {
164
                [$drbd_dev, $drbd_data] = explode(':', $drbd_entry);
165
                if (preg_match('/^drbd/', $drbd_dev)) {
166
                    $agent_data['app']['drbd'][$drbd_dev] = $drbd_data;
167
                    if (dbFetchCell('SELECT COUNT(*) FROM `applications` WHERE `device_id` = ? AND `app_type` = ? AND `app_instance` = ?', [$device['device_id'], 'drbd', $drbd_dev]) == '0') {
168
                        echo "Found new application 'DRBd' $drbd_dev\n";
169
                        dbInsert(['device_id' => $device['device_id'], 'app_type' => 'drbd', 'app_status' => '', 'app_instance' => $drbd_dev], 'applications');
170
                    }
171
                }
172
            }
173
        }
174
    }//end if
175
176
    // Use agent DMI data if available
177
    if (isset($agent_data['dmi'])) {
178
        if ($agent_data['dmi']['system-product-name']) {
179
            $hardware = ($agent_data['dmi']['system-manufacturer'] ? $agent_data['dmi']['system-manufacturer'] . ' ' : '') . $agent_data['dmi']['system-product-name'];
180
181
            // Clean up Generic hardware descriptions
182
            DeviceCache::getPrimary()->hardware = rewrite_generic_hardware($hardware);
183
            unset($hardware);
184
        }
185
186
        if ($agent_data['dmi']['system-serial-number']) {
187
            DeviceCache::getPrimary()->serial = $agent_data['dmi']['system-serial-number'];
188
        }
189
        DeviceCache::getPrimary()->save();
190
    }
191
192
    if (! empty($agent_sensors)) {
193
        echo 'Sensors: ';
194
        check_valid_sensors($device, 'temperature', $valid['sensor'], 'agent');
195
        d_echo($agent_sensors);
196
        if (count($agent_sensors) > 0) {
197
            record_sensor_data($device, $agent_sensors);
198
        }
199
        echo "\n";
200
    }
201
202
    echo "\n";
203
}//end if
204