mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
151 lines
5.9 KiB
PHP
151 lines
5.9 KiB
PHP
<?php
|
|
|
|
if ($device['os_group'] == 'unix') {
|
|
echo $config['project_name'].' UNIX Agent: ';
|
|
|
|
// FIXME - this should be in config and overridable in database
|
|
$agent_port = '6556';
|
|
|
|
$agent_start = utime();
|
|
$agent = fsockopen($device['hostname'], $agent_port, $errno, $errstr, $config['unix-agent-connection-time-out']);
|
|
|
|
// Set stream timeout (for timeouts during agent fetch
|
|
stream_set_timeout($agent, $config['unix-agent-read-time-out']);
|
|
$agentinfo = stream_get_meta_data($agent);
|
|
|
|
if (!$agent) {
|
|
echo 'Connection to UNIX agent failed on port '.$port.'.';
|
|
}
|
|
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 = utime();
|
|
$agent_time = round(($agent_end - $agent_start) * 1000);
|
|
|
|
if (!empty($agent_raw)) {
|
|
echo 'execution time: '.$agent_time.'ms';
|
|
$agent_rrd = $config['rrd_dir'].'/'.$device['hostname'].'/agent.rrd';
|
|
if (!is_file($agent_rrd)) {
|
|
rrdtool_create($agent_rrd, 'DS:time:GAUGE:600:0:U '.$config['rrd_rra']);
|
|
}
|
|
|
|
$fields = array(
|
|
'time' => $agent_time,
|
|
);
|
|
|
|
rrdtool_update($agent_rrd, $fields);
|
|
$graphs['agent'] = true;
|
|
|
|
foreach (explode('<<<', $agent_raw) as $section) {
|
|
list($section, $data) = explode('>>>', $section);
|
|
list($sa, $sb) = explode('-', $section, 2);
|
|
|
|
$agentapps = array(
|
|
"apache",
|
|
"mysql",
|
|
"nginx",
|
|
"bind",
|
|
"proxmox",
|
|
"tinydns");
|
|
|
|
if (in_array($section, $agentapps)) {
|
|
$agent_data['app'][$section] = trim($data);
|
|
}
|
|
|
|
if (!empty($sa) && !empty($sb)) {
|
|
$agent_data[$sa][$sb] = trim($data);
|
|
}
|
|
else {
|
|
$agent_data[$section] = trim($data);
|
|
}
|
|
}//end foreach
|
|
|
|
d_echo($agent_data);
|
|
|
|
include 'unix-agent/packages.inc.php';
|
|
include 'unix-agent/munin-plugins.inc.php';
|
|
|
|
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");
|
|
|
|
include "unix-agent/$key.inc.php";
|
|
}
|
|
}
|
|
|
|
// Processes
|
|
if (!empty($agent_data['ps'])) {
|
|
echo 'Processes: ';
|
|
dbDelete('processes', 'device_id = ?', array($device['device_id']));
|
|
$data=array();
|
|
foreach (explode("\n", $agent_data['ps']) as $process) {
|
|
$process = preg_replace('/\((.*),([0-9]*),([0-9]*),([0-9\:]*),([0-9]*)\)\ (.*)/', '\\1|\\2|\\3|\\4|\\5|\\6', $process);
|
|
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);
|
|
}
|
|
}
|
|
if (count($data) > 0) {
|
|
dbBulkInsert('processes',$data);
|
|
}
|
|
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, array('apache', 'mysql', 'nginx', 'proxmox'))) {
|
|
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), 'applications');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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_instance' => $memcached_host), 'applications');
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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_instance' => $drbd_dev), 'applications');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}//end if
|
|
|
|
if (!empty($agent_sensors)) {
|
|
echo 'Sensors: ';
|
|
check_valid_sensors($device, 'temperature', $valid['sensor'], 'agent');
|
|
echo "\n";
|
|
}
|
|
|
|
echo "\n";
|
|
}//end if
|