1. 程式人生 > 實用技巧 >Nagios 監控伺服器安裝及配置文件

Nagios 監控伺服器安裝及配置文件

一:需求軟體 RHEL 5.6 Nagios Nagios-plugins Nrpe 下載地址: http://down1.chinaunix.net/distfiles/nagios-3.2.3.tar.gz http://down1.chinaunix.net/distfiles/nagios-plugins-1.4.10.tar.gz http://nchc.dl.sourceforge.net/project/nagios/nrpe-2.x/nrpe-2.12/nrpe-2.12.tar.gz 二:安裝準備工作 1:搭建yum伺服器 [[email protected] ~]# vim /etc/yum.repos.d/rhel-source.repo [Server] name=Server baseurl=file:///mnt/Server enabled=1 gpgcheck=0 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release 2:安裝環境所需要的安裝軟體包 [
[email protected]
~]# yum –y install httpd php gcc glibc glibc-common gd gd-devel openssl openssl-devel 三:開始安裝nagios軟體 1:解壓軟體包 [[email protected] ~]# tar xf nagios-3.2.3.tar.gz -C /usr/src/ [[email protected] ~]# tar xf nagios-plugins-1.4.10.tar.gz -C /usr/src/ [[email protected] ~]# tar xf nrpe-1.8.tar.gz -C /usr/src/ 2:建立使用者 [
[email protected]
~]# groupadd nagcmd [[email protected] ~]# useradd –G nagcmd nagios [[email protected] ~]# usermod –G nagcmd apache 3:編譯安裝nagios [[email protected] ~]# cd /usr/src/nagios-3.2.3 [[email protected] nagios-3.2.3]# ./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios --with-command-group=nagcmd [
[email protected]
nagios-3.2.3]# make all [[email protected] nagios-3.2.3]# make install 編譯安裝 [[email protected] nagios-3.2.3]# make install-init 生成啟動指令碼 [[email protected] nagios-3.2.3]# make install-commandmode 為外部命令檔案配置目錄許可權 [[email protected] nagios-3.2.3]# make install-config 生成配置檔案 [[email protected] nagios-3.2.3]# make install-webconf 生成一個與apache介面的配置檔案 4:編譯安裝nagios-plugins [[email protected] nagios-3.2.3]# cd ../nagios-plugins-1.4.10/ [[email protected] nagios-plugins-1.4.15]# ./configure –with-nagios-user=nagios –with-nagios-group=nagios –prefix=/usr/local/nagios [[email protected] nagios-plugins-1.4.15]# make;make install 5:新增為開機自啟動服務 [[email protected] ~]# chkconfig --add nagios [[email protected] ~]# chkconfig nagios on 6:新增web使用者 [[email protected] ~]# htpasswd -c /usr/local/nagios/etc/passwrd.users nagiosadmin 7:啟動服務,檢查配置 [[email protected] ~]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg 檢查下nagios 配置檔案的正確性 Total Warnings: 0 檢視這兩個為“0”,說明配置檔案沒問題,可以啟動服務了。 Total Errors: 0 [[email protected] ~]# /etc/init.d/httpd start [[email protected] ~]# /etc/init.d/nagios start 8:登入nagios Http://127.0.0.1/nagios 出現下面這個登入介面:
登入後的介面:

