librenms/includes/polling/unix-agent.inc.php

163 lines
6.3 KiB
PHP
Raw Normal View History

<?php
2015-07-13 18:10:26 +00:00
if ($device['os_group'] == 'unix') {
echo $config['project_name'].' UNIX Agent: ';
$agent_port = get_dev_attrib($device,'override_Unixagent_port');
if (empty($agent_port)) {
$agent_port = $config['unix-agent']['port'];
}
if (empty($config['unix-agent']['connection-timeout'])) {
$config['unix-agent']['connection-timeout'] = $config['unix-agent-connection-time-out'];
}
if (empty($config['unix-agent']['read-timeout'])) {
$config['unix-agent']['read-timeout'] = $config['unix-agent-read-time-out'];
}
2015-07-13 18:10:26 +00:00
$agent_start = microtime(true);
$agent = fsockopen($device['hostname'], $agent_port, $errno, $errstr, $config['unix-agent']['connection-timeout']);
2015-07-13 18:10:26 +00:00
// Set stream timeout (for timeouts during agent fetch
stream_set_timeout($agent, $config['unix-agent']['read-timeout']);
2015-07-13 18:10:26 +00:00
$agentinfo = stream_get_meta_data($agent);
if (!$agent) {
echo 'Connection to UNIX agent failed on port '.$port.'.';
}
2015-07-13 18:10:26 +00:00
else {
// fetch data while not eof and not timed-out
while ((!feof($agent)) && (!$agentinfo['timed_out'])) {
$agent_raw .= fgets($agent, 128);
$agentinfo = stream_get_meta_data($agent);
}
if ($agentinfo['timed_out']) {
echo 'Connection to UNIX agent timed out during fetch on port '.$port.'.';
}
}
$agent_end = microtime(true);
2015-07-13 18:10:26 +00:00
$agent_time = round(($agent_end - $agent_start) * 1000);
2015-07-13 18:10:26 +00:00
if (!empty($agent_raw)) {
echo 'execution time: '.$agent_time.'ms';
$tags = array(
'rrd_def' => 'DS:time:GAUGE:600:0:U',
);
$fields = array(
'time' => $agent_time,
);
data_update($device, 'agent', $tags, $fields);
2015-08-19 20:58:02 +00:00
2015-07-13 18:10:26 +00:00
$graphs['agent'] = true;
$agentapps = array(
"apache",
"bind",
"ceph",
"mysql",
"nginx",
"ntpd",
"powerdns",
2016-08-17 19:06:10 +00:00
"powerdns-recursor",
"proxmox",
"rrdcached",
"tinydns");
2015-07-13 18:10:26 +00:00
foreach (explode('<<<', $agent_raw) as $section) {
list($section, $data) = explode('>>>', $section);
list($sa, $sb) = explode('-', $section, 2);
2015-08-20 10:25:47 +00:00
if (in_array($section, $agentapps)) {
$agent_data['app'][$section] = trim($data);
2015-07-13 18:10:26 +00:00
}
if (!empty($sa) && !empty($sb)) {
$agent_data[$sa][$sb] = trim($data);
}
else {
$agent_data[$section] = trim($data);
}
}//end foreach
d_echo($agent_data);
2015-07-13 18:10:26 +00:00
include 'unix-agent/packages.inc.php';
include 'unix-agent/munin-plugins.inc.php';
2015-07-13 18:10:26 +00:00
foreach (array_keys($agent_data) as $key) {
if (file_exists("includes/polling/unix-agent/$key.inc.php")) {
d_echo("Including: unix-agent/$key.inc.php");
2015-07-13 18:10:26 +00:00
include "unix-agent/$key.inc.php";
}
}
// Processes
if (!empty($agent_data['ps'])) {
echo 'Processes: ';
dbDelete('processes', 'device_id = ?', array($device['device_id']));
$data=array();
2015-07-13 18:10:26 +00:00
foreach (explode("\n", $agent_data['ps']) as $process) {
2016-04-21 14:38:21 +00:00
$process = preg_replace('/\((.*),([0-9]*),([0-9]*),([0-9\:\.]*),([0-9]*)\)\ (.*)/', '\\1|\\2|\\3|\\4|\\5|\\6', $process);
2015-07-13 18:10:26 +00:00
list($user, $vsz, $rss, $cputime, $pid, $command) = explode('|', $process, 6);
if (!empty($command)) {
$data[]=array('device_id' => $device['device_id'], 'pid' => $pid, 'user' => $user, 'vsz' => $vsz, 'rss' => $rss, 'cputime' => $cputime, 'command' => $command);
2015-07-13 18:10:26 +00:00
}
}
if (count($data) > 0) {
dbBulkInsert('processes',$data);
}
2015-07-13 18:10:26 +00:00
echo "\n";
}
foreach (array_keys($agent_data['app']) as $key) {
if (file_exists("includes/polling/applications/$key.inc.php")) {
d_echo("Enabling $key for ".$device['hostname']." if not yet enabled\n");
if (in_array($key, $agentapps)) {
if (dbFetchCell('SELECT COUNT(*) FROM `applications` WHERE `device_id` = ? AND `app_type` = ?', array($device['device_id'], $key)) == '0') {
echo "Found new application '$key'\n";
dbInsert(array('device_id' => $device['device_id'], 'app_type' => $key, 'app_status' => '', 'app_instance' => ''), 'applications');
}
}
2015-07-13 18:10:26 +00:00
}
}
2015-07-13 18:10:26 +00:00
// memcached
if (!empty($agent_data['app']['memcached'])) {
$agent_data['app']['memcached'] = unserialize($agent_data['app']['memcached']);
foreach ($agent_data['app']['memcached'] as $memcached_host => $memcached_data) {
if (dbFetchCell('SELECT COUNT(*) FROM `applications` WHERE `device_id` = ? AND `app_type` = ? AND `app_instance` = ?', array($device['device_id'], 'memcached', $memcached_host)) == '0') {
echo "Found new application 'Memcached' $memcached_host\n";
dbInsert(array('device_id' => $device['device_id'], 'app_type' => 'memcached', 'app_status' => '', 'app_instance' => $memcached_host), 'applications');
2015-07-13 18:10:26 +00:00
}
}
}
2015-07-13 18:10:26 +00:00
// DRBD
if (!empty($agent_data['drbd'])) {
$agent_data['app']['drbd'] = array();
foreach (explode("\n", $agent_data['drbd']) as $drbd_entry) {
list($drbd_dev, $drbd_data) = explode(':', $drbd_entry);
if (preg_match('/^drbd/', $drbd_dev)) {
$agent_data['app']['drbd'][$drbd_dev] = $drbd_data;
if (dbFetchCell('SELECT COUNT(*) FROM `applications` WHERE `device_id` = ? AND `app_type` = ? AND `app_instance` = ?', array($device['device_id'], 'drbd', $drbd_dev)) == '0') {
echo "Found new application 'DRBd' $drbd_dev\n";
dbInsert(array('device_id' => $device['device_id'], 'app_type' => 'drbd', 'app_status' => '', 'app_instance' => $drbd_dev), 'applications');
2015-07-13 18:10:26 +00:00
}
}
}
}
}//end if
2015-07-13 18:10:26 +00:00
if (!empty($agent_sensors)) {
echo 'Sensors: ';
check_valid_sensors($device, 'temperature', $valid['sensor'], 'agent');
echo "\n";
}
2015-07-13 18:10:26 +00:00
echo "\n";
}//end if