helios_log_clean.pl untested

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 7 Podpod n/a totaltotal 0.0% 0 / 99 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/bin/env perl
2  
3 use strict;
4 use warnings;
5 use Error qw(:try);
6 use Getopt::Long;
7  
8 use Helios::Service;
9 use Helios::Error;
10  
11 our $VERSION = '2.80';
12  
13 # CHANGES:
14 # [LH] 2013-08-04: Changed service instantiation to use new Helios::Config API.
15 # [LH] [2014-02-28]: Changed shebang line to use env instead of /usr/bin/perl
16  
17 =head1 NAME
18  
19 helios_log_clean.pl - Clean old log and history entries from the Helios database
20  
21 =head1 SYNOPSIS
22  
23 helios_log_clean.pl [--days=<days>] [--silent]
24  
25 =head1 DESCRIPTION
26  
27 Use helios_log_clean.pl to delete old entries from the log and job history in the Helios database
28 (helios_log_tb and helios_job_history_tb, respectively).
29  
30 =head1 COMMAND OPTIONS
31  
32 =head2 --days
33  
34 Specify the number of days of logs you wish to keep using the --days option. If not specified,
35 logs and job history from the past 7 days are kept. Older entries will be deleted.
36  
37 =head2 --silent
38  
39 In silent mode, helios_log_clean.pl will only print output if an error occurs. Useful for running
40 from cron.
41  
42 =head2 --help
43  
44 Print this help.
45  
46 =cut
47  
48 our $SECONDS_IN_DAY = 86400;
49 our $DEBUG_MODE = 0;
50 our $DAYS = 0;
51 our $HELP_MODE = 0;
52 our $SILENT_MODE = 0;
53 GetOptions ("days=i" => \$DAYS,
54 "silent" => \$SILENT_MODE,
55 "help" => \$HELP_MODE,
56 "debug" => \$DEBUG_MODE
57 );
58  
59 # print help if asked
60 if ( $HELP_MODE ) {
61 require Pod::Usage;
62 Pod::Usage::pod2usage(-verbose => 2, -exitstatus => 0);
63 }
64  
65 # honor HELIOS_DEBUG env var
66 if ( defined($ENV{HELIOS_DEBUG}) && $ENV{HELIOS_DEBUG} == 1 ) {
67 $DEBUG_MODE = 1;
68 }
69  
70 # DEBUG overrides SILENT
71 if ($DEBUG_MODE) { $SILENT_MODE = 0; }
72  
73 # default to 7 days if --days wasn't specified
74 unless ($DAYS) { $DAYS = 7; }
75  
76 # instantiate the base worker class just to get the [global] INI params
77 # (we need to know where the Helios db is)
78 our $WORKER = new Helios::Service;
79 # BEGIN [LH] 2012-08-04: Changed old service init to use new Helios::Config API
80 $WORKER->prep();
81 my $params = $WORKER->getConfig();
82 # END [LH] 2012-08-04
83 my $sql;
84 my $epoch_horizon = time() - ($DAYS * $SECONDS_IN_DAY);
85  
86 # connect to the Helios database
87 unless ($params->{dsn}) { throw Helios::Error::Fatal("Helios dsn not defined!"); }
88 my $dbh = $WORKER->dbConnect($params->{dsn}, $params->{user}, $params->{password});
89 if ($DBI::errstr) { throw Helios::Error::DatabaseError($DBI::errstr); }
90 if ($DEBUG_MODE) { print "Connected to Helios database.\n"; }
91  
92 # clean the log table
93 unless ($SILENT_MODE) { print "Deleting log entries older than $DAYS days...\n"; }
94 $sql = "DELETE FROM helios_log_tb WHERE log_time <= $epoch_horizon";
95 if ($DEBUG_MODE) { print $sql,"\n"; }
96 $dbh->do($sql);
97 print "...done.\n";
98  
99  
100 # clean the job history table
101 unless ($SILENT_MODE) { print "Deleting job history older than $DAYS days...\n"; }
102 $sql = "DELETE FROM helios_job_history_tb WHERE complete_time <= $epoch_horizon";
103 if ($DEBUG_MODE) { print $sql,"\n"; }
104 $dbh->do($sql);
105 unless ($SILENT_MODE) { print "...done.\n"; }
106  
107  
108  
109 =head1 AUTHOR
110  
111 Andrew Johnson, E<lt>lajandy at cpan dotorgE<gt>
112  
113 =head1 COPYRIGHT AND LICENSE
114  
115 Copyright (C) 2008-9 by CEB Toolbox, Inc.
116  
117 This library is free software; you can redistribute it and/or modify
118 it under the same terms as Perl itself, either Perl version 5.8.0 or,
119 at your option, any later version of Perl 5 you may have available.
120  
121 =head1 WARRANTY
122  
123 This software comes with no warranty of any kind.
124  
125 =cut
126