Nagios 除了監控功能很強大之外,它的報警功能也很不錯。從運維的角度來講,報警一般就是郵件和短信報警這兩種方式,這次實驗實現的是Nagios 的郵件報警功能。
1、 安裝sendmail 組件
這里要確保sendmail 相關組件的完整安裝,
[root@server ~]# yum install sendmail* mailx
安裝完成后重啟sendmail 服務
[root@server ~]# service sendmail restart
Shutting down sendmail: [ OK ]
Starting sendmail: [ OK ]
Starting sm-client: [ OK ]
測試sendmail 發信情況
[root@server ~]# /bin/mail -s "`date +%Y-%m-%d` Server SMS Test" 159158*****@163.com
hello
admin
.
EOT
2、 郵件報警的配置
一般而言,如果在整個系統中只有一個管理員,那么只需要在contact.cfg文件中,添加一下管理員的郵箱即可。但如果監控的內容中服務器有單獨的管理員,那就需要定義多個contact(聯系人),然后再通過contactgroup(聯系組)對各個contact 進行分組。
(1) 修改contact.cfg
[root@server ~]# vim /usr/local/nagios/etc/objects/contacts.cfg
define contact{
contact_name nagios
use generic-contact
alias Nagios
email 159158*****@163.com,159158*****@139.com // 改為自己的郵箱地址即可
}
define contactgroup{
contactgroup_name admins
alias Nagios Administrators
members nagios
}
(2) 設置報警方式
[root@server ~]# vim /usr/local/nagios/etc/objects/templates.cfg
define contact{
name generic-contact ; The name of this contact template
service_notification_period 24x7 ; service notifications can be sent anytime
host_notification_period 24x7 ; host notifications can be sent anytime
service_notification_options w,u,c,r,f,s ; send notifications for all service states, flapping events, and scheduled downtime events
host_notification_options d,u,r,f,s ; send notifications for all host states, flapping events, and scheduled downtime events
service_notification_commands notify-service-by-email ; send service notifications via email
host_notification_commands notify-host-by-email ; send host notifications via email
register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL CONTACT, JUST A TEMPLATE!
}
(3) 修改commands.cfg
[root@server ~]# vim /usr/local/nagios/etc/objects/commands.cfg
# 'notify-host-by-email' command definition
define command{
command_name notify-host-by-email
command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$
}
# 'notify-service-by-email' command definition
define command{
command_name notify-service-by-email
command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$" | /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
}
看到這里已經默認配置好,我們不用修改直接調用就行了。
(4) 通過-v參數檢查配置無誤之后,重啟nagios
[root@server ~]# service nagios restart
Running configuration check...done.
Stopping nagios: .done.
Starting nagios: done.
(5) 郵箱設置
這里以139郵箱為例,設置收信
點擊左上角“設置”可以看到這個版塊
點擊“手機通知”,打開郵箱到達通知,設置接收時間段,設置完保存。
返回到這個版塊,點擊“白名單設置”,將服務器域名設置進白名單
(6) 測試收信情況
可以隨便關閉客戶機上的一個服務做測試
設置郵件到達提醒后,還可以收到告警短信,效果如下:(需要設置成長短信的形式)
PS:139郵箱的短信報警免費是好,但是有時延,這里提供另外一種方式報警——微信告警
可以QQ郵箱將域名設置進白名單,然后微信設置QQ郵箱推送即可(由于設置簡單,這里就不演示了)效果如下:
另外有需要云服務器可以了解下創新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
新聞名稱:Nagios(七)——Nagios郵件,短信告警-創新互聯
標題來源:http://newbst.com/article34/hhgpe.html
成都網站建設公司_創新互聯,為您提供微信小程序、響應式網站、定制網站、品牌網站建設、建站公司、品牌網站設計
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