11# !/usr/bin/perl
22use strict;
3- use Data::Dumper;
43use DBI;
54use Sys::Syslog qw( :standard :macros) ;
65use DateTime;
7- use POSIX qw( strftime) ;
86
97openlog(" mysql_zbx_part" , " ndelay,pid" , LOG_LOCAL0);
108
3129 };
3230my $amount_partitions = 10;
3331
34- my $curr_tz = ' Europe/Amsterdam ' ;
32+ my $curr_tz = ' Etc/UTC ' ;
3533
3634my $part_tables ;
3735
7270$dbh -> disconnect();
7371
7472sub check_have_partition {
75- my $result = 0;
76-
7773# MySQL 5.5
7874# #my $sth = $dbh->prepare(qq{SELECT variable_value FROM information_schema.global_variables WHERE variable_name = 'have_partitioning'});
7975# return 1 if $row eq 'YES';
@@ -111,19 +107,15 @@ sub create_next_partition {
111107 my $period = shift ;
112108
113109 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 );
122111
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 {
124116 syslog(LOG_INFO, " Creating a partition for $table_name table ($next_name )" );
125117 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))' ;
127119 syslog(LOG_DEBUG, $query );
128120 $dbh -> do($query );
129121 }
@@ -136,25 +128,10 @@ sub remove_old_partitions {
136128 my $period = shift ;
137129 my $keep_history = shift ;
138130
139- my $curr_date = DateTime-> now;
140- $curr_date -> set_time_zone( $curr_tz );
131+ my $curr_date = DateTime-> now( time_zone => $curr_tz );
141132
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 );
158135
159136 foreach my $partition (sort keys %{$table_part }) {
160137 if ($table_part -> {$partition }-> {' partition_description' } <= $curr_date -> epoch) {
@@ -174,27 +151,23 @@ sub name_next_part {
174151
175152 my $name_template ;
176153
177- my $curr_date = DateTime-> now;
178- $curr_date -> set_time_zone( $curr_tz );
154+ my $curr_date = DateTime-> now( time_zone => $curr_tz );
179155
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 );
183158
159+ if ( $period eq ' day' ) {
184160 $name_template = $curr_date -> strftime(' p%Y_%m_%d' );
185161 }
186162 elsif ($period eq ' week' ) {
187- my $curr_date = $curr_date -> truncate ( to => ' week' );
188- $curr_date -> add(days => 7 * $curr_part );
189-
190163 $name_template = $curr_date -> strftime(' p%Y_%m_w%W' );
191164 }
192165 elsif ($period eq ' month' ) {
193- my $curr_date = $curr_date -> truncate ( to => ' month' );
194- $curr_date -> add(months => 1 + $curr_part );
195-
196166 $name_template = $curr_date -> strftime(' p%Y_%m' );
197167 }
168+ else {
169+ die " unsupported partitioning scheme '$period '\n " ;
170+ }
198171
199172 return $name_template ;
200173}
@@ -203,29 +176,12 @@ sub date_next_part {
203176 my $period = shift ;
204177 my $curr_part = shift ;
205178
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 );
224180
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 );
227183
228- return $period_date ;
184+ return $curr_date -> strftime( ' %Y-%m-%d ' ) ;
229185}
230186
231187sub delete_old_data {
0 commit comments