nagios安装与配置脚本 | Linux运维笔记

网友投稿 1121 2022-09-30

本站部分文章、图片属于网络上可搜索到的公开信息,均用于学习和交流用途,不能代表睿象云的观点、立场或意见。我们接受网民的监督,如发现任何违法内容或侵犯了您的权益,请第一时间联系小编邮箱jiasou666@gmail.com 处理。

nagios安装与配置脚本 | Linux运维笔记

以下为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.gzlibiconv-1.13.1.tar.gzphp-5.3.10.tar.gznagios-3.3.1.tar.gznagios-plugins-1.4.15.tar.gznrpe-2.13.tar.gz

使用方法:

cd rootwget /blog.linuxeye.com/wp-content/uploads/nagios.zipunzip nagios.zipcd nagiossh nagios_install.sh

脚本内容:server

#!/bin/bash#by LinuxEye#BLOG: //blog.linuxeye.com#安装依赖包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-develuseradd -M -s /sbin/nologin wwwif [ -e "/root/nagios" ];thenecho -e "e[0;34m start installe[0m"cd /root/nagios/elseecho -e "e[1;31mPlease send nagios directory moved to /roote[0m"exit 1fi#安装apachetar xzf httpd-2.2.22.tar.gzcd httpd-2.2.22./configure --prefix=/usr/local/apache --enable-so --enable-rewrite --enable-headers --enable-deflate --with-mpm=workermake && make installcp /usr/local/apache/bin/apachectl /etc/init.d/httpdsed -i '2a # chkconfig: - 85 15' /etc/init.d/httpdsed -i '3a # description: Apache is a World Wide Web server. It is used to serve' /etc/init.d/httpdchmod 755 /etc/init.d/httpdchkconfig --add httpdchkconfig httpd oncd ..#安装phptar xzf libiconv-1.13.1.tar.gzcd libiconv-1.13.1./configure --prefix=/usr/local/libiconvmake && make installcd ..tar xzf php-5.3.10.tar.gzcd 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/libiconvmake && make installcp php.ini-production /usr/local/php/lib/php.inicd ..#修改配置文件sed -i 's@;date.timezone =@date.timezone = Asia/Shanghai@g' /usr/local/php/lib/php.iniif [ "$(hostname -i)" == "127.0.0.1" ];thenecho "OK"elsesed -i "s/127.0.0.1.*$/127.0.0.1 `hostname` localhost.localdomain localhost/g" /etc/hostsfised -i 's/User daemon/User www/g' /usr/local/apache/conf/httpd.confsed -i 's/Group daemon/User www/g' /usr/local/apache/conf/httpd.confsed -i 's/#ServerName example.com:80/ServerName 127.0.0.1:80/g' /usr/local/apache/conf/httpd.confsed -i '288a AddType application/x-httpd-php .php .phtml' /usr/local/apache/conf/httpd.confsed -i '289a AddType application/x-httpd-php-source .phps' /usr/local/apache/conf/httpd.confsed -i 's@DirectoryIndex index.html@DirectoryIndex index.html index.php@g' /usr/local/apache/conf/httpd.confcat >> /usr/local/apache/conf/httpd.conf << EOFAlias /nagios/cgi-bin /usr/local/nagios/sbinAuthType BasicOptions ExecCGIAddHandler cgi-script .cgiAllowOverride NoneOrder allow,denyAllow from allAuthName "Nagios Access"AuthUserFile /usr/local/nagios/etc/htpasswdRequire valid-userAlias /nagios /usr/local/nagios/shareAuthType BasicOptions NoneAllowOverride NoneOrder allow,denyAllow from allAuthName "nagios Access"AuthUserFile /usr/local/nagios/etc/htpasswdRequire valid-userEOFuseradd nagios;usermod -G nagios wwwtar xzf nagios-3.3.1.tar.gzcd nagios./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios --with-command-user=nagiosmake all && make install && make install-init && make install-commandmode && make install-configcd ../tar xzf nagios-plugins-1.4.15.tar.gzcd nagios-plugins-1.4.15./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios && make && make installchkconfig --add nagiossed -i '138a chmod 777 $NagiosVarDir/rw/nagios.cmd' /etc/init.d/nagioscd ..#安装nrpetar xzf nrpe-2.13.tar.gzcd nrpe-2.13./configuremake && make installcp sample-config/nrpe.cfg /usr/local/nagios/etc/chown nagios.nagios /usr/local/nagios/etc/nrpe.cfgcd ..sed -i 's@#server_address=127.0.0.1@server_address=192.168.1.114@g' /usr/local/nagios/etc/nrpe.cfgsed -i 's@allowed_hosts=.*$@allowed_hosts=127.0.0.1,192.168.1.114,192.168.1.113@g' /usr/local/nagios/etc/nrpe.cfgsed -i 's@command[check_hda1].*$@@g' /usr/local/nagios/etc/nrpe.cfgsed -i '205a command[check_df]=/usr/local/nagios/libexec/check_disk -w 20 -c 10' /usr/local/nagios/etc/nrpe.cfgsed -i '206a command[check_cpu_procs]=/usr/local/nagios/libexec/check_procs -w 50 -c 90 --metric=CPU' /usr/local/nagios/etc/nrpe.cfgsed -i '207a command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%' /usr/local/nagios/etc/nrpe.cfgecho '/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 -diptables -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.cfgsed -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.cfgsed -i '32a cfg_file=/usr/local/nagios/etc/objects/hosts.cfg' /usr/local/nagios/etc/nagios.cfgsed -i '33a cfg_file=/usr/local/nagios/etc/objects/hostgroups.cfg' /usr/local/nagios/etc/nagios.cfgsed -i '34a cfg_file=/usr/local/nagios/etc/objects/contactsgroups.cfg' /usr/local/nagios/etc/nagios.cfgsed -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.cfgsed -i 's@authorized_for_configuration_information=.*$@authorized_for_configuration_information=admin@g' /usr/local/nagios/etc/cgi.cfgsed -i 's@authorized_for_system_commands=.*$@authorized_for_system_commands=admin@g' /usr/local/nagios/etc/cgi.cfgsed -i 's@authorized_for_all_services=.*$@authorized_for_all_services=admin@g' /usr/local/nagios/etc/cgi.cfgsed -i 's@authorized_for_all_hosts=.*$@authorized_for_all_hosts=admin@g' /usr/local/nagios/etc/cgi.cfgsed -i 's@authorized_for_all_service_commands=.*$@authorized_for_all_service_commands=admin@g' /usr/local/nagios/etc/cgi.cfgsed -i 's@authorized_for_all_host_commands=.*$@authorized_for_all_host_commands=admin@g' /usr/local/nagios/etc/cgi.cfg#hosts.cfgcat > /usr/local/nagios/etc/objects/hosts.cfg << EOFdefine host {host_name linux4alias nagios-serveraddress 192.168.1.114contact_groups sagroupcheck_command check-host-alivemax_check_attempts 10notification_interval 5notification_period 24x7notification_options d,u,r}define host {host_name linux3alias 192.168.1.113address 192.168.1.113contact_groups sagroupcheck_command check-host-alivemax_check_attempts 10notification_interval 5notification_period 24x7notification_options d,u,r}EOFchown nagios.nagios /usr/local/nagios/etc/objects/hosts.cfgchmod 664 /usr/local/nagios/etc/objects/hosts.cfg#hostgroups.cfgcat > /usr/local/nagios/etc/objects/hostgroups.cfg << EOFdefine hostgroup {hostgroup_name sa-serversalias sa serversmembers linux4,linux3}EOFchown nagios.nagios /usr/local/nagios/etc/objects/hostgroups.cfgchmod 664 /usr/local/nagios/etc/objects/hostgroups.cfg#contacts.cfgcp /usr/local/nagios/etc/objects/contacts.cfg /usr/local/nagios/etc/objects/contacts.cfg.bkcat > /usr/local/nagios/etc/objects/contacts.cfg << EOFdefine contact {contact_name adminalias system administratorservice_notification_period 24x7host_notification_period 24x7service_notification_options w,u,c,rhost_notification_options d,u,rhost_notification_commands notify-host-by-emailservice_notification_commands notify-service-by-emailemail go@linuxeye.com}EOF#services.cfgcat > /usr/local/nagios/etc/objects/services.cfg << EOFdefine service {host_name linux4,linux3service_description check-host-alivecheck_period 24x7max_check_attempts 4normal_check_interval 3retry_check_interval 2contact_groups sagroupnotification_interval 10notification_period 24x7notification_options w,u,c,rcheck_command check-host-alive}define service{host_name linux4,linux3service_description check_sshcheck_command check_sshmax_check_attempts 4normal_check_interval 3retry_check_interval 2check_period 24x7notification_interval 10notification_period 24x7notification_options w,u,c,rcontact_groups sagroup}define service{host_name linux4,linux3service_description check-diskcheck_command check_nrpe!check_dfmax_check_attempts 4normal_check_interval 3retry_check_interval 2check_period 24x7notification_interval 10notification_period 24x7notification_options w,u,c,rcontact_groups sagroup}define service{host_name linux4,linux3service_description check_cpu_procscheck_command check_nrpe!check_cpu_procsmax_check_attempts 4normal_check_interval 3retry_check_interval 2check_period 24x7notification_interval 10notification_period 24x7notification_options w,u,c,rcontact_groups sagroup}define service{host_name linux4,linux3service_description check_loadcheck_command check_nrpe!check_loadmax_check_attempts 4normal_check_interval 3retry_check_interval 2check_period 24x7notification_interval 10notification_period 24x7notification_options w,u,c,rcontact_groups sagroup}define service{host_name linux4,linux3service_description check_userscheck_command check_nrpe!check_usersmax_check_attempts 4normal_check_interval 3retry_check_interval 2check_period 24x7notification_interval 10notification_period 24x7notification_options w,u,c,rcontact_groups sagroup}define service{host_name linux4,linux3service_description check_swapcheck_command check_nrpe!check_swapmax_check_attempts 4normal_check_interval 3retry_check_interval 2check_period 24x7notification_interval 10notification_period 24x7notification_options w,u,c,rcontact_groups sagroup}define service{host_name linux4,linux3service_description check_zombie_procscheck_command check_nrpe!check_zombie_procsmax_check_attempts 4normal_check_interval 3retry_check_interval 2check_period 24x7notification_interval 10notification_period 24x7notification_options w,u,c,rcontact_groups sagroup}EOFchown nagios.nagios /usr/local/nagios/etc/objects/services.cfgchmod 664 /usr/local/nagios/etc/objects/services.cfg#commands.cfgecho 'define command{command_name check_nrpecommand_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$}' >> /usr/local/nagios/etc/objects/commands.cfg#contactsgroups.cfgcat > /usr/local/nagios/etc/objects/contactsgroups.cfg << EOFdefine contactgroup {contactgroup_name sagroupalias system administrator groupmembers admin}EOFchown nagios.nagios /usr/local/nagios/etc/objects/contactsgroups.cfgchmod 664 /usr/local/nagios/etc/objects/contactsgroups.cfgservice nagios startservice httpd start

