用SHELL脚本自动化安装Nagios服务器端和客户端的
公司的一个项目网站,上线几天就因为磁盘暴满而出了问题,由于其服务器都集中在某一IDC机房内,所以考虑部署套Nagios监控报警系统,考虑到其它项目网站可能有类似需求,所以将其安装过程整理成脚本,方便以后的工作。
Nagios服务器自动安装脚本,如下所示(此脚本在CentOS5.5|5.6|5.8 x86_64下通过):
yum -y install httpd gcc gcc-c++ glibc glibc-common gd gd-devel
useradd nagios
groupadd nagcmd
usermod -G nagcmd nagios
usermod -G nagcmd apache
cd /usr/local/src
wget http://nchc.dl.sourceforge.net/sourceforge/nagios/nagios-3.0.6.tar.gz
wget http://nchc.dl.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.13.tar.gz
wget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.13.tar.gz
tar zxvf nagios-3.0.6.tar.gz
cd nagios-3.0.6
./configure --with-command-group=nagcmd --prefix=/usr/local/nagios
make all
make install
make install-init
make install-config
make install-commandmode
make install-webconf
cd ../
tar zxvf nagios-plugins-1.4.13.tar.gz
cd nagios-plugins-1.4.13
./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios
make && make install
cd ../
tar zxvf nrpe-2.13.tar.gz
cd nrpe-2.13
./configure
make all
make install-plugin
make install-daemon
make install-daemon-config
htpasswd -bc /usr/local/nagios/etc/htpasswd.users nagiosadmin nagiosadmin101
echo "alias nagioscheck='/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg' " >> /root/.bashrc
source /root/.bashrc
chkconfig nagios on
chkconfig httpd on
service httpd start
service nagios start
#基本安装过程这里就不重复了,htpasswd -bc这行代码的作用是不需要人为干预的生成密码名为nagiosadmin101的angiosadmin用户,而后期由于经常要修改/usr/local/nagios/etc/nagios.cfg配置文件,所以需要检测其语法,所以将其长命令生成名为nagioscheck的别名命令。
Nagios客户端自动安装脚本,如下所示:
useradd nagios
cd /usr/local/src
wget http://nchc.dl.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.13.tar.gz
wget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.13.tar.gz
tar zxvf nagios-plugins-1.4.13.tar.gz
cd nagios-plugins-1.4.13
./configure
make
make install
chown nagios:nagios /usr/local/nagios
chown -R nagios:nagios /usr/local/nagios/libexec
cd ../
tar zxvf nrpe-2.13.tar.gz
cd nrpe-2.13
./configure
make all
make install-plugin
make install-daemon
make install-daemon-config
sed -i 's@allowed_hosts=127.0.0.1@allowed_hosts=114.112.11.11@' /usr/local/nagios/etc/nrpe.cfg
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
echo "/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d" >> /etc/rc.local
#allowed_hosts这行是将原先的127.0.0.1由现在的服务器地址114.112.11.11地址代替(已作无害处理),这行代码大家可以根据实际需要考虑是否增加,接下来的工作就是调试Nagios服务器端了,具体过程这里略过。
事实上,在机器进IDC机房之前,像nagios和puppet客户端软件,应该在每台上线机器上部署安装好,机器上线前肯定首先是集中在自己的局域网内Kickstart后然后初始化,这时网络稳定、调试也方便,所以用puppet自动化维化既方便又省力,这样免得机器上线以后又远程维护安装,即耗了人力成本,而且也增加了机器风险。
另注:我看许多朋友都是自行编写SHELL脚本作为Nagios插件来作为对MySQL主从同步的补充功能,其实Nagios中有自带的check_mysql及check_mysql_health插件来实现,由于check_mysql在部署过程中老是频繁报“段错误”,所以目前主要还是用check_mysql_health,因为我们不仅仅是监控从机上面的SQL和IO进程,更重要的是,我们要监控从机的延迟时间。