1
1
# !/usr/bin/perl
2
2
use strict;
3
- use Data::Dumper;
4
3
use DBI;
5
4
use Sys::Syslog qw( :standard :macros) ;
6
5
use DateTime;
7
- use POSIX qw( strftime) ;
8
6
9
7
openlog(" mysql_zbx_part" , " ndelay,pid" , LOG_LOCAL0);
10
8
31
29
};
32
30
my $amount_partitions = 10;
33
31
34
- my $curr_tz = ' Europe/Amsterdam ' ;
32
+ my $curr_tz = ' Etc/UTC ' ;
35
33
36
34
my $part_tables ;
37
35
72
70
$dbh -> disconnect();
73
71
74
72
sub check_have_partition {
75
- my $result = 0;
76
-
77
73
# MySQL 5.5
78
74
# #my $sth = $dbh->prepare(qq{SELECT variable_value FROM information_schema.global_variables WHERE variable_name = 'have_partitioning'});
79
75
# return 1 if $row eq 'YES';
@@ -111,19 +107,15 @@ sub create_next_partition {
111
107
my $period = shift ;
112
108
113
109
for (my $curr_part = 0; $curr_part < $amount_partitions ; $curr_part ++) {
114
- my $next_name = name_next_part($tables -> {$table_name }-> {' period' }, $curr_part );
115
- my $found = 0;
116
- foreach my $partition (sort keys %{$table_part }) {
117
- if ($next_name eq $partition ) {
118
- syslog(LOG_INFO, " Next partition for $table_name table has already been created. It is $next_name " );
119
- $found = 1;
120
- }
121
- }
110
+ my $next_name = name_next_part($period , $curr_part );
122
111
123
- if ( $found == 0 ) {
112
+ if (grep { $_ eq $next_name } keys %{$table_part }) {
113
+ syslog(LOG_INFO, " Next partition for $table_name table has already been created. It is $next_name " );
114
+ }
115
+ else {
124
116
syslog(LOG_INFO, " Creating a partition for $table_name table ($next_name )" );
125
117
my $query = ' ALTER TABLE ' ." $db_schema .$table_name " .' ADD PARTITION (PARTITION ' .$next_name .
126
- ' VALUES less than (UNIX_TIMESTAMP("' .date_next_part($tables -> { $table_name } -> { ' period' } , $curr_part ).' ") div 1))' ;
118
+ ' VALUES less than (UNIX_TIMESTAMP("' .date_next_part($period , $curr_part ).' ") div 1))' ;
127
119
syslog(LOG_DEBUG, $query );
128
120
$dbh -> do($query );
129
121
}
@@ -136,25 +128,10 @@ sub remove_old_partitions {
136
128
my $period = shift ;
137
129
my $keep_history = shift ;
138
130
139
- my $curr_date = DateTime-> now;
140
- $curr_date -> set_time_zone( $curr_tz );
131
+ my $curr_date = DateTime-> now( time_zone => $curr_tz );
141
132
142
- if ( $period eq ' day' ) {
143
- $curr_date -> add(days => -$keep_history );
144
- $curr_date -> add(hours => -$curr_date -> strftime(' %H' ));
145
- $curr_date -> add(minutes => -$curr_date -> strftime(' %M' ));
146
- $curr_date -> add(seconds => -$curr_date -> strftime(' %S' ));
147
- }
148
- elsif ( $period eq ' week' ) {
149
- }
150
- elsif ( $period eq ' month' ) {
151
- $curr_date -> add(months => -$keep_history );
152
-
153
- $curr_date -> add(days => -$curr_date -> strftime(' %d' )+1);
154
- $curr_date -> add(hours => -$curr_date -> strftime(' %H' ));
155
- $curr_date -> add(minutes => -$curr_date -> strftime(' %M' ));
156
- $curr_date -> add(seconds => -$curr_date -> strftime(' %S' ));
157
- }
133
+ $curr_date -> subtract($period .' s' => $keep_history );
134
+ $curr_date -> truncate (to => $period );
158
135
159
136
foreach my $partition (sort keys %{$table_part }) {
160
137
if ($table_part -> {$partition }-> {' partition_description' } <= $curr_date -> epoch) {
@@ -174,27 +151,23 @@ sub name_next_part {
174
151
175
152
my $name_template ;
176
153
177
- my $curr_date = DateTime-> now;
178
- $curr_date -> set_time_zone( $curr_tz );
154
+ my $curr_date = DateTime-> now( time_zone => $curr_tz );
179
155
180
- if ( $period eq ' day' ) {
181
- my $curr_date = $curr_date -> truncate ( to => ' day' );
182
- $curr_date -> add(days => 1 + $curr_part );
156
+ $curr_date -> truncate ( to => $period );
157
+ $curr_date -> add( $period .' s' => $curr_part );
183
158
159
+ if ( $period eq ' day' ) {
184
160
$name_template = $curr_date -> strftime(' p%Y_%m_%d' );
185
161
}
186
162
elsif ($period eq ' week' ) {
187
- my $curr_date = $curr_date -> truncate ( to => ' week' );
188
- $curr_date -> add(days => 7 * $curr_part );
189
-
190
163
$name_template = $curr_date -> strftime(' p%Y_%m_w%W' );
191
164
}
192
165
elsif ($period eq ' month' ) {
193
- my $curr_date = $curr_date -> truncate ( to => ' month' );
194
- $curr_date -> add(months => 1 + $curr_part );
195
-
196
166
$name_template = $curr_date -> strftime(' p%Y_%m' );
197
167
}
168
+ else {
169
+ die " unsupported partitioning scheme '$period '\n " ;
170
+ }
198
171
199
172
return $name_template ;
200
173
}
@@ -203,29 +176,12 @@ sub date_next_part {
203
176
my $period = shift ;
204
177
my $curr_part = shift ;
205
178
206
- my $period_date ;
207
-
208
- my $curr_date = DateTime-> now;
209
- $curr_date -> set_time_zone( $curr_tz );
210
-
211
- if ( $period eq ' day' ) {
212
- my $curr_date = $curr_date -> truncate ( to => ' day' );
213
- $curr_date -> add(days => 2 + $curr_part );
214
- $period_date = $curr_date -> strftime(' %Y-%m-%d' );
215
- }
216
- elsif ($period eq ' week' ) {
217
- my $curr_date = $curr_date -> truncate ( to => ' week' );
218
- $curr_date -> add(days => 7 * $curr_part + 1);
219
- $period_date = $curr_date -> strftime(' %Y-%m-%d' );
220
- }
221
- elsif ($period eq ' month' ) {
222
- my $curr_date = $curr_date -> truncate ( to => ' month' );
223
- $curr_date -> add(months => 2 + $curr_part );
179
+ my $curr_date = DateTime-> now( time_zone => $curr_tz );
224
180
225
- $period_date = $ curr_date-> strftime( ' %Y-%m-%d ' );
226
- }
181
+ $ curr_date-> truncate ( to => $period );
182
+ $curr_date -> add( $period . ' s ' => 1 + $curr_part );
227
183
228
- return $period_date ;
184
+ return $curr_date -> strftime( ' %Y-%m-%d ' ) ;
229
185
}
230
186
231
187
sub delete_old_data {
0 commit comments