我的服務器是阿里雲裏的服務器,由於服務器有限,因此以下操作都是在一臺服務器上進行的,也就是我自己監控我自己,不過無傷大雅,微信報警機器人還是會報警的。下面你準備好了麻,要開始辣~
監控環境準備
部署zabbix-server【監控端】
部署zabbix-agent【被監控端】
實現對web服務器監控【監控端】
zabbix 企業微信報警【監控端】
一.監控環境準備
1.1在你的服務器上添加安全組,HTTP端口號80、zabbix-server端口號10051、zabbix-agent端口號10050
zabbix-server和zabbix-agent的安全組規則類似
二.部署zabbix-server【監控端】
2.1 安裝zabbix-server
[root@bcf ~]# rpm -ivh http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-2.el7.noarch.rpm (先下載rpm包)
[root@bcf ~]# yum -y install zabbix-server-mysql zabbix-web-mysql zabbix-agent httpd mariadb mariadb-server
2.2 啓動mariadb
[root@bcf ~]# systemctl restart mariadb
[root@bcf ~]# systemctl enable mariadb // 設置開機自啓
2.3 準備數據庫及授權
[root@bcf ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 5.5.64-MariaDB MariaDB ServerCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)MariaDB [(none)]> grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix';
Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]> Ctrl-C -- exit!
Aborted
2.4 導入數據庫文件
[root@bcf ~]# zcat /usr/share/doc/zabbix-server-mysql-3.4.15/create.sql.gz | mysql -uzabbix -pzabbix zabbix
2.5 修改zabbix配置文件
[root@bcf ~]# vim /etc/zabbix/zabbix_server.conf
DBHost=localhost
DBName=zabbix
DBUser=zabbix
DBPassword=zabbix
注意刪除前面的空格
2.6 啓動zabbix-server
[root@bcf ~]# systemctl restart zabbix-server zabbix-agent
[root@bcf ~]# systemctl enable zabbix-server zabbix-agent
2.7 修改Apache配置文件,修改時區
[root@bcf ~]# vim /etc/httpd/conf.d/zabbix.conf
php_value date.timezone Asia/Shanghai
2.8 啓動Apache服務
[root@bcf ~]# systemctl restart httpd
[root@bcf ~]# systemctl enable httpd
2.9 訪問IP
打開瀏覽器選用公網ip的方式訪問: http://101.37.30.108/zabbix
安裝成功之後,用戶名爲:Admin 密碼爲:zabbix
三、部署zabbix-agent【被監控端】
由於我們只有一臺雲主機,我們就拿同一臺雲主機作爲被監控端
3.1 zabbix-agent配置
[root@bcf ~]# vim /etc/zabbix/zabbix_agentd.conf
Server=101.37.30.108 //zabbix-server IP 即監控端的IP
ServerActive=101.37.30.108 //zabbix-server IP 即監控端的IP
3.2 啓動zabbix-agent
[root@bcf ~]# systemctl restart zabbix-agent
[root@bcf ~]# systemctl enable zabbix-agent
四、 實現對web服務器監控【監控端】
4.1 添加【主機組】
4.2 添加主機
暫不點擊添加按鈕
4.3 關聯http模板
下一步一定要點擊,添加,否則不生效
上步做完,再點擊添加
4.4 查看或測試觸發器效果(如果是2臺服務器的話可以將被監控端的http服務down,然後在觸發器裏面可以看到效果,但是我們目前就一個服務器,如果把它down掉的話,也不能訪問這個IP了,所以就不測試了)
測試之前先將主機web1的觸發器的更新間隔改爲3或5s
五、 zabbix 微信報警【監控端】
5.1 下載企業微信【手機】,創建企業
5.2 企業登錄後臺【pc】
這裏注意可見範圍一定要有一個企業,否則可能報警失敗,下面需要用記事本保存一些有用的信息
應用:報警機器人
AgentID 1000004
Secret Ke3oLYC_svLJdBK3OX6qlATpZGYZY5hsN6FuYioDhYU
企業CorpID ww39ce87f07c13a896
部門id 1
5.4 python報警監控腳本
需要修改腳本中的四個信息:
self.__corpid = 'ww39ce87f07c13a896' //公司的corpid
self.__secret = 'Ke3oLYC_svLJdBK3OX6qlATpZGYZY5hsN6FuYioDhYU' //應用的secret
'toparty':1, //部門id
'agentid':"1000004", //應用id
沒有這個腳本的在/usr/lib/zabbix/alertscripts/的目錄下創建一個wechat.py文件,並給文件加上執行權限
[root@bcf ~]# cd /usr/lib/zabbix/alertscripts/
[root@bcf alertscripts]# ls
wechat.py
[root@bcf alertscripts]# chmod +x wechat.py
[root@bcf alertscripts]# vim wechat.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-import urllib,urllib2,json
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
class WeChat(object):
__token_id = ''
# init attribute
def __init__(self,url):
self.__url = url.rstrip('/')
self.__corpid = 'ww39ce87f07c13a896'
self.__secret = 'Ke3oLYC_svLJdBK3OX6qlATpZGYZY5hsN6FuYioDhYU'
# Get TokenID
def authID(self):
params = {'corpid':self.__corpid, 'corpsecret':self.__secret}
data = urllib.urlencode(params)
content = self.getToken(data)
try:
self.__token_id = content['access_token']
# print content['access_token']
except KeyError:
raise KeyError
# Establish a connection
def getToken(self,data,url_prefix='/'):
url = self.__url + url_prefix + 'gettoken?'
try:
response = urllib2.Request(url + data)
except KeyError:
raise KeyError
result = urllib2.urlopen(response)
content = json.loads(result.read())
return content# Get sendmessage url
def postData(self,data,url_prefix='/'):
url = self.__url + url_prefix + 'message/send?access_token=%s' % self.__token_id
request = urllib2.Request(url,data)
try:
result = urllib2.urlopen(request)
except urllib2.HTTPError as e:
if hasattr(e,'reason'):
print 'reason',e.reason
elif hasattr(e,'code'):
print 'code',e.code
return 0
else:
content = json.loads(result.read())
result.close()
return content# send message
def sendMessage(self,touser,message):
self.authID()
data = json.dumps({
'touser':touser,
'toparty':1,
'msgtype':"text",
'agentid':"1000004",
'text':{
'content':message
},
'safe':"0"
},ensure_ascii=False)
response = self.postData(data)
print responseif __name__ == '__main__':
a = WeChat('https://qyapi.weixin.qq.com/cgi-bin')
a.sendMessage(sys.argv[1],sys.argv[3])
監控腳本測試:
注:i爲上圖的企業微信名 test爲標題 2爲內容,標題和內容均可自定義
[root@bcf alertscripts]# ./wechat.py i test 2
結果是{u'invaliduser': u'', u'errcode': 0, u'errmsg': u'ok'}說明測試成功
5.6 zabbix 創建告警媒介
注:(以哪種方式發送報警信息,短信,腳本等等)
5.7 zabbix 添加告警用戶
創建用戶組:web-group,包含yangge、alice...等用戶
創建用戶:yangge、alice...等用戶,並關聯到微信告警
希望的結果是:發微信到組web-group,組中的用戶yangge、alice都能收到
設置用戶組web-group對主機羣組bcf-webserver擁有讀寫(發送消息)權限
5.8 Zabbix 添加告別警動作
如果希望中文顯示:
告警主機: {HOST.NAME}
告警地址: {HOST.IP}
監控項目: {ITEM.NAME}
告警等級: {TRIGGER.SEVERITY}
告警信息: {TRIGGER.NAME}
告警時間: {EVENT.DATE} {EVENT.TIME}
5.9 測試微信報警
被監控端操作
[root@bcf ~]# systemctl stop httpd
發現手機企業微信上發來以下短信
以上就是這個項目的全部內容辣,這個項目是千峯的石老師教我們的,還是叫飛哥親切,這4天的實訓中,非常感謝飛哥的付出,每次都會很認真的給我們每個人解決我們出現的問題,嘿嘿,飛哥辛苦辣~~,希望以後有緣江湖再見咯~