nagios安裝配置 nagios監控利器部署詳細過程

一、準備工作:
1、更換Yum源163
cd /etc/yum.repos.d/
mv CentOS-Base.repo{,.bak}
wget http://mirrors.163.com/.help/CentOS-Base-163.repo
yum makecache
2、LAMP環境優化編譯安裝,都加了簡單優化選項,可以直接當LAMP環境的網站使用
apache安裝
cd /usr/local/src
wget http://mirrors.sohu.com/apache/httpd-2.2.21.tar.bz2
yum -y install openssl openssl-devel
tar jxvf httpd-2.2.21.tar.bz2
cd httpd-2.2.21
./configure --prefix=/usr/local/apache2 --enable-so --enable-ssl --enable-rewrite --enable-deflate --enable-expires
make && make install
cd ..
爲了方便以後安裝其它需要sql的監控服務,裝上mysql
wget http://mirrors.sohu.com/mysql/MySQL-5.1/mysql-5.1.57.tar.gz
groupadd mysql
useradd mysql -g mysql -s /sbin/nologin
tar zxvf mysql-5.1.57.tar.gz
yum -y install ncurses ncurses-devel
./configure --prefix=/usr/local/mysql --localstatedir=/var/lib/mysql --sysconfdir=/etc --enable-thread-safe-client --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static --with-unix-socket-path=/tmp/mysql.sock --enable-assembler --without-debug --with-plugins=utf8,gbk --with-plugins=innobase
make && make install
cp my-small.cnf /etc/my.cnf
cp mysql.server /etc/init.d/mysql.server
chmod 755 /etc/init.d/mysql.server
vi /etc/init.d/mysql.server
46行 basedir=/usr/local/mysql
47行 datadir=/usr/local/mysql/var
cd /usr/local/mysql/
chown -R mysql.mysql .
bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/var
chown -R root .
chown -R mysql /usr/local/mysql/var
chkconfig --add mysql.server
chkconfig mysql.server on
/etc/init.d/mysql.server start
echo "export PATH=$PATH:/usr/local/mysql/bin" >>/etc/profile
source /etc/profile
nagios3以後版本需要php的支持
PHP的安裝
wget http://mirrors.sohu.com/php/php-5.3.6.tar.bz2
因爲需要有圖片產生,所以先需要php的gd庫支持
wget ftp://217.146.241.3/pub/linux/lib/gd-2.0.33.tar.gz
yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel libXpm libXpm-devel
tar zxvf gd-2.0.33.tar.gz
cd gd-2.0.33
./configure --prefix=/usr/local/gd2 --with-png --with-freetype --with-jpeg
make && make install
編譯php
tar jxvf php-5.3.6.tar.bz2
cd php-5.3.6
./configure --prefix=/usr/local/php5 --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --enable-mbstring=cn --enable-force-cgi-redirect --enable-ftp -with-gd -with-jpeg -with-zlib -with-png -with-freetype --disable-debug --enable-inline-optimization --enable-sockets --enable-bcmath
make && make install
增加php與apache的關聯
vi /usr/local/apache2/conf/httpd.conf
確認有這樣一行LoadModule php5_module modules/libphp5.so添加:AddType application/x-httpd-php .php
修改apache的運行用戶和組爲nagios
phpinfo測試下是否成功
<? phpinfo() ?>
nagios的安裝
wget http://cdnetworks-kr-2.dl.sourceforge.net/project/nagios/nagios-3.x/nagios-3.2.3/nagios-3.2.3.tar.gz
wget http://cdnetworks-kr-2.dl.sourceforge.net/project/nagiosplug/nagiosplug/1.4.15/nagios-plugins-1.4.15.tar.gz
wget http://cdnetworks-kr-1.dl.sourceforge.net/project/nagios/nrpe-2.x/nrpe-2.12/nrpe-2.12.tar.gz
groupadd nagios
useradd nagios -g nagios
tar zxvf nagios-3.2.3.tar.gz
cd nagios-3.2.3
./configure --prefix=/usr/local/nagios
mkdir -p /etc/httpd/conf.d/
make all //編譯
make install //安裝nagios主要程序,cgi和html文件等
make install-init //安裝nagios啓動腳本
make install-config //將配置文件的例子複製到nagios配置文件裏
make install-commandmode //配置nagios目錄權限。
make install-webconf 因爲這個命令執行的時候,會在/etc/httpd/conf.d/創建 nagios.conf文件
cat /etc/httpd/conf.d/nagios.conf >>/usr/local/apache2/conf/httpd.conf
安裝nagios插件
tar zxvf nagios-plugins-1.4.15.tar.gz
cd nagios-plugins-1.4.15
./configure --prefix=/usr/local/nagios/
make && make install
安裝完成後,會在/usr/local/nagios/ 目錄下面生成libexe插件目錄,nagios所有的插件都會在這個目錄裏面。
chown -R nagios.nagios /usr/local/nagios/
安裝nrpe
tar zxvf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure && make all
make install-plugin //安裝check_nrpe插件
make install-daemon
make install-daemon-config
make install-xinetd //安裝xinetd腳本
yum -y install xinetd
vi /etc/xinetd.d/nrpe
only_from = 127.0.0.1 註釋掉 allowshosts =192.168.1.221 添加監控主機,一般爲nagios服務器的Ip
vi /etc/services
添加兩行
nrpe 5666/tcp
nrpe 5666/udp
啓動xinetd
/etc/init.d/xinetd start 看看5666端口是否開啓
添加apache授權用戶 -c選項爲創建文件,創建第二個選項不需要
/usr/local/apache2/bin/htpasswd -c /usr/local/nagios/etc/htpasswd.users itv1
修改cgi.cfg #vi /usr/local/nagios/etc/cgi.cfg
找到這幾行,後面添加 erbin用戶,可以自己定義用戶,使用“,”隔開
authorized_for_system_information=nagiosadmin,itv1
authorized_for_configuration_information=nagiosadmin,itv1
authorized_for_system_commands=nagiosadmin,itv1
authorized_for_all_services=nagiosadmin,itv1
authorized_for_all_hosts=nagiosadmin,itv1
authorized_for_all_service_commands=nagiosadmin,itv1
authorized_for_all_host_commands=nagiosadmin,itv1

 
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg測試下,如果沒有eror就可以啓動了,
/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg啓動就行了
重新啓動下apache,然後通過http://IP/nagios訪問,輸入用戶名密碼如果成功登陸,到此安裝完成。
三、使用
現在跟着我的步驟走,不要懷疑,堅決執行吧哈哈
cd /usr/local/nagios/etc/objects/
ls看一下,看下就可以哈,然後
rm -f contacts.cfg printer.cfg switch.cfg templates.cfg windows.cfg
###如果你是頭次安裝,建議看下這些文件內容
contacts.cfg ------默認聯繫人配置文件
printer.cfg ------默認監控打印機的配置文件
switch.cfg ------默認監控交換機的配置問及愛你
templates.cfg------默認的模板文件
windows.cfg ------默認監控windows的配置文件
commands.cfg ------命令的配置文件
timeperiods.cfg----時間配置文件
進入正題:
首先介紹下nagios的幾個重要的項,就這麼叫吧,哈哈
host(主機)------hostgroup(主機組)
service(服務)------servicegroup(服務組)
contact(聯繫人) ------contactgroup(聯繫人組)
address(監控主機的ip)
service_description(定義檢查服務的名稱,在nagios頁面上顯示的)
check_command(檢查命令)
 
