添加client節點node
####添加一臺client:新加入的是一臺開啓了防火牆的阿里雲服務器linux
首先搭建client環境ios
#監控Linux、Unix主機,安裝客戶端git
添加nagios用戶github
useradd -m nagios -s /sbin/nologinweb
#安裝插件nagios-plugins
vim
cd /home/huang/tools/服務器
wget http://www.nagios-plugins.org/download/nagios-plugins-2.1.1.tar.gzssh
tar xfz nagios-plugins-2.1.1.tar.gztcp
cd nagios-plugins-2.1.1
./configure --with-nagios-user=nagios --with-nagios-group=nagios --prefix=/usr/local/nagios
make
make install
chown -R nagios.nagios /usr/local/nagios/
#安裝NRPE
cd /home/huang/tools/
wget https://github.com/NagiosEnterprises/nrpe/archive/3.0.tar.gz
tar xf 3.0.tar.gz
cd nrpe-3.0/
./configure
make all
make install
make install-plugin
make install-daemon
make install-config
#修改配置文件,添加nagios主機
sed -i 's#allowed_hosts=127.0.0.1#allowed_hosts=127.0.0.1,192.168.1.155#g' /usr/local/nagios/etc/nrpe.cfg
##編輯hosts.cfg配置文件,添加阿里雲服務器的信息
vim ../objects/hosts.cfg
一、define host{
use linux-server
host_name client01
alias client01
address 120.26.68.152
}
二、加入組
define hostgroup{
hostgroup_name linux-servers
alias Linux Servers
members nagios_server,client01(client01新加入的)
}
三、定義主動模式(不須要nrpe服務) 此時是主動拉取數據,因此並無用到nrpe,也就是不使用check_nrpe,也不須要在客戶端開啓nrpe服務
cd /usr/local/nagios/etc/
mkdir services
chown -R nagios.nagios services
cd services
vim port-client.cfg(nagios服務的services目錄下)
define service{
use generic-service
host_name client01
service_description ntp_port
check_period 24x7
check_interval 5
retry_interval 1
max_check_attempts 3
check_command check_http!-p 123
notification_period 24x7
notification_interval 30
notification_options w,u,c,r
contact_groups admins
}
三、手動測試數據是否有收集
[root@RS1 services]# /usr/local/nagios/libexec/check_tcp -H 120.26.68.152 -p 123
CRITICAL - Socket timeout after 10 seconds
報錯如上
解決辦法:防火牆的123端口沒有向外開放(因爲不想開放這個端口,因此不理會這個報錯,從新監控其餘端口)
這裏選擇監控sshd服務的端口22389
當監控sshd的端口22389
[root@RS1 services]# /usr/local/nagios/libexec/check_tcp -H 120.26.68.152 -p 22389
TCP OK - 0.092 second response time on 120.26.68.152 port 22389|time=0.092055s;;;0.000000;10.000000
修改配置文件的123端口爲22389
重啓檢測語法以及重啓服務,在web頁面觀察到以下錯誤:
check_tcp: Port must be a positive integer
手動可以收集數據,可是nagios的web界面報錯:check_tcp: Port must be a positive integer
解決辦法:修改service配置文件:
check_command check_tcp!22389(不須要加-p了)
#####主動模式小結
通常客戶端對外開啓的服務,用主動模式監控,例如:port,uri
主動模式的監控配置過程:
一、在服務端的命令行把要監控的命令先調試好
二、在commands.cfg裏定義好命令,同時調用命令行的插件,先手動收集一下數據
三、在服務的配置文件裏定義要監控的服務,調用commands.cfg裏定義的監控命令
建立第二臺client服務器,ip爲192.168.1.11
首先仍是搭建nrpe環境,這裏省略
此次選擇客戶端被動模式收集數據
在client02上操做
[root@node2 ~]# /usr/local/nagios/libexec/check_tcp -H 192.168.1.11 -p 80
TCP OK - 0.001 second response time on 192.168.1.11 port 80|time=0.000810s;;;0.000000;10.000000
手動抓取數據正常,因而配置服務器端:
define service{
use generic-service
host_name client02 ----》已經定義了hosts文件(192.168.1.11爲client02)
service_description web_port
check_command check_nrpe!check_tcp
}
而後重啓服務,出現以下錯誤:
NRPE: Command 'check_tcp' not defined
解決辦法:這是由於客戶端配置文件nrpe.cfg並無定義check_tcp這個命令,編輯配置文件,添加下面一行:
command[check_tcp]=/usr/local/nagios/libexec/check_tcp -H 192.168.1.11 -p 80
而後在服務端進行手動測試抓取數據:
[root@RS1 etc]# /usr/local/nagios/libexec/check_nrpe -H 192.168.1.11 -c check_tcp
TCP OK - 0.001 second response time on 192.168.1.11 port 80|time=0.000670s;;;0.000000;10.000000
成功解決
而後被動模式成功
#########對每一個服務進行分組(好幾臺服務器監控的服務都是load負載)
新建:vim /usr/local/nagios/etc/services/servicegroup.cfg
define servicegroup{
servicegroup_name load ----》這裏對應service配置文件的service_description
alias Linux Servers
members nagios_server,load,client01,load,client02,load
}
將三個主機(nagios_server、client0一、client02)的監控的load服務定義到一個服務組中,
修改server.cfg中的description以下:
vim ../objects/services.cfg
define service{
use generic-service
host_name nagios_server 因爲這裏只定義了一個主機,因此仍是顯示了一個
service_description load -----》這裏改爲和服務組定義的名稱一致
check_command check_nrpe!check_load
}
#####模板選擇與修改
編輯的services.cfg
define service{
use generic-service ----》這裏使用的是generic-service這個模板,在配置文件templates.cfg中有定義,本身能夠在模板中定義一個別的service模板,而後這裏修改就行
host_name client01
service_description ntp_port
check_period 24x7
check_interval 5
retry_interval 1
max_check_attempts 3
check_command check_http!22389
notification_period 24x7
notification_interval 30
notification_options w,u,c,r
contact_groups admins
} ----》後面這些和模板中相同的參數配置均可以省略掉,由於和模板配置文件同樣,恰好就調用了模板文件
模板定義:vim templates.cfg
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
process_perf_data 1
retain_status_information 1
retain_nonstatus_information 1
is_volatile 0
check_period 24x7
max_check_attempts 3
normal_check_interval 10 這個能夠稍微改小一點
retry_check_interval 2
contact_groups admins
notification_options w,u,c,r warning unknown critical recovery
notification_interval 60
notification_period 24x7
register 0
}
########定義分組報警
vim contacts.cfg
如下是默認聯繫人
define contact{
contact_name nagiosadmin
use generic-contact
alias Nagios Admin
email nagios@localhost
}
自定義聯繫人:
define contact{
contact_name huang
use generic-contact
alias Nagios Admin
email 123456@qq.com
}
define contact{ ---》聯繫中能夠加入手機登設參數
contact_name huangsir
use generic-contact ---》這裏使用的模板就是模板文件中的contact定義
alias Nagios Admin
email 123456@139.com
}
將定義好的聯繫加到聯繫組中:
define contactgroup{ -----------》默認聯繫組
contactgroup_name admins
alias Nagios Administrators
members nagiosadmin
}
新增聯繫組:
define contactgroup{
contactgroup_name sas
alias Nagios Administrators
members huang,huangsir
}
而後修改模板中service的定義,將
contact_groups admins改成
contact_groups admins,sas
提示:根據不一樣的監控內容將報警設置分配不一樣,這時候模板就能夠刪掉這個參數,依次在監控的內容中定義須要報警的組便可,以下:
define service{
use generic-service
host_name nagios_server
service_description load
check_command check_nrpe!check_load
contact_groups admins,sas ---》單獨報警一個組也行,根據報警的重要性來分配
}
#######聯繫人的模板
vim templates.cfg
# Generic contact definition template - This is NOT a real contact, just a template!
define contact{
name generic-contact
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r,f,s
host_notification_options d,u,r,f,s
service_notification_commands notify-service-by-email 這個命令在commands.cfg配置文件中有定義
host_notification_commands notify-host-by-email
register 0
}
vim commands.cfg
定義了notify-service-by-email、notify-host-by-email這兩個命令,若是要使用短信報警
這裏就須要在後面用逗號隔開,再者是在commands配置文件中定義命令
新建菜鳥學習交流羣:584498750