zabbix服務-Grafana

第1章 Grafana自定義圖形

1.安裝grafana

[root@m01 /data/soft]# wget https://dl.grafana.com/oss/release/grafana-6.3.2-1.x86_64.rpm
[root@m01 /data/soft]# yum localinstall grafana-6.3.2-1.x86_64.rpm -y
[root@m01 /data/soft]# systemctl start grafana-server.service
[root@m01 /data/soft]# systemctl enable grafana-server.service
訪問grafana:http://10.0.0.61:3000
賬號密碼:admin admin

在這裏插入圖片描述

2.安裝並激活zabbix插件
[root@m01 ~]# grafana-cli plugins list-remote|grep zabbix
id: alexanderzobnin-zabbix-app version: 3.10.4
[root@m01 ~]# grafana-cli plugins install alexanderzobnin-zabbix-app
[root@m01 ~]# systemctl restart grafana-server.service
網頁操作-激活zabbix插件

在這裏插入圖片描述

網頁操作-添加zabbix數據源

在這裏插入圖片描述

在這裏插入圖片描述
在這裏插入圖片描述
網頁操作-導入模版

在這裏插入圖片描述
3.數據展示

4.自定義圖形儀表盤

在這裏插入圖片描述
在這裏插入圖片描述在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

5.自定義圖形餅圖

1.安裝插件

在線安裝方式

grafana-cli plugins install grafana-piechart-panel
離線安裝方式

wget -nv https://grafana.com/api/plugins/grafana-piechart-panel/versions/latest/download -O /tmp/grafana-piechart-panel.zip
unzip -q /tmp/grafana-piechart-panel.zip -d /tmp
mv /tmp/grafana-piechart-panel-* /var/lib/grafana/plugins/grafana-piechart-panel
service grafana-server restart
2.配置圖形

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

第2章 percona模版監控mysql

參考強哥的博客

https://www.qstack.com.cn/archives/213.html

1.安裝php環境

percona需要php環境

[root@m01 /data/soft]# yum install php php-mysql -y

2.下載軟件

在這裏插入圖片描述
在這裏插入圖片描述
注意,安裝完成後會有提示模版的路徑位置

[root@m01 ~]# cd /data/soft/
[root@m01 /data/soft]# wget https://www.percona.com/downloads/percona-monitoring-plugins/percona-monitoring-plugins-1.1.8/binary/redhat/7/x86_64/percona-zabbix-templates-1.1.8-1.noarch.rpm
[root@m01 /data/soft]# rpm -ivh percona-zabbix-templates-1.1.8-1.noarch.rpm
警告:percona-zabbix-templates-1.1.8-1.noarch.rpm: 頭V4 DSA/SHA1 Signature, 密鑰 ID cd2efd2a: NOKEY
準備中… ################################# [100%]
正在升級/安裝…
1:percona-zabbix-templates-1.1.8-1 ################################# [100%]

Scripts are installed to /var/lib/zabbix/percona/scripts
Templates are installed to /var/lib/zabbix/percona/templates

3.查看目錄

進入安裝目錄會發現有2個目錄,一個是腳本目錄,一個是模版目錄

[root@m01 ~]# cd /var/lib/zabbix/percona/
[root@m01 /var/lib/zabbix/percona]# tree
.
├── scripts
│ ├── get_mysql_stats_wrapper.sh
│ └── ss_get_mysql_stats.php
└── templates
├── userparameter_percona_mysql.conf
└── zabbix_agent_template_percona_mysql_server_ht_2.0.9-sver1.1.8.xml
其中腳本目錄裏有2個腳本,用來獲取數據庫信息

[root@m01 /var/lib/zabbix/percona]# cd scripts/
[root@m01 /var/lib/zabbix/percona/scripts]# ls
get_mysql_stats_wrapper.sh ss_get_mysql_stats.php

4.修改get_mysql_stats_wrapper.sh

修改get_mysql_stats_wrapper數據庫登陸信息
第19行添加mysql賬號密碼

