诚之和:如何 一键搭建nagios监控系统
这篇文章将为大家详细讲解有关如何 一键搭建nagios监控系统,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
#! /bin/bash
################## nagios server configure ########################
installserver() { #定义安装nagios函数
read -p " Please input the alarm email address : " email #报警邮箱
read -p " Please input the login nagios user: " loginuser #网页登录时帐户
read -p " Please input the login nagios pass: " loginpass #网页登录时的密码
echo "############### Install nagios server #################"
yum -y install httpd php php-gd php-xml php-mbstring php-ldap php-pear php-xmlrpc httpd-manual mod_ssl mod_perl zlib libpng freetype libart_lgpl libart_lgpl-devel libxml* pango*
#安装 apache php 及其他
wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.3.tar.gz
useradd nagios -s /sbin/nologin
tar -xvf nagios-3.2.3.tar.gz
cd nagios-3.2.3
./configure --prefix=/usr/local/nagios
make all
make install
make install-init
make install-config
make install-commandmode
make install-webconf
cd .. #安装nagios软件
wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.15.tar.gz
tar -xvf nagios-plugins-1.4.15.tar.gz
cd nagios-plugins-1.4.15
./configure
make
make install
cd .. #安装nagios监控插件
wget http://nchc.dl.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gz
tar -xvf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure
make all
make install
make install-plugin
make install-daemon
make install-daemon-config
cd .. #安装nrpe插件
wget http://oss.oetiker.ch/rrdtool/pub/rrdtool-1.4.7.tar.gz
tar -xvf rrdtool-1.4.7.tar.gz
cd rrdtool-1.4.7
./configure --prefix=/usr/local/rrdtool/ &&make &&make install
cd .. #安装绘图工具
wget http://pkgs.fedoraproject.org/repo/pkgs/pnp4nagios/pnp-0.4.12.tar.gz/eb833a4769a5b58aad0ac53cae3e3e9f/pnp-0.4.12.tar.gz
tar -xvf pnp-0.4.12.tar.gz
cd pnp-0.4.12
./configure --with-nagios-user=nagios --with-nagios-group-nagios --with-rrdtool=/usr/local/rrdtool/bin/rrdtool
make
make all
make install
make install-config
make install-init
cd .. #安装绘图工具
## 以上为安装nagios服务的一些软件。
wget http://218.6.16.170/somenagios.tar #这里的我个人找的一些 cpu ,内存,及mysql的插件。 打包在这。
tar -xvf somenagios.tar -C /usr/local/nagios/libexec/
echo "################install nagios server completed#############" #nagios安装完成。
echo "################# configure nagios server start###############" #下面是常规的一些配置。
htpasswd -bc /usr/local/nagios/etc/htpasswd.users $loginuser $loginpass #创建用户密码,就是上面要求输入的内容。
# cgi.cfg configure#
sed -i "s#use_authentication=1#use_authentication=0#g" /usr/local/nagios/etc/cgi.cfg #更改use_authentication参数
#nagios.cfg configure#
sed -i '35i\cfg_file=/usr/local/nagios/etc/objects/hosts.cfg' /usr/local/nagios/etc/nagios.cfg #在nagios.cfg里添加 host.cfg
sed -i '35i\cfg_file=/usr/local/nagios/etc/objects/services.cfg' /usr/local/nagios/etc/nagios.cfg #在nagios.cfg里添加 services.cfg
sed -i '35i\cfg_file=/usr/local/nagios/etc/objects/hostgroup.cfg' /usr/local/nagios/etc/nagios.cfg #在nagios.cfg里添加 hostgroup.cfg
sed -i "s#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg#\#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg#g" /usr/local/nagios/etc/nagios.cfg #禁用监控本地的配置文件
sed -i "s#process_performance_data=0#process_performance_data=1#g" /usr/local/nagios/etc/nagios.cfg #更改 process_performance_data 为1
sed -i "s/#host_perfdata_command=process-host-perfdata/host_perfdata_command=process-host-perfdata/g" /usr/local/nagios/etc/nagios.cfg #开启 host_perfdata_commandsed -i "s/#service_perfdata_command=process-service-perfdata/service_perfdata_command=process-service-perfdata/g" /usr/local/nagios/etc/nagios.cfg #开启service_perfdata_command
#command.cfg configure#
line1=`sed -n '/process-host-perfdata$/=' /usr/local/nagios/etc/objects/commands.cfg` #查找commands.cfg里 process-host-perfdata的行
line2=`sed -n '/process-service-perfdata$/=' /usr/local/nagios/etc/objects/commands.cfg` #同上
sed -i "$(($line1+1))"d /usr/local/nagios/etc/objects/commands.cfg #删除上面找的行的下一行
sed -i "$(($line1+1))i\ command_line /usr/local/nagios/libexec/process_perfdata.pl" /usr/local/nagios/etc/objects/commands.cfg #插入新的命令
sed -i "$(($line2+1))"d /usr/local/nagios/etc/objects/commands.cfg #删除上面找的行的下一行
sed -i "$(($line2+1))i\ command_line /usr/local/nagios/libexec/process_perfdata.pl" /usr/local/nagios/etc/objects/commands.cfg #插入新的命令
echo '
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ -t 30
}
define command {
command_name check_mysql
command_line $USER1$/check_mysql -H $HOSTADDRESS$ -unagdb -pnagdb -d nagdb
}
'>>/usr/local/nagios/etc/objects/commands.cfg # 在commands.cfg里添加两条命令。 nrpe及监控mysql
#templates.cfg configure#
echo '
define host {
name hosts-pnp
register 0
action_url /nagios/pnp/index.php?host=$HOSTNAME$
process_perf_data 1
}
define service {
name services-pnp
register 0
action_url /nagios/pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$
process_perf_data 1
}'>>/usr/local/nagios/etc/objects/templates.cfg
# 上面是在templates.cfg里添加性能分析图表的功能
#pnp configure#
cp /usr/local/nagios/etc/pnp/process_perfdata.cfg-sample /usr/local/nagios/etc/pnp/process_perfdata.cfg
cp /usr/local/nagios/etc/pnp/npcd.cfg-sample /usr/local/nagios/etc/pnp/npcd.cfg
cp /usr/local/nagios/etc/pnp/rra.cfg-sample /usr/local/nagios/etc/pnp/rra.cfg
chown -R nagios:nagios /usr/local/nagios/etc/pnp/*
#上面把pnp的模板文件改成真的配置文件 并授权
sed -i 's#LOG_LEVEL = 0#LOG_LEVEL = 2#g' /usr/local/nagios/etc/pnp/process_perfdata.cfg
#更改 LOG_LEVEL 为2
#contacts.cfg configure#
sed -i "s#nagios@localhost#$email#g" /usr/local/nagios/etc/objects/contacts.cfg
clear
#更改contacts.cfg里面的联系人为所输入的联系人
echo "################### nagios server install and configure completed,plase add hosts and services then start it ##################"
} #以上nagios安装及配置完成。
restartnagios(){ # 这里是定义nagios,nrpe,及apache重启的函数
echo "stopping ......"
service nagios stop #nagios stop
killall -9 nrpe #nrpe stop
service httpd stop #apache stop
echo "starting ......"
service nagios start #nagios start
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d # nrpe start
service httpd start #apache start
}
addservices(){ #这里定义了在nagios端添加客户机的函数
read -p "please input the alias name : " host #输入添加机器别名
read -p "please input the clint ip : " ip #输入添加机器的ip
echo "define host {
host_name $host
use linux-server,hosts-pnp
alias nagios server
address $ip
contact_groups admins
check_command check-host-alive
max_check_attempts 5
notification_interval 10
notification_period 24x7
notification_options d,u,r
}" >> /usr/local/nagios/etc/objects/hosts.cfg #在hosts.cfg里添加一个主机 (参数的意思可以在网上看下)
echo "define hostgroup {
hostgroup_name myserver
alias myserver
members $host
}" >>/usr/local/nagios/etc/objects/hostgroup.cfg #在hostgroup.cfg里添中一个主机组,$host就是添加的别名
是添加监控的具体服务。
echo "define service {
host_name $host
use local-service,services-pnp
service_description CPU
check_period 24x7
max_check_attempts 4
normal_check_interval 1
retry_check_interval 1
contact_groups admins
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_cpu
} #监控客户机CPU
define service {
host_name $host
use local-service,services-pnp
service_description CPU_load
check_period 24x7
max_check_attempts 4
normal_check_interval 1
retry_check_interval 1
contact_groups admins
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_load
} #监控客户机负载
define service {
host_name $host
use local-service,services-pnp
service_description HTTP
check_period 24x7
max_check_attempts 4
normal_check_interval 1
retry_check_interval 1
contact_groups admins
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_http
} #监控客户机http服务
define service {
host_name $host
use local-service,services-pnp
service_description MEMORY
check_period 24x7
max_check_attempts 4
normal_check_interval 1
retry_check_interval 1
contact_groups admins
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_mem
} #监控客户机的内存使用情况
define service {
host_name $host
service_description connect-count
check_period 24x7
max_check_attempts 4
normal_check_interval 1
retry_check_interval 1
contact_groups admins
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_ips
} #监控客户机的连接情况
define service {
host_name $host
service_description DISK
check_period 24x7
max_check_attempts 4
normal_check_interval 1
retry_check_interval 1
contact_groups admins
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_disk
} #监控客户机的硬盘分区使用情况
define service {
host_name $host
service_description mysql
check_period 24x7
max_check_attempts 4
normal_check_interval 1
retry_check_interval 1
contact_groups admins
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_mysql
} #监控客户机的mysql监控状态
define service {
host_name $host
service_description PING
use local-service,services-pnp
check_period 24x7
max_check_attempts 4
normal_check_interval 1
retry_check_interval 1
contact_groups admins
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_ping!100.0,20%!500.0,60%
} #监控客户机的ping是否正常
define service {
host_name $host
use local-service,services-pnp
service_description FTP
check_period 24x7
max_check_attempts 4
normal_check_interval 60
retry_check_interval 10
contact_groups admins
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_ftp
} #监控客户机的ftp服务
" >>/usr/local/nagios/etc/objects/services.cfg #把上面内容添加到services.cfg里面 (参数的意思可以在网上看下)
echo "############### Add services completed ########"
}
######################### nagios configure completed ########################
#以上为nagios服务端配置。
####################### client server configure ########################
#以下为被监控端配置
clientnagios() { #定义客户机的安装配置
### install soft###
read -p "Please input the nagios server ip : " nagiosip #输入naiogs服务端ip
read -p "Please input the mysql password : " mysqlpass #输入本机的mysql密码。
useradd nagios
yum install zlib libpng freetype libart_lgpl libart_lgpl-devel libxml* pango*
#安装所需要的包
wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.15.tar.gz
tar -xvf nagios-plugins-1.4.15.tar.gz
cd nagios-plugins-1.4.15
./configure
make
make install
cd ..
wget http://nchc.dl.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gz
tar -xvf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure
make all
make install
make install-plugin
make install-daemon
make install-daemon-config
cd ..
#以上为安装被监控机所需要的软件
wget http://218.6.16.170/somenagios.tar
tar -xvf somenagios.tar -C /usr/local/nagios/libexec/ #这里是个人找的几个监控插件
### install completed ###
### configure ####
rm /usr/local/nagios/etc/nrpe.cfg #删除安装的nrpe.cfg配置文件
cat >>/usr/local/nagios/etc/nrpe.cfg<<EOF #添加以下内容到nrpe.cfg配置文件里。主要就是一些监控本机的命令,供nagios服务端调用
log_facility=daemon
pid_file=/var/run/nrpe.pid
server_port=5666
nrpe_user=nagios
nrpe_group=nagios
allowed_hosts=$nagiosip
dont_blame_nrpe=1
debug=1
command_timeout=30
connection_timeout=300
command[check_http]=/usr/local/nagios/libexec/check_http -H -u 127.0.0.1 /index.php -t 60
command[check_disk]=/usr/local/nagios/libexec/check_disk -w 15% -c 10% -A -i '/dev/shm'
command[check_cpu]=/usr/local/nagios/libexec/check_cpu.sh
command[check_mem]=/usr/local/nagios/libexec/check_mem.sh
command[check_ips]=/usr/local/nagios/libexec/ip_conn.sh 400 600
command[check_load]=/usr/local/nagios/libexec/check_load -w 14,12,10 -c 18,15,12
command[check_ftp]=/usr/local/nagios/libexec/check_ftp 127.0.0.1 -w 10 -c 60
EOF
mysql -uroot -p"$mysqlpass" <<EOF
create database nagdb;
grant all on nagdb.* to nagdb@"$nagiosip" identified by 'nagdb';
flush privileges;
\q
EOF #这里是创建一个只能是nagios服务端访问的库。监控mysql状态是需有的。
killall -9 nrpe
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d #重启被监控机的nrpe服务
############ configure completed ############
}
#客户机配置完成
real=`grep -l '\^H' /root/.bash_profile`
if [ $? -eq 1 ];then
echo 'stty erase ^H' >> /root/.bash_profile
source /root/.bash_profile #这几行主要就是让在使用read键时能使用回删键。写错了,回删了,重启写。不用这段的话,回删键会变成乱码。
fi
echo -e '\033[0;33;1m #################nagios################## \033[0m' #让echo能弄点颜色出来好看点。。。
echo "nagios server install and configure or addserver please input : 1"
echo " clint server install or configure please input : 2"
echo " exit please input : 3"
echo -e '\033[0;33;1m ######################################### \033[0m'
read -p "please chose : " frist #定义输入的值
if [ $frist -eq 1 ];then #值等于1时执行nagios服务器配置
echo -e '\033[0;33;1m #################nagios################## \033[0m'
echo "nagios server install and configue please input : 1"
echo " Add server and services please input : 2"
echo " exit please input : 3"
echo -e '\033[0;33;1m ######################################### \033[0m'
read -p "please chose : " nagiosfrist #定义nagiosfrist输入的值
if [ $nagiosfrist -eq 1 ];then #当nagiosfrist等于1时,执行下面 安装,添加服务 入重启服务的函数
installserver
addservices
restartnagios
elif [ $nagiosfrist -eq 2 ];then #当nagiosfrist等于2时,执行添加服务,重启服务函数
addservices
restartnagios
elif [ $nagiosfrist -eq 3 ];then #nagiosfrist等于3,就退出
clear
exit
else
echo "plase chose [1|2|3]" #都不等于的话,打印这个
fi
elif [ $frist -eq 2 ];then # 当frist等于2时 执行被监控端的函数
clientnagios
elif [ $frist -eq 3 ];then #等于3时退出
clear
exit
else
echo "plase chose [1|2|3]" #都不等的话,就打印这个
fi