好了,瞭解這些足夠了,來一點點建立你自己的nagios
1、建立個模板文件,名字隨意.cfg,就叫moban.cfg吧通俗易懂
1 define contact{
2 name generic-contact---模板名稱
3 #聯繫組名稱
4 service_notification_period 24x7---和下面一項是在timeperiods.cfg定義的,自己看下就明白
5 #服務報警週期
6 host_notification_period 24x7
7 #主機報警週期
8 service_notification_options w,u,c,r,s
9 #服務通知選項
10 host_notification_options d,u,r,s
11 #主機通知選項
12 service_notification_commands notify-service-by-sendEmail ----command.cfg中定義,下面說
13 #服務通知方式
14 host_notification_commands notify-host-by-sendEmail
15 #主機通知方式
16 register 0
17 }
18
19 define host{
20 name generic-host----足跡模板名稱
21 #服務器名稱
22 notifications_enabled 1
23 #開啓主機通知
24 event_handler_enabled 1
25 #開啓時間處理
26 flap_detection_enabled 1
27 #啓用狀態抖動檢測
28 failure_prediction_enabled 1
29 #啓用故障預測
30 process_perf_data 1
31 #啓用進程性能數據記錄
32 retain_status_information 1
33 #啓用狀態信息保存功能
34 retain_nonstatus_information 1
35 #啓用非狀態信息保存功能
36 notification_period 24x7
37 #報警的時間週期 timeperiods.cfg配置
38 register 0
39 check_interval 1
40 #正常檢測的時間間隔(單位:分)
41 retry_interval 2
42 #重試檢測間隔時間
43 max_check_attempts 3
44 #最大嘗試次數
45 check_command check-host-alive----在command.cfg中定義的
46 #檢查主機存貨(command.cfg中定義)
47 notification_period 24x7
48 #監控週期
49 notification_interval 120
50 #同一問題的報警間隔
51 notification_options d,u,r
52 #主機通知報警選項 d--down, u--unknown, r--recover
53 contact_groups SMC----自己定義的聯繫人組,下面定義
54 }
55
56 define service{
57 name generic-service
58 active_checks_enabled 1
59 passive_checks_enabled 1
60 parallelize_check 1
61 obsess_over_service 1
62 check_freshness 0
63 notifications_enabled 1
64 event_handler_enabled 1
65 flap_detection_enabled 1
66 failure_prediction_enabled 1
67 process_perf_data 1
68 retain_status_information 1
69 retain_nonstatus_information 1
70 is_volatile 0
71 check_period 24x7
72 max_check_attempts 3
73 normal_check_interval 2
74 retry_check_interval 1
75 notification_options w,u,c,r
76 notification_interval 60
77 notification_period 24x7
78 register 0
79 contact_groups SMC
80 }
 
 
2、建立個聯繫人文件,還叫contact.cfg吧
define contact{
contact_name duty1---聯繫人名稱
use generic-contact---關聯generic-contact,在上面定義了
alias duty1---別名
email [email protected] ---郵件地址
}
 