client

#!/bin/bash#by LinuxEye#BLOG: //blog.linuxeye.comif [ -e "/root/nagios" ];thenecho -e "e[0;34m start installe[0m"cd /root/nagios/elseecho -e "e[1;31mPlease send nagios directory moved to /roote[0m"exit 1fiuseradd -M -s /sbin/nologin nagioscd nagiostar xzf nagios-plugins-1.4.15.tar.gzcd nagios-plugins-1.4.15./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios && make && make installcd ..#安装nrpetar xzf nrpe-2.13.tar.gzcd nrpe-2.13./configuremake && make installmkdir /usr/local/nagios/etc/cp sample-config/nrpe.cfg /usr/local/nagios/etc/chown nagios.nagios /usr/local/nagios/etc/nrpe.cfgcd ..sed -i 's@#server_address=127.0.0.1@server_address=192.168.1.113@g' /usr/local/nagios/etc/nrpe.cfgsed -i 's@allowed_hosts=.*$@allowed_hosts=192.168.1.114@g' /usr/local/nagios/etc/nrpe.cfgsed -i 's@command[check_hda1].*$@@g' /usr/local/nagios/etc/nrpe.cfgsed -i '205a command[check_df]=/usr/local/nagios/libexec/check_disk -w 20 -c 10' /usr/local/nagios/etc/nrpe.cfgsed -i '206a command[check_cpu_procs]=/usr/local/nagios/libexec/check_procs -w 50 -c 90 --metric=CPU' /usr/local/nagios/etc/nrpe.cfgsed -i '207a command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%' /usr/local/nagios/etc/nrpe.cfgecho '/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 -diptables -A INPUT -p tcp -s 192.168.1.114 -m state --state NEW -m tcp --dport 5666 -j ACCEPT

上一篇:PowerShell:自动化运维工具
下一篇:运维必备-监控工具Zabbix简介
相关文章

 发表评论

暂时没有评论,来抢沙发吧~