Install redis (nagios plug-in) using perl scripts

Source: Internet
Author: User
Tags vars install redis
Author: A Hui http: farmerluo. googlecode. how to install comfilescheck_redis.pl: When the script uses the perl Redis database, you must first install this: # perl-MCPAN-eshell # installRediswgethttp: farmerluo. googlecode. comfilescheck_redis.plcpcheck_redis.p

Author: A Hui
Http: // faRmErluo.googlecode.com/FileS/check_rEdIs. pl

This section describes how to install:
The script uses the perl Redis database. You need to install this first:
# Perl-MCPAN-e shell
# Install Redis

Wget http://farmerluo.googlecode.com/files/check_redis.pl

CpCheck_redis.pl/etc/nagios/command/

ChownCacti. nagios check_redis.pl

Add this plugin to nagios:

Vi/etc/nagios/objects/command. cfg

# \ 'Check _ redis \ 'command definition
Define command {
Command_name check_redis
Command_line/etc/nagios/command/check_redis.pl-h $ HOSTADDRESS $ ARG1 $
}

Add a service:
Vi/etc/nagios/objects/linuxhost. cfg

Define service {
Use generic-service; Name of service template to use
Host_name memcached. ha2, meMcAched. web2
Service_description redis
Check_command check_redis
NotifiCatIons _EnableD 1
}

 

Check whether the nagios configuration is correct:
Nagios-v/etc/nagios. cfg

Nagios Core 3.2.1
Copyright (c) 2009-2010 Nagios Core Development Team and Community ConTrIbutors
Copyright (c) 1999-2009 Ethan GaLsTad
Last Modified: 03-09-2010
License: GPL

Web: http://www.nagios.org
Reading configuration data...
Read main config file okay...
Processing object config file \ '/etc/nagios/objects/commands. cfg \'...
Processing object config file \ '/etc/nagios/objects/contacts. cfg \'...
Processing object config file \ '/etc/nagios/objects/TimePeriods. cfg \'...
Processing object config file \ '/etc/nagios/objects/templates. cfg \'...
Processing object config file \ '/etc/nagios/objects/linuxhosts. cfg \'...
Processing object config file \ '/etc/nagios/objects/windows. cfg \'...
Read object config files okay...

Running pre-flight check on configuration data...

Checking services...
Checked 32 services.
Checking hosts...
Checked 14 hosts.
Checking host groups...
Checked 4 host groups.
Checking service groups...
Checked 0 service groups.
Checking contacts...
Checked 3 contacts.
Checking contact groups...
Checked 2 contact groups.
Checking service escalations...
Checked 0 service escalations.
Checking service dependeNcIes...
Checked 0 service dependencies.
Checking host escalations...
Checked 0 host escalations.
Checking host dependencies...
Checked 0 host dependencies.
Checking commands...
Checked 29 commands.
Checking time periods...
Checked 5 time periods.
Checking for circular paths between hosts...
Checking for circular host and service dependencies...
Checking global event handlers...
Checking obsessive compulsive processor commands...
Checking miscSet...

Total Warnings: 0
Total Errors: 0

ThingsLookOkay-No serious problems were detectedDuRing the pre-flight check

No problem. Let's re-load the configuration.

Service nagios reload

Next, we will introduce the notes for using perl to write the nagios plug-in:

1. Always generate some output content;
2. Add the reference \ 'use utils \ 'and reference some common modules for output ($ TIMEOUT % ERRORS & print_revision & support );
3. I always know some standard habits of Perl plug-ins, such:
1. Always exitExIt includes $ ERRORS {CRITICAL}, $ ERRORS {OK}, etc;
2. Use the getopt function for processingCommandLine;
3. The program handles excessive issues;
4. If no command parameter is provided, you must call print_usage;
5. Use the standard command line options (such as-H \ 'host \ ',-V \ 'version ).

Check_redisl.pl code:

#! /Usr/bin/perl

# Nagios:-epn

######################################## ########################################
# Check_redis-Nagios Plugin for Redis checks.
#
# @ Author farmer. luo at gmail.com
#@Date2010-05-12
# @ License GPL v2
#
# Check_nagios.pl-h -P -W -C
#
# Run the script need:
#
# Perl-MCPAN-e shell
# Install Redis
#
######################################## ########################################

Use strict;
Use warnings;
Use Redis;
Use File: Basename;
Use utils qw ($ TIMEOUT % ERRORS & print_revision & SuPport );
Use Time: Local;
Use vars qw ($ opt_h); # Redis host
Use vars qw ($ opt_p); # Redis Port
Use vars qw ($ opt_w); # A warning is issued when the time limit is exceeded.
Use vars qw ($ opt_c); # A severe warning is issued when the time limit is exceeded.
Use Getopt: Std;

$ Opt_h =;
$ Opt_p = 6379;
$ Opt_w = 5;
$ Opt_c = 10;
My $ r =;

Getopt (\ 'hp WcD \');

If ($ opt_h eq ){
Help ();
Exit (1 );
}

My $ start = time ();

Redis_connect ();

# Print $ @;
If ($ @){
Print UNKNOWN-cann \'t connect to redis server:. $ opt_h ..;
Exit $ ERRORS {UNKNOWN };
}

If (redis_set ()){
Print WARNING-redis server:. $ opt_h., set key error .;
Exit $ ERRORS {WARNING };
}

If (redis_get ()){
Print WARNING-redis server:. $ opt_h., get key error .;
Exit $ ERRORS {WARNING };
}

If (redis_del ()){
Print WARNING-redis server:. $ opt_h., del key error .;
Exit $ ERRORS {WARNING };
}

# Sleep(3 );
My $ stop = time ();

My $ run = $ stop-$ start;

If ($ run> $ opt_c ){

Print CRITICAL-redis server (. $ opt_h.) run for. $ run. seconds !;
Exit $ ERRORS {CRITICAL };

} Elsif ($ run> $ opt_w ){

Print WARNING-redis server (. $ opt_h.) run for. $ run. seconds !;
Exit $ ERRORS {WARNING };

} Else {

Redis_info ();
Redis_quit ();
Exit $ ERRORS {OK };

}


Sub help {

Die Usage: n, basename ($0),-h hostname-p port-w warning time-c critical time-d down timen

}

Sub redis_connect {

My $ redis_hp = $ opt_h.:. $ opt_p;

Eval{$ R = Redis-> new (server => $ redis_hp );};

}

Sub redis_set {

$ R-> set (redis_nagios_key => \ 'test \ ') | return 1;

Return 0;
}

Sub redis_get {

My $ value = $ r-> get (\ 'redis _ nagios_key \ ') | return 1;

Return 0;
}

Sub redis_del {

$ R-> del (\ 'redis _ nagios_key \ ') | return 1;

Return 0;
}

Sub redis_info {

My $ info_hash = $ r-> info;

Print OK-redis server (. $ opt_h.) info :;

While (my ($ key, $ value) = each (% $ info_hash )){
Print $ key => $ value ,;
}

}

Sub redis_quit {

$ R-> quit ();

}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.