[root@m01 v]# sed -n ‘19p’ get_mysql_stats_wrapper.sh
RES=`HOME=~zabbix mysql -uroot -p123456 -e ‘SHOW SLAVE STATUS\G’ | egrep ‘(Slave_IO_Running|Slave_SQL_Running):’ | awk -F: ‘{print $2}’ | tr ‘\n’

5.修改ss_get_mysql_stats.php

[root@m01 /var/lib/zabbix/percona/scripts]# sed -n ‘30,31p’ ss_get_mysql_stats.php
$mysql_user = ‘root’;
$mysql_pass = ‘123456’;

6.複製自定義監控項配置文件到zabbix目錄

[root@m01 ~]# cd /var/lib/zabbix/percona/templates/
[root@m01 /var/lib/zabbix/percona/templates]# cp userparameter_percona_mysql.conf /etc/zabbix/zabbix_agentd.d/
[root@m01 /var/lib/zabbix/percona/templates]# cd /etc/zabbix/zabbix_agentd.d/
[root@m01 /etc/zabbix/zabbix_agentd.d]# ls
userparameter_mysql.conf userparameter_percona_mysql.conf

7.重啓agent

[root@m01 ~]# systemctl restart zabbix-agent

8.測試key

[root@m01 ~]# zabbix_get -s 10.0.1.61 -k MySQL.Sort-scan
16

9.導入模版

官方自帶的模版有點問題,需要先裝在2.x版本然後導出來,這裏使用網友已經修改好的模版上傳

http://pan.baidu.com/s/1pL1wDYj
在這裏插入圖片描述

10.主機鏈接模版

在這裏插入圖片描述

xx.報錯解決

查看監控發現沒有數據顯示不支持類型
查看zabbix-server發現因爲tmp的文件沒有權限,因爲剛纔手動執行了腳本,所以文件屬性是root,將文件刪除後由zabbix自己創建解決問題
報錯日誌如下:

2846:20190811:202708.785 item “Zabbix server:MySQL.State-init” became not supported: Value “rm: 無法刪除”/tmp/localhost-mysql_cacti_stats.txt": 不允許的操作
0" of type “string” is not suitable for value type “Numeric (float)”
2843:20190811:202709.787 item “Zabbix server:MySQL.State-locked” became not supported: Value “rm: 無法刪除”/tmp/localhost-mysql_cacti_stats.txt": 不允許的操作
0" of type “string” is not suitable for value type “Numeric (float)”
2844:20190811:202710.788 item “Zabbix server:MySQL.State-login” became not supported: Value “rm: 無法刪除”/tmp/localhost-mysql_cacti_stats.txt": 不允許的操作
0" of type “string” is not suitable for value type “Numeric (float)”

第3章 自動發現和自動註冊

1.自動發現

web頁面操作

在這裏插入圖片描述
image.png
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

2.自動註冊

修改zabbix-agent配置文件

[root@web02 ~]# cat /etc/zabbix/zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=10.0.1.61
ServerActive=10.0.1.61
Hostname=web02
Include=/etc/zabbix/zabbix_agentd.d/*.conf
web頁面操作

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
刷新查看發現已經添加上了
在這裏插入圖片描述

第4章 主動模式和被動模式

默認爲被動模式:100個監控項要100個來回,要的時候才返回
主動模式:100個監控項1個回合,將所需要的100個打包,然後一次發過去,發過去之後,客戶端全部執行完再一次返回給服務端。

1.克隆模版

完全克隆原來被動模式的模版爲主動模式
在這裏插入圖片描述

在這裏插入圖片描述
2.修改克隆後的模版爲主動模式
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

3.修改監控主機關聯的模版爲主動模式

在這裏插入圖片描述

4.修改客戶端配置文件並重啓

[root@web01 ~]# cat /etc/zabbix/zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=10.0.1.61
ServerActive=10.0.1.61
Hostname=web01
Include=/etc/zabbix/zabbix_agentd.d/*.conf
[root@web01 ~]# systemctl restart zabbix-agent.service

5.查看最新數據

發現獲取數據的時間是一樣的

在這裏插入圖片描述

第5章 低級自動發現

監控端口自動發現

1.查看系統自帶分區自動發現

系統自帶的自動發現會顯示紅字,比如自帶的磁盤分區發現規則

在這裏插入圖片描述
在這裏插入圖片描述

1.查看zabbbix所有的key過濾後展示

在這裏插入圖片描述

2.解析成json後的格式
在這裏插入圖片描述
3.過濾規則
實質上是從mount命令獲取的分區名和類型
在這裏插入圖片描述

image.png

但是我們zabbix顯示的並沒有這麼多
是因爲做了正則表達式過濾
在這裏插入圖片描述
而正則表達式是在管理裏面配置的
在這裏插入圖片描述
4.使用zabbix_get獲取key
因爲根據過濾規則,只發現了一個xfs的key,使用zabbix_get可以查看到這個key
[root@m01 ~]# zabbix_get -s 10.0.1.61 -k vfs.fs.size[{#FSNAME},free]
ZBX_NOTSUPPORTED: Cannot obtain filesystem information: [2] No such file or directory
[root@m01 ~]# zabbix_agentd -p|grep vfs.fs.size
vfs.fs.size[/,free] [u|15713636352]
[root@m01 ~]# zabbix_get -s 10.0.1.61 -k vfs.fs.size[/,free]
15713693696

2.查看系統自帶的網卡自動發現

1.查看網絡自動發現規則

在這裏插入圖片描述

2.過濾規則

在這裏插入圖片描述
在這裏插入圖片描述
2.命令行過濾

[root@m01 ~]# zabbix_agentd -p|grep net.if.discovery
net.if.discovery [s|{“data”:[{"{#IFNAME}":“tun0”},{"{#IFNAME}":“eth0”},{"{#IFNAME}":“eth1”},{"{#IFNAME}":“lo”}]}]
3.查看自動添加的監控項
我們會發現添加了四個監控項
2個eth0
2個eth1

在這裏插入圖片描述
在這裏插入圖片描述
4.查看key的值

[root@m01 ~]# zabbix_get -s 10.0.1.61 -k net.if.in[eth0]
2191453
[root@m01 ~]# zabbix_get -s 10.0.1.61 -k net.if.in[eth1]
7152

3.監控mysql多實例

參考強哥博客

https://www.qstack.com.cn/archives/108.html
1.複製並修改數據庫配置文件

[root@m01 ~]# cp /etc/my.cnf /etc/my3307.cnf
[root@m01 ~]# vim /etc/my3307.cnf
[root@m01 ~]# cat /etc/my3307.cnf
[mysqld]
datadir=/data/3307/
socket=/data/3307/mysql.sock
port=3307
user=mysql
symbolic-links=0
[mysqld_safe]
log-error=/data/3307/mysqld.log
pid-file=/data/3307/mysqld.pid
[root@m01 ~]# cp /etc/my3307.cnf /etc/my3308.cnf
[root@m01 ~]# sed -i ‘s#3307#3308#g’ /etc/my3308.cnf
2.創建數據目錄並初始化

[root@m01 ~]# mkdir /data/{3307,3308}
[root@m01 ~]# chown -R mysql:mysql /data/330*
[root@m01 ~]# mysql_install_db --user=mysql --defaults-file=/etc/my3307.cnf
[root@m01 ~]# mysql_install_db --user=mysql --defaults-file=/etc/my3308.cnf
3.啓動多實例

[root@m01 ~]# mysqld_safe --defaults-file=/etc/my3307.cnf &
[root@m01 ~]# mysqld_safe --defaults-file=/etc/my3308.cnf &
4.檢查端口

[root@m01 ~]# netstat -lntup|grep mysql
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2042/mysqld
tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 84790/mysqld
tcp 0 0 0.0.0.0:3308 0.0.0.0:* LISTEN 85439/mysqld
5.創建自動發現配置文件

[root@m01 ~]# vim /etc/zabbix/zabbix_agentd.d/mysql_discovery.conf
[root@m01 ~]# cat /etc/zabbix/zabbix_agentd.d/mysql_discovery.conf
UserParameter=mysql.discovery,/bin/bash /server/scripts/mysql_discovery.sh
6.創建自動發現多實例腳本

[root@m01 ~]# cat /server/scripts/mysql_discovery.sh
#!/bin/bash
#mysql low-level discovery
res=$(netstat -lntp|awk -F “[ :\t]+” '/mysqld/{printKaTeX parse error: Expected 'EOF', got '}' at position 2: 5}̲') port=(res)
printf ‘{’
printf ‘“data”:[’
for key in !port[@]doif[["{!port[@]} do if [[ "{#port[@]}" -gt 1 && “key"ne"{key}" -ne "((KaTeX parse error: Expected '}', got '#' at position 2: {#̲port[@]}-1))" ]…{port[KaTeX parse error: Expected 'EOF', got '}' at position 7: {key}]}̲\"}," …{key}” -eq “((KaTeX parse error: Expected '}', got '#' at position 2: {#̲port[@]}-1))" ]…{port[${key}]}”}"
fi
done
printf ‘]’
printf ‘}\n’
7.測試自動發現腳本

[root@m01 ~]# bash /server/scripts/mysql_discovery.sh
{“data”:[{"{#MYSQLPORT}":“3306”},{"{#MYSQLPORT}":“3307”},{"{#MYSQLPORT}":“3308”}]}
8.重啓zabbix-agent

[root@m01 ~]# systemctl restart zabbix-agent.service
9.zabbix_get測試取key

[root@m01 ~]# zabbix_get -s 10.0.1.61 -k mysql.discovery
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
{“data”:[]}
這時我們發現取不出來並提示了個錯誤
原因是zabbix用戶不能使用netstat的-p參數
解決方法爲給netstat命令添加s權限

[root@m01 ~]# which netstat
/usr/bin/netstat
[root@m01 ~]# chmod u+s /usr/bin/netstat
然後再次測試就發現可以取到值了

[root@m01 ~]# zabbix_get -s 10.0.1.61 -k mysql.discovery
{“data”:[{"{#MYSQLPORT}":“3306”},{"{#MYSQLPORT}":“3307”},{"{#MYSQLPORT}":“3308”}]}
10.web頁面創建自動發現規則模版
在這裏插入圖片描述

創建模版
在這裏插入圖片描述
創建自動發現規則
在這裏插入圖片描述
設置過濾規則
在這裏插入圖片描述
創建過濾 規則

11.模仿zabbix自帶的mysql監控配置修改監控項

[root@m01 ~]# cat /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
UserParameter=mysql.status[*],echo “show global status where Variable_name=’$1’;” | HOME=/var/lib/zabbix mysql -uroot -p123456 -P $2 -N | awk ‘{print $$2}’
[root@m01 ~]# systemctl restart zabbix-agent.service
12.測試訪問監控項

[root@m01 ~]# zabbix_get -s 10.0.1.61 -k mysql.status[Uptime,3307]
23202
[root@m01 ~]# zabbix_get -s 10.0.1.61 -k mysql.status[Uptime,3308]
23204
13.web頁面添加監控項原型

在這裏插入圖片描述
監控項原型
12.web頁面設置主機關聯模版
在這裏插入圖片描述

主機關聯模版
13.查看是否已經自動添加成功

在這裏插入圖片描述

第6章 性能優化

1.監控數據分析

zabbix監控主機和監控項較少的時候,不需要優化
數據庫 200臺主機 * 200個監控項 = 40000監控項/30秒 = 1333次寫入/每秒
寫多 讀少

2.優化思路

1.mariadb 5.5 innodb 升級到mysql5.7 tokudb
2.去掉無用監控項,增加監控項的取值間隔,減少歷史數據的保存週期
3.被動模式改爲主動模式
4.針對zabbix-server進程數量調優
5.針對zabbix-server緩存調優,誰的剩餘內存少,就加大他的緩存

3.升級存儲引擎

TokuDB性能比InnoDB要好

實施步驟:

1.找一臺機器安裝好mysql5.7
2.將mariadb的數據導出,然後替換sql文件裏的存儲引擎爲TokuDB
3.將替換之後的數據導入到mysql5.7
4.停掉mariadb
5.檢查測試

4.優化進程數

在這裏插入圖片描述
可以人爲製造進程繁忙,把自動發現調整IP範圍爲1-254

在這裏插入圖片描述
這個時候觀察會發現自動發現進程變得繁忙了

修改進程數

[root@zabbix-11 ~]# grep “^StartDiscoverers” /etc/zabbix/zabbix_server.conf
StartDiscoverers=10
[root@zabbix-11 ~]# systemctl restart zabbix-server.service
調整之後發現進程不這麼繁忙了

在這裏插入圖片描述
5.緩存調優
在這裏插入圖片描述
調整配置文件

[root@zabbix-11 ~]# grep “^Cache” /etc/zabbix/zabbix_server.conf
CacheSize=128M

第7章 zabbix高可用

思路:
2臺zabbix-server使用keepavied做高可用
數據庫做主從複製
keepalived兩端都做backup角色,設置不搶佔VIP
keepalived設置如果發生改變就將自身的從庫數據庫修改爲主庫設置
然後另一臺修復上線後,手動介入重新做主從同步,變成從庫

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章