軟件說明
軟件 |
版本 |
備注 |
zabbix-server-mysql |
5.0.2 |
宿主機(jī)部署 |
zabbix-agent |
5.0.2 |
宿主機(jī)部署 |
zabbix-web-nginx-mysql |
5.0.2 |
docker方式部署 |
mysql |
8.0.21 |
宿主機(jī)部署 |
grafana |
6.6.2 |
宿主機(jī)部署 |
最終效果預(yù)覽
安裝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./mysql/Downloads/MySQL-8.0/mysql-community-client-8.0.21-1.el7.x86_64.rpm
wget https://mirrors./mysql/Downloads/MySQL-8.0/mysql-community-devel-8.0.21-1.el7.x86_64.rpm
wget https://mirrors./mysql/Downloads/MySQL-8.0/mysql-community-embedded-compat-8.0.21-1.el7.x86_64.rpm
wget https://mirrors./mysql/Downloads/MySQL-8.0/mysql-community-libs-8.0.21-1.el7.x86_64.rpm
wget https://mirrors./mysql/Downloads/MySQL-8.0/mysql-community-libs-compat-8.0.21-1.el7.x86_64.rpm
wget https://mirrors./mysql/Downloads/MySQL-8.0/mysql-community-server-8.0.21-1.el7.x86_64.rpm
wget https://mirrors./mysql/Downloads/MySQL-8.0/mysql-community-common-8.0.21-1.el7.x86_64.rpm
wget https://mirrors./zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-server-mysql-5.0.2-1.el7.x86_64.rpm
wget https://mirrors./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
?? 修改配置文件(更改數(shù)據(jù)存放路徑)
[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
?? 啟動(dòng)
[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 #過濾出默認(rèn)密碼
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)
?? 創(chuàng)建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
?? 導(dǎo)入zabbix 數(shù)據(jù)庫文件
[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 |
+----------------------------+
?? 啟動(dòng)
[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."],
"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
?? 啟動(dòng)zabbix-web
[root@tfryb-04-30-02 ~]# docker run --name zabbix-web-nginx-mysql -e DB_SERVER_HOST="172.24." -e MYSQL_DATABASE="zabbix" -e MYSQL_USER="zabbix" -e MYSQL_PASSWORD="Zabbix@123" -e ZBX_SERVER_HOST="172.24." -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. ,默認(rèn)賬戶:Admin 密碼:zabbix
解決亂碼問題
# 1.上傳字體到服務(wù)器
[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.進(jìn)入容器中進(jìn)行配置
[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
?? 驗(yàn)證修改后的字體
安裝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./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 #需要安裝的主機(jī),或者主機(jī)組
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
?? 準(zhǔn)備配置文件模板
[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.
ServerActive=172.24.: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]
......
?? 執(zhí)行完成驗(yàn)證下配置文件是否正確
[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
?? 報(bào)警腳本編寫
[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", #企業(yè)號(hào)中的用戶賬號(hào),在zabbix用戶Media中配置,如果配置不正常,將按部門發(fā)送。
"toparty":"2PURL", #企業(yè)號(hào)中的部門id。
"msgtype":"text", #消息類型。
"agentid":"1000038", #企業(yè)號(hào)中的應(yīng)用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傳過來的第一個(gè)參數(shù)
subject = str(sys.argv[2]) #zabbix傳過來的第二個(gè)參數(shù)
content = str(sys.argv[3]) #zabbix傳過來的第三個(gè)參數(shù)
corpid = 'ww235ddb83xxxxxxxx' #企業(yè)號(hào)的標(biāo)識(shí)(上文中提到獲取位置)
corpsecret = 'NEtVoiQmgQBsBZ2IOrMdHG2CU9vxxxxxxxxxxx' #管理組憑證密鑰(上文中提到獲取位置)
accesstoken = gettoken(corpid,corpsecret)
senddata(accesstoken,user,subject,content)
?? web 界面添加報(bào)警媒介
?? 添加完成可以進(jìn)行測試
?? 給用戶添加報(bào)警媒介
?? 配置一個(gè)告警動(dòng)作
上面步驟配置完成后,可以測試模擬一個(gè)故障,便會(huì)通過微信接收到告警信息。
補(bǔ)充:告警消息模板
告警操作消息內(nèi)容:
告警主機(jī):{HOST.NAME}
主機(jī)IP: {HOST.IP}
告警時(shí)間:{EVENT.DATE} {EVENT.TIME}
告警等級(jí):{TRIGGER.SEVERITY}
告警詳情:{TRIGGER.NAME}
問題狀態(tài):{ITEM.VALUE}
當(dāng)前狀態(tài):{TRIGGER.STATUS}
事件ID: {EVENT.ID}
告警恢復(fù)消息內(nèi)容:
告警主機(jī):{HOST.NAME}
主機(jī)IP: {HOST.IP}
告警時(shí)間:{EVENT.DATE} {EVENT.TIME}
告警等級(jí):{TRIGGER.SEVERITY}
告警詳情:{TRIGGER.NAME}
問題狀態(tài):{ITEM.VALUE}
當(dāng)前狀態(tài):{TRIGGER.STATUS}
事件ID: {EVENT.ID}
安裝Grafana
?? 安裝grafana
[root@tfryb-04-30-02 ~]# yum localinstall grafana-6.6.2-1.x86_64.rpm -y
?? 啟動(dòng)
[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/
默認(rèn)賬戶:admin 密碼:admin ,首次登陸后會(huì)要求修改密碼。Admin#123zaQ
?? 安裝zabbix 插件
由于網(wǎng)絡(luò)問題,該插件下載巨慢,故采用離線方式進(jìn)行安裝
[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插件會(huì)出現(xiàn)問題
grafana里面plugins列表里面有zabbix,但是添加數(shù)據(jù)源的時(shí)候列表里面找不到,zabbix已經(jīng)enable
解決辦法
1.修改配置文件
[root@tfryb-04-30-02 ~]# vim /etc/grafana/grafana.ini
#默認(rèn)情況下,未簽名的插件不會(huì)加載,因此請(qǐng)?jiān)贕rafana配置文件中允許它
allow_loading_unsigned_plugins = alexanderzobnin-zabbix-datasource
2.重啟grafana
[root@tfryb-04-30-02 ~]# systemctl restart grafana-server
?? 添加zabbix 數(shù)據(jù)源
?? 配置模板
該模板需要安裝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:///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
導(dǎo)入模板Zabbix5.0.2服務(wù)器基礎(chǔ)監(jiān)控grafana模板.json
?? 備注:
grafana的"Zabbix5.0.2服務(wù)器基礎(chǔ)監(jiān)控grafana模板.json"模板需要和zabbix 的監(jiān)控模板"zbx_Base_Monitor_templates.xml"配合一起使用。 該模板如需要,請(qǐng)聯(lián)系我。
|