Shell脚本入门监控自动化菜鸟运维

快速配置:nagios自动化安装与配置脚本

以下为nagios的安装与配置脚本,脚本中server:192.168.1.114、client:192.168.1.113,主要监控host-
alive、disk、cpu_procs、load、ssh、swap、users、zombie_procs等,请根据实际环境修改配置文件。
脚本中开源软件版本:

httpd-2.2.22.tar.gz
libiconv-1.13.1.tar.gz
php-5.3.10.tar.gz
nagios-3.3.1.tar.gz
nagios-plugins-1.4.15.tar.gz
nrpe-2.13.tar.gz

使用方法:

cd /root
wget http://LinuxEye/uploads/nagios.zip
unzip nagios.zip
cd nagios
sh nagios_install.sh
脚本内容-服务端:
server.sh

#!/bin/bash
#by LinuxEye

#安装依赖包
yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel nss_ldap openldap openldap-devel  openldap-clients openldap-servers libxslt-devel libevent-devel ntp  libtool-ltdl bison libtool vim-enhanced gd gd-devel
useradd -M -s /sbin/nologin www
if [ -e "/root/nagios" ];then
echo -e "e[0;34m start installe[0m"
cd /root/nagios/
else
echo -e "e[1;31mPlease send nagios directory moved to /roote[0m"
exit 1
fi
#安装apache
tar xzf httpd-2.2.22.tar.gz
cd httpd-2.2.22
./configure --prefix=/usr/local/apache --enable-so --enable-rewrite --enable-headers --enable-deflate --with-mpm=worker
make && make install
cp /usr/local/apache/bin/apachectl  /etc/init.d/httpd
sed -i '2a # chkconfig: - 85 15' /etc/init.d/httpd
sed -i '3a # description: Apache is a World Wide Web server. It is used to serve' /etc/init.d/httpd
chmod 755 /etc/init.d/httpd
chkconfig --add httpd
chkconfig httpd on
cd ..
 
#安装php
tar xzf libiconv-1.13.1.tar.gz
cd libiconv-1.13.1
./configure --prefix=/usr/local/libiconv
make && make install
cd ..
tar xzf php-5.3.10.tar.gz
cd php-5.3.10
./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache/bin/apxs --enable-mbregex --enable-bcmath --enable-mbstring --with-curl --enable-ftp --with-libxml-dir --with-libexpat-dir --with-zlib-dir --with-jpeg-dir --with-freetype-dir --with-png-dir --with-zlib-dir --with-gd --disable-debug --with-iconv=/usr/local/libiconv
make && make install
cp php.ini-production /usr/local/php/lib/php.ini
cd ..
 
#修改配置文件
sed -i 's@;date.timezone =@date.timezone = Asia/Shanghai@g' /usr/local/php/lib/php.ini
if [ "$(hostname -i)" == "127.0.0.1" ];then
echo "OK"
else
sed -i "s/127.0.0.1.*$/127.0.0.1       `hostname`  localhost.localdomain localhost/g" /etc/hosts
fi
sed -i 's/User daemon/User www/g' /usr/local/apache/conf/httpd.conf
sed -i 's/Group daemon/User www/g' /usr/local/apache/conf/httpd.conf
sed -i 's/#ServerName www.example.com:80/ServerName 127.0.0.1:80/g' /usr/local/apache/conf/httpd.conf
sed -i '288a AddType application/x-httpd-php .php .phtml' /usr/local/apache/conf/httpd.conf
sed -i '289a AddType application/x-httpd-php-source .phps' /usr/local/apache/conf/httpd.conf
sed -i 's@DirectoryIndex index.html@DirectoryIndex index.html index.php@g' /usr/local/apache/conf/httpd.conf
cat >> /usr/local/apache/conf/httpd.conf << EOF
Alias /nagios/cgi-bin /usr/local/nagios/sbin
<Directory "/usr/local/nagios/sbin">
AuthType Basic
Options ExecCGI
AddHandler cgi-script .cgi
AllowOverride None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthUserFile /usr/local/nagios/etc/htpasswd
Require valid-user
</Directory>
Alias /nagios /usr/local/nagios/share
<Directory "/usr/local/nagios/share">
AuthType Basic
Options None
AllowOverride None
Order allow,deny
Allow from all
AuthName "nagios Access"
AuthUserFile /usr/local/nagios/etc/htpasswd
Require valid-user
</Directory>
EOF
 
