FreeBSD下部署nagios監控
一、首先安裝apache,apache應該是我們最常用的部署了,它可以被弄得很復雜,但在nagios
這個平臺上,我們只需要簡單的功能即可。
引用
tar xzvf httpd-2.2.16.tar.gz
cd
httpd-2.2.16
./configure --prefix=/usr/local/apache
make
make
install
二、安裝php
cd php-5.3.4
./configure --prefix=/usr/local/php
--with-apxs2=/usr/local/apache/bin/apxs --with-zlib
--with-jpeg-dir=/usr/local/jpeg6 --with-png-dir=/usr/local/libpng2
--with-iconv=/usr/local/libiconv --with-config-file-path=/usr/local/lib
--disable-debug --enable-safe-mode --enable-short-tags
make
make
install
cp php.ini-production
/usr/local/php/etc/php.ini
三、安裝nagios
1.添加帳戶,用來運行nagios
pw groupadd nagios
pw useradd nagios -g nagios -s
/usr/sbin/nologin
2.編譯安裝nagios
tar xzvf nagios-3.2.3.tar.gz
cd
nagios-3.2.3
./configure --prefix=/usr/local/nagios --with-nagios-user=nagios
--with-nagios-group=nagios
make all
make install
make
install-commandmode
make
install-config
四、配置apache
修改httpd.conf文件,找打
<IfModule dir_module>
DirectoryIndex
index.html
</IfModule>
修改為
<IfModule dir_module>
DirectoryIndex
index.php index.html
</IfModule>
找到
AddType application/x-compress .Z
AddType
application/x-gzip .gz .tgz
在后面添加:
AddType application/x-httpd-php .php
AddType
application/x-httpd-php .php3
AddType application/x-httpd-php .htm
AddType
application/x-httpd-php .html
在httpd.conf末尾添加
ScriptAlias /nagios/cgi-bin
"/usr/local/nagios/sbin"
<Directory "/usr/local/nagios/sbin">
#
SSLRequireSSL
Options ExecCGI
AllowOverride None
Order
allow,deny
Allow from all
# Order deny,allow
# Deny from all
#
Allow from 127.0.0.1
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd
Require valid-user
#
AddHandler cgi-script cgi pl
</Directory>
Alias /nagios
"/usr/local/nagios/share"
<Directory
"/usr/local/nagios/share">
# SSLRequireSSL
Options None
AllowOverride None
Order allow,deny
Allow from all
# Order
deny,allow
# Deny from all
# Allow from 127.0.0.1
AuthName "Nagios
Access"
AuthType Basic
AuthUserFile
/usr/local/nagios/etc/htpasswd
Require
valid-user
</Directory>
創建web驗證用戶
/usr/local/apache/bin/htpasswd -c
/usr/local/nagios/etc/htpasswd
admin
五、安裝nagios插件
tar xzvf nagios-plugins-1.4.13.tar.gz
cd
nagios-plugins-1.4.13
./configure -prefix=/usr/local/nagios
--with-nagios-user=nagios --with-nagios-group=nagios
make
make
install
可以看到在/usr/local/nagios/libexec
目錄下多出很多文件,這些就是nagios插件,如果編譯安裝后看不到check_
mysql插件,需要本機安裝mysql 并添加編譯選項
--with-mysql=/usr/bin/mysql_config 指出mysql_config 路徑。
六、安裝nrpe
tar xzvf nrpe-2.12.tar.gz
cd
nrpe-2.12
./configure --prefix=/usr/local/nrpe
make all
make
install
在其它被監控主機上安裝nrpe同樣需要添加nagios用戶,并安裝nagios插件,要不然被監控主機缺少插件有些監控功能不能正常使用。
安裝完nrpe后,在安裝目錄/usr/local/nrpe/libexec只有一個文件check_nrpe,而在nagios插件目錄,卻缺少這個文件,因此需要把這個文件復制到nagios插件目錄;同樣, 因為nrpe需要調用的諸如check_disk等插件在自己的目錄沒有,可是這些文件確是nagios插件所存在的,所以也需要從nagios目錄復制一份過來。我們把復制過程列舉出來:
引用
cp
/usr/local/nrpe/libexec/check_nrpe /usr/local/nagios/libexec
cp
/usr/local/nagios/libexec/* /usr/local/nrpe/libexec
配置nrpe
安裝完nrpe以后,在安裝目錄并沒有可用的配置文件,但我們只需把解壓目錄的樣例文件復制到安裝目錄,然后修改這個文件.
mkdir
/usr/local/nrpe/etc
cp
sample-config/nrpe.cfg /usr/local/nrpe/etc
修改nrpe配置文件,找到
#server_address=127.0.0.1
修改為
server_address=172.16.27.23
此IP地址為被監控主機的IP地址
找到
更改為
allowed_hosts=127.0.0.1,172.16.27.23
此IP地址為監控主機的IP地址,因為我監控機和被監控機都是同一臺及其,請注意區分。
啟動nrpe
/usr/local/nrpe/bin/nrpe -c
/usr/local/nrpe/etc/nrpe.cfg -d
# sockstat |grep 5666
nagios nrpe 12439
4 tcp4 172.16.27.23:5666
*:*
出現上述信息則代表nrpe已正常啟動。
檢查被監控主機的插件功能
# /usr/local/nagios/libexec/check_nrpe -H
172.16.27.23
NRPE
v2.12
出現此信息代表被監控主機的插件功能Ok
通過nrpe檢查主機資源,此方法就是通過在監控主機上使用check_nrpe使用命令行方式檢查被監控主機資源。
-c
check_load 參數中的check_load命令定義在被監控主機的nrpe.cfg文件中。
# /usr/local/nagios/libexec/check_nrpe -H 172.16.27.23
-c check_load
OK - load average: 0.00, 0.00,
0.00|load1=0.000;15.000;30.000;0; load5=0.000;10.000;25.000;0;
load15=0.000;5.000;20.000;0;
七、配置nagios
nagios的配置文件路徑位于:/usr/local/nagios/etc
1.首先修改nagios.cfg
查找到
cfg_file=/usr/local/nagios/etc/objects/localhost.cfg
修改為
#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg
查找到
#cfg_dir=/usr/local/nagios/etc/servers
修改為
cfg_dir=/usr/local/nagios/etc/servers
添加此行
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg
然后
mkdir /usr/local/nagios/etc/servers
chown -R
nagios:nagios
/usr/local/nagios/etc/servers
創建此文件夾用于存放被監控主機的配置文件
2. 進入object目錄 修改contacts.cfg文件,此文件用于創建聯系人及其組。
define
contact{
contact_name zhangsan
alias
Nagios zhangsan
service_notification_period
24x7
host_notification_period 24x7
service_notification_options u,c,r
host_notification_options
d,r
service_notification_commands
notify-service-by-email
host_notification_commands notify-host-by-email
email
zhangsan@test.com.cn
}
可按照此方法繼續添加聯系人
define contactgroup{
contactgroup_name
admins
alias Nagios
Administrators
members
zhangsan,lisi,wangwu
}
添加聯系人所在的組 多個聯系人用逗號分開
3.創建hosts.cfg文件,此文件用于定義被監控主機的ip及其主機名和添加到主機組
define
host{
host_name 172-16-27-23
alias BSD-HOST
address
172.16.27.23
contact_groups
admins
check_command
check-host-alive
max_check_attempts
10
notification_interval 20
notification_period 24x7
notification_options
d,u,r
}
八、創建被監控主機的配置文件
首先在/usr/local/nagios/etc/objects/commands.cfg中定義check_nrpe命令,添加如下:
define
command{
command_name check_nrpe
command_line /usr/local/nagios/libexec/check_nrpe
-H $HOSTADDRESS$ -c $ARG1$
}
在/usr/local/nagios/etc/servers中創建文件(文件名最好能標示主機為好)例如BSD-HOST.cfg,檢查根分區的配置文件為
define
service{
host_name
172-16-27-23
service_description check load
check_period 24x7
max_check_attempts 4
normal_check_interval
5
retry_check_interval 1
contact_groups admins
notification_interval
5
notification_period
24x7
notification_options w,u,c,r
check_command check_nrpe!check_load
}
創建完成后檢查nagios配置是否正確,如果出現錯誤則nagios不能啟動
/usr/local/nagios/bin/nagios -v
/usr/local/nagios/etc/nagios.cfg
Total Warnings: 0
Total Errors:
0
錯誤為0的話,則代表nagios沒有錯誤可以啟動,啟動方法如下:
/usr/local/nagios/bin/nagios -d
/usr/local/nagios/etc/nagios.cfg
ps -axuww | grep nagios
nagios
12490 0.0 0.8 3608 1960 ?? Ss 6:02PM 0:00.06
/usr/local/nagios/bin/nagios -d
/usr/local/nagios/etc/nagios.cfg
nagios出現It appears as
though you do not have permission to view information for any of the services
you requested...解決辦法為
vi /usr/local/nagios/etc/cgi.cfg
use_authentication=1 #把1修改為0,保存
重啟nagios,錯誤解決。http://172.16.27.23/nagios/ 訪問。