设为首页 收藏本站
查看: 1045|回复: 0

一键搭建nagios监控系统之一 脚本篇

[复制链接]

尚未签到

发表于 2019-1-15 12:45:20 | 显示全部楼层 |阅读模式
    最近接了个项目,要求帮客户的6台服务器做一个监控系统,主要就是监控 cpuload;cpu使用率;硬盘分区使用情况;ftp是否健康;http是否健康;内存使用率;ping值;正常连接数以及mysql健康的监控. 而且要做到警告时,检测失败时以及恢复时要发送邮件到客户邮箱进行报警。(本来想是用飞信的,客户是电信189用户,不支持飞信,将邮件发送到189邮箱,然后邮箱邮件会自动发送到手机。) 基于这样的需求,决定来搭建一个nagios监控系统。可是正在准备着手搭建的时候,完了,时间长了忘记了怎么去搭建及配置了。有点杯具了。不过经过半天的摸索,终于上道了,花点时间搭建好了。也交差了。不过后来想想,始终靠记忆去记东西,靠不住啊。 决定把nagios服务器及客户端的配置,写到一个脚本里。只要在服务端和客户端执行 这个脚本,选择相应的操作,就能自动的将nagios监控系统完整的搭建完成。以后只要拿着这个脚本在手,就再不也怕啥"难够死"了。
经过一段时间的编写,这个脚本终于面试了。
1,脚本内容如下:



  • #! /bin/bash  
  • ##################  nagios server configure ########################  
  • installserver() {  
  • 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*  
  •   
  • 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  ..  
  •   
  • 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://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 ..  
  •   
  • wget http://218.6.16.170/somenagios.tar  
  • tar -xvf somenagios.tar -C /usr/local/nagios/libexec/  
  •   
  • echo  "################install nagios server  completed#############"  
  •   
  • 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  
  •   
  • #nagios.cfg configure#  
  • sed -i '35i\cfg_file=/usr/local/nagios/etc/objects/hosts.cfg' /usr/local/nagios/etc/nagios.cfg  
  • sed -i '35i\cfg_file=/usr/local/nagios/etc/objects/services.cfg' /usr/local/nagios/etc/nagios.cfg  
  • sed -i '35i\cfg_file=/usr/local/nagios/etc/objects/hostgroup.cfg' /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 "s#process_performance_data=0#process_performance_data=1#g" /usr/local/nagios/etc/nagios.cfg  
  • sed -i "s/#host_perfdata_command=process-host-perfdata/host_perfdata_command=process-host-perfdata/g" /usr/local/nagios/etc/nagios.cfg  
  • sed -i "s/#service_perfdata_command=process-service-perfdata/service_perfdata_command=process-service-perfdata/g" /usr/local/nagios/etc/nagios.cfg  
  •   
  • #command.cfg configure#  

  • line1=`sed -n '/process-host-perfdata$/=' /usr/local/nagios/etc/objects/commands.cfg`  
  • 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  
  •   
  • #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  
  •   
  • #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/*  
  •   
  • sed -i 's#LOG_LEVEL = 0#LOG_LEVEL = 2#g'  /usr/local/nagios/etc/pnp/process_perfdata.cfg  
  •   
  • #contacts.cfg configure#  
  •   
  • sed -i  "s#nagios@localhost#$email#g" /usr/local/nagios/etc/objects/contacts.cfg   
  • clear  
  •   
  • echo "################### nagios server install and  configure completed,plase add hosts and services  then start it ##################"  
  •   
  • }  

  • restartnagios(){  
  •     echo "stopping ......"  
  •     service nagios stop  
  •     killall -9 nrpe  
  •     service httpd stop  
  •     echo "starting ......"  
  •     service nagios start  
  •     /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d  
  •     service httpd start  
  • }  
  • addservices(){  
  •   
  • read -p "please input the alias name : "  host  
  • read -p "please input the clint 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  
  •   
  •   
  • echo "define hostgroup {  
  •         hostgroup_name myserver  
  •         alias          myserver  
  •         members        $host  
  • }" >>/usr/local/nagios/etc/objects/hostgroup.cfg  

  • 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  
  •        }  
  • 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  
  •        }  
  • 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  
  •        }  
  • 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%  
  •        }  
  • 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  
  •        }  
  •   
  • " >>/usr/local/nagios/etc/objects/services.cfg  
  • echo "###############  Add services completed ########"  
  • }  
  • ######################### nagios  configure  completed ########################  
  •   
  • ####################### client  server configure ########################  
  •   
  • clientnagios() {  
  •   
  • ### install soft###  
  • read -p "Please input the nagios server ip : " nagiosip  
  •   
  • read -p "Please input the mysql password : " mysqlpass  
  •   
  • useradd nagios  
  • yum -y 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  
  •   
  • cat >>/usr/local/nagios/etc/nrpe.cfg

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-663602-1-1.html 上篇帖子: 运维监控利器Nagios-Linux 下篇帖子: 用escalations.cfg来限制 nagios 邮件 短信 报警发送次数
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表