由于發布文章字數有限制。腳本解釋將在這一篇文章里講解。如下:
- #! /bin/bash
- ## QQ:410018348
- ################## nagios server configure ########################
- installserver() { #定義安裝nagios函數
- 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*
- #安裝 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監控插件
- 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服務的一些軟件。
- wget http://218.6.16.170/somenagios.tar #這里的我個人找的一些 cpu ,內存,及mysql的插件。 打包在這。
- tar -xvf somenagios.tar -C /usr/local/nagios/libexec/
- echo "################install nagios server completed#############" #nagios安裝完成。
- 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 #更改use_authentication參數
- #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 #禁用監控本地的配置文件
- 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及監控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的模板文件改成真的配置文件 并授權
- 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里面的聯系人為所輸入的聯系人
- echo "################### nagios server install and configure completed,plase add hosts and services then start it ##################"
- } #以上nagios安裝及配置完成。
- restartnagios(){ # 這里是定義nagios,nrpe,及apache重啟的函數
- 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端添加客戶機的函數
- read -p "please input the alias name : " host #輸入添加機器別名
- read -p "please input the clint ip : " 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 #在hosts.cfg里添加一個主機 (參數的意思可以在網上看下)
- echo "define hostgroup {
- hostgroup_name myserver
- alias myserver
- members $host
- }" >>/usr/local/nagios/etc/objects/hostgroup.cfg #在hostgroup.cfg里添中一個主機組,$host就是添加的別名
- 是添加監控的具體服務。
- 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
- } #監控客戶機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
- } #監控客戶機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
- } #監控客戶機的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%
- } #監控客戶機的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
- } #監控客戶機的ftp服務
- " >>/usr/local/nagios/etc/objects/services.cfg #把上面內容添加到services.cfg里面 (參數的意思可以在網上看下)
- echo "############### Add services completed ########"
- }
- ######################### nagios configure completed ########################
- #以上為nagios服務端配置。
- ####################### client server configure ########################
- #以下為被監控端配置
- clientnagios() { #定義客戶機的安裝配置
- ### install soft###
- read -p "Please input the nagios server ip : " nagiosip #輸入naiogs服務端ip
- read -p "Please input the mysql password : " mysqlpass #輸入本機的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 ..
- #以上為安裝被監控機所需要的軟件
- 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 #刪除安裝的nrpe.cfg配置文件
- cat >>/usr/local/nagios/etc/nrpe.cfg<<EOF #添加以下內容到nrpe.cfg配置文件里。主要就是一些監控本機的命令,供nagios服務端調用
- 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 #這里是創建一個只能是nagios服務端訪問的庫。監控mysql狀態是需有的。
- killall -9 nrpe
- /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d #重啟被監控機的nrpe服務
- ############ configure completed ############
- }
- #客戶機配置完成
- 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能弄點顏色出來好看點。。。
- 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時執行nagios服務器配置
- 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 #當nagiosfrist等于1時,執行下面 安裝,添加服務 入重啟服務的函數
- installserver
- addservices
- restartnagios
- elif [ $nagiosfrist -eq 2 ];then #當nagiosfrist等于2時,執行添加服務,重啟服務函數
- addservices
- restartnagios
- elif [ $nagiosfrist -eq 3 ];then #nagiosfrist等于3,就退出
- clear
- exit
- else
- echo "plase chose [1|2|3]" #都不等于的話,打印這個
- fi
- elif [ $frist -eq 2 ];then # 當frist等于2時 執行被監控端的函數
- clientnagios
- elif [ $frist -eq 3 ];then #等于3時退出
- clear
- exit
- else
- echo "plase chose [1|2|3]" #都不等的話,就打印這個
- fi
腳本解析完成。本人也做了一系列的測試??茨_本是否能正常運行。答案是可以的。在測試當中,本人也裝一些圖抓了下來,在下篇博客 《一鍵搭建nagios監控系統之三 測試篇 》中我們來看下。
QQ:410018348
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。