useradd nagios;usermod -G nagios www
tar xzf nagios-3.3.1.tar.gz
cd nagios
./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios --with-command-user=nagios
make all && make install && make install-init && make install-commandmode && make install-config
cd ../
tar xzf nagios-plugins-1.4.15.tar.gz
cd nagios-plugins-1.4.15
./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios  && make && make install
chkconfig --add nagios
sed -i '138a              chmod 777 $NagiosVarDir/rw/nagios.cmd' /etc/init.d/nagios
cd ..
 
#安装nrpe
tar xzf nrpe-2.13.tar.gz
cd nrpe-2.13
./configure
make && make install
cp sample-config/nrpe.cfg  /usr/local/nagios/etc/
chown nagios.nagios /usr/local/nagios/etc/nrpe.cfg
cd ..
 
sed -i 's@#server_address=127.0.0.1@server_address=192.168.1.114@g' /usr/local/nagios/etc/nrpe.cfg
sed -i 's@allowed_hosts=.*$@allowed_hosts=127.0.0.1,192.168.1.114,192.168.1.113@g' /usr/local/nagios/etc/nrpe.cfg
sed -i 's@command[check_hda1].*$@@g' /usr/local/nagios/etc/nrpe.cfg
sed -i '205a command[check_df]=/usr/local/nagios/libexec/check_disk -w 20 -c 10' /usr/local/nagios/etc/nrpe.cfg
sed -i '206a command[check_cpu_procs]=/usr/local/nagios/libexec/check_procs -w 50 -c 90 --metric=CPU' /usr/local/nagios/etc/nrpe.cfg
sed -i '207a command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%' /usr/local/nagios/etc/nrpe.cfg
echo '/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d' >> /etc/rc.local
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 5666 -j ACCEPT
#创建web验证用户
/usr/local/apache/bin/htpasswd -nb admin 123456 > /usr/local/nagios/etc/htpasswd;chown nagios.nagios /usr/local/nagios/etc/htpasswd
#修改配置文件归类
sed -i 's@cfg_file=/usr/local/nagios/etc/objects/templates.cfg@#cfg_file=/usr/local/nagios/etc/objects/templates.cfg@g' /usr/local/nagios/etc/nagios.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 '32a cfg_file=/usr/local/nagios/etc/objects/hosts.cfg' /usr/local/nagios/etc/nagios.cfg
sed -i '33a cfg_file=/usr/local/nagios/etc/objects/hostgroups.cfg' /usr/local/nagios/etc/nagios.cfg
sed -i '34a cfg_file=/usr/local/nagios/etc/objects/contactsgroups.cfg' /usr/local/nagios/etc/nagios.cfg
sed -i '35a cfg_file=/usr/local/nagios/etc/objects/services.cfg' /usr/local/nagios/etc/nagios.cfg
#命令检查时间间隔
sed -i 's@^command_check_interval.*$@command_check_interval=10s@g' /usr/local/nagios/etc/nagios.cfg
#指定用户admin可以通过浏览器操纵nagios服务的关闭、重启等各种操作
sed -i 's@authorized_for_system_information=.*$@authorized_for_system_information=admin@g' /usr/local/nagios/etc/cgi.cfg
sed -i 's@authorized_for_configuration_information=.*$@authorized_for_configuration_information=admin@g' /usr/local/nagios/etc/cgi.cfg
sed -i 's@authorized_for_system_commands=.*$@authorized_for_system_commands=admin@g' /usr/local/nagios/etc/cgi.cfg
sed -i 's@authorized_for_all_services=.*$@authorized_for_all_services=admin@g' /usr/local/nagios/etc/cgi.cfg
sed -i 's@authorized_for_all_hosts=.*$@authorized_for_all_hosts=admin@g' /usr/local/nagios/etc/cgi.cfg
sed -i 's@authorized_for_all_service_commands=.*$@authorized_for_all_service_commands=admin@g' /usr/local/nagios/etc/cgi.cfg
sed -i 's@authorized_for_all_host_commands=.*$@authorized_for_all_host_commands=admin@g' /usr/local/nagios/etc/cgi.cfg
 
