2012-03-26 09:37:55 +00:00
< ? php
2012-05-20 18:23:40 +00:00
global $debug ;
2012-03-26 09:37:55 +00:00
2012-05-20 18:23:40 +00:00
if ( $device [ 'os_group' ] == " unix " )
{
2013-11-04 23:33:32 +00:00
echo ( $config [ 'project_name' ] . " UNIX Agent: " );
2012-04-21 00:36:55 +00:00
2012-05-25 12:24:34 +00:00
// FIXME - this should be in config and overridable in database
2012-04-27 17:18:26 +00:00
$agent_port = '6556' ;
$agent_start = utime ();
$agent = fsockopen ( $device [ 'hostname' ], $agent_port , $errno , $errstr , 10 );
2012-04-21 00:36:55 +00:00
if ( ! $agent )
2012-03-26 09:37:55 +00:00
{
2012-04-21 00:30:55 +00:00
echo " Connection to UNIX agent failed on port " . $port . " . " ;
} else {
while ( ! feof ( $agent ))
{
$agent_raw .= fgets ( $agent , 128 );
}
2012-03-26 09:37:55 +00:00
}
2012-05-21 10:15:00 +00:00
2012-04-27 17:18:26 +00:00
$agent_end = utime (); $agent_time = round (( $agent_end - $agent_start ) * 1000 );
2012-04-21 00:36:55 +00:00
if ( ! empty ( $agent_raw ))
2012-03-26 09:37:55 +00:00
{
2012-04-27 17:18:26 +00:00
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' ]);
}
rrdtool_update ( $agent_rrd , " N: " . $agent_time );
$graphs [ 'agent' ] = TRUE ;
2012-04-21 00:30:55 +00:00
foreach ( explode ( " <<< " , $agent_raw ) as $section )
2012-04-05 16:37:46 +00:00
{
2012-04-21 00:30:55 +00:00
list ( $section , $data ) = explode ( " >>> " , $section );
2012-04-26 11:57:52 +00:00
list ( $sa , $sb ) = explode ( " - " , $section , 2 );
2012-04-27 17:18:26 +00:00
2012-05-02 22:02:30 +00:00
if ( $section == " apache " ) { $sa = " app " ; $sb = " apache " ; }
if ( $section == " mysql " ) { $sa = " app " ; $sb = " mysql " ; }
2012-05-07 21:56:38 +00:00
if ( $section == " nginx " ) { $sa = " app " ; $sb = " nginx " ; }
2015-01-16 18:30:53 +00:00
if ( $section == " bind " ) { $sa = " app " ; $sb = " bind " ; }
2015-01-17 19:27:44 +00:00
if ( $section == " tinydns " ) { $sa = " app " ; $sb = " tinydns " ; }
2012-05-02 22:02:30 +00:00
# if ($section == "drbd") { $sa = "app"; $sb = "drbd"; }
2012-04-27 17:18:26 +00:00
2012-05-02 22:02:30 +00:00
if ( ! empty ( $sa ) && ! empty ( $sb ))
2012-04-19 15:36:46 +00:00
{
2012-04-26 11:57:52 +00:00
$agent_data [ $sa ][ $sb ] = trim ( $data );
2012-04-05 16:37:46 +00:00
} else {
2012-04-26 11:57:52 +00:00
$agent_data [ $section ] = trim ( $data );
2012-03-26 09:37:55 +00:00
}
}
2012-04-21 00:36:55 +00:00
2012-05-20 18:23:40 +00:00
if ( $debug ) { print_r ( $agent_data ); }
2012-04-26 11:57:52 +00:00
include ( " unix-agent/packages.inc.php " );
include ( " unix-agent/munin-plugins.inc.php " );
2012-05-20 23:14:08 +00:00
2012-05-20 18:23:40 +00:00
foreach ( array_keys ( $agent_data ) as $key )
{
if ( file_exists ( " includes/polling/unix-agent/ $key .inc.php " ))
{
if ( $debug ) { echo ( " Including: unix-agent/ $key .inc.php " ); }
2012-05-20 23:14:08 +00:00
2012-05-20 18:23:40 +00:00
include ( " unix-agent/ $key .inc.php " );
}
}
foreach ( array_keys ( $agent_data [ 'app' ]) as $key )
{
if ( file_exists ( " includes/polling/applications/ $key .inc.php " ))
{
if ( $debug ) { echo ( " Including: applications/ $key .inc.php " ); }
2012-05-20 23:14:08 +00:00
2012-05-20 18:23:40 +00:00
include ( " applications/ $key .inc.php " );
}
}
2012-04-21 00:36:55 +00:00
2012-05-25 12:24:34 +00:00
// Processes
2012-04-21 00:30:55 +00:00
if ( ! empty ( $agent_data [ 'ps' ]))
{
2012-05-20 18:23:40 +00:00
echo ( " Processes: " );
2015-01-15 07:18:10 +00:00
dbDelete ( 'processes' , 'device_id = ?' , array ( $device [ 'device_id' ]));
2012-04-21 00:30:55 +00:00
foreach ( explode ( " \n " , $agent_data [ 'ps' ]) as $process )
{
2015-01-15 07:18:10 +00:00
$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 ) ) {
dbInsert ( array ( 'device_id' => $device [ 'device_id' ], 'pid' => $pid , 'user' => $user , 'vsz' => $vsz , 'rss' => $rss , 'cputime' => $cputime , 'command' => $command ), 'processes' );
}
2012-04-21 00:30:55 +00:00
}
2012-05-20 18:23:40 +00:00
echo ( " \n " );
2012-04-19 15:36:46 +00:00
}
2012-04-21 00:36:55 +00:00
2012-05-25 12:24:34 +00:00
// Apache
2012-04-27 17:18:26 +00:00
if ( ! empty ( $agent_data [ 'app' ][ 'apache' ]))
2012-04-19 15:36:46 +00:00
{
2012-04-21 00:30:55 +00:00
$app_found [ 'apache' ] = TRUE ;
2012-04-21 00:36:55 +00:00
if ( dbFetchCell ( " SELECT COUNT(*) FROM `applications` WHERE `device_id` = ? AND `app_type` = ? " , array ( $device [ 'device_id' ], 'apache' )) == " 0 " )
2012-04-21 00:30:55 +00:00
{
echo ( " Found new application 'Apache' \n " );
dbInsert ( array ( 'device_id' => $device [ 'device_id' ], 'app_type' => 'apache' ), 'applications' );
}
2012-04-19 15:36:46 +00:00
}
2012-04-21 00:36:55 +00:00
2012-05-25 12:24:34 +00:00
// memcached
2012-04-27 17:18:26 +00:00
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' );
}
}
}
2012-05-25 12:24:34 +00:00
// MySQL
2012-04-27 17:18:26 +00:00
if ( ! empty ( $agent_data [ 'app' ][ 'mysql' ]))
2012-04-19 15:36:46 +00:00
{
2012-04-21 00:30:55 +00:00
$app_found [ 'mysql' ] = TRUE ;
2012-04-21 00:36:55 +00:00
if ( dbFetchCell ( " SELECT COUNT(*) FROM `applications` WHERE `device_id` = ? AND `app_type` = ? " , array ( $device [ 'device_id' ], 'mysql' )) == " 0 " )
2012-04-21 00:30:55 +00:00
{
echo ( " Found new application 'MySQL' \n " );
dbInsert ( array ( 'device_id' => $device [ 'device_id' ], 'app_type' => 'mysql' ), 'applications' );
}
2012-04-19 15:36:46 +00:00
}
2012-04-21 00:36:55 +00:00
2012-05-25 12:24:34 +00:00
// DRBD
2012-04-30 15:13:13 +00:00
if ( ! empty ( $agent_data [ 'drbd' ]))
2012-04-19 15:36:46 +00:00
{
2012-04-30 15:13:13 +00:00
$agent_data [ 'app' ][ 'drbd' ] = array ();
foreach ( explode ( " \n " , $agent_data [ 'drbd' ]) as $drbd_entry )
2012-04-19 15:36:46 +00:00
{
2012-04-21 00:30:55 +00:00
list ( $drbd_dev , $drbd_data ) = explode ( " : " , $drbd_entry );
2012-04-21 00:36:55 +00:00
if ( preg_match ( " /^drbd/ " , $drbd_dev ))
2012-04-19 15:36:46 +00:00
{
2012-04-30 15:13:13 +00:00
$agent_data [ 'app' ][ 'drbd' ][ $drbd_dev ] = $drbd_data ;
2012-04-21 00:36:55 +00:00
if ( dbFetchCell ( " SELECT COUNT(*) FROM `applications` WHERE `device_id` = ? AND `app_type` = ? AND `app_instance` = ? " , array ( $device [ 'device_id' ], 'drbd' , $drbd_dev )) == " 0 " )
2012-04-21 00:30:55 +00:00
{
echo ( " Found new application 'DRBd' $drbd_dev\n " );
dbInsert ( array ( 'device_id' => $device [ 'device_id' ], 'app_type' => 'drbd' , 'app_instance' => $drbd_dev ), 'applications' );
}
2012-04-19 15:36:46 +00:00
}
}
}
}
2012-05-20 23:14:08 +00:00
2012-05-20 18:23:40 +00:00
if ( ! empty ( $agent_sensors ))
{
echo ( " Sensors: " );
check_valid_sensors ( $device , 'temperature' , $valid [ 'sensor' ], 'agent' );
echo ( " \n " );
}
2012-04-21 00:36:55 +00:00
2012-04-21 00:30:55 +00:00
echo ( " \n " );
2012-03-26 09:37:55 +00:00
}
?>