2012-03-26 09:37:55 +00:00
< ? php
2012-04-21 00:30:55 +00:00
if ( $device [ 'os_group' ] == " unix " )
2012-03-26 09:37:55 +00:00
{
2012-04-21 00:30:55 +00:00
echo ( " Observium UNIX Agent: " );
2012-04-21 00:36:55 +00:00
2012-04-27 17:18:26 +00:00
### FIXME - this should be in config and overridable in database
2012-04-21 00:36:55 +00:00
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-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:36:55 +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
if ( $section == " apache " ) { $sa = " app " ; $sb = " apache " ; }
if ( $section == " mysql " ) { $sa = " app " ; $sb = " mysql " ; }
2012-04-30 15:13:13 +00:00
# if($section == "drbd") { $sa = "app"; $sb = "drbd"; }
2012-04-27 17:18:26 +00:00
2012-04-26 11:57:52 +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-04-27 17:18:26 +00:00
# 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-04-21 00:36:55 +00:00
2012-04-21 00:30:55 +00:00
### Processes
if ( ! empty ( $agent_data [ 'ps' ]))
{
echo ( " \n Processes: " );
foreach ( explode ( " \n " , $agent_data [ 'ps' ]) as $process )
{
2012-04-19 15:36:46 +00:00
$process = preg_replace ( " / \ ((.*),([0-9]*),([0-9]*),([0-9 \ .]*) \ ) \ (.*)/ " , " \\ 1| \\ 2| \\ 3| \\ 4| \\ 5 " , $process );
list ( $user , $vsz , $rss , $pcpu , $command ) = explode ( " | " , $process , 5 );
2012-04-21 00:36:55 +00:00
$processlist [] = array ( 'user' => $user , 'vsz' => $vsz , 'rss' => $rss , 'pcpu' => $pcpu , 'command' => $command );
2012-04-21 00:30:55 +00:00
}
#print_r($processlist);
2012-04-19 15:36:46 +00:00
}
2012-04-21 00:36:55 +00:00
2012-04-21 00:30:55 +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-04-27 17:18: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_instance' => $memcached_host ), 'applications' );
}
}
}
2012-04-21 00:30:55 +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-04-21 00:30:55 +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-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
}
?>