注意: 如果是開啟了selinux服務,就要執行下面兩步: [[email protected] ~]# chcon -R -t httpd_sys_content_t /usr/local/nagios/sbin/ [[email protected] ~]# chcon -R -t httpd_sys_content_t /usr/local/nagios/share/ 到此,安裝部分也就完成了!接下來我們就做下新增監控伺服器配置。 先觀看下監控原理圖:
四:配置nagios監控linux主機 1:主配置檔案修改(改動部分內容) [[email protected] ~]# vim /usr/local/nagios/etc/nagios.cfg # Definitions for monitoring the local (Linux) host cfg_file=/usr/local/nagios/etc/objects/localhost.cfg 定義監控本機服務(如不需要監控本機,將註釋這行) cfg_file=/usr/local/nagios/etc/objects/hosts.cfg 新增此行,定義被監控的主機名和地址 cfg_file=/usr/local/nagios/etc/objects/hostgroups.cfg 新增此行,定義被監控主機的分組管理 cfg_file=/usr/local/nagios/etc/objects/contactgroups.cfg 新增此行,定義聯絡人組 cfg_dir/usr/local/nagios/etc/servers 開啟此行,定義需要監控的服務項 check_external_commands=1 定義在web介面下重啟nagios服務 command_check_interval=10s 定義命令檢查服務的間隔時間 [[email protected] ~]# vim /usr/local/nagios/cfi.cfg authorized_for_system_information=nagiosadmin,test authorized_for_configuration_information=nagiosadmin,test authorized_for_system_commands=nagiosadmin,test authorized_for_all_services=nagiosadmin,test authorized_for_all_hosts=nagiosadmin,test authorized_for_all_service_commands=nagiosadmin,test authorized_for_all_host_commands=nagiosadmin,test 可以定義多個使用者,新增到後面用逗號隔開就好。 2:object檔案配置(在此目錄下新增在.1中所講到的檔案) 建立聯絡人和聯絡人組配置檔案 [[email protected] objects]# vi contacts.cfg define contact { contact_name admin #聯絡人名 alias system administrator #別名 service_notification_period 24x7 #服務通知的時間段 host_notification_period 24x7 #主機通知的時間段 service_notification_options w,u,c,r #當服務出現w—報警,u—未知,c—嚴重,r—從異常恢復到正常,在這四種情況下通知聯絡人 host_notification_options d,u,r #當主機出現d----—當機,u—返回不可達,r—從異常情況恢復正常,在這3種情況下通知聯絡人 service_notification_commands notify-service-by-email #服務出問題通知採用的命令 host_notification_commands notify-host-by-email #同上 email [email protected] #指定聯絡的人email地址 pager 13800138000 #定義通過手機簡訊的方式傳送警報的手機號碼 pager 13810255206 } [[email protected] objects]# vi contactgroups.cfg define contactgroup{ contactgroup_name sagroup #定義組名 alias system administrator group #組別名 members admin #定義聯絡人名(contacts.cfg中的聯絡人名) } 建立被監控的主機和主機組檔案 [[email protected] objects]# vi hosts.cfg define host { host_name web #被監控的主機名 alias tomas #別名 address 192.168.2.6 #被監控主機地址 contact_groups sagroup #聯絡人組 check_command check-host-alive #檢查主機狀態的名字 check_period 24x7 #提醒週期 max_check_attempts 5 #檢查失敗後重試的次數 notification_interval 5 #提醒的間隔時間 notification_options d,u,r #在什麼情況提醒 } define host { host_name nagios-server alias tomas1 address 192.168.2.7 contact_groups sagroup check_command check-host-alive check_period 24x7 max_check_attempts 5 notification_interval 5 notification_options d,u,r } define host { host_name linux alias tomas2 address 192.168.2.8 contact_groups sagroup check_command check-host-alive check_period 24x7 max_check_attempts 5 notification_interval 5 notification_options d,u,r } [[email protected] objects]# vi hostgroups.cfg define hostgroup{ hostgroup_name sa-servers alias sa servers members web,nagios-server,linux } 配置監控主機服務項 /usr/local/nagios/etc/servers/web.cfg #在這下面依次再建nagios-server.cfg和linux.cfg define service{ host_name web #必須是hosts.cfg中定義的主機 service_description check-host-alive # check_command check-host-alive #在commands.cfg檔案中定義或在nrpe.cfg裡面定義的命令 max_check_attempts 5 #最大重試次數 normal_check_interval 5 #檢查間隔的單位是分鐘 retry_check_interval 2 #檢查間隔的單位是分鐘 check_period 24x7 notification_interval 10 #探測到故障後,每隔多長時間傳送一次報警資訊,單位是分鐘 notification_period 24x7 #通知選項跟聯絡人配置檔案相同 notification_options w,u,c,r contact_groups sagroup #配置檔案contactgroup.cfg定義的組名稱 } define service{ host_name web service_description check_tcp 80 check_command check_tcp!80 check_period 24x7 max_check_attempts 4 normal_check_interval 3 retry_check_interval 2 contact_groups sagroup notification_interval 10 notification_period 24x7 notification_options w,u,c,r } define service{ host_name web service_description cpu load check_command check_nrpe!check_load<!--[if !supportAnnotations]--><!--[endif]--> <!--[if !supportAnnotations]--><!--[endif]--> check_period 24x7 max_check_attempts 4 normal_check_interval 3 retry_check_interval 2 contact_groups sagroup notification_interval 10 notification_period 24x7 notification_options w,u,c,r } define service{ host_name web service_description total-procs check_command check_nrpe!check_total_procs check_period 24x7 max_check_attempts 4 normal_check_interval 3 retry_check_interval 2 contact_groups sagroup notification_interval 10 notification_period 24x7 notification_options w,u,c,r } 監控主機檔案配置完成了,我們可以重啟下服務,檢視下nagios的介面。 驗證下配置檔案: [[email protected] objects]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg Nagios Core 3.2.3 Copyright (c) 2009-2010 Nagios Core Development Team and Community Contributors Copyright (c) 1999-2009 Ethan Galstad Last Modified: 10-03-2010 License: GPL Website: http://www.nagios.org Reading configuration data... Read main config file okay... Processing object config file '/usr/local/nagios/etc/objects/commands.cfg'... Processing object config file '/usr/local/nagios/etc/objects/contacts.cfg'... Processing object config file '/usr/local/nagios/etc/objects/timeperiods.cfg'... Processing object config file '/usr/local/nagios/etc/objects/templates.cfg'... Processing object config file '/usr/local/nagios/etc/objects/localhost.cfg'... Processing object config file '/usr/local/nagios/etc/objects/hosts.cfg'... Processing object config file '/usr/local/nagios/etc/objects/hostgroups.cfg'... Processing object config file '/usr/local/nagios/etc/objects/contactgroups.cfg'... Processing object config directory '/usr/local/nagios/etc/servers'... Processing object config file '/usr/local/nagios/etc/servers/linux.cfg'... Processing object config file '/usr/local/nagios/etc/servers/web.cfg'... Processing object config file '/usr/local/nagios/etc/servers/nagios-server.cfg'.... Read object config files okay... Running pre-flight check on configuration data... Checking services... Checked 12 services. Checking hosts... Checked 2 hosts. Checking host groups... Checked 2 host groups. Checking service groups... Checked 0 service groups. Checking contacts... Checked 3 contacts. Checking contact groups... Checked 2 contact groups. Checking service escalations... Checked 0 service escalations. Checking service dependencies... Checked 0 service dependencies. Checking host escalations... Checked 0 host escalations. Checking host dependencies... Checked 0 host dependencies. Checking commands... Checked 24 commands. Checking time periods... Checked 5 time periods. Checking for circular paths between hosts... Checking for circular host and service dependencies... Checking global event handlers... Checking obsessive compulsive processor commands... Checking misc settings... Total Warnings: 0 #表示檔案沒有任何警告 Total Errors: 0 #表示檔案沒有任何錯誤 重新啟動服務: [[email protected] objects]# /etc/init.d/nagios restart Running configuration check...done. Stopping nagios: done. Starting nagios: done. [[email protected] objects]# /etc/init.d/httpd restart 登入介面: Http://IP/nagios
<!--[if !supportAnnotations]-->
<!--[if !supportAnnotations]--> <!--[endif]--> 可以清晰的看到前面所做的監控配置了。 遠端監控外掛NRPE的安裝就不再記錄了,在用NRPE監控的時候記得去修改services.cfg、command.cfg配置文件,網路上文件都很詳細。 在被監控的伺服器上只要安裝以下這兩個軟體包就好: Nagios-plugins Nrpe

http://1556359.blog.51cto.com/1546359/852341 友情連線

轉載於:https://blog.51cto.com/wujingfeng/1020113