2012-03-26 09:37:55 +00:00
< ? php
2017-02-23 22:45:50 +00:00
use LibreNMS\RRD\RrdDefinition ;
2015-07-13 20:10:26 +02:00
if ( $device [ 'os_group' ] == 'unix' ) {
echo $config [ 'project_name' ] . ' UNIX Agent: ' ;
2016-08-28 12:32:58 -05:00
$agent_port = get_dev_attrib ( $device , 'override_Unixagent_port' );
2015-11-17 00:20:06 +00:00
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 20:10:26 +02:00
2016-01-08 13:33:32 +01:00
$agent_start = microtime ( true );
2015-11-17 00:20:06 +00:00
$agent = fsockopen ( $device [ 'hostname' ], $agent_port , $errno , $errstr , $config [ 'unix-agent' ][ 'connection-timeout' ]);
2015-07-13 20:10:26 +02:00
// Set stream timeout (for timeouts during agent fetch
2015-11-17 00:20:06 +00:00
stream_set_timeout ( $agent , $config [ 'unix-agent' ][ 'read-timeout' ]);
2015-07-13 20:10:26 +02:00
$agentinfo = stream_get_meta_data ( $agent );
if ( ! $agent ) {
echo 'Connection to UNIX agent failed on port ' . $port . '.' ;
2016-08-28 12:32:58 -05:00
} else {
2015-07-13 20:10:26 +02:00
// 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 . '.' ;
}
2012-03-26 09:37:55 +00:00
}
2012-04-21 00:36:55 +00:00
2016-01-08 13:33:32 +01:00
$agent_end = microtime ( true );
2015-07-13 20:10:26 +02:00
$agent_time = round (( $agent_end - $agent_start ) * 1000 );
2012-04-26 11:57:52 +00:00
2015-07-13 20:10:26 +02:00
if ( ! empty ( $agent_raw )) {
echo 'execution time: ' . $agent_time . 'ms' ;
2012-05-20 23:14:08 +00:00
2016-01-10 19:31:39 +10:00
$tags = array (
2017-02-23 22:45:50 +00:00
'rrd_def' => RrdDefinition :: make () -> addDataset ( 'time' , 'GAUGE' , 0 ),
2016-01-10 19:31:39 +10:00
);
2015-08-18 16:26:55 +00:00
$fields = array (
'time' => $agent_time ,
);
2016-01-10 19:31:39 +10:00
data_update ( $device , 'agent' , $tags , $fields );
2015-08-19 20:58:02 +00:00
2015-07-13 20:10:26 +02:00
$graphs [ 'agent' ] = true ;
2016-06-08 20:40:51 -05:00
$agentapps = array (
" apache " ,
" bind " ,
" ceph " ,
" mysql " ,
" nginx " ,
" powerdns " ,
2016-08-17 21:06:10 +02:00
" powerdns-recursor " ,
2016-06-08 20:40:51 -05:00
" proxmox " ,
" rrdcached " ,
2016-10-08 15:25:50 -05:00
" tinydns " ,
" gpsd " ,
);
2016-06-08 20:40:51 -05:00
2015-07-13 20:10:26 +02:00
foreach ( explode ( '<<<' , $agent_raw ) as $section ) {
list ( $section , $data ) = explode ( '>>>' , $section );
list ( $sa , $sb ) = explode ( '-' , $section , 2 );
2015-08-20 12:25:47 +02:00
if ( in_array ( $section , $agentapps )) {
$agent_data [ 'app' ][ $section ] = trim ( $data );
2015-07-13 20:10:26 +02:00
}
if ( ! empty ( $sa ) && ! empty ( $sb )) {
$agent_data [ $sa ][ $sb ] = trim ( $data );
2016-08-28 12:32:58 -05:00
} else {
2015-07-13 20:10:26 +02:00
$agent_data [ $section ] = trim ( $data );
}
} //end foreach
2015-08-20 13:26:18 +02:00
d_echo ( $agent_data );
2012-05-20 23:14:08 +00:00
2015-07-13 20:10:26 +02:00
include 'unix-agent/packages.inc.php' ;
include 'unix-agent/munin-plugins.inc.php' ;
2012-05-20 18:23:40 +00:00
2015-07-13 20:10:26 +02:00
foreach ( array_keys ( $agent_data ) as $key ) {
if ( file_exists ( " includes/polling/unix-agent/ $key .inc.php " )) {
2015-08-20 13:26:18 +02:00
d_echo ( " Including: unix-agent/ $key .inc.php " );
2012-05-20 23:14:08 +00:00
2015-07-13 20:10:26 +02:00
include " unix-agent/ $key .inc.php " ;
}
}
// Processes
if ( ! empty ( $agent_data [ 'ps' ])) {
echo 'Processes: ' ;
dbDelete ( 'processes' , 'device_id = ?' , array ( $device [ 'device_id' ]));
2015-07-16 00:16:25 +02:00
$data = array ();
2015-07-13 20:10:26 +02:00
foreach ( explode ( " \n " , $agent_data [ 'ps' ]) as $process ) {
2016-09-28 08:20:16 +00:00
$process = preg_replace ( '/\((.*),([0-9]*),([0-9]*),([0-9\:\.\-]*),([0-9]*)\)\ (.*)/' , '\\1|\\2|\\3|\\4|\\5|\\6' , $process );
2015-07-13 20:10:26 +02:00
list ( $user , $vsz , $rss , $cputime , $pid , $command ) = explode ( '|' , $process , 6 );
if ( ! empty ( $command )) {
2015-07-16 00:16:25 +02:00
$data [] = array ( 'device_id' => $device [ 'device_id' ], 'pid' => $pid , 'user' => $user , 'vsz' => $vsz , 'rss' => $rss , 'cputime' => $cputime , 'command' => $command );
2015-07-13 20:10:26 +02:00
}
}
2015-07-16 00:16:25 +02:00
if ( count ( $data ) > 0 ) {
2016-08-28 12:32:58 -05:00
dbBulkInsert ( 'processes' , $data );
2015-07-14 22:57:15 +02:00
}
2015-07-13 20:10:26 +02:00
echo " \n " ;
}
2012-04-21 00:36:55 +00:00
2015-08-20 12:36:37 +02:00
foreach ( array_keys ( $agent_data [ 'app' ]) as $key ) {
if ( file_exists ( " includes/polling/applications/ $key .inc.php " )) {
2015-08-20 13:26:18 +02:00
d_echo ( " Enabling $key for " . $device [ 'hostname' ] . " if not yet enabled \n " );
2015-08-20 12:36:37 +02:00
2016-06-08 21:42:18 -05:00
if ( in_array ( $key , $agentapps )) {
2015-08-20 12:36:37 +02:00
if ( dbFetchCell ( 'SELECT COUNT(*) FROM `applications` WHERE `device_id` = ? AND `app_type` = ?' , array ( $device [ 'device_id' ], $key )) == '0' ) {
echo " Found new application ' $key ' \n " ;
2015-11-16 21:07:50 -08:00
dbInsert ( array ( 'device_id' => $device [ 'device_id' ], 'app_type' => $key , 'app_status' => '' , 'app_instance' => '' ), 'applications' );
2015-08-20 12:36:37 +02:00
}
}
2015-07-13 20:10:26 +02:00
}
2012-04-27 17:18:26 +00:00
}
2015-07-13 20:10:26 +02: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 " ;
2015-11-16 21:07:50 -08:00
dbInsert ( array ( 'device_id' => $device [ 'device_id' ], 'app_type' => 'memcached' , 'app_status' => '' , 'app_instance' => $memcached_host ), 'applications' );
2015-07-13 20:10:26 +02:00
}
}
}
2012-04-21 00:36:55 +00:00
2015-07-13 20:10:26 +02: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 " ;
2015-11-16 21:07:50 -08:00
dbInsert ( array ( 'device_id' => $device [ 'device_id' ], 'app_type' => 'drbd' , 'app_status' => '' , 'app_instance' => $drbd_dev ), 'applications' );
2015-07-13 20:10:26 +02:00
}
}
}
}
} //end if
2012-04-21 00:36:55 +00:00
2015-07-13 20:10:26 +02:00
if ( ! empty ( $agent_sensors )) {
echo 'Sensors: ' ;
check_valid_sensors ( $device , 'temperature' , $valid [ 'sensor' ], 'agent' );
2017-02-24 01:25:05 +00:00
d_echo ( $agent_sensors );
if ( count ( $agent_sensors ) > 0 ) {
record_sensor_data ( $device , $agent_sensors );
}
2015-07-13 20:10:26 +02:00
echo " \n " ;
}
2012-03-26 09:37:55 +00:00
2015-07-13 20:10:26 +02:00
echo " \n " ;
} //end if