#hosts.cfg
cat > /usr/local/nagios/etc/objects/hosts.cfg << EOF
define host {
host_name                 linux4
alias                     nagios-server
address                   192.168.1.114
contact_groups            sagroup
check_command             check-host-alive
max_check_attempts          10
notification_interval       5
notification_period         24x7
notification_options        d,u,r
}
define host {
host_name                 linux3
alias                     192.168.1.113
address                   192.168.1.113
contact_groups            sagroup
check_command             check-host-alive
max_check_attempts          10
notification_interval       5
notification_period         24x7
notification_options        d,u,r
}
EOF
chown nagios.nagios /usr/local/nagios/etc/objects/hosts.cfg
chmod 664 /usr/local/nagios/etc/objects/hosts.cfg
#hostgroups.cfg
cat > /usr/local/nagios/etc/objects/hostgroups.cfg << EOF
define hostgroup {
hostgroup_name  sa-servers
alias           sa servers
members         linux4,linux3
}
EOF
chown nagios.nagios /usr/local/nagios/etc/objects/hostgroups.cfg
chmod 664 /usr/local/nagios/etc/objects/hostgroups.cfg
#contacts.cfg
cp /usr/local/nagios/etc/objects/contacts.cfg /usr/local/nagios/etc/objects/contacts.cfg.bk
cat > /usr/local/nagios/etc/objects/contacts.cfg << EOF
define contact {
contact_name    admin
alias           system administrator
service_notification_period    24x7
host_notification_period       24x7
service_notification_options   w,u,c,r
host_notification_options       d,u,r
host_notification_commands     notify-host-by-email
service_notification_commands  notify-service-by-email
email                          go@andyx.net
}
EOF
#services.cfg
cat > /usr/local/nagios/etc/objects/services.cfg << EOF
define service {
host_name             linux4,linux3
service_description   check-host-alive
check_period          24x7
max_check_attempts    4
normal_check_interval 3
retry_check_interval  2
contact_groups        sagroup
notification_interval   10
notification_period     24x7
notification_options    w,u,c,r
check_command           check-host-alive
}
define service{
host_name               linux4,linux3
service_description     check_ssh
check_command           check_ssh
max_check_attempts      4
normal_check_interval   3
retry_check_interval    2
check_period            24x7
notification_interval   10
notification_period     24x7
notification_options    w,u,c,r
contact_groups          sagroup
}
define service{
host_name             linux4,linux3
service_description     check-disk
check_command           check_nrpe!check_df
max_check_attempts      4
normal_check_interval   3
retry_check_interval    2
check_period            24x7
notification_interval   10
notification_period     24x7
notification_options    w,u,c,r
contact_groups          sagroup
}
define service{
host_name             linux4,linux3
service_description     check_cpu_procs
check_command           check_nrpe!check_cpu_procs
max_check_attempts      4
normal_check_interval   3
retry_check_interval    2
check_period            24x7
notification_interval   10
notification_period     24x7
notification_options    w,u,c,r
contact_groups          sagroup
}
define service{
host_name             linux4,linux3
service_description     check_load
check_command           check_nrpe!check_load
max_check_attempts      4
normal_check_interval   3
retry_check_interval    2
check_period            24x7
notification_interval   10
notification_period     24x7
notification_options    w,u,c,r
contact_groups          sagroup
}
define service{
host_name             linux4,linux3
service_description     check_users
check_command           check_nrpe!check_users
max_check_attempts      4
normal_check_interval   3
retry_check_interval    2
check_period            24x7
notification_interval   10
notification_period     24x7
notification_options    w,u,c,r
contact_groups          sagroup
}
define service{
host_name             linux4,linux3
service_description     check_swap
check_command           check_nrpe!check_swap
max_check_attempts      4
normal_check_interval   3
retry_check_interval    2
check_period            24x7
notification_interval   10
notification_period     24x7
notification_options    w,u,c,r
contact_groups          sagroup
}
define service{
host_name             linux4,linux3
service_description     check_zombie_procs
check_command           check_nrpe!check_zombie_procs
max_check_attempts      4
normal_check_interval   3
retry_check_interval    2
check_period            24x7
notification_interval   10
notification_period     24x7
notification_options    w,u,c,r
contact_groups          sagroup
}
EOF
chown nagios.nagios /usr/local/nagios/etc/objects/services.cfg
chmod 664 /usr/local/nagios/etc/objects/services.cfg
#commands.cfg
echo 'define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}' >> /usr/local/nagios/etc/objects/commands.cfg
#contactsgroups.cfg
cat > /usr/local/nagios/etc/objects/contactsgroups.cfg << EOF
define contactgroup {
contactgroup_name    sagroup
alias                system administrator group
members              admin
}
EOF
chown nagios.nagios /usr/local/nagios/etc/objects/contactsgroups.cfg
chmod 664 /usr/local/nagios/etc/objects/contactsgroups.cfg
service nagios start
service httpd start

