还是PHP当道,自动从Hadoop集群的所有节点的Log中发现异常(ERROR),并打印出来
其中作法很简单,ssh到每个节点上用grep搜索,使用PHP的SSH2扩展库
#!/usr/bin/php
<?php
require_once '/home/hadoop/bin/common/hadoop.inc';
function check_log($host, $match, $key = 'ERROR')
{
$error_info = rexec($host, "grep -A 3 $key -h -s ". HADOOP_HOME. "/logs/*$match*.log");
if( trim($error_info) <> '')
{
$lines = explode("\n", $error_info);
if( count($lines) > 0 ) { // 找到错误
echo $error_info;
}
}
}
$masters = f2a(HADOOP_HOME . '/conf/masters');
$slaves = f2a(HADOOP_HOME . '/conf/slaves');
$master_items = array('namenode', 'secondarynamenode', 'jobtracker');
$slave_items = array('datanode', 'tasktracker');
foreach($masters as $master)
{
echo "Checking Master [$master]...\n";
echo "-------------------------------\n";
foreach($master_items as $item)
{
echo "[$item]\n";
check_log($master, $item);
}
echo "-------------------------------\n";
}
echo "\n\n";
foreach($slaves as $slave)
{
echo "Checking Slave [$slave]...\n";
echo "-------------------------------\n";
foreach($slave_items as $item)
{
echo "[$item]\n";
check_log($slave, $item);
}
echo "-------------------------------\n";
}
?>
Hadoop.Inc文件
<?php
define('HADOOP_HOME', '/home/hadoop/hadoop-0.18.1');
define('HADOOP_TMP_DIR','/home/hadoop/tmp');
define('HADOOP_NAME_DIR','/home/hadoop/name');
define('HADOOP_DATA_DIR','/home/hadoop/data');
function rexec($host, $cmd)
{
$connection = ssh2_connect($host, 22);
// ssh2_auth_password($connection, 'username', 'password');
if (ssh2_auth_pubkey_file($connection, 'hadoop',
'/home/hadoop/.ssh/id_dsa.pub',
'/home/hadoop/.ssh/id_dsa')) {
// echo "Public Key Authentication Successful\n";
} else {
echo("Accessing $host: Public Key Authentication Failed\n");
return '';
}
$stream = ssh2_exec($connection, $cmd);
/*
while($line = fgets($stream)) {
flush();
echo $line."<br />";
}
*/
stream_set_blocking($stream, true);
return stream_get_contents($stream);
}
function ping_host($host)
{
$connection = @ssh2_connect($host, 22);
// ssh2_auth_password($connection, 'username', 'password');
if( $connection === false)
return false;
if (@ssh2_auth_pubkey_file($connection, 'hadoop',
'/home/hadoop/.ssh/id_dsa.pub',
'/home/hadoop/.ssh/id_dsa')) {
// echo "Public Key Authentication Successful\n";
return true;
} else {
// echo("Accessing $host: Public Key Authentication Failed\n");
return false;
}
}
function f2a( $filename )
{
return file( $filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
}
?>