Perl实现批量安装NRPE客户端

近期要安装N多台Linux主机的NRPE客户端,话说我这么“懒”的人怎么可能会一个个自己安.批量处理的简单小程序就用perl来做吧!

 

# tar -tf BatchNrpe.tar.gz
BatchNrpe/
BatchNrpe/install.sh
BatchNrpe/Check_Nrpe.pl
BatchNrpe/nrpe-2.15.tar.gz
BatchNrpe/Batch_Installation_Nrpe.pl
BatchNrpe/hostlist
BatchNrpe/nagios-plugins-1.5.tar.gz

BatchNrpe.tar.gz为主主程序包.

 

install.sh是自动部署NRPE的Shell脚本

useradd nagios

/bin/tar -zxvf nagios-plugins-1.5.tar.gz
cd nagios-plugins-1.5
./configure
make
make install
/bin/chown nagios.nagios /usr/local/nagios
cd ..
yum -y install xinetd --nogpgcheck
/bin/tar -zxvf nrpe-2.15.tar.gz
cd nrpe-2.15
./configure
make all
make install-plugin
make install-daemon
make install-daemon-config
make install-xinetd
perl -i -p -e 's/(127.0.0.1)/$1 172.20.11.2/' /etc/xinetd.d/nrpe
echo "nrpe            5666/tcp              # nrpe" >> /etc/services
/etc/init.d/xinetd restart

 

hostlist是需要安装NRPE的主机列表

#IC Server

#格式 IP->密码
172.20.105.1->sunnorth
172.20.105.2->sunnorth
172.20.105.3->7234567
172.20.105.4->1128@123
172.20.105.5->8888@321
172.20.105.6->ggggggg@
172.20.105.7->sunnorth
172.20.105.8->9999990000@
172.20.105.9->sun

 

 

nagios-plugins-1.5.tar.gz  nrpe-2.15.tar.gz是安装必须要的Tarball

 

Batch_Installation_Nrpe.pl用于自动scp必要的Tarball和install.sh安装脚本至hostlist列表主机

#!/bin/env perl
#
# This program is used to batch install NRPE
#
#
# Created : Yumeng
# Creation Date : 4 December 2013
#
# E-mail  : mixmaomao@163.com
#
# Use Statement
#
use strict;
use warnings;
use Expect;
#
# Read the installation list
# File Format: IP address->Passwd
# such as : 172.20.1.1->password
#
open HOSTLIST,"<","hostlist" or die "Can't open file :$!\n";
my @hostlist = <HOSTLIST>;
close HOSTLIST;
#
my %hostlist;
for (@hostlist)
{
    chomp;
    next if m{^#};
    my $host;   my $pass;
    $host = (split(/\->/))[0];
    $pass = (split(/\->/))[1];
    $hostlist{$host}=$pass;
}
#
# Login host
#
while (my ($host,$pass)=each %hostlist)
{
    my $exp_scp = Expect->new;
       $exp_scp = Expect->spawn("scp install.sh  nagios-plugins-1.5.tar.gz  nrpe-2.15.tar.gz $host:/root");
       $exp_scp->expect(2, [
                      'password',
                      sub {
                           my $self_scp = shift;
                              $self_scp->send("$pass\n");
                          }
                      ],
                      [
                      '\(yes/no\)?',
                      sub {
                           my $self_scp = shift;
                              $self_scp->send("yes\n");
                              exp_continue;
                          }
                      ],
                   );
       #$exp->send("exit\n")  if ($exp->expect(undef,'#'));
    my $exp_ssh = Expect->spawn("ssh $host");
       $exp_ssh->expect(2, [
                      'password',
                      sub {
                           my $self = shift;
                              $self->send("$pass\n");
                          }
                      ],
                      [
                      '\(yes/no\)?',
                      sub {
                           my $self = shift;
                              $self->send("yes\n");
                              exp_continue;
                          }
                      ],
                   );
      $exp_ssh->send("sh install.sh\n") if ($exp_ssh->expect(undef,'#'));
      $exp_ssh->send("exit\n")  if ($exp_ssh->expect(undef,'#'));
}

 

Check_Nrpe.pl用于检测NRPE是否连通

#!/bin/env perl

open HOSTLIST,"<","hostlist" or die "Can't open file :$!\n";
my @hostlist = <HOSTLIST>;
close HOSTLIST;
for (@hostlist)
{
    chomp;
    next if m{^#};
    my $host;
    $host = (split(/\->/))[0];
    system("/usr/local/nagios/libexec/check_nrpe -H $host");
}

你可能感兴趣的:(perl)