配置各项数据的保留时间:
启用Housekeeper:
对于大型Zabbix部署,Housekeeper可能效率不高,可以直接操作数据库:
-- 清理30天前的历史数据
DELETE FROM history WHERE clock < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 DAY));
DELETE FROM history_uint WHERE clock < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 DAY));
DELETE FROM history_str WHERE clock < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 DAY));
-- 其他history_*表类似
-- 清理30天前的事件
DELETE FROM events WHERE clock < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 DAY));
-- 优化表(可选)
OPTIMIZE TABLE history, history_uint, history_str, events;
可以创建存储过程并设置定时任务执行。
-- 清理30天前的历史数据
DELETE FROM history WHERE clock < extract(epoch from (now() - interval '30 days'));
DELETE FROM history_uint WHERE clock < extract(epoch from (now() - interval '30 days'));
-- 其他表类似
-- 清理后建议执行VACUUM
VACUUM ANALYZE history;
VACUUM ANALYZE history_uint;
#!/bin/bash
# 使用Zabbix API清理历史数据
ZABBIX_URL="http://your-zabbix-server/zabbix/api_jsonrpc.php"
ZABBIX_USER="Admin"
ZABBIX_PASS="zabbix"
# 获取认证令牌
AUTH_TOKEN=$(curl -s -X POST -H 'Content-Type: application/json-rpc' -d '{
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "'$ZABBIX_USER'",
"password": "'$ZABBIX_PASS'"
},
"id": 1
}' $ZABBIX_URL | jq -r .result)
# 计算30天前的时间戳
THIRTY_DAYS_AGO=$(date -d "30 days ago" +%s)
# 清理历史数据
curl -s -X POST -H 'Content-Type: application/json-rpc' -d '{
"jsonrpc": "2.0",
"method": "history.delete",
"params": {
"time_till": '$THIRTY_DAYS_AGO'
},
"auth": "'$AUTH_TOKEN'",
"id": 1
}' $ZABBIX_URL
# 登出
curl -s -X POST -H 'Content-Type: application/json-rpc' -d '{
"jsonrpc": "2.0",
"method": "user.logout",
"params": [],
"auth": "'$AUTH_TOKEN'",
"id": 1
}' $ZABBIX_URL
对于超大型Zabbix部署,可以考虑使用分区表按时间分区,然后直接删除旧分区。
选择哪种方法取决于您的Zabbix部署规模、数据库类型和性能要求。对于大多数中小型部署,内置的Housekeeper功能已经足够。