define contact{
contact_name duty2
use generic-contact
alias duty2
}
define contact{
contact_name duty3
use generic-contact
alias duty1
}
 
define contact{
contact_name duty4
use generic-contact
alias duty2
}
 
 
define contactgroup {---定義聯繫組
contactgroup_name SMC---組名
alias SMC---組別名
members duty1,duty2---成員,必須爲已經定義的聯繫人
}
 
define contactgroup {
contactgroup_name SMC2
alias SMC2
members duty3,duty4
}
 
兩個小問題解釋下:
1、use generic-contact的作用,關聯此模板的所有已定義內容到此define contact中,省去很多筆墨
以duty2爲例,完整的就是
define contact{
contact_name duty2
use generic-contact
alias duty2
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r,s
host_notification_options d,u,r,s
service_notification_commands notify-service-by-sendEmail
host_notification_commands notify-host-by-sendEmail
register 0
}
還一點,定義單個{}與模板中相同參數的優先性 :自定義參數 > 模板中相同參數,也就是說模板的參數是缺省值
2、報警方式(139短信)
現在支持的報警方式很多,後面會詳細說,這裏就介紹下報警的過程。
nagios產生監控狀態硬性變化,調用郵件功能,將報警信息發送至mail.139.com,監控人員開通139短信通知,會自動收到139發來的短信
 
