您好,登錄后才能下訂單哦!
由于發(fā)布文章字?jǐn)?shù)有限制。腳本解釋將在這一篇文章里講解。如下:
- #! /bin/bash
- ## QQ:410018348
- ################## nagios server configure ########################
- installserver() { #定義安裝nagios函數(shù)
- read -p " Please input the alarm email address : " email #報警郵箱
- read -p " Please input the login nagios user: " loginuser #網(wǎng)頁登錄時帳戶
- read -p " Please input the login nagios pass: " loginpass #網(wǎng)頁登錄時的密碼
- 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*
- #安裝 apache php 及其他
- 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 .. #安裝nagios軟件
- 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 .. #安裝nagios監(jiān)控插件
- 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 .. #安裝nrpe插件
- 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 .. #安裝繪圖工具
- ## 以上為安裝nagios服務(wù)的一些軟件。
- wget http://218.6.16.170/somenagios.tar #這里的我個人找的一些 cpu ,內(nèi)存,及mysql的插件。 打包在這。
- tar -xvf somenagios.tar -C /usr/local/nagios/libexec/
- echo "################install nagios server completed#############" #nagios安裝完成。
- echo "################# configure nagios server start###############" #下面是常規(guī)的一些配置。
- htpasswd -bc /usr/local/nagios/etc/htpasswd.users $loginuser $loginpass #創(chuàng)建用戶密碼,就是上面要求輸入的內(nèi)容。
- # cgi.cfg configure#
- sed -i "s#use_authentication=1#use_authentication=0#g" /usr/local/nagios/etc/cgi.cfg #更改use_authentication參數(shù)
- #nagios.cfg configure#
- sed -i '35i\cfg_file=/usr/local/nagios/etc/objects/hosts.cfg' /usr/local/nagios/etc/nagios.cfg #在nagios.cfg里添加 host.cfg
- sed -i '35i\cfg_file=/usr/local/nagios/etc/objects/services.cfg' /usr/local/nagios/etc/nagios.cfg #在nagios.cfg里添加 services.cfg
- sed -i '35i\cfg_file=/usr/local/nagios/etc/objects/hostgroup.cfg' /usr/local/nagios/etc/nagios.cfg #在nagios.cfg里添加 hostgroup.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 #禁用監(jiān)控本地的配置文件
- sed -i "s#process_performance_data=0#process_performance_data=1#g" /usr/local/nagios/etc/nagios.cfg #更改 process_performance_data 為1
- sed -i "s/#host_perfdata_command=process-host-perfdata/host_perfdata_command=process-host-perfdata/g" /usr/local/nagios/etc/nagios.cfg #開啟 host_perfdata_commandsed -i "s/#service_perfdata_command=process-service-perfdata/service_perfdata_command=process-service-perfdata/g" /usr/local/nagios/etc/nagios.cfg #開啟service_perfdata_command
- #command.cfg configure#
- line1=`sed -n '/process-host-perfdata$/=' /usr/local/nagios/etc/objects/commands.cfg` #查找commands.cfg里 process-host-perfdata的行
- 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 # 在commands.cfg里添加兩條命令。 nrpe及監(jiān)控mysql
- #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
- # 上面是在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/*
- #上面把pnp的模板文件改成真的配置文件 并授權(quán)
- sed -i 's#LOG_LEVEL = 0#LOG_LEVEL = 2#g' /usr/local/nagios/etc/pnp/process_perfdata.cfg
- #更改 LOG_LEVEL 為2
- #contacts.cfg configure#
- sed -i "s#nagios@localhost#$email#g" /usr/local/nagios/etc/objects/contacts.cfg
- clear
- #更改contacts.cfg里面的聯(lián)系人為所輸入的聯(lián)系人
- echo "################### nagios server install and configure completed,plase add hosts and services then start it ##################"
- } #以上nagios安裝及配置完成。
- restartnagios(){ # 這里是定義nagios,nrpe,及apache重啟的函數(shù)
- echo "stopping ......"
- service nagios stop #nagios stop
- killall -9 nrpe #nrpe stop
- service httpd stop #apache stop
- echo "starting ......"
- service nagios start #nagios start
- /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d # nrpe start
- service httpd start #apache start
- }
- addservices(){ #這里定義了在nagios端添加客戶機(jī)的函數(shù)
- read -p "please input the alias name : " host #輸入添加機(jī)器別名
- read -p "please input the clint ip : " ip #輸入添加機(jī)器的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 #在hosts.cfg里添加一個主機(jī) (參數(shù)的意思可以在網(wǎng)上看下)
- echo "define hostgroup {
- hostgroup_name myserver
- alias myserver
- members $host
- }" >>/usr/local/nagios/etc/objects/hostgroup.cfg #在hostgroup.cfg里添中一個主機(jī)組,$host就是添加的別名
- 是添加監(jiān)控的具體服務(wù)。
- 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
- } #監(jiān)控客戶機(jī)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
- } #監(jiān)控客戶機(jī)負(fù)載
- 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
- } #監(jiān)控客戶機(jī)http服務(wù)
- 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
- } #監(jiān)控客戶機(jī)的內(nèi)存使用情況
- 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
- } #監(jiān)控客戶機(jī)的連接情況
- 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
- } #監(jiān)控客戶機(jī)的硬盤分區(qū)使用情況
- 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
- } #監(jiān)控客戶機(jī)的mysql監(jiān)控狀態(tài)
- 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%
- } #監(jiān)控客戶機(jī)的ping是否正常
- 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
- } #監(jiān)控客戶機(jī)的ftp服務(wù)
- " >>/usr/local/nagios/etc/objects/services.cfg #把上面內(nèi)容添加到services.cfg里面 (參數(shù)的意思可以在網(wǎng)上看下)
- echo "############### Add services completed ########"
- }
- ######################### nagios configure completed ########################
- #以上為nagios服務(wù)端配置。
- ####################### client server configure ########################
- #以下為被監(jiān)控端配置
- clientnagios() { #定義客戶機(jī)的安裝配置
- ### install soft###
- read -p "Please input the nagios server ip : " nagiosip #輸入naiogs服務(wù)端ip
- read -p "Please input the mysql password : " mysqlpass #輸入本機(jī)的mysql密碼。
- useradd nagios
- yum 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 ..
- #以上為安裝被監(jiān)控機(jī)所需要的軟件
- wget http://218.6.16.170/somenagios.tar
- tar -xvf somenagios.tar -C /usr/local/nagios/libexec/ #這里是個人找的幾個監(jiān)控插件
- ### install completed ###
- ### configure ####
- rm /usr/local/nagios/etc/nrpe.cfg #刪除安裝的nrpe.cfg配置文件
- cat >>/usr/local/nagios/etc/nrpe.cfg<<EOF #添加以下內(nèi)容到nrpe.cfg配置文件里。主要就是一些監(jiān)控本機(jī)的命令,供nagios服務(wù)端調(diào)用
- log_facility=daemon
- pid_file=/var/run/nrpe.pid
- server_port=5666
- nrpe_user=nagios
- nrpe_group=nagios
- allowed_hosts=$nagiosip
- dont_blame_nrpe=1
- debug=1
- command_timeout=30
- connection_timeout=300
- command[check_http]=/usr/local/nagios/libexec/check_http -H -u 127.0.0.1 /index.php -t 60
- command[check_disk]=/usr/local/nagios/libexec/check_disk -w 15% -c 10% -A -i '/dev/shm'
- command[check_cpu]=/usr/local/nagios/libexec/check_cpu.sh
- command[check_mem]=/usr/local/nagios/libexec/check_mem.sh
- command[check_ips]=/usr/local/nagios/libexec/ip_conn.sh 400 600
- command[check_load]=/usr/local/nagios/libexec/check_load -w 14,12,10 -c 18,15,12
- command[check_ftp]=/usr/local/nagios/libexec/check_ftp 127.0.0.1 -w 10 -c 60
- EOF
- mysql -uroot -p"$mysqlpass" <<EOF
- create database nagdb;
- grant all on nagdb.* to nagdb@"$nagiosip" identified by 'nagdb';
- flush privileges;
- \q
- EOF #這里是創(chuàng)建一個只能是nagios服務(wù)端訪問的庫。監(jiān)控mysql狀態(tài)是需有的。
- killall -9 nrpe
- /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d #重啟被監(jiān)控機(jī)的nrpe服務(wù)
- ############ configure completed ############
- }
- #客戶機(jī)配置完成
- real=`grep -l '\^H' /root/.bash_profile`
- if [ $? -eq 1 ];then
- echo 'stty erase ^H' >> /root/.bash_profile
- source /root/.bash_profile #這幾行主要就是讓在使用read鍵時能使用回刪鍵。寫錯了,回刪了,重啟寫。不用這段的話,回刪鍵會變成亂碼。
- fi
- echo -e '\033[0;33;1m #################nagios################## \033[0m' #讓echo能弄點(diǎn)顏色出來好看點(diǎn)。。。
- echo "nagios server install and configure or addserver please input : 1"
- echo " clint server install or configure please input : 2"
- echo " exit please input : 3"
- echo -e '\033[0;33;1m ######################################### \033[0m'
- read -p "please chose : " frist #定義輸入的值
- if [ $frist -eq 1 ];then #值等于1時執(zhí)行nagios服務(wù)器配置
- echo -e '\033[0;33;1m #################nagios################## \033[0m'
- echo "nagios server install and configue please input : 1"
- echo " Add server and services please input : 2"
- echo " exit please input : 3"
- echo -e '\033[0;33;1m ######################################### \033[0m'
- read -p "please chose : " nagiosfrist #定義nagiosfrist輸入的值
- if [ $nagiosfrist -eq 1 ];then #當(dāng)nagiosfrist等于1時,執(zhí)行下面 安裝,添加服務(wù) 入重啟服務(wù)的函數(shù)
- installserver
- addservices
- restartnagios
- elif [ $nagiosfrist -eq 2 ];then #當(dāng)nagiosfrist等于2時,執(zhí)行添加服務(wù),重啟服務(wù)函數(shù)
- addservices
- restartnagios
- elif [ $nagiosfrist -eq 3 ];then #nagiosfrist等于3,就退出
- clear
- exit
- else
- echo "plase chose [1|2|3]" #都不等于的話,打印這個
- fi
- elif [ $frist -eq 2 ];then # 當(dāng)frist等于2時 執(zhí)行被監(jiān)控端的函數(shù)
- clientnagios
- elif [ $frist -eq 3 ];then #等于3時退出
- clear
- exit
- else
- echo "plase chose [1|2|3]" #都不等的話,就打印這個
- fi
腳本解析完成。本人也做了一系列的測試??茨_本是否能正常運(yùn)行。答案是可以的。在測試當(dāng)中,本人也裝一些圖抓了下來,在下篇博客 《一鍵搭建nagios監(jiān)控系統(tǒng)之三 測試篇 》中我們來看下。
QQ:410018348
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。