脚本内容-客户端:

client.sh

#!/bin/bash
#by LinuxEye

if [ -e "/root/nagios" ];then
echo -e "e[0;34m start installe[0m"
cd /root/nagios/
else
echo -e "e[1;31mPlease send nagios directory moved to /roote[0m"
exit 1
fi
useradd -M -s /sbin/nologin nagios
cd nagios
tar xzf nagios-plugins-1.4.15.tar.gz
cd nagios-plugins-1.4.15
./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios && make && make install
cd ..
 
#安装nrpe
tar xzf nrpe-2.13.tar.gz
cd nrpe-2.13
./configure
make && make install
mkdir /usr/local/nagios/etc/
cp sample-config/nrpe.cfg /usr/local/nagios/etc/
chown nagios.nagios /usr/local/nagios/etc/nrpe.cfg
cd ..
 
sed -i 's@#server_address=127.0.0.1@server_address=192.168.1.113@g' /usr/local/nagios/etc/nrpe.cfg
sed -i 's@allowed_hosts=.*$@allowed_hosts=192.168.1.114@g' /usr/local/nagios/etc/nrpe.cfg
sed -i 's@command[check_hda1].*$@@g' /usr/local/nagios/etc/nrpe.cfg
sed -i '205a command[check_df]=/usr/local/nagios/libexec/check_disk -w 20 -c 10' /usr/local/nagios/etc/nrpe.cfg
sed -i '206a command[check_cpu_procs]=/usr/local/nagios/libexec/check_procs -w 50 -c 90 --metric=CPU' /usr/local/nagios/etc/nrpe.cfg
sed -i '207a command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%' /usr/local/nagios/etc/nrpe.cfg
echo '/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d' >> /etc/rc.local
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
iptables -A INPUT -p tcp -s 192.168.1.114 -m state --state NEW -m tcp --dport 5666 -j ACCEPT

更多相关文章请参见:《菜鸟入门:使用Nagios搭建监控服务器详细教程

(END)

本文来源于-Linux运维笔记: nagios安装与配置脚本