3、command.cfg文件
添加nrpe插件的命令
# 'check_nrpe' command definition
define command{
command_name check_nrpe
command_line /usr/local/nagios/libexec/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
 
介紹下我的報警發郵件sendEmail,一個perl寫的命令行發郵件工具。在我的系統應用了一年多,非常出色
下載
wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
解開後,將可執行文件sendEmail複製到/usr/local/bin/目錄並且修改爲nagios可執行
發個郵件測試一下
/usr/local/bin/sendEmail -f [email protected] -t 138xxxxxxx -s smtp.163.com -u "test" -m "hello" -xu old_hoodlum -xp password -l /var/log/sendEmail.log
-f --from
-t --to
-s 使用的smtp域名
-u 標題
-m 內容
-xu smtp登陸用戶名
-xp smtp登陸密碼
建議,nagios報警儘量不要使用本機sendmail,在大網站註冊個免費郵箱發去吧,也減少了很多麻煩,比如sendmail問題導致的報警不能及時到達
以下爲command中定義的發郵件命令,根據自己的實際情況修改
command.cfg中加入
define command{
command_name notify-host-by-sendEmail
command_line /usr/bin/printf "%b" "***** Nagios-BJ *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/local/bin/sendEmail -f [email protected] -t $CONTACTEMAIL$ -s smtp.163.com -u "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" -xu old_hoodlum -xp xxxxxxxx -l /var/log/sendEmail.log
}
 
define command{
command_name notify-service-by-sendEmail
command_line /usr/bin/printf "%b" "***** Nagios-BJ *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTNAME$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /usr/local/bin/sendEmail -f [email protected] -t $CONTACTEMAIL$ -s smtp.163.com -u "** $NOTIFICATIONTYPE$ Service Alert: $HOSTNAME$/$SERVICEDESC$ is $SERVICESTATE$ **" -xu old_hoodlum -xp xxxxxxxx -l /var/log/sendEmail.log
}
 
4、現在萬事具備了,說最重點的主機、服務的監控策略
假如,公司內網n個網段,192.168.x.0/24(x=1-8),每個x運行不同的業務,各有不同的系統,需要監控不同的服務,怎麼規劃呢?
如果只寫一個host.cfg和一個service.cfg的話,維護起來是非常困難的,我們可以這樣
建立1-x.cfg.....8-x.cfg,添加每個網段的主機,根據不同的業務及不同系統對主機分組,也可根據需要監控的內容將類似的主機分組,總而言之,怎麼方便你少寫字你就怎麼來
這就是開源的魅力,隨心隨意不必拘泥於形式,你可以給nagios定義100個配置文件,只要這麼做方便你管理就好,寫完了配置文件,只要在nagios.cfg文件加入你這個文件名就可以了,cfg_file=/usr/local/nagios/etc/objects/x.cfg
定義service(service_group),一樣的道理,監控同個服務的組就好了,這樣做會省去你很多的時間,舉個例子吧
##定義主機
define host{
use generic-host
host_name BJ-OUTPUT-sanlihe
alias BJ-OUTPUT-sanlihe
address 120.72.48.70
}
 
define host{
use generic-host
host_name BJ-GATEWAY-sanlihe
alias BJ-GATEWAY-sanlihe
address 120.72.48.1
}
 
define host{
use generic-host
host_name NA-GATEWAY
alias NA-GATEWAY
address 218.30.50.1
}
#定義單個主機服務
define service {
use generic-service
host_name NA-GATEWAY
max_check_attempts 3
normal_check_interval 1
retry_check_interval 2
contact_groups SMC
service_description PING
check_command check_ping!850.0,90%!1600.0,95%
}
 
define host{
use generic-host
host_name GW-dianxintong
alias GW-dianxintong
address 218.247.13.60
}
 
define host{
use generic-host
host_name BJ-dianxintong
alias BJ-dianxintong
address 218.247.13.62
}
#定義主機組
define hostgroup{
hostgroup_name itv
alias itv
members BJ-OUTPUT-sanlihe,BJ-GATEWAY-sanlihe,GW-dianxintong,BJ-dianxintong
}
#定義主機組服務
define service{
use generic-service
hostgroup_name itv
max_check_attempts 3
normal_check_interval 1
retry_check_interval 2
contact_groups SMC
service_description PING
check_command check_ping!500.0,90%!1000.0,95%
}
 
假如想用多種方式同時報警怎麼辦?
在moban.cfg的service_notification_commands 和host....中添加多種報警方式就好了,中間用“,”隔開
 
想讓每個人在不同時間以不同方式收到報警?
聯繫人中定義不同報警方式和報警時間就行了
 
想讓QQ給你報警?
沒戲,哈哈,發到QQ郵箱,如果QQ在線會自動彈出來的
 
nagios有多少種報警方式?
我也不知道有多少種,我用過的有短信貓、email、飛信、qq、msn....
 
都想用?
當然可以,escalations會幫你完成你的需求
 
想讓nagios幫你處理?
可以,event_hander會做到,但是強烈建議在你不是非常精通你所要操作的服務時,請不要使用
 
怎麼能讓nagios檢測到硬性變化後立刻報警?
報警時間跟這幾個參數有關,一看就能明白
1、interval_length 在nagios.cfg文件731行左右,默認爲60,單位s
2、check_interval
3、retry_interval
4、max_check_attempts
 
nagios分佈式??
假如你有100個nagios server,你想看看總況,總不能打開100個web頁面吧?nagios分佈式玩玩可以,個人建議不用
 
我想監控...
用我們經理的一句話:“你能想到它就能做到”,知道nagios監控插件的0、1、2就行了,http://exchange.nagios.org/ 插件最多的地方
 
個人一些建議:
(1)、nagios服務器強烈建議放在最安全的地方,在戰爭年代,它是一個哨兵,也是你的最後一道防線,如果監控掛了,服務器再掛了,你如何知道?
(2)、報警方式,個人意見:短信貓>email(socket方式)>msn || qq || fetion,以穩爲主
(3)、每天定點在nagios server上給自己發個消息是個很好的習慣
(4)、對於服務的檢查次數,設置爲3-4此比較合理,如果太短你可能會經歷一次狼來了的故事,太長的話,你可能都不會知道問題的存在
(5)、報警方式,如果有條件最好不要選擇單種,自己設置好策略
(6)、nagios本機的系統信息也很重要,可能一些時候的誤報是因爲主機系統資源緊張造成的,比如ping
(7)、儘量不去監控無所謂的程序或者應用,儘量不去使用snmp收集數據
(8)、總之、不要把nagios只當個輔助來對待,它是運維的尖銳利

本文出自 “擱淺” 博客,請務必保留此出處http://hoodlum.blog.51cto.com/4075497/743722

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章