Skip to content


nagios 使用check_file.pl 检查服务是否运行

服务运行时通常会产生个pid文件,我们可以检查这个文件是否存在来简单判定服务是否在运行。

http://exchange.nagios.org/directory/Plugins/Uncategorized/Operating-Systems/Linux/checkfile/details

check_file.pl

#!/usr/bin/perl -w

############################## check_file.pl ##############
# Version : 0.1
# Date : Oct 15 2007
# Author : Samuel Mutel
# Licence : GPL – http://www.fsf.org/licenses/gpl.txt
###########################################################

use strict;
use Getopt::Long;

my $Version=’0.1′;
my %ERRORS=(‘OK’=>0,’WARNING’=>1,’CRITICAL’=>2,’UNKNOWN’=>3,’DEPENDENT’=>4);

my $o_help=undef; # wan’t some help ?
my $o_version=undef; # print version
my $o_exist=undef; # Check if file exist
my $o_nexist=undef; # Check if a file doesn’t exist
my $o_empty=undef; # Check if a file is empty
my $o_nempty=undef; # Check if a file is not empty
my $o_paramok=undef;

sub print_version {
print “check_file version : $Version\n”;
}

sub print_usage {
print “Usage: check_file.pl [-v] [-h] [-e|-n|-m|-t] \n”;
}

sub print_help {
print “\nNagios Plugin to check if a file exist/doesn’t exist.\n”;
print “It check too if a file is empty or not.\n\n”;
print_usage();
print < \$o_help, ‘help’ => \$o_help,
‘v’ => \$o_version, ‘version’ => \$o_version,
‘e’ => \$o_exist, ‘exist’ => \$o_exist,
‘n’ => \$o_nexist, ‘nexist’ => \$o_nexist,
‘m’ => \$o_empty, ’empty’ => \$o_empty,
‘t’ => \$o_nempty, ‘nempty’ => \$o_nempty,
);

if (defined ($o_help)) { print_help(); exit $ERRORS{“UNKNOWN”}};
if (defined ($o_version)) { print_version(); exit $ERRORS{“UNKNOWN”}};
if (!defined ($o_exist) && !defined ($o_nexist) && !defined ($o_empty) && !defined ($o_nempty)) { print_usage(); exit $ERRORS{“UNKNOWN”}};;
}

###### MAIN ######

check_options();

if (@ARGV != 1) {
print_usage();
exit $ERRORS{“UNKNOWN”};
}

my $file = $ARGV[0];

if (defined ($o_exist)) {
if (-e $file) {
print “File ” . $file . ” exist.”;
exit $ERRORS{“OK”}
}
else {
print “File ” . $file . ” does not exist.”;
exit $ERRORS{“CRITICAL”}
}
}

if (defined ($o_nexist)) {
if (! -e $file) {
print “File ” . $file . ” does not exist.”;
exit $ERRORS{“OK”}
}
else {
print “File ” . $file . ” exist.”;
exit $ERRORS{“CRITICAL”}
}
}

if (defined ($o_empty)) {
if (! -s $file) {
print “File ” . $file . ” is empty.”;
exit $ERRORS{“OK”}
}
else {
print “File ” . $file . ” is not empty.”;
exit $ERRORS{“CRITICAL”}
}
}

if (defined ($o_nempty)) {
if (-s $file) {
print “File ” . $file . ” is not empty.”;
exit $ERRORS{“OK”}
}
else {
print “File ” . $file . ” is empty.”;
exit $ERRORS{“CRITICAL”}
}
}

在被监控机上
wget http://exchange.nagios.org/components/com_mtree/attachment.php?link_id=1094&cf_id=24

mv check_file.pl /usr/local/nagios/libexec/
cd !$
chown nagios:nagios check_file.pl
chmod 0775 check_file.pl

#在nrpe.cfg中增加

command[check_msnbot]=/usr/local/nagios/libexec/check_file.pl -e /opt/lampp/htdocs/msnbot/log/msnbot.pid

#重启nrpe
ps aux|grep nrpe
kill $pid
sudo -u nagios /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d

在监控机上
在需要监控的服务器配置文件中增加

define service{
use local-service ; Name of service template to use
host_name c1gserver
service_description msnbot
check_command check_nrpe!check_msnbot
notifications_enabled 1
}

重启nagios服务后就可以看到了

service nagios reload

Posted in Nagios, 技术.

Tagged with , .


No Responses (yet)

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.