系統:centos 6.3 最小化安裝的操作系統
nagios-3.2.3.tar.gz nagios 的服務安裝包
nagios-plugins-1.4.14.tar.gz 命令插件
nagios-cn-3.2.3.tar.bz2 中文補丁
httpd-2.2.4.tar.gz apache安裝文件
php-5.2.6.tar.gz php的安裝文件
安裝前準備工作
useradd nagios
mkdir /usr/local/nagios 創建nagios 的安裝目錄
chown -R nagios.nagios /usr/local/nagios 將nagios 目錄下所有的內容加入nagios用戶與nagios組
yum install gcc gcc-c++ libtool libxml2-devel zlib-devel gd-devel gd-devel perl-devel ncurses-devel glib2-devel make pango-devel cairo-devel perl-Time-HiRes 安裝這些文件 後面再繼續安裝有關nagios的服務 就不會再出現報錯。如果在報錯 就可能是版本不同或者系統不同導致的
安裝nagios
將所有準備好的安裝包 上傳至 /tmp裏面
tar -xvf nagios-3.2.3.tar.gz
cd nagios.3.2.3
./configure --prefix=/usr/local/nagios 指定了nagios的安裝目錄
make all && make install 編譯 並安裝 nagios主程序
make install-init 在/etc/rc.d/init.d目錄下創建nagios的啓動腳本
make install-commandmode 通過此命令來配置目錄權限
mak install-config 用來安裝nagios 配置文件,這裏安裝路徑是/usr/local/nagios/etc
設置開機自動啓動
chkconfig --add nagios 將nagios 啓動項加入到系統服務
chkconfig --list nagios 查看nagios 是否成功加入
nagios 各個目錄的名稱及其用途說明:
bin nagios的命令所在的目錄
etc nagios的配置文件都在此目錄
sbin nagiosCGI文件的目錄,執行外部命令所需要文件的目錄
share nagios的網頁文件所在目錄
libexec nagios-plugins 安裝在此目錄,此目錄默認爲空,安裝了插件會生成可執行文件
var 存放日誌文件的目錄
安裝nagios 插件
cd /tmp
tar -xvf nagios-plugins-1.4.14
cd nagios-plugins-1.4.14
./configure --prefix=/usr/local/nagios
make && make install
安裝漢化插件
cd /tmp
tar -xvf nagios-cn-3.2.3.tar.bz2
cd nagios-cn.3.2.3
./configure --prefix=/usr/local/nagios
make all && make install
安裝 apache
tar -xvf httpd-2.2.4.tar.gz
cd httpd-2.2.4
./configure --prefix=/usr/local/apache2 \
--enable-so \
--enable-mods-shared=most \
--enable-proxy \
--enable-proxy-connect \
還需要什麼模塊可以根據自己的需要對apache 進行安裝 做爲監控其實也無所謂了
安裝php
tar -xvf php-5.2.6.tar.gz
cd php.5.2.6
./configure --prefix=/usr/local/php \
--with-apxs2=/usr/local/apache2/bin/apxs
make && make install
配置apache
apache 的基本修改 就不多講了,這裏只記錄關於nagios的配置
vi /usr/local/apache2/conf/http.conf
修改 User nagios
Group nagios
DirectoryIndex index.php index.html
添加以下內容
AddType application/x-httpd-php .php
爲了安全,我們訪問nagios的頁面 需要設置賬號密碼的驗證,獲得許可後才能瀏覽。
在最後 添加以下內容
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>
創建瀏覽驗證文件
在apache的bin目錄下的 htpasswd
/usr/local/apache2/bin/htpasswd -c /usr/local/nagios/etc/htpasswd admin
New password:123456"這裏根據自己需要自行修改"
Re-type new password:請再輸入一次
Adding password for user admin 這裏就建好了一個賬號爲admin密碼爲123456的驗證信息
測試一下服務是否可以正常運行
關閉防火牆 service iptables stop 或者允許80端口不然 無法瀏覽頁面
/usr/local/apache2/bin/apachectl start 啓動apache
service nagios start 啓動nagios
然後打開 瀏覽器 輸入 http://ip/nagios
彈出 登錄驗證框 輸入賬號密碼 就可以正常訪問
***********************至此nagios的運行環境搭建好了***********************
現在講下nagios配置文件
配置文件 放在nagios/etc下面
cgi.cfg 控制CGI訪問的配置文件
nagios.cfg nagios主配置文件
resource.cfg 變量定義文件,這個一般不做修改。
/etc/objects/ 這是存放配置文件模板的目錄,定義 要監控的對象和相關信息的配置文件
objects/commands.cfg 定義監控某個功能所使用的命令的配置文件。
objects/contacts.cfg 定義常用聯繫人和聯繫組的配置文件
objects/localhost.cfg 定義本地主機的配置文件
objects/templates.cfg 定義主機和服務的一個模板
objects/timeperiods.cfg 定義監控時間段的配置文件
還有printer.cfg switch.cfg windows.cfg 這幾個默認不啓動的配置文件,不做介紹了。
等了解了naigos 的配置文件之間的關係,自己就可以隨意添加項要監控的內容了。
配置文件之間的關係
nagios 這些配置文件 之間存在很多相互引用,因爲相互引用這樣,使得nagios 的管理更加方便,但是 對於 剛剛接觸nagios的朋友來說,反而顯得 關係很混亂,但是隻要把關係弄清楚,就算是熟練掌握了nagios了
配置nagios
(1)templates.cfg文件
define contact{
name generic-contact 定義聯繫人名稱
service_notification_period 24
#定義出現服務異常時,發送通知的時間段,24在timeperiods.cfg定義的,這24參數也算引用
host_notification_period 24
#定義出現主機異常時,發送通知的時間段,24在timeperiods.cfg定義的
service_notification_options w,u,c,r
#定義通知可以被髮錯的情況,w表示警告warn,u表示不明unknown
#c表示緊急criticle,r表示恢復recover
host_notification_options d,u,r
#d:宕機 u:不可到達狀態 r:恢復狀態
service_notification_commands notify-service-by-email
#服務故障時,通過email方式進行通知,notify-service-by-email 在commands.cfg中定義的
host_notification_commands notify-host-by-email
#主機故障時,通過email方式進行通知,notify-host-by-email在commands.cfg中定義的
register 0
}
define host{
name generic-host 定義主機模板的名稱,不是電腦的主機名,只針對這個define host模板
notifications_enabled 1
event_handler_enabled 1
flap_detection_enabled 1
failure_prediction_enabled 1
process_perf_data 1 開啓數據傳輸功能,後面將用到
retain_status_information 1
retain_nonstatus_information 1
notification_period 24
register 0
}
define host{
name linux-server 定義這個主機模板的名稱,每個模板 裏面的屬性不一樣,不要鬧混了。
use generic-host 引用generic-host這個主機的屬性,到當前主機當中,use表示引用
check_period 24 表示檢查主機的時間段
check_interval 5 表示檢查主機的時間間隔 這裏是5分鐘
retry_interval 1 第一次檢查失敗時,重試檢查間隔 這裏是1分鐘
max_check_attempts 10 主機出現異常時,會嘗試10次檢查,再最後判斷是否真正的出現異常
check_command check-host-alive 指定檢查主機狀態的命令,在commands.cfg中定義的
notification_period 24 主機故障時,發送通知的時間範圍,在timeperiods.cfg中定義的
notification_interval 120 表示異常後,間隔120分鐘進行第二次通知,若爲0 表示只進行1次通知
notification_options d,u,r 定義 主機在什麼狀態下會發送通知d:宕機 u:不可到達 r:恢復狀態
contact_groups admins 指定聯繫人組 這個admin 在contacts.cfg文件中定義的
register 0
}
define service{
name generic-service 定義一個服務的名稱
active_checks_enabled 1
passive_checks_enabled 1
parallelize_check 1
obsess_over_service 1
check_freshness 0
notifications_enabled 1
event_handler_enabled 1
flap_detection_enabled 1
failure_prediction_enabled 1
process_perf_data 1
retain_status_information 1
retain_nonstatus_information 1
is_volatile 0
check_period 24
max_check_attempts 3 指定了nagios對服務的最大檢查次數
normal_check_interval 10 設置服務檢查時間間隔
retry_check_interval 2 重試檢查時間間隔,這裏是2分鐘
contact_groups admins 指定聯繫人組
notification_options w,u,c,r
notification_interval 60
notification_period 24
register 0
}
define service{
name local-service
use generic-service
max_check_attempts 2
normal_check_interval 5
retry_check_interval 1
register 0
}
(2)resource.cfg 定義nagios的變量文件 文件內容 只用到了一行
$USER1$=/usr/local/nagios/libexec
不用解釋相信大家也明白這句意思了。
(3)commands.cfg
以chkec_ping 爲例:
define command{
command_name check_ping
command_line $USER1$/check_ping -H $HOSTADDRESS$ -W $ARG1$ -C $ARG2$ -P 5
}
nagios服務運行後 就會檢查ping 如果能ping通 代表機器運行正常,僅此而已
裏面有很多命令 ,把自己不需要的註釋掉,自定義項添加的 可以根據裏面的格式自己編輯
(4)host.cfg文件,次文件默認不存在,需要自己創建。裏面是被監控主機的屬性。
define host{
use linux-server 引用linux-server的屬性到當前主機裏
host_name web 定義被監控的主機名
alias yanzhe-web 主機別名 nagios瀏覽器顯示的就是主機別名
address 192.168.1.101 被監控的主機IP地址,也可以是域名
}
define host{
use linux-server
host_name mysql
alias yanzhe-mysql
address 192.168.1.102
}
define hostgroup{ 定義一個主機組
hostgroup_name sa-servers 主機組名稱
alias sa servers 主機組的別名
members web,mysql 組包括的成員
}
(5)services.cfg文件 進行定義了都對主機進行 那些監控服務,用什麼命令。
define service{
use local-service 引用local-service服務的屬性。
host_name web 監控web主機上的服務 web在hosts.cfg中定義的。
service_description PING 監控服務的內容 。
check_command check_ping!100.0,20%!500.0,60% 指定檢查的命令 check_ping在commands.cfg中進行的定義,後跟兩個參數 命令與參數之間用!分割開。
}
define service{
use local-service
host_name web
service_description SSH
check_command check_ssh
}
define service{
use local-service
host_name web
service_description SSHD
check_command check_tcp!22
}
define service{
use local-service
host_name web
service_description http
check_command check_http
}
#******************************mysql*****************************
define service{
use local-service
host_name mysql
service_description PING
check_command check_ping!100.0,20%!500.0,60%
}
define service{
use local-service
host_name mysql
service_description SSH
check_command check_ssh
}
define service{
use local-service
host_name mysql
service_description ftp
check_command check_ftp
}
define service{
use local-service
host_name mysql
service_description mysqlport
check_command check_tcp!3306
}
(6)contacts.cfg 文件 定義聯繫人和聯繫組的,出現故障後,通過email或者短信發送給誰。
define contact{
contact_name sasystem 定義聯繫人的名稱
use generic-contact 引用generic-contact的屬性
alias sa-system 聯繫人別名
email [email protected] 用139郵箱 手機也可以收到短信,但比直接用短信通知慢一些
}
define contactgroup{
contactgroup_name admins 定義聯繫人組名稱
alias system administrator group 聯繫人組的描述
members sasystem 就是上面定義的聯繫人。
}
(7)timeperiods.cfg文件 定義監控時間段。
define timeperiod{
timeperiod_name 24
alias 24 Hours A Day, 7 Days A Week
sunday 00:00-24:00
monday 00:00-24:00
tuesday 00:00-24:00
wednesday 00:00-24:00
thursday 00:00-24:00
friday 00:00-24:00
saturday 00:00-24:00
}
這裏來設置時間段的控制 其他的設置不用修改
(8)cgi.cfg文件 添加 admin的權限
default_user_name=admin
authorized_for_system_information=nagiosadmin,admin
authorized_for_configuration_information=nagiosadmin,admin
authorized_for_system_commands=admin
authorized_fro_all_services=nagiosadmin,admin
authorized_fro_all_hosts=nagiosadmin,admin
authorized_fro_all_service_commands=nagiosadmin,admin
authorized_fro_all_host_commands=nagiosadmin,admin
(9)nagios.cfg文件
添加hosts.cfg 和 services.cfg 的 指引路徑
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg
cfg_file=/usr/local/nagios/etc/objects/services.cfg
註釋localhost.cfg的指引路徑 #cfg_file=/usr/local/nagios/etc/objects/localhost.cfg
修改:
command_check_interval=2
*******************nagios的配置已經完成了*******************
出處:http://yanzhe.blog.51cto.com/5279577/1129608