您好,登錄后才能下訂單哦!
這篇文章主要介紹了nagios整合cact實現網絡監控報警的方法,具有一定借鑒價值,需要的朋友可以參考下。如下資料是關于實現網絡監控報警的詳細步驟內容。
系統環境:rhel6.3
selinux and iptables disabled
整合cacti和nagios是利用了cacti的一個插件nagios for cacti,它的原理是將nagios的數據通過ndo2db導入到mysql數據庫(cacti的庫中),然后cacti讀取數據庫信息將nagios的結果展示出來。
一、nagios監控本地主機
localhost.cfg中主機與服務是在一塊的,為了便于管理,將主機與服務的配置信息分隔開。注釋掉localhost.cfg,新增加hosts.cfg,services.cfg文件:
[root@server34 etc]#vim /usr/local/nagios/etc/nagios.cfg
# You can specify individual object config files as shown below:
cfg_file=/usr/local/nagios/etc/objects/commands.cfg
cfg_file=/usr/local/nagios/etc/objects/contacts.cfg
cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg
cfg_file=/usr/local/nagios/etc/objects/templates.cfg
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg
cfg_file=/usr/local/nagios/etc/objects/services.cfg
# Definitions for monitoring the local (Linux) host
#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg
更改主機:
[root@server34 objects]# cp localhost.cfg hosts.cfg
[root@server34 objects]# vim hosts.cfg
define host{
use linux-server
host_name server34.example.com
alias Manager
address 192.168.0.34
icon_p_w_picpath server.gif
statusmap_p_w_picpath server.gd2
2d_coords500,200
3d_coords500,200,100
}
###############################################################################
###############################################################################
#
# HOST GROUP DEFINITION
#
###############################################################################
###############################################################################
# Define an optional hostgroup for Linux machines
define hostgroup{
hostgroup_name linux-servers ; The name of the hostgroup
alias Linux Servers ; Long name of the group
members * ; Comma separated list of hosts that belong to this group
}
[root@server34 objects]# cp localhost.cfg services.cfg
[root@server34 objects]# vim hosts.cfg
define host{
use linux-server
host_name server34.example.com
alias Manager
address 192.168.0.34
icon_p_w_picpath server.gif
statusmap_p_w_picpath server.gd2
2d_coords500,200
3d_coords500,200,100
}
###############################################################################
###############################################################################
#
# HOST GROUP DEFINITION
#
###############################################################################
###############################################################################
# Define an optional hostgroup for Linux machines
define hostgroup{
hostgroup_name linux-servers ; The name of the hostgroup
alias Linux Servers ; Long name of the group
members * ; Comma separated list of hosts that belong to this group
}
[root@server34 objects]# vim services.cfg
define servicegroup{
servicegroup_name 系統負荷檢查
alias 負荷檢查
members server34.example.com,進程總數,server34.example.com,登錄用戶數,server34.example.com,根分區,server34.example.com,交換空間利用率
}
define servicegroup{
servicegroup_name 全部聯通性檢查
alias 聯通性檢查
members server34.example.com,PING
}
###############################################################################
###############################################################################
#
# SERVICE DEFINITIONS
#
###############################################################################
###############################################################################
# Define a service to "ping" the local machine
define service{
use local-service ; Name of service template to use
host_name *
service_description PING
check_commandcheck_ping!100.0,20%!500.0,60%
}
# Define a service to check the disk space of the root partition
# on the local machine. Warning if < 20% free, critical if
# < 10% free space on partition.
define service{
use local-service ; Name of service template to use
host_name server34.example.com
service_description 根分區
check_commandcheck_local_disk!20%!10%!/
}
# Define a service to check the number of currently logged in
# users on the local machine. Warning if > 20 users, critical
# if > 50 users.
define service{
use local-service ; Name of service template to use
host_name server34.example.com
service_description 登錄用戶數
check_commandcheck_local_users!20!50
}
# Define a service to check the number of currently running procs
# on the local machine. Warning if > 250 processes, critical if
# > 400 users.
define service{
use local-service ; Name of service template to use
host_name server34.example.com
service_description 進程總數
check_commandcheck_local_procs!250!400!RSZDT
}
# Define a service to check the load on the local machine.
define service{
use local-service ; Name of service template to use
host_name server34.example.com
service_description 系統負荷
check_commandcheck_local_load!5.0,4.0,3.0!10.0,6.0,4.0
}
# Define a service to check the swap usage the local machine.
# Critical if less than 10% of swap is free, warning if less than 20% is free
define service{
use local-service ; Name of service template to use
host_name server34.example.com
service_description 交換空間利用率
check_commandcheck_local_swap!20!10
}
# Define a service to check SSH on the local machine.
# Disable notifications for this service by default, as not all users may have SSH enabled.
define service{
use local-service ; Name of service template to use
host_name server34.example.com
service_description SSH
check_commandcheck_tcp!22!1.0!10.0
notifications_enabled0
}
# Define a service to check HTTP on the local machine.
# Disable notifications for this service by default, as not all users may have HTTP enabled.
define service{
use local-service ; Name of service template to use
host_name server34.example.com
service_description HTTP
check_commandcheck_http
notifications_enabled0
}
檢測nagios的配置文件
[root@server34 objects]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
[root@server34 objects]# /etc/init.d/nagios reload
http://192.168.0.34/nagios
點擊服務,看到以上配置的服務則證明配置成功
二、nagios監控遠程主機
1、監控遠程主機的網絡服務
監控遠程主機的網絡服務(如監控mysql服務等)
監控主機:192.168.0.34
遠程主機(被監控主機):192.168.0.17
rhel6.4 selinux disabled and iptables
此處以監控遠程主機的mysql服務為例來說明nagios是如何監控遠程主機的網絡服務
安裝mysql
[root@server17 ~]# yum install mysql -y
啟動mysql
創建nagios數據庫,并給nagios用戶授予在監控主機上登錄nagios庫的權限。
mysql> create database nagios;
mysql> grant select on nagios.* to nagios@'192.168.0.34' identified by 'westos';
檢測能否采集到信息:
[root@server34 libexec]# ./check_mysql -H 192.168.0.17 -u nagios -p westos -d nagios
Uptime: 1648 Threads: 1 Questions: 15 Slow queries: 0 Opens: 15 Flush tables: 1 Open tables: 8 Queries per second avg: 0.9|Connections=8c;;; Open_files=16;;; Open_tables=8;;; Qcache_free_memory=0;;; Qcache_hits=0c;;; Qcache_inserts=0c;;; Qcache_lowmem_prunes=0c;;; Qcache_not_cached=0c;;; Qcache_queries_in_cache=0;;; Queries=15c;;; Questions=15c;;; Table_locks_waited=0c;;; Threads_connected=1;;; Threads_running=1;;; Uptime=1648c;;;
[root@server34 libexec]# pwd
/usr/local/nagios/libexec
添加檢測命令:
[root@server34 objects]# vim commands.cfg
# 'check_mysql' command definition
define command{
command_name check_mysql
command_line $USER1$/check_mysql -H $HOSTADDRESS$ -u $ARG1$ -p $ARG2$ -d $ARG3$
}
添加主機:
[root@server34 objects]# vim hosts.cfg
define host{
use linux-server
host_name server17.example.com
alias mysql
address 192.168.0.17
icon_p_w_picpath server.gif
statusmap_p_w_picpath server.gd2
2d_coords 600,300
3d_coords 600,300,100
}
添加服務:
[root@server34 objects]# vim services.cfg ###添加服務組
define servicegroup{
servicegroup_name 全部聯通性檢查
alias 聯通性檢查
members server34.example.com,PING,server17.example.com,PING
}
################################check_mysql#################################
define service{ ###添加服務
use local-service ; Name of service template to use
host_name server17.example.com
service_description check_mysql
check_command check_mysql!nagios!westos!nagios
}
驗證配置文件更改是否正確:
[root@server34 objects]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
重新加載nagios:
[root@server34 objects]# /etc/init.d/nagios reload
檢測:http://192.168.0.34/nagios
點擊主機->出現server17.example.com
點擊服務->出現check_mysql服務
2、通過nrpe監控遠程主機的基本信息(如磁盤的使用情況等)
在被監控主機上操作
下載nagios插件包、nrpe包
nagios-plugins-1.5.tar.gz, nrpe-2.15.tar.gz
[root@server17 ~]# tar zxf nagios-plugins-1.5.tar.gz
創建nagios用戶
[root@server17 ~]# useradd nagios
編譯安裝nagios-plugin
[root@server17 ~]# cd nagios-plugins-1.5
[root@server17 nagios-plugins-1.5]# ./configure --with-nagios-user=nagios --with-nagios-user=nagios
[root@server17 nagios-plugins-1.5]# make && make install
編譯安裝nrpe,nrpe是守護進程,需安裝更改xinetd
[root@server17 nrpe-2.15]# yum install xinetd -y
[root@server17 nrpe-2.15]# vim /etc/xinetd.d/nrpe
only_from = 192.168.0.34
[root@server17 nrpe-2.15]# vim /etc/services
nrpe 5666/tcp
[root@server17 nrpe-2.15]# ./configure
[root@server17 nrpe-2.15]# make all
[root@server17 nrpe-2.15]# make install-plugin
[root@server17 nrpe-2.15]# make install-daemon
[root@server17 nrpe-2.15]# make install-daemon-config
[root@server17 nrpe-2.15]# make install-xinetd
[root@server17 nrpe-2.15]# /etc/init.d/xinetd start
配置nrpe
[root@server34 etc]# vim nrpe.cfg
command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
command[check_hda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/hda1
command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200
[root@server17 ~]# /etc/init.d/xinetd start
##########################################
在監控主機上
下載nrpe包:
nrpe-2.15.tar.gz
[root@server17 nrpe-2.15]# ./configure
[root@server17 nrpe-2.15]# make all
[root@server17 nrpe-2.15]# make install-plugin
[root@server17 nrpe-2.15]# make install-daemon
[root@server17 nrpe-2.15]# make install-daemon-config
安裝檢測:
[root@server34 libexec]# ./check_nrpe -H 192.168.0.17
NRPE v2.15
檢測磁盤使用情況:
[root@server34 libexec]# ./check_nrpe -H 192.168.0.17 -c check_disk
DISK OK - free space: / 14459 MB (85% inode=91%);| /=2540MB;14327;16118;0;17909
添加check_nrpe檢測命令
[root@server34 objects]# vim commands.cfg
# 'check_nrpe' command definition
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
[root@server34 objects]# vim services.cfg
define servicegroup{
servicegroup_name 系統負荷檢查
alias 負荷檢查
members server34.example.com,進程總數,server34.example.com,登錄用戶數,server34.example.com,根分區,server34.example.com,交換空間利用率
}
define servicegroup{
servicegroup_name 全部聯通性檢查
alias 聯通性檢查
members server34.example.com,PING,server17.example.com,PING
}
配置檢測:
[root@server34 objects]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
檢測:
http ://192.168.0.34/nagios
點擊服務,查看結果
三、整合nagios與微信、飛信
nagios與微信、飛信整合,使得nagios的監控報警具有了錦上添花的效果。這樣,管理員不用經常性的查看系統運行是否正常,磁盤使用情況等等,有什么異常情況,微信、飛信會第一時間的通知你,可以達到7*24小時的監控。你要做的就是手機保持暢通就ok了。既然這么好,那大家就跟隨我一塊愉快的感受下吧!
首先打開相關服務:
[root@server34 objects]# /etc/init.d/nagios start
[root@server34 objects]# /etc/init.d/httpd start
[root@server34 objects]# /etc/init.d/mysqld start
檢測nagios@server34.example.com是否能收發郵件
[root@server34 objects]# mail nagios@server34.example.com
切換到nagios用戶,查看到以下郵件時則成功
[root@server34 objects]# su - nagios
-bash-4.1$ mail
Heirloom Mail version 12.4 7/29/08. Type ? for help.
"/var/spool/mail/nagios": 1 message
> 1 root Sun Apr 27 09:54 20/642 "dsdssds"
&
一般自定義域名會被QQ郵箱當作垃圾郵件處理,在QQ郵箱中設置白名單
-bash-4.1$ ping mail.qq.com //檢測網絡是否暢通
PING mail.qq.com (14.17.32.178) 56(84) bytes of data.
64 bytes from 14.17.32.178: icmp_seq=1 ttl=53 time=54.4 ms
64 bytes from 14.17.32.178: icmp_seq=2 ttl=53 time=1088 ms
添加郵箱:
[root@server34 objects]# vim contacts.cfg
email 281588847@qq.com
[root@server34 objects]# /etc/init.d/nagios reload
下載fetion,和飛信的庫文件linuxso_20101113.tar.gz
[root@server34 ~]# tar zxf linuxso_20101113.tar.gz -C /lib^C
[root@server34 ~]# cp fetion /usr/local/nagios/libexec/
給以下文件加可執行權限
[root@server34 libexec]# chmod +x libACE*^C
[root@server34 libexec]# chmod +x libcrypto.so.4 libssl.so.4
[root@server34 libexec]# chmod nagios.nagios fetion
[root@server34 libexec]# su - nagios
根據提示的錯誤依次安裝以下軟件包:
-bash-4.1$ /usr/local/nagios/libexec/fetion
-bash: /usr/local/nagios/libexec/fetion: /lib/ld-linux.so.2: bad ELF interpreter: No such file or directory
[root@server34 ~]# yum install /lib/ld-linux.so.2 libstdc++.so.6 libgssapi_krb5.so.2 libz.so.1 -y
-bash-4.1$ /usr/local/nagios/libexec/fetion --mobile=1870924**** --pwd=****** --to=187********,187******** --msg-utf8="hahahahhhhhhhhhhhaaaa"//第一次發送時會要求輸入校驗碼,以后發送時則不需要
為了查看校驗碼的內容,將校驗碼拷貝到物理主機的桌面上
[root@server34 libexec]# scp 18709242146.jpg 192.168.0.39:/root/Desktop
整合飛信與nagios
因發送的對象與內容可能發生改變,故用腳本實現則更為方便,以下是實現該功能的腳本:
-bash-4.1$ vim fetion.sh
/usr/local/nagios/libexec/fetion --mobile=187******** --pwd=****** --to="$1" --msg-utf8="$2" &>/dev/null
給fetion.sh加可執行權限
-bash-4.1$ chmod +x fetion.sh
測試:
-bash-4.1$ /usr/local/nagios/libexec/fetion.sh 1870924**** "westos linux"
-bash-4.1$ vim contacts.cfg
define contact{
contact_name nagiosadmin ; Short name of user
use generic-contact ; Inherit default values from generic-contact template (defined above)
alias Nagios Admin ; Full name of user
email 281588847@qq.com ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
pager 1870924****
}
-bash-4.1$ vim commands.cfg
# 'notify-host-by-fetion' command definition
define command{
command_name notify-host-by-fetion
command_line $USER1$/fetion.sh $CONTACTPAGER$ " $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$"
}
# 'notify-service-by-FETION' command definition
define command{
command_name notify-service-by-fetion
command_line $USER1$/fetion.sh $CONTACTPAGER$ " $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$"
}
-bash-4.1$ vim templates.cfg
service_notification_commands notify-service-by-email,notify-service-by-fetion ; send service notifications via email
host_notification_commands notify-host-by-email,notify-host-by-fetion ; send host notifications via email
檢測配置文件
[root@server34 objects]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
測試:
[root@server17 ~]# /etc/init.d/mysqld stop
Stopping mysqld: [ OK ]
當收到飛信報警信息時配置成功
看完這篇文章,你們學會nagios整合cact實現網絡監控報警的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。