📄 checker
字号:
#!/usr/bin/env perl# Use mandatory external modulesuse strict;use Cwd;use File::Basename;use Data::Dumper;use POSIX;use Config;# Determine installation dir nameour $SELF_DIR = dirname(dirname(dirname(Cwd::abs_path(__FILE__))));# Include parts of the systemrequire $SELF_DIR . '/lib/config.pm';# Unbuffered io$| = 1;# Read config filemy $postfix = "";if (scalar(@ARGV) && $ARGV[0] =~ /^@(.*)/) { shift(@ARGV); $postfix = "_$1";}my $cfg_file = "mmm_mon$postfix.conf";our $config = ReadConfig($cfg_file);MyExit("ERROR: Usage: $0 [\@cluster_name] <check_name>") if (scalar(@ARGV) < 1);# Get checker namemy $check_name = $ARGV[0];# Load checker modulemy $module_file = "$SELF_DIR/lib/check_modules/$check_name.pm";my $res = do $module_file;MyExit("ERROR: Can't load checker module '$check_name'") if (!$res);# Read timeout from config fileour $timeout = $config->{check}->{$check_name}->{timeout};# Read max_checksour $checks_to_restart = $config->{check}->{$check_name}->{restart_after};# Workaround to prevent checker from hanging in case of unnoticed broken pipe errorsour $max_empty_commands = 100;our $empty_commands = 0;# Process loopwhile (!eof(STDIN)) { # Check if it is time to die if (defined($checks_to_restart) && --$checks_to_restart < 1) { CheckerLog("Max checks performed, restarting..."); last; } CheckerLog("Waiting for a command..."); # Get command chomp(my $cmd = <STDIN>); CheckerLog("Got cmd = '$cmd'"); # Die if there are too many empty commands in a row if ($cmd eq '') { if (++$empty_commands > $max_empty_commands) { CheckerLog("WARNING: Too many empty commands ($empty_commands) in a row - looks like pipe is broken! Exiting!"); last; } next; } else { $empty_commands = 0; } my @command = split(/\s+/, $cmd); if ($cmd eq 'ping') { CheckerLog("Ping received"); print "OK: Pong!\n"; next; } if ($cmd ne '' && (scalar(@command) < 1 || scalar(@command) > 2)) { CheckerLog("Invalid command format! Command was: '$cmd'"); print "Invalid command format. Use <command> [param]\n"; next; } # Exit if asked last if ($command[0] =~ /^quit/i); # Process command and output result if ($command[0] eq 'check') { print PerformCheck($timeout, $command[1]) . "\n"; next; } print "ERROR: Invalid command ($cmd)\n";}CheckerLog("Exit");print("OK: Finished\n");exit(0);#-------------------------------------------------sub MyExit($) { $res = shift; print "$res\n"; CheckerLog("EXIT: $res"); exit(0);}#-------------------------------------------------sub CheckerLog($) { my $msg = shift; my $now = strftime("%Y-%m-%d %H:%M:%S", localtime); my $cluster = $postfix; $cluster ||= "_default"; my $f = "$SELF_DIR/var/checker/$check_name$cluster.log"; if (open(LOG, ">>$f")) { print LOG "[$now]: $$: $msg\n"; close(LOG); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -