【代码实现】
具体代码实现可以使用各种代码了,C/C++、Ruby、Python、PHP ,只要能够访问文件、Socket ,能够定期执行的语言都可以,我们下面的代码采用 Perl 来构建,因为 Perl 是很好的系统管理脚本语言,任何 Unix/Linux 都缺省安装了 Perl 引擎,能够很方便的在任何机器上面运行,同时 Perl 的灵活性强,而且有强大的 CPAN 包库,所以编写代码很方便,在系统管理中也是值得推荐大家使用的,当然了,很多系统管理工作使用 shell 脚本也许更方便。
下面的代码实现对远程监控、本地日志文件监控、本地进程监控都进行了实现,但是只使用了远程端口监控的方式,因为这样就能够监控多台机器和服务了,如果只是单台机器或者只是想监控本地进程,可以很简单的修改主函数来实现。同时通知方式主要是采用邮件通知的方式,并且函数实现了SMTP协议进行邮件发送(因为我发现Perl内置的 Net::SMTP 在进行型验证的时候,并不是很靠谱),当然了,在报警通知方面,完全可以改写成发送短信或者按照时间来分别调用短信和邮件的方式。
代码中主要监控了包括 Apache、MySQL、Memcache、Search(假如你有的话)等服务,可以在这个基础上进行增删不同的服务器监控,只需要增加一个常量配置和修改 main 函数代码。
说明:以下Perl代码在 RHEL 4 + Perl v5.8.6 环境下测试通过
#!/usr/bin/perl
use IO::Socket;
use IO::File;
use MIME::Base64;
##############################
# Constant define (configure)
##############################
# mail config
use constant MAIL_ADDR => ('to'=>'webmaster@example.com', 'from'=>'webmaster@example.com');
use constant SMTP_INFO => ('host'=>'smtp.example.com', 'user'=>'webmaster', 'password'=>'pass',
'debug'=>1, 'bufsize'=>1024);
# common config
use constant MD5SUM_FILE => '/tmp/__monitor_md5sum_hash';
use constant APACHE_LOG_PATH => '/usr/local/apache2/logs/access';
# apache
use constant APACHE_PORT => 80;
use constant APACHE_SERVERS => ('web1.example.com', 'web2.example.com');
# mysql
use constant MYSQL_PORT => 3306;
use constant MYSQL_SERVERS => ('db1.example.com', 'db2.example.com');
# memcache
use constant MEMCACHE_PORT => 11211;
use constant MEMCACHE_SERVERS => ('cache1.example.com', 'cache2.example.com');
# search
use constant SEARCH_PORT => 8000;
use constant SEARCH_SERVERS => ('search1.example.com');
##############################
# Server port is alive check
##############################
sub check_server_alive {
my($server, $port) = @_;
$sock = IO::Socket::INET->new(PeerAddr=>$server, PeerPort=>$port, Proto=>'tcp', Timeout=>3);
if (!$sock){
return 0;
}
$sock->close();
return 1;
}
##############################
# Check process is exist
##############################
sub check_process_exist {
my $proc_name = shift;
$line = `/bin/ps auxw | /bin/grep $proc_name | /bin/grep -v grep | /usr/bin/wc -l`;
$line =~ s/^s+|s+$//g;
if ($line == 0){
return 0;
}
return 1;
}
##############################
# Check file md5 fingerprint
##############################
sub check_file_md5sum {
my $io, $line;
$filename = shift;
@arr = split(/s/, `/usr/bin/md5sum $filename`);
$filehash = shift(@arr);
$io = IO::File->new();
$io->open(MD5SUM_FILE, O_RDWR);
if (!($line = $io->getLine())){
$io->syswrite($filehash);
$io->close;
return true;
}
if ($line != $filehash){
$io->truncate(0);
$io->syswrite($filehash);
$io->close;
return true;
}
return true;
}
##############################
# SMTP execute command
##############################
sub smtp_cmd {
my ($sock, $cmd, $blocking) = @_;
my %smtpinfo = SMTP_INFO;
my $buf, $bufsize = $smtpinfo{'bufsize'}, $debug=$smtpinfo{'debug'};
$sock->syswrite($cmd);
if ($debug == 1){
print ">>> $cmd ";
}
if ($blocking == 1){
$sock->sysread($buf, $bufsize);
if ($debug){
print "<<< $buf";
}
}
}
##############################
# Send notice mail
##############################
sub send_mail {
my ($subject, $content) = @_;
my $sock;
my %mailaddr = MAIL_ADDR;
my %smtpinfo = SMTP_INFO;
my $debug = $smtpinfo{'debug'};
# Count date time
($sec, $min, $hour, $day, $mon, $year, $wday, $yday, $isdst) = localtime(time());
$datetime = sprintf("%s-%s-%s %s:%s:%s", "20".substr($year,1,2), length($mon)==1?"0$mon":$mon, length($day)==1?"0$day":$day, length($hour)==1?"0$hour":$hour, length($min)==1?"0$min":$min, length($sec)==1?"0$sec":$sec);
$subject .= "[$datetime]";
# Connect to SMTP server
$sock = IO::Socket::INET->new(PeerAddr=>$smtpinfo{'host'}, PeerPort=>25, Proto=>'tcp', Timeout=>10);
$sock->blocking(1);
# Send smtp command
if ($debug == 1){
print "<<< ". $sock->sysread($buf, $smtpinfo{'bufsize'});
}
smtp_cmd($sock, "HELO locahost ", 1);
smtp_cmd($sock, "AUTH LOGIN ", 1);
smtp_cmd($sock, encode_base64($smtpinfo{'user'}), 1);
smtp_cmd($sock, encode_base64($smtpinfo{'password'}), 1);
smtp_cmd($sock, "MAIL FROM: <". $mailaddr{'from'} ."> ", 1);
smtp_cmd($sock, "RCPT TO: <". $mailaddr{'to'} ."> ", 1);
smtp_cmd($sock, "DATA ", 1);
smtp_cmd($sock, "From: ". $smtpinfo{'from'} ." ", 0);
smtp_cmd($sock, "To: ". $smtpinfo{'to'} ." ", 0);
smtp_cmd($sock, "Subject: $subject ", 0);
smtp_cmd($sock, "$content ", 0);
smtp_cmd($sock, " . ", 1);
smtp_cmd($sock, "QUIT ", 0);
$sock->close();
return 1;
}
##############################
# Check server alive main
##############################
sub monitor_main {
# check apache
foreach $item (APACHE_SERVERS) {
if (!check_server_alive($item, APACHE_PORT)) {
send_mail("$item apache server is down", "$item apache server is down. please timely restoration");
}
}
# check mysql
foreach $item (MYSQL_SERVERS) {
if (!check_server_alive($item, MYSQL_PORT)) {
send_mail("$item mysql server is down", "$item mysql server is down. please timely restoration");
}
}
# check memcache
foreach $item (MEMCACHE_SERVERS) {
if (!check_server_alive($item, MEMCACHE_PORT)) {
send_mail("$item memcache server is down", "$item memcache server is down. please timely restoration");
}
}
# check search
foreach $item (SEARCH_SERVERS) {
if (!check_server_alive($item, SEARCH_PORT)) {
send_mail("$item search server is down", "$item search server is down. please timely restoration");
}
}
}
##############################
# Main running
##############################
monitor_main();