偷瓜的贼 发表于 2019-1-17 07:36:48

nagios 监控fastcgi

监控脚本


#!/usr/bin/perl -w

# check_fastcgi.pl checks if a php-cgi server (or, theorically, any
# other fastcgi server) is alive.
#
# Copyright (c) 2009 Rodolfo Gonzalez
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.    If not, see .

# include modules

use strict;
use FCGI::Client;
use IO::Socket::INET;
use Getopt::Long;

# command line parameters with some defaults

my $host = '';               # server host
my $port = 9999;             # tcp port
my $script = '';             # test script (absolute path starting at / - root directory -)
my $query_string = ''; # query string
my $expected = 'OK';   # expected string
my $timeout = 5;             # timeout in seconds

# check command line options

GetOptions ('H=s' => \$host, 'p=i' => \$port, 's=s' => \$script, 'q=s' => \$query_string, 'e=s' => \$expected, 't=i' => \$timeout);

if (($host eq '') || ($script eq '')) {
   print "Usage: check_php-cgi.pl -H host -s[-p port] [-q ] [-e ] [-t ]\n";
   exit(-1);
}

# run check

my $sock = IO::Socket::INET->new(
      PeerAddr => $host,
      PeerPort => $port,
      Timeout    => $timeout,
      Proto      => 'tcp',
) or &_bad();

my $client = FCGI::Client::Connection->new( sock => $sock ) or &_bad();

my ( $stdout, $stderr ) = $client->request(
      +{
                REQUEST_METHOD    => 'GET',
                PHP_SELF                => $script,
                SCRIPT_FILENAME => $script,
                QUERY_STRING      => $query_string,
      },
      ''
) or &_bad();

if ($stdout =~ /$expected/) {
   &_good();
}
else {
   &_bad();
}

sub _good()
{
   print "OK: fastcgi server is working.";
   exit(0);
}

sub _bad()
{
   print "FastCGI CRITICAL: fastcgi has not responded.";
   exit(2);
}






先使用cpan安装


strict
FCGI::Client
IO::Socket::INET
Getopt::Long


建立一个/tmp/test.php文件
echo "ok" ;
?>




测试 脚本 :
./check_fastcgi.pl -H 127.0.0.1 -p 9000 -s /tmp/test.php -e ok
OK: fastcgi server is working


配置:
我对脚本进行了修改。指定了文件的目录。
可根据个人情况进行修改
my $host = '';         # server host
my $port = 9999;       # tcp port
my $script = '';       # test script (absolute path starting at / - root directory -)
my $query_string = ''; # query string
my $expected = 'OK';   # expected string
my $timeout = 5;       # timeout in seconds



commands.cfg
define command{
      command_name    check_fastcgi
      command_line    $USER1$/check_fastcgi
      }


监控配置:
define service{
      use                           generic-service
      host_name                     126
      service_description             Fastcgi
      check_command                   check_nrpe!check_fastcgi
      notifications_enabled         0
      }










页: [1]
查看完整版本: nagios 监控fastcgi