-
Notifications
You must be signed in to change notification settings - Fork 46
/
partitiontables_zabbix.sh
162 lines (150 loc) · 6.72 KB
/
partitiontables_zabbix.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/bin/bash
# author: itnihao
# mail: itnihao#qq.com
# Apache License Version 2.0
# date: 2018-06-06
# funtion: create parition for zabbix MySQL
# repo: https://github.com/zabbix-book/partitiontables_zabbix
#配置环境变量
ZABBIX_USER="zabbix"
ZABBIX_PWD="zabbix"
ZABBIX_DB="zabbix"
ZABBIX_PORT="3306"
ZABBIX_HOST="127.0.0.1"
MYSQL_BIN="mysql"
#历史数据保留时间,单位天
HISTORY_DAYS=30
#趋势数据保留时间,单位月
TREND_MONTHS=12
HISTORY_TABLE="history history_log history_str history_text history_uint"
TREND_TABLE="trends trends_uint"
#MYSQL连接命令
MYSQL_CMD=$(echo ${MYSQL_BIN} -u${ZABBIX_USER} -p${ZABBIX_PWD} -P${ZABBIX_PORT} -h${ZABBIX_HOST} ${ZABBIX_DB})
function create_partitions_history() {
#给历史表创建分区
for PARTITIONS_CREATE_EVERY_DAY in $(date +"%Y%m%d") $(date +"%Y%m%d" --date='1 days') $(date +"%Y%m%d" --date='2 days') $(date +"%Y%m%d" --date='3 days') $(date +"%Y%m%d" --date='4 days') $(date +"%Y%m%d" --date='5 days') $(date +"%Y%m%d" --date='6 days') $(date +"%Y%m%d" --date='7 days')
do
TIME_PARTITIONS=$(date -d "$(echo ${PARTITIONS_CREATE_EVERY_DAY} 23:59:59)" +%s)
for TABLE_NAME in ${HISTORY_TABLE}
do
SQL1=$(echo "show create table ${TABLE_NAME};")
RET1=$(${MYSQL_CMD} -e "${SQL1}"|grep "PARTITION BY RANGE"|wc -l)
#表结构中的表分区不存在,则创建
if [ "${RET1}" == "0" ];then
SQL2=$(echo "ALTER TABLE $TABLE_NAME PARTITION BY RANGE( clock ) (PARTITION p${PARTITIONS_CREATE_EVERY_DAY} VALUES LESS THAN (${TIME_PARTITIONS}));")
RET2=$(${MYSQL_CMD} -e "${SQL2}")
if [ "${RET2}" != "" ];then
echo ${RET2}
echo "${SQL2}"
else
printf "table %-12s create partitions p${PARTITIONS_CREATE_EVERY_DAY}\n" ${TABLE_NAME}
fi
continue
fi
#表结构中的表分区已经存在,则创建分区
if [ "${RET1}" != "0" ];then
SQL3=$(echo "show create table ${TABLE_NAME};")
RET3=$(${MYSQL_CMD} -e "${SQL3}"|grep "p${PARTITIONS_CREATE_EVERY_DAY}"|wc -l)
if [ "${RET3}" == "0" ];then
TIME_PARTITIONS=$(date -d "$(echo ${PARTITIONS_CREATE_EVERY_DAY} 23:59:59)" +%s)
SQL4=$(echo "ALTER TABLE $TABLE_NAME ADD PARTITION (PARTITION p${PARTITIONS_CREATE_EVERY_DAY} VALUES LESS THAN (${TIME_PARTITIONS}));")
RET4=$(${MYSQL_CMD} -e "${SQL4}")
if [ "${RET4}" != "" ];then
echo ${RET4}
echo "${SQL4}"
else
printf "table %-12s create partitions p${PARTITIONS_CREATE_EVERY_DAY}\n" ${TABLE_NAME}
fi
fi
fi
done
done
}
function drop_partitions_history() {
#删除历史表分区
for PARTITIONS_DELETE_DAYS_AGO in $(date +"%Y%m%d" --date="${HISTORY_DAYS} days ago")
do
for TABLE_NAME in ${HISTORY_TABLE}
do
SQL=$(echo -e "show create table ${TABLE_NAME};")
RET=$(${MYSQL_CMD} -e "${SQL}"|grep "p${PARTITIONS_DELETE_DAYS_AGO}"|wc -l)
if [ "${RET}" == "1" ];then
SQL=$(echo "ALTER TABLE ${TABLE_NAME} DROP PARTITION p${PARTITIONS_DELETE_DAYS_AGO};")
RET=$(${MYSQL_CMD} -e "${SQL}")
if [ "${RET}" != "" ];then
echo ${RET}
echo "${SQL}"
else
printf "table %-12s drop partitions p${PARTITIONS_DELETE_DAYS_AGO}\n" ${TABLE_NAME}
fi
fi
done
done
}
function create_partitions_trend() {
#创建趋势表分区
for PARTITIONS_CREATE_EVERY_MONTHS in $(date +"%Y%m") $(date +"%Y%m" --date='1 months') $(date +"%Y%m" --date='2 months') $(date +"%Y%m" --date='3 months') $(date +"%Y%m" --date='4 months') $(date +"%Y%m" --date='5 months')
do
TIME_PARTITIONS=$(date -d "$(echo ${PARTITIONS_CREATE_EVERY_MONTHS}01 00:00:00)" +%s)
for TABLE_NAME in ${TREND_TABLE}
do
SQL1=$(echo "show create table ${TABLE_NAME};")
RET1=$(${MYSQL_CMD} -e "${SQL1}"|grep "PARTITION BY RANGE"|wc -l)
#表结构中的表分区不存在,则创建
if [ "${RET1}" == "0" ];then
SQL2=$(echo "ALTER TABLE $TABLE_NAME PARTITION BY RANGE( clock ) (PARTITION p${PARTITIONS_CREATE_EVERY_MONTHS} VALUES LESS THAN (${TIME_PARTITIONS}));")
RET2=$(${MYSQL_CMD} -e "${SQL2}")
if [ "${RET2}" != "" ];then
echo ${RET2}
echo "${SQL2}"
else
printf "table %-12s create partitions p${PARTITIONS_CREATE_EVERY_MONTHS}\n" ${TABLE_NAME}
fi
continue
fi
#表结构中的表分区已经存在,则创建分区
if [ "${RET1}" != "0" ];then
SQL3=$(echo "show create table ${TABLE_NAME};")
RET3=$(${MYSQL_CMD} -e "${SQL3}"|grep "p${PARTITIONS_CREATE_EVERY_MONTHS}"|wc -l)
if [ "${RET3}" == "0" ];then
SQL4=$(echo "ALTER TABLE ${TABLE_NAME} ADD PARTITION (PARTITION p${PARTITIONS_CREATE_EVERY_MONTHS} VALUES LESS THAN (${TIME_PARTITIONS}));")
RET4=$(${MYSQL_CMD} -e "${SQL4}")
if [ "${RET4}" != "" ];then
echo ${RET4}
echo "${SQL4}"
else
printf "table %-12s create partitions p${PARTITIONS_CREATE_EVERY_MONTHS}\n" ${TABLE_NAME}
fi
fi
fi
done
done
}
function drop_partitions_trend() {
#删除趋势表分区
for PARTITIONS_DELETE_MONTHS_AGO in $(date +"%Y%m" --date="${TREND_MONTHS} months ago")
do
for TABLE_NAME in ${TREND_TABLE}
do
SQL=$(echo "show create table ${TABLE_NAME};")
RET=$(${MYSQL_CMD} -e "${SQL}"|grep "p${PARTITIONS_DELETE_MONTHS_AGO}"|wc -l)
if [ "${RET}" == "1" ];then
SQL=$(echo "ALTER TABLE ${TABLE_NAME} DROP PARTITION p${PARTITIONS_DELETE_MONTHS_AGO};")
RET=$(${MYSQL_CMD} -e "${SQL}")
if [ "${RET}" != "" ];then
echo ${RET}
echo "${SQL}"
else
printf "table %-12s drop partitions p${PARTITIONS_DELETE_MONTHS_AGO}\n" ${TABLE_NAME}
fi
fi
done
done
}
function main() {
create_partitions_history
create_partitions_trend
drop_partitions_history
drop_partitions_trend
}
main