測試環境
主監控機:CentOS 7.5 192.168.131.10
被監控機:CentOS 6.2 192.168.131.20
安裝包:nagios-3.5.1.tar.gz
nrpe-2.15.tar.gz
nagios-plugins-2.1.1.tar.gz
主機:192.168.131.10配置
1.安裝依賴包
yum install -y gcc glibc glibc-common php gd gd-devel libpng libmng libjpeg zlib
systemctl start httpd
2.添加nagios用戶
useradd nagios
groupadd nagcmd
usermod -G nagcmd apache
3.二進制包安裝nagios
mkdir /opt/nagios
tar zxvf nagios-3.5.1.tar.gz -C /opt/nagios
cd /opt/nagios
./configure --with-command-group=nagcmd
make all
make install && make install-init && make install-commandmode && make install-config && make install-webconf
//make install #安裝生成/usr/local/nagios/,其中/usr/local/nagios/share即nagiosWEB 訪問界面的站點目錄
//make install-init #安裝生成/etc/rc.d/init.d/nagios 啓動腳本
//make install-config #安裝生成/usr/local/nagios/etc下的nagios相關配置文件
//make install-commandmode #設定相應nagios工作目錄的權限
//make install-webconf #安裝Nagios的WEB配置文件到Apache的conf.d目錄下
認識一下nagios的配置文件
vim /usr/local/nagios/etc/nagios.cfg
*logfile=/var/log/nagios/nagios.log #日誌位置
cfgfile=/etc/nagios/objects/commands.cfg #這個文件定義了很多命令
cfgfile=/etc/nagios/objects/contacts.cfg #定義聯繫人,怎麼聯繫
cfgfile=/etc/nagios/objects/timeperiods.cfg #定義了時間段
cfgfile=/etc/nagios/objects/templates.cfg #模板(聯繫人,主機,時間)
cfgfile=/etc/nagios/objects/localhost.cfg #監控本機相關配置文件
#cfgfile=/etc/nagios/objects/windows.cfg #windows,默認不監控
#cfgfile=/etc/nagios/objects/switch.cfg #交換機路由器監控,默認不監控
#cfgfile=/etc/nagios/objects/printer.cfg #打印機監控,默認不監控
#cfgdir=/etc/nagios/servers #定義了服務合集(多個使用)
#cfgdir=/etc/nagios/printers #定義了打印機合集(多個使用)
#cfgdir=/etc/nagios/switches #定義了交換合集(多個使用)
#cfgdir=/etc/nagios/routers #定義了路由合集(多個使用)
resourcefile=/etc/nagios/private/resource.cfg 資源變量配置文件,包括$USER1$變量(一個路徑)等
statusupdateinterval=10 #狀態更新時間,單位s
logrotationmethod=d #日誌滾動,默認天
servicechecktimeout=60 #服務檢查超時時間
hostchecktimeout=30 #主機檢查超時時間
eventhandlertimeout=30
notificationtimeout=30
ocsptimeout=5
perfdatatimeout=5**
vim /usr/local/nagios/etc/cgi.cfg
//此配置文件比nagios.cfg優先級高
main_config_file= /usr/local/nagios/etc/nagios.cfg #主配置文件
physical_html_path= /usr/local/nagios/share #物理路徑
url_html_path=/nagios #在URL後面加上/nagios才能訪問
use_authentication=1 #使用認證
use_ssl_authentication=0 #不使用ssl
authorized_for_system_information=nagiosadmin #認證用戶
Nagios訪問測試
systemctl restart httpd
訪問地址
http://192.168.131.10/nagios
提示需要賬戶和密碼
vi /etc/httpd/conf.d/nagios.conf
htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
systemctl restart httpd
通過-v參數檢測配置文件是否有問題
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
兩者參數都爲0,表示沒有錯誤,以守護進程方式啓動
/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
安裝nagios-plugins插件
爲了能夠監控mysql,我們要在主監控機上安裝mysql客戶端
yum install mysql mysql-devel
tar xvf nagios-plugins-2.1.1.tar.gz -C /opt/nagios
cd /opt/nagios/nagios-plugins-2.1.1
./configure --with-nagios-user=nagios --with-nagios-group=nagcmd
make && make install
重啓nagios
systemctl restart httpd; /etc/init.d/nagios restart
瀏覽器測試:登陸之後,點擊hosts,可以看到,默認監控的是本機,說明環境
nagios監控本機服務器
監控本地nfs狀態
yum -y install nfs-utils
cd /usr/local/nagios/etc/objects
vi localhost.cfg
*define service{
use local-service
hostname localhost
servicedescription NFS
checkcommand checktcp!2049
}
#在使用check命令之前,要確保下/usr/local/nagios/libexec/ 目錄下有沒有你需要的check命令,如果有,直接調用,如果沒有,檢測端口來代替,檢測格式: checktcp!端口號*
重啓nagios
/etc/init.d/nagios restart
修改主題的話 把相應主題包解壓到 /usr/local/nagios/share/目錄下,重啓nagios就可以生效
unzip vautour_style.zip -d /usr/local/nagios/share/
nagios監控遠程mysql
在主監控機192.168.131.10上配置nagios服務
vi /usr/local/nagios/etc/nagios.cfg
cfgfile=/usr/local/nagios/etc/objects/localhost.cfg
#在這兩行下面添加下面兩行
cfgfile=/usr/local/nagios/etc/objects/hosts.cfg
cfgfile=/usr/local/nagios/etc/objects/service.cfg
vi /usr/local/nagios/etc/objects/hosts.cfg
define host{
use linux-server #使用的模板名字
hostname sep-mysql #主機名
address 192.168.131.20 #IP地址
}
vi /usr/local/nagios/etc/objects/service.cfg
define service{
use local-service
hostname sep-mysql
servicedescription MySqlSev
checkcommand checkmysql
}
vi /usr/local/nagios/etc/objects/commands.cfg
define command{
commandname checkmysql
commandline $USER1$/checkmysql -H $HOSTADDRESS$ -u nagios –p123456 -d nagios
}
本機通過該命令測試
/usr/local/nagios/libexec/check_mysql -H192.168.131.20 -unagios -p123456 -d nagios
通過nrpe監控外部主機私有信息
客戶端的NRPE安裝
1:安裝nagios- plugins插件
2:安裝NRPE( make install-daemon-config make install-xinetd)
3:配置我們xinted服務,允許誰來拿數據
4:啓動xinetd
5:查看端口是否正常啓動
解決依賴 監控機和被監控機都安裝
yum install -y openssl openssl-devel
服務端安裝NRPE
tar xvf nrpe-2.15.tar.gz -C /opt/nagios
cd /opt/nagios/nrpe-2.15/
./configure && make && make install
make install-plugin && make install-daemon
#前者安裝插件,後者以守護經常來運行
ls /usr/local/nagios/libexec/check_nrpe #這個命令需要安裝nrpe之後纔有
客戶端配置
useradd -s /sbin/nologin nagios
groupadd nagcmd
usermod -G nagcmd nagios
yum install -y xinetd
tar xvf nagios-plugins-2.1.1.tar.gz -C /opt/nagios
tar xvf nrpe-2.15.tar.gz -C /opt/nagios
cd /opt/nagios/nagios-plugins-2.1.1/
./configure && make && make install
cd ../nrpe-2.15/
./configure && make && make install
make install-daemon-config //這裏是客戶端,不用make-install-plugin
make install-xinetd
vim /etc/xinetd.d/nrpe
service nrpe
{
flags = REUSE
socket_type = stream
port =5666
wait = no
user = nagios
group = nagios
server =/usr/local/nagios/bin/nrpe
server_args = -c /usr/local/nagios/etc/nrpe.cfg --inetd
log_on_failure += USERID
disable = no
only_from =127.0.0.1 192.168.131.10
}
echo "nrpe 5666/tcp # NRPE" >> /etc/services //端口註冊
netstat -antup | grep 5666
tcp 0 0:::5666 :::* LISTEN 68667/xinetd
客戶端nrpe命令
vim /usr/local/nagios/etc/nrpe.cfg
command[check_sdc1]=/usr/local/nagios/libexec/check_disk -w 38% -c 35% -p /dev/sdc1
#-w爲警告 -C爲告急
systemctl restart xinetd
服務端手動測試
/usr/local/nagios/libexec/check_nrpe -H192.168.131.20
NRPE v2.15
[root@xuegod63 ~]# /usr/local/nagios/libexec/check_nrpe -H192.168.131.20 -c check_sdc1
定義被監控主機
cd /usr/local/nagios/etc/objects/
vim hosts.cfg
define host{
use linux-server
host_name sep-mysql
address 192.168.131.20
}
定義需要監控的服務
[root@xuegod63 objects]# vim service.cfg
define service{
use local-service
host_name sep-mysql
service_description sdac_Partition
check_command check_nrpe!check_sdc1
}
定義NRPE監控命令
vim commands.cfg
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
$ARG1$ 表示調用後面的參數。
通過http://www.onealert.com/設置報警
點擊配置有相應的操作方式
這邊配置微信綁定和設置QQ郵箱 就可以收到報警通知