#!/bin/bash # filename: nagios-install.sh ###################脚本运行的环境############################### ########1、服务端和客户端能正常通信; ########2、所需软件包:(nagios-3.2.0.tar.gz nagios-plugins-1.4.14.tar.gz pnp-0.4.13.tar.gz ######## nagios-cn-3.2.0.tar.bz2 nrpe-2.12.tar.gz); ########3、这些软件包需放在服务端的root用户的家目录下,root的密码是'666666' ###################(前期准备工作)############################### ## (3、定义服务端和客户端的IP变量)############################ read -p "请输入服务端的IP地址:" SIP read -p "请输入客户端的IP地址:" CIP ##(2、定义非交互式登录客户机的函数)######################### auto_login_ssh () { expect -c "set timeout -1; spawn -noecho ssh -o StrictHostKeyChecking=no $2 ${@:3}; expect *assword:*; send -- $1\r; interact;"; } ##(3、定义非交互式scp的函数)################################ auto_scp () { expect -c "set timeout -1; spawn scp -o StrictHostKeyChecking=no ${@:2}; expect { *assword:* {send -- $1\r; expect { *denied* {exit 1;} eof } } eof {exit 1;} } " return $? } ##(4、复制tar包到客户端)############################################## auto_scp 666666 ~/nagios-plugins-1.4.14.tar.gz root@$CIP:~/ &>/dev/null && echo "非交互式完成nagios-plugins-1.4.14.tar.gz的scp复制。" auto_scp 666666 ~/nrpe-2.12.tar.gz root@$CIP:~/ &>/dev/null && echo "非交互式完成nrpe-2.12.tar.gz的scp复制。" ##(4、让此脚本中需要在客户端执行的行,保存到新的脚本client.sh.并把client.sh复制到客户端并执行。) sed '/^#/! s/^/#/; 47,80 s/#//' ~/nagios-install.sh >~/client.sh sed -i "/^#/! s/\$SIP/$SIP/" ~/client.sh auto_scp 666666 ~/client.sh root@$CIP:~/ &>/dev/null && echo "非交互式完成client.sh的scp复制。" echo "远程到客户端,正在客户端执行脚本client.sh,无需任何操作。请稍作等待" auto_login_ssh 666666 root@$CIP /bin/bash client.sh echo "客户端上执行脚本client.sh完成。" ##################(需在客户端执行的47-80行脚本)###################### ##(1、用yum安装mysql,vsftp) #[ -e /mnt/Server ] || mount /dev/cdrom /mnt #cat > /etc/yum.repos.d/aa.repo <<EOF #[base] #name=dvd server #baseurl=file:///mnt/Server #gpgcheck=0 #EOF #yum -y install mysql* vsftpd* &>/dev/null && echo "install mysql* vsftpd* install is ok" #service mysqld start &>/dev/null && echo "mysqld start is ok." #service vsftpd start &>/dev/null && echo "vsftpd start is ok." ##(2、安装nagios-plugins、nrpe) #cd ~ #useradd -s /sbin/nologin nagios #tar zxf nagios-plugins-1.4.14.tar.gz &>/dev/null #cd nagios-plugins-1.4.14 #./configure &>/dev/null && echo "nagios-plug_cfg is ok." #make &>/dev/null && echo "nagios-plug make is ok." #make install &>/dev/null && echo "nagios-plug make install is ok." #chown nagios:nagios /usr/local/nagios/ #chown -R nagios.nagios /usr/local/nagios/libexec/ #cd ~ #tar xf nrpe-2.12.tar.gz #cd nrpe-2.12 #./configure &>/dev/null; #make all &>/dev/null; #make install-plugin &>/dev/null; #make install-daemon &>/dev/null; #make install-daemon-config &>/dev/null && echo "nrpe install is ok" #sed -i "79 s/$/,$SIP/" /usr/local/nagios/etc/nrpe.cfg #sed -i '199,203 s/\]/_1\]/' /usr/local/nagios/etc/nrpe.cfg #/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d #/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 &>/dev/null #[ $? -eq 0 ] && echo "nrpe 已经正常工作。" ###########(一、Nagios服务器安装过程)####################### useradd -s /sbin/nologin nagios mkdir /usr/local/nagios chown -R nagios.nagios /usr/local/nagios/ ##(1、安装Nagio)########################################## cd ~ tar zxf nagios-3.2.0.tar.gz &>/dev/null cd nagios-3.2.0 ./configure --prefix=/usr/local/nagios/ &>/dev/null && echo "configure nagios ok." make all &>/dev/null && echo "nagios make all ok." make install &>/dev/null && echo "nagios make install ok." make install-init &>/dev/null && echo "nagios make ins-init ok." make install-commandmode &>/dev/null && echo "nagios commandmode ok." make install-config &>/dev/null && echo "nagios make all ok." chkconfig --add nagios chkconfig nagios on ##(2、安装Nagios-plugins)################################################# cd ~ tar zxf nagios-plugins-1.4.14.tar.gz &>/dev/null cd nagios-plugins-1.4.14 ./configure --prefix=/usr/local/nagios/ &>/dev/null && echo "nagios-plug make all ok." make &>/dev/null && echo "nagios-plug make ok." make install &>/dev/null && echo "nagios-plug make install ok." ##(3、安装Nagios的汉化包)############################################################## cd ~ tar jxf nagios-cn-3.2.0.tar.bz2 &>/dev/null cd nagios-cn-3.2.0 ./configure &>/dev/null && echo "nagios-cn config ok." make all &>/dev/null && echo "nagios-cn make all ok." make install &>/dev/null && echo "nagios-cn make install ok." ##(4、安装LAMP架构)############################################### [ -e /mnt/Server ] || mount /dev/cdrom /mnt cat > /etc/yum.repos.d/aa.repo <<EOF [base] name=dvd server baseurl=file:///mnt/Server gpgcheck=0 EOF yum -y install php* mysql* http* &>/dev/null && echo "install php* mysql* http* is ok" sed -i '/^DirectoryIndex/ s/DirectoryIndex/DirectoryIndex index.php/' /etc/httpd/conf/httpd.conf sed -i '$a\ #setting for nagios\ ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"\ <Directory "/usr/local/nagios/sbin">\ AuthType Basic\ Options ExecCGI\ 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 ExecCGI\ AllowOverride None\ Order allow,deny\ Allow from all\ AuthName "nagios access"\ AuthUserFile /usr/local/nagios/etc/htpasswd\ Require valid-user\ </Directory>' /etc/httpd/conf/httpd.conf htpasswd -bc /usr/local/nagios/etc/htpasswd benet benet service httpd start &>/dev/null if [ $? -eq 0 ]; then echo "httpd start OK." else echo "httpd start failed." exit 3 fi chkconfig httpd on #######################(二、Nagios服务器配置过程)####################### cd /usr/local/nagios/etc/ touch hosts.cfg services.cfg cat >hosts.cfg <<EOF define host{ use linux-server host_name web alias benet-web address $SIP } define host{ use linux-server host_name mysql alias benet-mysql address $CIP } define hostgroup{ hostgroup_name sa-servers alias sa servers members web,mysql } EOF cat >services.cfg <<EOF ################- benet web -########################## define service{ use local-service host_name web service_description PING check_command check_ping!100.0,20%!500.0,60% } define service{ use local-service host_name web service_description SSH check_command check_ssh } define service{ use local-service host_name web service_description SSHD check_command check_tcp!22 } define service{ use local-service host_name web service_description http check_command check_http } #####################- MYSQL -########################## define service{ use local-service host_name mysql service_description PING check_command check_ping!100.0,20%!500.0,60% } define service{ use local-service host_name mysql service_description SSH check_command check_ssh } define service{ use local-service host_name mysql service_description ftp check_command check_ftp } define service{ use local-service host_name mysql service_description mysqlport check_command check_tcp!3306 } EOF sed -i '107 s/#//; 107 s/guest/benet/; 144 s/nagiosadmin/benet/; s/nagiosadmin/nagiosadmin,benet/' cgi.cfg sed -i '29 a\cfg_file=/usr/local/nagios//etc/hosts.cfg\ cfg_file=/usr/local/nagios//etc/services.cfg' nagios.cfg service nagios start &>/dev/null if [ $? -eq 0 ]; then echo "nagios start OK." else echo "nagios start failed." exit 4 fi #######################(三、Nagios性能分析图表的实现)###################### ##(1、安装rrdtool)################ cd ~ tar zxf rrdtool-1.4.5.tar.gz &>/dev/null cd rrdtool-1.4.5 ./configure --prefix=/usr/local/rrdtool &>/dev/null && echo "rrdtool config ok" make &>/dev/null && echo "rrdtool make ok" make install &>/dev/null && echo "rrdtool make install ok" ##(2、安装pnp)##################################################### cd ~ tar zxf pnp-0.4.13.tar.gz &>/dev/null cd pnp-0.4.13 ./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-rrdtool=/usr/local/rrdtool/bin/rrdtool --with-perfdata-dir=/usr/local/nagios/share/perfdata &>/dev/null && echo "pnp config ok" make all &>/dev/null && echo "pnp make all ok" make install &>/dev/null && echo "pnp make install ok" make install-config &>/dev/null && echo "pnp make install-cfg ok" make install-init &>/dev/null && echo "pnp install ok" cd /usr/local/nagios/etc/pnp/ cp process_perfdata.cfg-sample process_perfdata.cfg cp npcd.cfg-sample npcd.cfg cp rra.cfg-sample rra.cfg chown -R nagios:nagios /usr/local/nagios/etc/pnp/ sed -i '44 s/0/2/' process_perfdata.cfg cd /usr/local/nagios/etc/objects/ sed -i '$a\ 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\ }' templates.cfg cd /usr/local/nagios/etc sed -i '833 s/#//; 833 s/0/1/; 845 s/#//; 846 s/#//' nagios.cfg cd objects sed -i '230 c\\tcommand_line /usr/local/nagios/libexec/process_perfdata.pl' commands.cfg sed -i '237 c\\tcommand_line /usr/local/nagios/libexec/process_perfdata.pl' commands.cfg cd ../ sed -i '/^use/ s/$/,hosts-pnp/' hosts.cfg sed -i '/^use/ s/$/,services-pnp/' services.cfg ##(3、测试nagios能否重启成功)########################## service nagios restart &>/dev/null if [ $? -eq 0 ]; then echo "nagios restart OK." else echo "nagios restart failed." exit 5 fi #######################(服务端安装nrpe)############################ cd ~ tar zxf nrpe-2.12.tar.gz &>/dev/null cd nrpe-2.12 ./configure &>/dev/null && echo "nrpe configure is ok." make &>/dev/null && echo "nrpe make is ok." make install-plugin &>/dev/null && echo "nrpe make-install is ok." /usr/local/nagios/libexec/check_nrpe -H $CIP &>/dev/null if [ $? -eq 0 ]; then echo "成功联系上客端." else echo "无法联系上客端,请检查错误。" exit 5 fi sed -i '$a\ define command{\ command_name check_nrpe\ command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$\ }' /usr/local/nagios/etc/objects/commands.cfg sed -i '$a\ define service{\ use local-service\ host_name mysql\ service_description users\ check_command check_nrpe!check_users_1\ }\ define service{\ use local-service\ host_name mysql\ service_description load\ check_command check_nrpe!check_load_1\ }\ define service{\ use local-service\ host_name mysql\ service_description disk\ check_command check_nrpe!check_sda1_1\ }\ define servicegroup{\ servicegroup_name server-group\ alias servergroup\ members web,PING,web,SSH,web,SSHD,web,http,mysql,users,mysql,load,mysql,disk\ }' /usr/local/nagios/etc/services.cfg service nagios restart &>/dev/null && echo "脚本全部执行完成。" || echo "nagios重启失败。"
nagios自动安装脚本(一个脚本实现服务端+客户端自动安装)
原创wdjwzy123 ©著作权
文章标签 nagios安装脚本 文章分类 iOS 移动开发
下一篇:SHELL中$@和$*的比较
-
服务端测试之PostMan自动生成测试脚本
PostMan智能化测试实战
接口测试 自动化测试 服务端测试 PostMan -
zabbix服务端,客户端一键安装脚本
zabbix服务端,客户端一键安装脚本
MySQL zabbix