軟件說明
軟件 | 版本 | 備注 |
---|---|---|
zabbix-server-mysql | 5.0.2 | 宿主機部署 |
zabbix-agent | 5.0.2 | 宿主機部署 |
zabbix-web-nginx-mysql | 5.0.2 | docker方式部署 |
mysql | 8.0.21 | 宿主機部署 |
grafana | 6.6.2 | 宿主機部署 |
最終效果預覽
安裝MySQL
✏️ 下載軟件包
[root@tfryb-04-30-02 ~]# mkdir /data/tools
[root@tfryb-04-30-02 ~]# cd /data/tools/
[root@tfryb-04-30-02 tools]# cat << EOF >download.sh
#!/bin/bash
wget https://mirrors.huaweicloud.com/mysql/Downloads/MySQL-8.0/mysql-community-client-8.0.21-1.el7.x86_64.rpm
wget https://mirrors.huaweicloud.com/mysql/Downloads/MySQL-8.0/mysql-community-devel-8.0.21-1.el7.x86_64.rpm
wget https://mirrors.huaweicloud.com/mysql/Downloads/MySQL-8.0/mysql-community-embedded-compat-8.0.21-1.el7.x86_64.rpm
wget https://mirrors.huaweicloud.com/mysql/Downloads/MySQL-8.0/mysql-community-libs-8.0.21-1.el7.x86_64.rpm
wget https://mirrors.huaweicloud.com/mysql/Downloads/MySQL-8.0/mysql-community-libs-compat-8.0.21-1.el7.x86_64.rpm
wget https://mirrors.huaweicloud.com/mysql/Downloads/MySQL-8.0/mysql-community-server-8.0.21-1.el7.x86_64.rpm
wget https://mirrors.huaweicloud.com/mysql/Downloads/MySQL-8.0/mysql-community-common-8.0.21-1.el7.x86_64.rpm
wget https://mirrors.huaweicloud.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-server-mysql-5.0.2-1.el7.x86_64.rpm
wget https://mirrors.huaweicloud.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-get-5.0.2-1.el7.x86_64.rpm
EOF
[root@tfryb-04-30-02 tools]# bash download.sh
✏️ 安裝
[root@tfryb-04-30-02 tools]# yum localinstall ./mysql-community-* -y
✏️ 修改配置文件(更改數據存放路徑)
[root@tfryb-04-30-02 ~]# grep -v -E '^#|^$' /etc/my.cnf
[mysqld]
default-storage-engine=InnoDB
datadir=/data/mysql/
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
✏️ 啟動
[root@tfryb-04-30-02 tools]# systemctl start mysqld
[root@tfryb-04-30-02 tools]# systemctl enable mysqld
✏️ 修改密碼
[root@tfryb-04-30-02 tools]# grep password /var/log/mysqld.log #過濾出默認密碼
2020-09-07T06:22:11.617861Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: Nyf9I2Eglh/q
[root@tfryb-04-30-02 tools]# mysql -u root -p
Enter password:
......
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY "Zabbix#qaz#123";
Query OK, 0 rows affected (0.01 sec)
✏️ 創建zabbix庫和用戶
mysql> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.01 sec)
mysql> create user 'zabbix'@'%' identified by 'Zabbix@123';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all privileges on zabbix.* to 'zabbix'@'%' with grant option;
Query OK, 0 rows affected (0.00 sec)
mysql> alter user 'zabbix'@'%' identified with mysql_native_password by "Zabbix@123";
Query OK, 0 rows affected (0.00 sec)
mysql> select user,host,plugin from mysql.user;
+------------------+-----------+-----------------------+
| user | host | plugin |
+------------------+-----------+-----------------------+
| zabbix | % | mysql_native_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session | localhost | caching_sha2_password |
| mysql.sys | localhost | caching_sha2_password |
| root | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+
5 rows in set (0.00 sec)
安裝zabbix-server
✏️ 安裝依賴fping
# 安裝依賴fping
[root@tfryb-04-30-02 tools]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@tfryb-04-30-02 tools]# yum install fping -y
✏️ 安裝zabbix
[root@tfryb-04-30-02 tools]# yum localinstall zabbix-server-mysql-5.0.2-1.el7.x86_64.rpm -y
[root@tfryb-04-30-02 tools]# yum localinstall zabbix-get-5.0.2-1.el7.x86_64.rpm -y
✏️ 配置zabbix
[root@tfryb-04-30-02 ~]# cp /etc/zabbix/zabbix_server.conf{,.bck}
[root@tfryb-04-30-02 ~]# grep -v -E "^#|^$" /etc/zabbix/zabbix_server.conf
LogFile=/var/log/zabbix/zabbix_server.log
LogFileSize=0
PidFile=/var/run/zabbix/zabbix_server.pid
SocketDir=/var/run/zabbix
DBName=zabbix
DBUser=zabbix
DBPassword=Zabbix@123
SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
Timeout=4
AlertScriptsPath=/usr/lib/zabbix/alertscripts
ExternalScripts=/usr/lib/zabbix/externalscripts
LogSlowQueries=3000
StatsAllowedIP=127.0.0.1
✏️ 導入zabbix
數據庫文件
[root@tfryb-04-30-02 ~]# zcat /usr/share/doc/zabbix-server-mysql-5.0.2/create.sql.gz |mysql -uroot -pYotta#qaz#123 zabbix
[root@tfryb-04-30-02 ~]# mysql -u root -pYotta#qaz#123 -e "show tables from zabbix"
mysql: [Warning] Using a password on the command line interface can be insecure.
+----------------------------+
| Tables_in_zabbix |
+----------------------------+
| acknowledges |
| actions |
| alerts |
| application_discovery |
| application_prototype |
| application_template |
| applications |
| auditlog |
| auditlog_details |
| autoreg_host |
| conditions |
| config |
| config_autoreg_tls |
| corr_condition |
| corr_condition_group |
| corr_condition_tag |
| corr_condition_tagpair |
| corr_condition_tagvalue |
| corr_operation |
| correlation |
| dashboard |
| dashboard_user |
| dashboard_usrgrp |
| dbversion |
| dchecks |
| dhosts |
| drules |
| dservices |
| escalations |
| event_recovery |
| event_suppress |
| event_tag |
| events |
| expressions |
| functions |
| globalmacro |
| globalvars |
| graph_discovery |
| graph_theme |
| graphs |
| graphs_items |
| group_discovery |
| group_prototype |
| history |
| history_log |
| history_str |
| history_text |
| history_uint |
| host_discovery |
| host_inventory |
| host_tag |
| hostmacro |
| hosts |
| hosts_groups |
| hosts_templates |
| housekeeper |
| hstgrp |
| httpstep |
| httpstep_field |
| httpstepitem |
| httptest |
| httptest_field |
| httptestitem |
| icon_map |
| icon_mapping |
| ids |
| images |
| interface |
| interface_discovery |
| interface_snmp |
| item_application_prototype |
| item_condition |
| item_discovery |
| item_preproc |
| item_rtdata |
| items |
| items_applications |
| lld_macro_path |
| lld_override |
| lld_override_condition |
| lld_override_opdiscover |
| lld_override_operation |
| lld_override_ophistory |
| lld_override_opinventory |
| lld_override_opperiod |
| lld_override_opseverity |
| lld_override_opstatus |
| lld_override_optag |
| lld_override_optemplate |
| lld_override_optrends |
| maintenance_tag |
| maintenances |
| maintenances_groups |
| maintenances_hosts |
| maintenances_windows |
| mappings |
| media |
| media_type |
| media_type_message |
| media_type_param |
| module |
| opcommand |
| opcommand_grp |
| opcommand_hst |
| opconditions |
| operations |
| opgroup |
| opinventory |
| opmessage |
| opmessage_grp |
| opmessage_usr |
| optemplate |
| problem |
| problem_tag |
| profiles |
| proxy_autoreg_host |
| proxy_dhistory |
| proxy_history |
| regexps |
| rights |
| screen_user |
| screen_usrgrp |
| screens |
| screens_items |
| scripts |
| service_alarms |
| services |
| services_links |
| services_times |
| sessions |
| slides |
| slideshow_user |
| slideshow_usrgrp |
| slideshows |
| sysmap_element_trigger |
| sysmap_element_url |
| sysmap_shape |
| sysmap_url |
| sysmap_user |
| sysmap_usrgrp |
| sysmaps |
| sysmaps_elements |
| sysmaps_link_triggers |
| sysmaps_links |
| tag_filter |
| task |
| task_acknowledge |
| task_check_now |
| task_close_problem |
| task_data |
| task_remote_command |
| task_remote_command_result |
| task_result |
| timeperiods |
| trends |
| trends_uint |
| trigger_depends |
| trigger_discovery |
| trigger_tag |
| triggers |
| users |
| users_groups |
| usrgrp |
| valuemaps |
| widget |
| widget_field |
+----------------------------+
✏️ 啟動
[root@tfryb-04-30-02 ~]# systemctl start zabbix-server.service
[root@tfryb-04-30-02 ~]# systemctl enable zabbix-server.service
安裝docker
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum -y install docker-ce
sed -i 's#ExecStart=/usr/bin/dockerd.*#ExecStart=/usr/bin/dockerd#' /usr/lib/systemd/system/docker.service
mkdir /etc/docker /data/docker
cat >/etc/docker/daemon.json<<EOF
{
"registry-mirrors": ["https://15y9mzqb.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"],
"graph": "/data/docker"
}
EOF
systemctl daemon-reload
systemctl enable docker
systemctl start docker
systemctl status docker
安裝zabbix-web
✏️ 下載鏡像
[root@tfryb-04-30-02 ~]# docker pull zabbix/zabbix-web-nginx-mysql:centos-5.0.2
[root@tfryb-04-30-02 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
zabbix/zabbix-web-nginx-mysql centos-5.0.2 c552ec85c6c8 7 weeks ago 439MB
✏️ 啟動zabbix-web
[root@tfryb-04-30-02 ~]# docker run --name zabbix-web-nginx-mysql \
-e DB_SERVER_HOST="172.24.xxx.xxx" \
-e MYSQL_DATABASE="zabbix" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="Zabbix@123" \
-e ZBX_SERVER_HOST="172.24.xxx.xxx" \
-e PHP_TZ="Asia/Shanghai" \
-p 80:8080 \
-d zabbix/zabbix-web-nginx-mysql:centos-5.0.2
[root@tfryb-04-30-02 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a3291b17f364 zabbix/zabbix-web-nginx-mysql:centos-5.0.2 "docker-entrypoint.sh" 3 seconds ago Up 10 minutes 8443/tcp, 0.0.0.0:80->8080/tcp zabbix-web-nginx-mysql
✏️ Web訪問 http://172.24.xxx.xxx
,默認賬戶:Admin
密碼:zabbix
解決亂碼問題
# 1.上傳字體到服務器
[root@tfryb-04-30-02 ~]# ls simkai.ttf
simkai.ttf
# 2.將字體copy到容器中
[root@tfryb-04-30-02 ~]# docker cp simkai.ttf zabbix-web-nginx-mysql:/usr/share/zabbix/assets/fonts/
# 3.進入容器中進行配置
[root@tfryb-04-30-02 ~]# docker exec -it zabbix-web-nginx-mysql /bin/bash
bash-4.4$ ls /usr/share/zabbix/assets/fonts/
DejaVuSans.ttf simkai.ttf
bash-4.4$ vi /usr/share/zabbix/include/defines.inc.php
#更改前
// define('ZBX_GRAPH_FONT_NAME', 'DejaVuSans'); // font file name
#更改后
define('ZBX_GRAPH_FONT_NAME', 'simkai'); // font file name
✏️ 驗證修改后的字體
安裝zabbix-agent
這里采用
ansible
安裝zabbix-agent
✏️ 下載軟件包(在ansible server
端下載zabbix-agent
軟件包)
[root@tfryb-04-30-02 ~]# cd /data/tools/
[root@tfryb-04-30-02 ~]# wget https://mirrors.huaweicloud.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-agent-5.0.2-1.el7.x86_64.rpm
✏️ 編寫playbook
[root@tfryb-04-30-02 ~]# mkdir /data/tools/playbook -p
[root@tfryb-04-30-02 ~]# cd /data/tools/playbook/
[root@tfryb-04-30-02 playbook]# cat playbook_zabbix_agent.yml
---
- hosts: tdsql_test #需要安裝的主機,或者主機組
remote_user: root
vars:
- src_package_path: /data/tools/zabbix-agent-5.0.2-1.el7.x86_64.rpm #ansible server上zabbix-agent包的路徑
- dest_package_path: /root/zabbix-agent-5.0.2-1.el7.x86_64.rpm
tasks:
- name: Copy package zabbix-agent
copy: src={{ src_package_path }} dest={{ dest_package_path }} backup=yes
- name: Install zabbix agent
yum: name={{ dest_package_path }} state=installed
- name: Config zabbix agent
template: src=zabbix_agentd.conf.j2 dest=/etc/zabbix/zabbix_agentd.conf
notify: Restart zabbix agent
- name: Start zabbix agent
service: name=zabbix-agent.service state=started
handlers:
- name: Restart zabbix agent
service: name=zabbix-agent.service state=restarted
✏️ 准備配置文件模板
[root@tfryb-04-30-02 ~]# cd /data/tools/playbook/
[root@tfryb-04-30-02 playbook]# cat zabbix_agentd.conf.j2
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=172.24.xxx.xxx
ServerActive=172.24.xxx.xxx:10051
Hostname={{ ansible_all_ipv4_addresses[0] }}
HostMetadataItem=system.uname
Include=/etc/zabbix/zabbix_agentd.d/*.conf
✏️ 安裝
[root@tfryb-04-30-02 playbook]# ansible-playbook playbook_zabbix_agent.yml
PLAY [tdsql_test] *************************************************************
GATHERING FACTS ***************************************************************
ok: [172.24.xxx.xx]
ok: [172.24.xxx.xx]
ok: [172.24.xxx.xx]
ok: [172.24.xxx.xx]
ok: [172.24.xxx.xx]
......
✏️ 執行完成驗證下配置文件是否正確
[root@tfryb-04-30-02 playbook]# ansible tdsql_test -m shell -a "cat /etc/zabbix/zabbix_agentd.conf"
微信告警
✏️ zabbix server
配置文件修改
[root@tfryb-04-30-02 ~]# vim /etc/zabbix/zabbix_server.conf
AlertScriptsPath=/usr/lib/zabbix/alertscripts
✏️ 報警腳本編寫
[root@tfryb-04-30-02 ~]# vim /usr/lib/zabbix/alertscripts/weixin.py
#!/usr/bin/python2.7.5
#_*_coding:utf-8 _*_
import urllib,urllib2
import json
import sys
import simplejson
reload(sys)
sys.setdefaultencoding('utf-8')
def gettoken(corpid,corpsecret):
gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + corpsecret
try:
token_file = urllib2.urlopen(gettoken_url)
except urllib2.HTTPError as e:
print e.code
print e.read().decode("utf8")
sys.exit()
token_data = token_file.read().decode('utf-8')
token_json = json.loads(token_data)
token_json.keys()
token = token_json['access_token']
return token
def senddata(access_token,user,subject,content):
send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token
send_values = {
"touser":"@all", #企業號中的用戶帳號,在zabbix用戶Media中配置,如果配置不正常,將按部門發送。
"toparty":"2PURL", #企業號中的部門id。
"msgtype":"text", #消息類型。
"agentid":"1000038", #企業號中的應用id。
"text":{
"content":subject + '\n' + content
},
"safe":"0"
}
send_data = simplejson.dumps(send_values, ensure_ascii=False).encode('utf-8')
send_request = urllib2.Request(send_url, send_data)
response = json.loads(urllib2.urlopen(send_request).read())
print str(response)
if __name__ == '__main__':
user = str(sys.argv[1]) #zabbix傳過來的第一個參數
subject = str(sys.argv[2]) #zabbix傳過來的第二個參數
content = str(sys.argv[3]) #zabbix傳過來的第三個參數
corpid = 'ww235ddb83xxxxxxxx' #企業號的標識(上文中提到獲取位置)
corpsecret = 'NEtVoiQmgQBsBZ2IOrMdHG2CU9vxxxxxxxxxxx' #管理組憑證密鑰(上文中提到獲取位置)
accesstoken = gettoken(corpid,corpsecret)
senddata(accesstoken,user,subject,content)
✏️ web
界面添加報警媒介
✏️ 添加完成可以進行測試
✏️ 給用戶添加報警媒介
✏️ 配置一個告警動作
上面步驟配置完成后,可以測試模擬一個故障,便會通過微信接收到告警信息。
補充:告警消息模板
告警操作消息內容:
告警主機:{HOST.NAME}
主機IP: {HOST.IP}
告警時間:{EVENT.DATE} {EVENT.TIME}
告警等級:{TRIGGER.SEVERITY}
告警詳情:{TRIGGER.NAME}
問題狀態:{ITEM.VALUE}
當前狀態:{TRIGGER.STATUS}
事件ID: {EVENT.ID}
告警恢復消息內容:
告警主機:{HOST.NAME}
主機IP: {HOST.IP}
告警時間:{EVENT.DATE} {EVENT.TIME}
告警等級:{TRIGGER.SEVERITY}
告警詳情:{TRIGGER.NAME}
問題狀態:{ITEM.VALUE}
當前狀態:{TRIGGER.STATUS}
事件ID: {EVENT.ID}
安裝Grafana
✏️ 安裝grafana
[root@tfryb-04-30-02 ~]# yum localinstall grafana-6.6.2-1.x86_64.rpm -y
✏️ 啟動
[root@tfryb-04-30-02 ~]# systemctl start grafana-server.service
[root@tfryb-04-30-02 ~]# systemctl enable grafana-server.service
✏️ Web訪問http://172.24.115.129:3000/
默認賬戶:admin
密碼:admin
,首次登陸后會要求修改密碼。Admin#123zaQ
✏️ 安裝zabbix
插件
由於網絡問題,該插件下載巨慢,故采用離線方式進行安裝
[root@tfryb-04-30-02 ~]# unzip alexanderzobnin-zabbix-app-4.0.1.zip
[root@tfryb-04-30-02 ~]# mv alexanderzobnin-zabbix-app /var/lib/grafana/plugins/
[root@tfryb-04-30-02 ~]# ls /var/lib/grafana/plugins/
alexanderzobnin-zabbix-app
[root@tfryb-04-30-02 ~]# systemctl restart grafana-server.service
✏️ web
界面激活zabbix
插件
注:如安裝最新的zabbix插件會出現問題
grafana里面plugins列表里面有zabbix,但是添加數據源的時候列表里面找不到,zabbix已經enable
解決辦法
1.修改配置文件
[root@tfryb-04-30-02 ~]# vim /etc/grafana/grafana.ini
#默認情況下,未簽名的插件不會加載,因此請在Grafana配置文件中允許它
allow_loading_unsigned_plugins = alexanderzobnin-zabbix-datasource
2.重啟grafana
[root@tfryb-04-30-02 ~]# systemctl restart grafana-server
✏️ 添加zabbix
數據源
✏️ 配置模板
該模板需要安裝grafana-piechart-panel
插件
[root@tfryb-04-30-02 ~]# grafana-cli plugins list-remote |grep grafana-piechart-panel
id: grafana-piechart-panel version: 1.6.0
[root@tfryb-04-30-02 ~]# grafana-cli plugins install grafana-piechart-panel
installing grafana-piechart-panel @ 1.6.0
from: https://grafana.com/api/plugins/grafana-piechart-panel/versions/1.6.0/download
into: /var/lib/grafana/plugins
✔ Installed grafana-piechart-panel successfully
Restart grafana after installing plugins . <service grafana-server restart>
[root@tfryb-04-30-02 ~]# systemctl restart grafana-server.service
導入模板Zabbix5.0.2服務器基礎監控grafana模板.json
☑️ 備注:
grafana的"Zabbix5.0.2服務器基礎監控grafana模板.json"模板需要和zabbix 的監控模板"zbx_Base_Monitor_templates.xml"配合一起使用。 該模板如需要,請聯系我。