近期公司需要需要搭建一個nagios的監控系統,以下是搭建的配置和過程。
操作系統:Ubuntu 12.04.3 LTS
內核:3.2.0-29-generic
位數:x86_64
首先安裝nagios安裝環境,apache+php+mysql
apt-get install apache2 libapache2-mod-php5 php5 mysql-server mysql-client libssl0.9.8 libssl-dev openssl gcc make libmysqlclient-dev
1,服務器端安裝
tar zxvf nagios-3.0.2.tar.gz
cd nagios-3.0.2
./configure --prefix=/usr/local/nagios
make all
make install
make install-init
make install-config
make install-commandmode
chkconfig --add nagios
chkconfig --level 35 nagios on
chkconfig --list nagios
tar zxvf nagios-plugins-1.4.16.tar.gz
cd nagios-plugins-1.4.16
./configure --prefix=/usr/local/nagios
make && make install
關於錯誤:
Validate.xs:208:5: error: duplicate case value
Validate.xs:205:5: error: previously used here
問題出在Params::Validate 0.88上,經測試,0.90沒有問題,所以,替換nagios plugin裏的Params::Validate爲0.90
下載http://cpan.metacpan.org/authors/id/D/DR/DROLSKY/Params-Validate-0.90.tar.gz
拷貝到目錄nagios-plugins-1.4.16/perlmods,刪除之前的0.88版本
編譯httpd.conf文件裏面添加如下內容:
ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"
<Directory "/usr/local/nagios/sbin">
AuthType Basic
Options ExecCGI
AllowOverride None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthUserFile /usr/local/nagios/etc/htpasswd //用於此目錄訪問身份驗證的文件
Require valid-user
</Directory>
Alias /nagios "/usr/local/nagios/share"
<Directory "/usr/local/nagios/share">
AuthType Basic
Options None
AllowOverride None
Order allow,deny
Allow from all
AuthName "nagios Access"
AuthUserFile /usr/local/nagios/etc/htpasswd
Require valid-user
</Directory>
添加nagios的web訪問賬號和密碼 可以自定義:
/usr/local/apache2/bin/htpasswd -c /usr/local/nagios/etc/htpasswd david
vi /usr/local/nagios/etc/cgi.cfg
把use_authentication=1修改爲use_authentication=0 不然會報錯
nagios web界面提示
It appears as though you do not have permission to view information for any of the services you requested...
authorized_for_system_commands=nagiosadmin,www --www爲http訪問授權用戶
authorized_for_all_services=nagiosadmin,www
authorized_for_all_hosts=nagiosadmin,www
authorized_for_all_service_commands=nagiosadmin,www
authorized_for_all_host_commands=nagiosadmin,www
tar -zxvf nrpe-2.8.1.tar.gz
cd nrpe-2.8.1
./configure
make all
make install
make install-plugin
make install-daemon
make install-daemon-config
chown -R nagios:nagios /usr/local/nagios/
編輯command.cfg文件
添加以下內容
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ -t 30
}
這樣服務器端要安裝的軟件就安全齊全了
啓動nagios服務:
service nagios start
Nagios 啓動的時候有下面的警告:
Starting nagios:No directory, logging in with HOME=/
done.
解決辦法:
修改/etc/passwd
nagios:x:1001:1001::/home/nagios:/bin/sh 改爲:
nagios:x:1001:1001::/usr/local/nagios:/bin/sh
開始配置服務器端的配置文件。
首先修改nrpe的配置文件nrpe.cgi文件
allowed_hosts=127.0.0.1,192.168.1.11,192.168.1.8 #多個ip地址用逗號隔開,這個是nrpe允許哪些ip地址來獲取數據。(添服務器端ip地址)
不然會出現如下報錯:
CHECK_NRPE: Error -Could not complete SSL handshake錯誤
這個報錯主要是因爲該服務器有多IP導致,解決辦法就是將服務器的所有IP都增加到nrpe.cfp配置文件中的參數allowed_hosts中便可解決
開啓nrpe服務:
啓動方式:/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
在nagios.cgi配置文件裏面可以自定義配置文件或者目錄如:
# You can specify individual object config files as shown below:
cfg_file=/usr/local/nagios/etc/objects/commands.cfg
cfg_file=/usr/local/nagios/etc/objects/contacts.cfg
cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg
cfg_file=/usr/local/nagios/etc/objects/templates.cfg
#cfg_file=/usr/local/nagios/etc/objects/service.cfg
cfg_dir=/usr/local/nagios/etc/objects/service
我直接添加了一個service目錄,以後再裏面添加配置文件方便一些
在service目錄下面添加配置文件192.168.1.101.cfg #記住配置文件的後綴必須是.cfg格式不然無法識別配置文件
define host{
use linux-server
host_name 192.168.1.101
address 192.168.1.101
}
define service{
use generic-service
host_name 192.168.1.101
service_description current load
check_command check_nrpe!check_load
}
define service{
use generic-service
host_name 192.168.1.101
service_description current disk
check_command check_nrpe!check_hda1
}
define service{
use generic-service
host_name 192.168.1.101
service_description current procs
check_command check_nrpe!check_total_procs
}
define service{
use generic-service
host_name 192.168.1.101
service_description current users
check_command check_nrpe!check_users
}
define service{
use generic-service
host_name 192.168.1.101
service_description crurrent zombie procs
check_command check_nrpe!check_zombie_procs
}
define service{
use generic-service
host_name 192.168.1.101
service_description nginx
check_command check_http!-H bj.hao123.com.cn -u /1.html
}
define service{
use generic-service
host_name 192.168.1.101
service_description tomcat
check_command check_http!-H bj.hao123.com.cn -u /test_jk.jsp
}
define service{
use generic-service
host_name 192.168.1.101
service_description ssh
check_command check_ssh
}
define service{
use generic-service
host_name 192.168.1.101
service_description ping
check_command check_ping!100.0,20%!200.0,50%
}
define service{
use generic-service
host_name 192.168.1.101
service_description current swap
check_command check_nrpe!check_swap
}
define service{
use generic-service
host_name 192.168.1.101
service_description mysql
check_command check_mysql!192.168.1.101!3306!nagios!nagios123
}
上面的配置文件就監控了一些服務,如果還有需要監控的服務直接在裏面添加就可以。
在安裝服務器端nagios的時候報了一些錯誤,現在總結一下看大家是否有遇見的。
ubuntu系統沒安裝libmysqlclient-dev,編譯nagios時會發現在nagios/libexec沒有check_mysql等命令
vi command.cfg
define command{
command_name check_mysql
command_line $USER1$/check_mysql -H $ARG1$ -P $ARG2$ -u $ARG3$ -p $ARG4$
}
2,在客戶端安裝nagios客戶端
tar zxvf nagios-plugins-1.4.16.tar.gz
cd nagios-plugins-1.4.16/perlmods
rm -rf Params-Validate-0.8*
wget http://cpan.metacpan.org/authors/id/D/DR/DROLSKY/Params-Validate-0.90.tar.gz
cd ..
./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-gourp=nagios --enable-perl-modules
make && make install
cd ..
tar -zxvf nrpe-2.8.1.tar.gz
cd nrpe-2.8.1
./configure
make all
make install
make install-plugin
make install-daemon
make install-daemon-config
chown -R nagios:nagios /usr/local/nagios/
sed -i 's#allowed_hosts=127.0.0.1#allowed_hosts=192.168.1.102,127.0.0.1#g' /usr/local/nagios/etc/nrpe.cfg
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
如果要監控客戶端上面的mysql就需要在客戶端的mysql裏面建立檢測的賬號
建立監控mysql數據庫
create database nagios;
grant select on nagios.* to nagios@'%' identified by 'nagios123';
flush privileges;
select User,Host from mysql.user;
如果在監控mysql的時候報錯:
ERROR 1045 (28000): Access denied for user 'nagios'@'localhost' (using password: YES)
解決辦法:
修改配置文件my.cnf
bind-address = 127.0.0.1 爲
bind-address = 0.0.0.0
3,nagios的郵件報警配置
在nagios服務器端安裝:
wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
tar -zxvf sendEmail-v1.56.tar.gz && cd sendEmail-v1.56
cp sendEmail /usr/local/bin
chmod 0755 /usr/local/bin/sendEmail
/usr/local/bin/sendEmail -f [email protected] -t [email protected] -s smtp.163.com -u "send by 123123" -xu wangba123 -xp wangba234 -m "222222222222222222222222222222211111111111111111111111111111"
各參數含義如下:
-f 表示發送者的郵箱, 可隨意設置
-t 表示接收者的郵箱
-s 表示SMTP服務器的域名或者IP
-u 表示郵件的主題
-m 表示郵件的內容
-xu 表示SMTP驗證的用戶名, 如果SMTP服務器需要驗證的話就加上該參數, 一般發給外網用戶則需要 -->163郵箱的賬號
-xp 表示SMTP驗證的密碼, 如果SMTP服務器需要驗證的話就加上該參數, 一般發給外網用戶則需要 --> 163郵箱密碼
測試成功後,修改command.cfg文件
define command{
command_name notify-host-by-email
command_line /usr/bin/printf "%b" "***** Nagios *****\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 $CONTACTEMA -u "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" -s smtp.163.com -xu wangba123 -xp wangba234
}
# 'notify-service-by-email' command definition
define command{
command_name notify-service-by-email
command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\
nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT
$" | /usr/local/bin/sendEmail -f [email protected] -t $CONTACTEMAIL$ -u "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERV
ICEDESC$ is $SERVICESTATE$ **" -s smtp.163.com -xu wangba123 -xp wangba234
}
到此郵件報警設置完成
4,nagios 增加Pnp畫圖的支持
首先安裝rrdtool
apt-install rrdtool librrds-perl php5-gd
tar zxvf pnp-0.4.12.tar.gz
cd pnp-0.4.12
./configure --with-nagios-user=nagios --with-nagios-group-nagios --with-rrdtool=/usr/bin/rrdtool --with-perfdata=/usr/local/nagios/share/perfdata
make
make all
make install
make install-config
make install-init
要產生圖形數據還需在commands.cfg中重定義命令:註釋掉原來的 process-service-perfdata命令不然會衝突
define command{
command_name process-service-perfdata
command_line /usr/local/nagios/libexec/process_perfdata.pl
}
編輯 nagios.cfg 修改以下內容
#process_performance_data=0
process_performance_data=1
service_perfdata_command=process-service-perfdata
在安裝rrdtool的過程中也許會出現如下錯誤:
RRDs... ***FAIL***
可安裝一下兩個包解決:
sudo apt-get install rrdtool
sudo apt-get install librrds-perl
以上能解決問題
若發現GD... ***FAIL***
則安裝一下包解決:
apt-get install libgd-gd2-perl
apt-get install php5-gd
nagios中 Status Map 無法顯示的解決辦法
查看/usr/local/nagios/sbin目錄下沒有statusmap.cgi文件。所以導致的Status Map無法顯示,
沒有這個statusmap.cgi的原因是因爲沒有gd-devel包,我的服務器上面安裝了gd包的但是還是沒有,
直接重新編譯源碼包
./configure --prefix=/usr/local/nagios
make all 之後就別make install了 直接進入 源碼包裏面的cgi目錄下面查看是否有statusmap.cgi文件。
發現真有這個文件 直接把文件拷貝到/usr/local/nagios/sbin目錄下這樣就不會覆蓋原來的配置。問題解決。重啓nagios