感謝譯者餘偉男,宏時數據Zabbix技術經理。
歡迎翻譯Zabbix官方博文,將有用資料分享給更多用戶!
本篇文章列出了在Zabbix中,哪些會佔用大量的磁盤空間以及哪些監控項和主機對象消耗磁盤空間最多。
包含以下內容:
數據庫中最大的表
進入到Zabbix的值最大的監控項(最新)
數據庫中最大的分區表
找到佔用空間最多的主機和監控項
1
●
最大的表
一般來說,在Zabbix的庫中,最佔空間的表以大小依次排序爲:
history |
history_str |
events |
' history_uint '表存儲整數類型的數據。' history '存儲十進制的數據。
' history_str ', ' history_text ', ' history_log '存儲文本類型的數據。
“events”表中記錄了問題事件、內部事件、代理自動註冊事件、自動發現的記錄。
用sql語句檢查哪些表佔用了最多的空間。
Mysql:
SELECT table_name,
table_rows,
data_length,
index_length,
round(((data_length + index_length) / 1024 / 1024 / 1024),2) "Size in GB"
FROM information_schema.tables
WHERE table_schema = "zabbix"
ORDER BY round(((data_length + index_length) / 1024 / 1024 / 1024),2) DESC
LIMIT 8;
PostgreSQL:
SELECT *, pg_size_pretty(total_bytes) AS total , pg_size_pretty(index_bytes) AS index ,
pg_size_pretty(toast_bytes) AS toast , pg_size_pretty(table_bytes) AS table
FROM (SELECT *, total_bytes-index_bytes-coalesce(toast_bytes, 0) AS table_bytes
FROM (SELECT c.oid,
nspname AS table_schema,
relname AS table_name ,
c.reltuples AS row_estimate ,
pg_total_relation_size(c.oid) AS total_bytes ,
pg_indexes_size(c.oid) AS index_bytes ,
pg_total_relation_size(reltoastrelid) AS toast_bytes
FROM pg_class c
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE relkind = 'r' ) a) a;
2
●
最近5分鐘內值最大的監控項
MySQL ‘history_log’:
SELECT SUM(LENGTH(value)) AS 'chars',
CONCAT('history.php?itemids%5B0%5D=', itemid ,'&action=showlatest' ) AS 'URL'
FROM history_log
WHERE clock > UNIX_TIMESTAMP(NOW() - INTERVAL 5 MINUTE)
GROUP BY itemid ORDER BY SUM(LENGTH(value)) DESC LIMIT 5;
MySQL ‘history_text’:
SELECT SUM(LENGTH(value)) AS 'chars',
CONCAT('history.php?itemids%5B0%5D=', itemid ,'&action=showlatest' ) AS 'URL'
FROM history_text
WHERE clock > UNIX_TIMESTAMP(NOW() - INTERVAL 5 MINUTE)
GROUP BY itemid ORDER BY SUM(LENGTH(value)) DESC LIMIT 5;
MySQL ‘history_str’:
SELECT SUM(LENGTH(value)) AS 'chars',
CONCAT('history.php?itemids%5B0%5D=', itemid ,'&action=showlatest' ) AS 'URL'
FROM history_str
WHERE clock > UNIX_TIMESTAMP(NOW() - INTERVAL 5 MINUTE)
GROUP BY itemid ORDER BY SUM(LENGTH(value)) DESC LIMIT 5;
PostgreSQL ‘history_text’:
SELECT CONCAT('history.php?itemids%5B0%5D=', itemid ,'&action=showlatest' ) AS URL,
SUM(LENGTH(value)) FROM history_text
WHERE clock > EXTRACT(epoch FROM NOW()-INTERVAL '5 MINUTE')
GROUP BY itemid ORDER BY SUM(LENGTH(value)) DESC LIMIT 5;
PostgreSQL ‘history_log’:
SELECT CONCAT('history.php?itemids%5B0%5D=', itemid ,'&action=showlatest' ) AS URL,
SUM(LENGTH(value)) FROM history_log
WHERE clock > EXTRACT(epoch FROM NOW()-INTERVAL '5 MINUTE')
GROUP BY itemid ORDER BY SUM(LENGTH(value)) DESC LIMIT 5;
PostgreSQL ‘history_str’:
SELECT CONCAT('history.php?itemids%5B0%5D=', itemid ,'&action=showlatest' ) AS URL,
SUM(LENGTH(value)) FROM history_str
WHERE clock > EXTRACT(epoch FROM NOW()-INTERVAL '5 MINUTE')
GROUP BY itemid ORDER BY SUM(LENGTH(value)) DESC LIMIT 5;
3
●
哪些主機佔用了最多的空間
MySQL ‘history_text’:
SELECT SUM(LENGTH(history_text.value)) AS 'chars', hosts.name AS 'name'
FROM history_text
JOIN items ON (items.itemid=history_text.itemid)
JOIN hosts ON (hosts.hostid=items.hostid)
WHERE history_text.clock > UNIX_TIMESTAMP(NOW() - INTERVAL 5 MINUTE)
GROUP BY hosts.name ORDER BY SUM(LENGTH(history_text.value)) DESC LIMIT 5;
MySQL ‘history_log’:
SELECT SUM(LENGTH(history_log.value)) AS 'chars', hosts.name AS 'name'
FROM history_log
JOIN items ON (items.itemid=history_log.itemid)
JOIN hosts ON (hosts.hostid=items.hostid)
WHERE history_log.clock > UNIX_TIMESTAMP(NOW() - INTERVAL 5 MINUTE)
GROUP BY hosts.name ORDER BY SUM(LENGTH(history_log.value)) DESC LIMIT 5;
MySQL ‘history_str’:
SELECT SUM(LENGTH(history_str.value)) AS 'chars', hosts.name AS 'name'
FROM history_str
JOIN items ON (items.itemid=history_str.itemid)
JOIN hosts ON (hosts.hostid=items.hostid)
WHERE history_str.clock > UNIX_TIMESTAMP(NOW() - INTERVAL 5 MINUTE)
GROUP BY hosts.name ORDER BY SUM(LENGTH(history_str.value)) DESC LIMIT 5;
PostgreSQL ‘history_text’:
SELECT SUM(LENGTH(history_text.value)) AS "chars", hosts.name AS "name"
FROM history_text
JOIN items ON (items.itemid=history_text.itemid)
JOIN hosts ON (hosts.hostid=items.hostid)
WHERE history_text.clock > EXTRACT(epoch FROM NOW()-INTERVAL '5 MINUTE')
GROUP BY hosts.name ORDER BY SUM(LENGTH(history_text.value)) DESC LIMIT 5;
PostgreSQL ‘history_log’:
SELECT SUM(LENGTH(history_log.value)) AS "chars", hosts.name AS "name"
FROM history_log
JOIN items ON (items.itemid=history_log.itemid)
JOIN hosts ON (hosts.hostid=items.hostid)
WHERE history_log.clock > EXTRACT(epoch FROM NOW()-INTERVAL '5 MINUTE')
GROUP BY hosts.name ORDER BY SUM(LENGTH(history_log.value)) DESC LIMIT 5;
PostgreSQL ‘history_str’:
SELECT SUM(LENGTH(history_str.value)) AS "chars", hosts.name AS "name"
FROM history_str
JOIN items ON (items.itemid=history_str.itemid)
JOIN hosts ON (hosts.hostid=items.hostid)
WHERE history_str.clock > EXTRACT(epoch FROM NOW()-INTERVAL '5 MINUTE')
GROUP BY hosts.name ORDER BY SUM(LENGTH(history_str.value)) DESC LIMIT 5;
4
●
從分區表層面分析(Mysql)
如果你將Mysql作爲你的數據庫並且做了表分區,可以列出佔用空間最大的分區:
cd /var/lib/mysql/zabbix
ls -lh history_log#*
將會輸出如下內容:
-rw-r-----. 1 mysql mysql 44M Jan 24 20:23 history_log#p#p2021_02w.ibd
-rw-r-----. 1 mysql mysql 24M Jan 24 21:20 history_log#p#p2021_03w.ibd
-rw-r-----. 1 mysql mysql 128K Jan 11 00:59 history_log#p#p2021_04w.ibd
根據上面的內容,我們可以拿到分區的名字“p2021_02w”,並在下面的sql語句中使用,用來分析這個分區表:
SELECT ho.hostid, ho.name, count(*) AS records,
(count(*)* (SELECT AVG_ROW_LENGTH FROM information_schema.tables
WHERE TABLE_NAME = 'history_log' and TABLE_SCHEMA = 'zabbix')/1024/1024) AS 'Total size average (Mb)',
sum(length(history_log.value))/1024/1024 +
sum(length(history_log.clock))/1024/1024 +
sum(length(history_log.ns))/1024/1024 +
sum(length(history_log.itemid))/1024/1024 AS 'history_log Column Size (Mb)'
FROM history_log PARTITION (p2021_02w)
LEFT OUTER JOIN items i on history_log.itemid = i.itemid
LEFT OUTER JOIN hosts ho on i.hostid = ho.hostid
WHERE ho.status IN (0,1)
GROUP BY ho.hostid
ORDER BY 4 DESC
LIMIT 10;
可以在列出時重現類似的場景:
ls -lh history_text#*
ls -lh history_str#*
5
●
如何釋放磁盤空間(Mysql)
在前端頁面刪除主機不會釋放MySQL上的空間。它將在表中創建空行,以便插入新數據。如果您想真正釋放磁盤空間,我們可以重建分區。
首先列出所有可能的分區名稱:
SHOW CREATE TABLE history\G
重建表分區:
ALTER TABLE history REBUILD PARTITION p202101160000;
6
●
如何釋放磁盤空間(PostgreSQL)
在PostgreSQL上,有一個進程負責清空表。如果想確定最近已進行清理,可以執行一下語句:
SELECT schemaname, relname, n_live_tup, n_dead_tup, last_autovacuum
FROM pg_stat_all_tables
WHERE n_dead_tup > 0
ORDER BY n_dead_tup DESC;
在輸出中,我們要注意‘n_dead_tup’,它表示一個死元組。
如果最近10天內沒有出現最後一次自動清理,那就糟糕了。我們必須做一個不同的配置。可以通過以下方式提高自動清理的優先級:
vacuum_cost_page_miss = 10
vacuum_cost_page_dirty = 20
autovacuum_vacuum_threshold = 50
autovacuum_vacuum_scale_factor = 0.01
autovacuum_vacuum_cost_delay = 20ms
autovacuum_vacuum_cost_limit = 3000
autovacuum_max_workers = 6
點擊“閱讀原文”,查看英文官方博文
9月11日(週六) Zabbix Meetup 北京場
免費參與,立即報名。
備註“使用Zabbix年限+企業+姓名”
進入交流羣,4000+用戶已加入
一個人走得快,一羣人走得遠
本文分享自微信公衆號 - Zabbix開源社區(china_zabbix)。
如有侵權,請聯繫 [email protected] 刪除。
本文參與“OSC源創計劃”,歡迎正在閱讀的你也加入,一起分享。