zabbix删除历史记录

Zabbix历史数据清理

 

一、统计数据库中每一个表所占的空间:html

 

 

SELECT table_name AS "Tables",round(((data_length + index_length) / 1024 / 1024), 2) "Size in MB" from information_schema.TABLES where table_schema = 'zabbix' ORDER BY (data_length + index_length) DESC;mysql

 

二、清理zabbix一周以前的历史数据:sql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
! /bin/bash
User= "zabbixuser"
Passwd= "zabbixpass"
Date=` date -d $( date -d "-7 day" +%Y%m%d) +%s` #取7天以前的时间戳
$( which mysql) -u${User} -p${Passwd} -e "
use zabbixdb;
DELETE FROM history WHERE 'clock' < $Date;
optimize table history ;
DELETE FROM history_str WHERE 'clock' < $Date;
optimize table history_str;
DELETE FROM history_uint WHERE 'clock' < $Date;
optimize table history_uint;
DELETE FROM history_text WHERE 'clock' < $Date;
optimize table history_text;
DELETE FROM  trends WHERE 'clock' < $Date;
optimize table  trends;
DELETE FROM trends_uint WHERE 'clock' < $Date;
optimize table trends_uint;
DELETE FROM events WHERE 'clock' < $Date;
optimize table events;
"

三、添加到系统计划任务:数据库

1
2
#remove the zabbix mysql data before 7 day's ago
0 3 * * 0 /usr/local/script/clearzabbix .sh > /usr/local/script/clearzabbix .log

 

另:能够使用truncate命令直接清空数据库:安全

1
2
3
4
5
6
7
truncate table history ;
truncate table history_uint;
truncate table history_str;
truncate table history_text;
truncate table trends;
truncate table trends_uint;
truncate table events;

若是想要删除表的全部数据,truncate语句要比 delete 语句快bash

由于 truncate 删除了表,而后根据表结构从新创建它,而 delete 删除的是记录,并无尝试去修改表。post

不过truncate命令虽然快,却不像delete命令那样对事务处理是安全的。ui

所以,若是咱们想要执行truncate删除的表正在进行事务处理,这个命令就会产生退出并产生错误信息。url