2012-03-26 09:37:55 +00:00
< ? php
2020-01-30 12:20:30 +00:00
use App\Models\Device ;
2017-02-23 22:45:50 +00:00
use LibreNMS\RRD\RrdDefinition ;
2020-05-26 14:04:38 +00:00
if ( $device [ 'os_group' ] == 'unix' || $device [ 'os' ] == 'windows' ) {
2019-06-23 05:29:12 +00:00
echo \LibreNMS\Config :: get ( 'project_name' ) . ' UNIX Agent: ' ;
2015-07-13 18:10:26 +00:00
2016-08-28 17:32:58 +00:00
$agent_port = get_dev_attrib ( $device , 'override_Unixagent_port' );
2015-11-17 00:20:06 +00:00
if ( empty ( $agent_port )) {
2019-06-23 05:29:12 +00:00
$agent_port = \LibreNMS\Config :: get ( 'unix-agent.port' );
2015-11-17 00:20:06 +00:00
}
2015-07-13 18:10:26 +00:00
2016-01-08 12:33:32 +00:00
$agent_start = microtime ( true );
2021-08-19 23:00:06 +00:00
$poller_target = \LibreNMS\Util\Rewrite :: addIpv6Brackets ( Device :: pollerTarget ( $device [ 'hostname' ]));
2020-02-11 18:14:11 +00:00
$agent = fsockopen ( $poller_target , $agent_port , $errno , $errstr , \LibreNMS\Config :: get ( 'unix-agent.connection-timeout' ));
2015-07-13 18:10:26 +00:00
2020-09-21 13:43:38 +00:00
if ( ! $agent ) {
echo 'Connection to UNIX agent failed on port ' . $agent_port . '.' ;
2016-08-28 17:32:58 +00:00
} else {
2021-03-20 16:19:09 +00:00
// Set stream timeout (for timeouts during agent fetch
stream_set_timeout ( $agent , \LibreNMS\Config :: get ( 'unix-agent.read-timeout' ));
$agentinfo = stream_get_meta_data ( $agent );
2015-07-13 18:10:26 +00:00
// fetch data while not eof and not timed-out
2020-09-21 13:43:38 +00:00
while (( ! feof ( $agent )) && ( ! $agentinfo [ 'timed_out' ])) {
2015-07-13 18:10:26 +00:00
$agent_raw .= fgets ( $agent , 128 );
2020-09-21 13:43:38 +00:00
$agentinfo = stream_get_meta_data ( $agent );
2015-07-13 18:10:26 +00:00
}
if ( $agentinfo [ 'timed_out' ]) {
2020-09-21 13:43:38 +00:00
echo 'Connection to UNIX agent timed out during fetch on port ' . $agent_port . '.' ;
2015-07-13 18:10:26 +00:00
}
2012-03-26 09:37:55 +00:00
}
2012-04-21 00:36:55 +00:00
2020-09-21 13:43:38 +00:00
$agent_end = microtime ( true );
2015-07-13 18:10:26 +00:00
$agent_time = round (( $agent_end - $agent_start ) * 1000 );
2012-04-26 11:57:52 +00:00
2020-09-21 13:43:38 +00:00
if ( ! empty ( $agent_raw )) {
echo 'execution time: ' . $agent_time . 'ms' ;
2012-05-20 23:14:08 +00:00
2020-09-21 13:43:38 +00:00
$tags = [
2017-02-23 22:45:50 +00:00
'rrd_def' => RrdDefinition :: make () -> addDataset ( 'time' , 'GAUGE' , 0 ),
2020-09-21 13:43:38 +00:00
];
$fields = [
2015-08-18 16:26:55 +00:00
'time' => $agent_time ,
2020-09-21 13:43:38 +00:00
];
2016-01-10 09:31:39 +00:00
data_update ( $device , 'agent' , $tags , $fields );
2015-08-19 20:58:02 +00:00
2020-07-23 14:57:22 +00:00
$os -> enableGraph ( 'agent' );
2015-07-13 18:10:26 +00:00
2020-09-21 13:43:38 +00:00
$agentapps = [
2020-09-21 13:59:34 +00:00
'apache' ,
'bind' ,
'ceph' ,
'mysql' ,
'nginx' ,
2022-08-03 13:31:06 +00:00
'php-fpm' ,
2020-09-21 13:59:34 +00:00
'powerdns' ,
'powerdns-recursor' ,
'proxmox' ,
'rrdcached' ,
'tinydns' ,
'gpsd' ,
2020-09-21 13:43:38 +00:00
];
2016-06-09 01:40:51 +00:00
2021-10-19 20:43:43 +00:00
global $agent_data ;
$agent_data = [];
2015-07-13 18:10:26 +00:00
foreach ( explode ( '<<<' , $agent_raw ) as $section ) {
2020-09-18 13:12:07 +00:00
[ $section , $data ] = explode ( '>>>' , $section );
2020-09-21 13:43:38 +00:00
[ $sa , $sb ] = explode ( '-' , $section , 2 );
2015-07-13 18:10:26 +00:00
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
}
2020-09-21 13:43:38 +00:00
if ( ! empty ( $sa ) && ! empty ( $sb )) {
2015-07-13 18:10:26 +00:00
$agent_data [ $sa ][ $sb ] = trim ( $data );
2016-08-28 17:32:58 +00:00
} else {
2015-07-13 18:10:26 +00:00
$agent_data [ $section ] = trim ( $data );
}
} //end foreach
2015-08-20 11:26:18 +00:00
d_echo ( $agent_data );
2012-05-20 23:14:08 +00:00
2015-07-13 18:10:26 +00: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 18:10:26 +00:00
foreach ( array_keys ( $agent_data ) as $key ) {
if ( file_exists ( " includes/polling/unix-agent/ $key .inc.php " )) {
2015-08-20 11:26:18 +00:00
d_echo ( " Including: unix-agent/ $key .inc.php " );
2012-05-20 23:14:08 +00:00
2015-07-13 18:10:26 +00:00
include " unix-agent/ $key .inc.php " ;
}
}
2020-05-26 14:04:38 +00:00
// Unix Processes
2020-09-21 13:43:38 +00:00
if ( ! empty ( $agent_data [ 'ps' ])) {
2015-07-13 18:10:26 +00:00
echo 'Processes: ' ;
2020-09-21 13:43:38 +00:00
dbDelete ( 'processes' , 'device_id = ?' , [ $device [ 'device_id' ]]);
$data = [];
2015-07-13 18:10:26 +00: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 );
2020-09-18 13:12:07 +00:00
[ $user , $vsz , $rss , $cputime , $pid , $command ] = explode ( '|' , $process , 6 );
2020-09-21 13:43:38 +00:00
if ( ! empty ( $command )) {
$data [] = [ '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
}
}
2015-07-15 22:16:25 +00:00
if ( count ( $data ) > 0 ) {
2018-08-17 20:29:20 +00:00
dbBulkInsert ( $data , 'processes' );
2015-07-14 20:57:15 +00:00
}
2015-07-13 18:10:26 +00:00
echo " \n " ;
}
2012-04-21 00:36:55 +00:00
2020-05-26 14:04:38 +00:00
// Windows Processes
2020-09-21 13:43:38 +00:00
if ( ! empty ( $agent_data [ 'ps:sep(9)' ])) {
2020-05-26 14:04:38 +00:00
echo 'Processes: ' ;
2020-09-21 13:43:38 +00:00
dbDelete ( 'processes' , 'device_id = ?' , [ $device [ 'device_id' ]]);
$data = [];
2020-05-26 14:04:38 +00:00
foreach ( explode ( " \n " , $agent_data [ 'ps:sep(9)' ]) as $process ) {
2020-06-26 16:41:29 +00:00
$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 );
2020-09-18 13:12:07 +00:00
[ $user , $VirtualSize , $WorkingSetSize , $zero , $processId , $PageFileUsage , $UserModeTime , $KernelModeTime , $HandleCount , $ThreadCount , $uptime , $process_name ] = explode ( '|' , $process , 12 );
2020-09-21 13:43:38 +00:00
if ( ! empty ( $process_name )) {
2020-05-26 14:04:38 +00:00
$cputime = ( $UserModeTime + $KernelModeTime ) / 10000000 ;
$days = floor ( $cputime / 86400 );
$hours = str_pad ( floor (( $cputime / 3600 ) % 24 ), 2 , '0' , STR_PAD_LEFT );
$minutes = str_pad ( floor (( $cputime / 60 ) % 60 ), 2 , '0' , STR_PAD_LEFT );
$seconds = str_pad (( $cputime % 60 ), 2 , '0' , STR_PAD_LEFT );
$cputime = ( $days > 0 ? " $days - " : '' ) . " $hours : $minutes : $seconds " ;
2020-09-21 13:43:38 +00:00
$data [] = [ 'device_id' => $device [ 'device_id' ], 'pid' => $processId , 'user' => $user , 'vsz' => $PageFileUsage + $WorkingSetSize , 'rss' => $WorkingSetSize , 'cputime' => $cputime , 'command' => $process_name ];
2020-05-26 14:04:38 +00:00
}
}
if ( count ( $data ) > 0 ) {
dbBulkInsert ( $data , 'processes' );
}
echo " \n " ;
}
2015-08-20 10:36:37 +00:00
foreach ( array_keys ( $agent_data [ 'app' ]) as $key ) {
if ( file_exists ( " includes/polling/applications/ $key .inc.php " )) {
2020-09-21 13:43:38 +00:00
d_echo ( " Enabling $key for " . $device [ 'hostname' ] . " if not yet enabled \n " );
2015-08-20 10:36:37 +00:00
2016-06-09 02:42:18 +00:00
if ( in_array ( $key , $agentapps )) {
2020-09-21 13:43:38 +00:00
if ( dbFetchCell ( 'SELECT COUNT(*) FROM `applications` WHERE `device_id` = ? AND `app_type` = ?' , [ $device [ 'device_id' ], $key ]) == '0' ) {
2015-08-20 10:36:37 +00:00
echo " Found new application ' $key ' \n " ;
2020-09-21 13:43:38 +00:00
dbInsert ([ 'device_id' => $device [ 'device_id' ], 'app_type' => $key , 'app_status' => '' , 'app_instance' => '' ], 'applications' );
2015-08-20 10:36:37 +00:00
}
}
2015-07-13 18:10:26 +00:00
}
2012-04-27 17:18:26 +00:00
}
2015-07-13 18:10:26 +00:00
// memcached
2020-09-21 13:43:38 +00:00
if ( ! empty ( $agent_data [ 'app' ][ 'memcached' ])) {
2015-07-13 18:10:26 +00:00
$agent_data [ 'app' ][ 'memcached' ] = unserialize ( $agent_data [ 'app' ][ 'memcached' ]);
foreach ( $agent_data [ 'app' ][ 'memcached' ] as $memcached_host => $memcached_data ) {
2020-09-21 13:43:38 +00:00
if ( dbFetchCell ( 'SELECT COUNT(*) FROM `applications` WHERE `device_id` = ? AND `app_type` = ? AND `app_instance` = ?' , [ $device [ 'device_id' ], 'memcached' , $memcached_host ]) == '0' ) {
2015-07-13 18:10:26 +00:00
echo " Found new application 'Memcached' $memcached_host\n " ;
2020-09-21 13:43:38 +00:00
dbInsert ([ 'device_id' => $device [ 'device_id' ], 'app_type' => 'memcached' , 'app_status' => '' , 'app_instance' => $memcached_host ], 'applications' );
2015-07-13 18:10:26 +00:00
}
}
}
2012-04-21 00:36:55 +00:00
2015-07-13 18:10:26 +00:00
// DRBD
2020-09-21 13:43:38 +00:00
if ( ! empty ( $agent_data [ 'drbd' ])) {
$agent_data [ 'app' ][ 'drbd' ] = [];
2015-07-13 18:10:26 +00:00
foreach ( explode ( " \n " , $agent_data [ 'drbd' ]) as $drbd_entry ) {
2020-09-18 13:12:07 +00:00
[ $drbd_dev , $drbd_data ] = explode ( ':' , $drbd_entry );
2015-07-13 18:10:26 +00:00
if ( preg_match ( '/^drbd/' , $drbd_dev )) {
$agent_data [ 'app' ][ 'drbd' ][ $drbd_dev ] = $drbd_data ;
2020-09-21 13:43:38 +00:00
if ( dbFetchCell ( 'SELECT COUNT(*) FROM `applications` WHERE `device_id` = ? AND `app_type` = ? AND `app_instance` = ?' , [ $device [ 'device_id' ], 'drbd' , $drbd_dev ]) == '0' ) {
2015-07-13 18:10:26 +00:00
echo " Found new application 'DRBd' $drbd_dev\n " ;
2020-09-21 13:43:38 +00:00
dbInsert ([ '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
2012-04-21 00:36:55 +00:00
2020-09-21 13:43:38 +00:00
// Use agent DMI data if available
2020-09-18 13:12:07 +00:00
if ( isset ( $agent_data [ 'dmi' ])) {
if ( $agent_data [ 'dmi' ][ 'system-product-name' ]) {
$hardware = ( $agent_data [ 'dmi' ][ 'system-manufacturer' ] ? $agent_data [ 'dmi' ][ 'system-manufacturer' ] . ' ' : '' ) . $agent_data [ 'dmi' ][ 'system-product-name' ];
2020-09-21 13:43:38 +00:00
// Clean up Generic hardware descriptions
2020-09-18 13:12:07 +00:00
DeviceCache :: getPrimary () -> hardware = rewrite_generic_hardware ( $hardware );
unset ( $hardware );
}
if ( $agent_data [ 'dmi' ][ 'system-serial-number' ]) {
DeviceCache :: getPrimary () -> serial = $agent_data [ 'dmi' ][ 'system-serial-number' ];
}
DeviceCache :: getPrimary () -> save ();
}
2020-09-21 13:43:38 +00:00
if ( ! empty ( $agent_sensors )) {
2015-07-13 18:10:26 +00:00
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 18:10:26 +00:00
echo " \n " ;
}
2012-03-26 09:37:55 +00:00
2015-07-13 18:10:26 +00:00
echo " \n " ;
} //end if