Merge branch 'master' of git+ssh://github.com/adefaria/clearscm
[clearscm.git] / clearadm / clearadmscrub.pl
1 #!/usr/local/bin/perl
2
3 =pod
4
5 =head1 NAME $RCSfile: clearadmscrub.pl,v $
6
7 Scrub Clearadm records
8
9 =head1 VERSION
10
11 =over
12
13 =item Author
14
15 Andrew DeFaria <Andrew@ClearSCM.com>
16
17 =item Revision
18
19 $Revision: 1.9 $
20
21 =item Created:
22
23 Sun Jan  2 19:40:28 EST 2011
24
25 =item Modified:
26
27 $Date: 2012/11/09 06:45:36 $
28
29 =back
30
31 =head1 SYNOPSIS
32
33  Usage clearadmscrub.pl: [-u|sage] [-ve|rbose] [-deb|ug]
34
35  Where:
36    -u|sage:     Displays usage
37  
38    -ve|rbose:   Be verbose
39    -deb|ug:     Output debug messages
40    
41 =head1 DESCRIPTION
42
43 This script will scrub all old records in the Clearadm database
44
45 =cut
46
47 use strict;
48 use warnings;
49
50 use FindBin;
51 use Getopt::Long;
52 use Sys::Hostname;
53
54 use lib "$FindBin::Bin/lib", "$FindBin::Bin/../lib";
55
56 use Clearadm;
57 use DateUtils;
58 use Display;
59 use TimeUtils;
60 use Utils;
61
62 my $VERSION  = '$Revision: 1.9 $';
63   ($VERSION) = ($VERSION =~ /\$Revision: (.*) /);
64
65 my $clearadm  = Clearadm->new;
66
67 my ($host, $fs);
68
69 # Main
70 GetOptions (
71   'usage'   => sub { Usage },
72   'verbose' => sub { set_verbose },
73   'debug'   => sub { set_debug },
74 ) or Usage "Invalid parameter";
75
76 Usage 'Extraneous options: ' . join ' ', @ARGV
77   if @ARGV;
78
79 # Announce ourselves
80 verbose "$FindBin::Script V$VERSION";
81
82 my ($err, $msg);
83
84 for my $system ($clearadm->FindSystem ($host)) {
85   ($err, $msg) = $clearadm->TrimLoadavg ($$system{name});
86   
87   if ($msg eq 'Records deleted' or $msg eq '') {
88     verbose "Scrub loadavg $$system{name}: $err $msg:";
89   } else {
90     error "#$err: $msg";
91   } # if
92   
93   for my $filesystem ($clearadm->FindFilesystem ($$system{name}, $fs)) {
94     ($err, $msg) = $clearadm->TrimFS ($$system{name}, $$filesystem{filesystem});
95     
96     if ($msg eq 'Records deleted' or $msg eq '') {
97       verbose "Scrub filesystem $$system{name}:$$filesystem{filesystem}: $err $msg";
98     } else {
99       error "#$err: $msg";
100     } # if
101   } # for
102 } # for
103
104 # TODO: These should be configurable
105 my $sixMonthsAgo = SubtractDays (Today2SQLDatetime, 180);
106
107 my %runlog = (
108   task    => 'Scrub',
109   started => Today2SQLDatetime,
110   system  => hostname(),
111 );
112
113 # Scrub old alertlogs
114 ($runlog{status}, $runlog{message}) = 
115   $clearadm->DeleteAlertlog ("timestamp<='$sixMonthsAgo'");
116
117 verbose "$runlog{task} alertlog: $runlog{status} $runlog{message}";
118
119 $clearadm->AddRunlog (%runlog);
120
121 $runlog{started} = Today2SQLDatetime;
122
123 # Scrub old runlogs
124 ($runlog{status}, $runlog{message}) = 
125   $clearadm->DeleteRunlog ("started<='$sixMonthsAgo'");
126   
127 verbose "$runlog{task} runlog: $runlog{status} $runlog{message}";
128
129 $clearadm->AddRunlog (%runlog);
130
131 =pod
132
133 =head1 CONFIGURATION AND ENVIRONMENT
134
135 DEBUG: If set then $debug is set to this level.
136
137 VERBOSE: If set then $verbose is set to this level.
138
139 TRACE: If set then $trace is set to this level.
140
141 =head1 DEPENDENCIES
142
143 =head2 Perl Modules
144
145 L<FindBin>
146
147 L<Getopt::Long|Getopt::Long>
148
149 =head2 ClearSCM Perl Modules
150
151 =begin man 
152
153  Clearadm
154  DateUtils
155  Display
156  TimeUtils
157  Utils
158
159 =end man
160
161 =begin html
162
163 <blockquote>
164 <a href="http://clearscm.com/php/scm_man.php?file=clearadm/lib/Clearadm.pm">Clearadm</a><br>
165 <a href="http://clearscm.com/php/scm_man.php?file=lib/DateUtils.pm">DateUtils</a><br>
166 <a href="http://clearscm.com/php/scm_man.php?file=lib/Display.pm">Display</a><br>
167 <a href="http://clearscm.com/php/scm_man.php?file=lib/TimeUtils.pm">TimeUtils</a><br>
168 <a href="http://clearscm.com/php/scm_man.php?file=lib/Utils.pm">Utils</a><br>
169 </blockquote>
170
171 =end html
172
173 =head1 BUGS AND LIMITATIONS
174
175 There are no known bugs in this script
176
177 Please report problems to Andrew DeFaria <Andrew@ClearSCM.com>.
178
179 =head1 LICENSE AND COPYRIGHT
180
181 Copyright (c) 2010, ClearSCM, Inc. All rights reserved.
182
183 =cut