Removed /usr/local from CDPATH
[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 my %opts = (
70   scrubdays => $Clearadm::CLEAROPTS{CLEARADM_SCRUBDAYS}
71 );
72
73 # Main
74 GetOptions(
75   \%opts,
76   'usage'    => sub { Usage },
77   'verbose'  => sub { set_verbose },
78   'debug'    => sub { set_debug },
79   "scrubdays=i",
80 ) or Usage "Invalid parameter";
81
82 Usage 'Extraneous options: ' . join ' ', @ARGV if @ARGV;
83
84 # Announce ourselves
85 verbose "$FindBin::Script V$VERSION";
86
87 my ($err, $msg);
88
89 for my $system ($clearadm->FindSystem($host)) {
90   ($err, $msg) = $clearadm->TrimLoadavg($system->{name});
91   
92   if ($msg eq 'Records deleted' or $msg eq '') {
93     verbose "Scrub loadavg $system->{name}: $err $msg:";
94   } else {
95     error "#$err: $msg";
96   } # if
97   
98   for my $filesystem ($clearadm->FindFilesystem($system->{name}, $fs)) {
99     ($err, $msg) = $clearadm->TrimFS($system->{name}, $filesystem->{filesystem});
100     
101     if ($msg eq 'Records deleted' or $msg eq '') {
102       verbose "Scrub filesystem $system->{name}:$filesystem->{filesystem}: $err $msg";
103     } else {
104       error "#$err: $msg";
105     } # if
106   } # for
107 } # for
108
109 my $scrubdate = SubtractDays(Today2SQLDatetime, $opts{scrubdays});
110
111 my %runlog = (
112   task    => 'Scrub',
113   started => Today2SQLDatetime,
114   system  => hostname(),
115 );
116
117 # Scrub view and vob storage records
118 for ($clearadm->FindVob) {
119   ($err, $msg) = $clearadm->TrimStorage('vob', $_->{tag}, $_->{region});
120
121   if ($msg eq 'Records deleted' or $msg eq '') {
122     verbose "Scub VOB $_->{tag} $err $msg";
123   } else {
124     error "#$err: $msg";
125   } # if
126 } # for
127
128 for ($clearadm->FindView) {
129   ($err, $msg) = $clearadm->TrimStorage('view', $_->{tag}, $_->{region});
130
131   if ($msg eq 'Records deleted' or $msg eq '') {
132     verbose "Scub View $_->{tag} $err $msg";
133   } else {
134     error "#$err: $msg";
135   } # if
136 } # for
137
138 # Make sure the Clearcase objects we have in Clearadm are still valid
139 my ($views, $vobs) = $clearadm->ValidateCCObjects;
140
141 if ($vobs !~ /^\d+/) {
142   error "Unable to validate Clearcase objects: $vobs", $views;
143 } else {
144   $runlog{status} = 0;
145
146   $runlog{message}  = "Deleted $views views\n" if $views;
147   $runlog{message} .= "Deleted $vobs vobs"     if $vobs;
148
149   $clearadm->AddRunlog(%runlog);
150 } # if
151
152 # Scrub old alertlogs
153 ($runlog{status}, $runlog{message}) = 
154   $clearadm->DeleteAlertlog ("timestamp<='$scrubdate'");
155
156 verbose "$runlog{task} alertlog: $runlog{status} $runlog{message}";
157
158 $clearadm->AddRunlog (%runlog);
159
160 $runlog{started} = Today2SQLDatetime;
161
162 # Scrub old runlogs
163 ($runlog{status}, $runlog{message}) = 
164   $clearadm->DeleteRunlog ("started<='$scrubdate'");
165   
166 verbose "$runlog{task} runlog: $runlog{status} $runlog{message}";
167
168 $clearadm->AddRunlog(%runlog);
169
170 =pod
171
172 =head1 CONFIGURATION AND ENVIRONMENT
173
174 DEBUG: If set then $debug is set to this level.
175
176 VERBOSE: If set then $verbose is set to this level.
177
178 TRACE: If set then $trace is set to this level.
179
180 =head1 DEPENDENCIES
181
182 =head2 Perl Modules
183
184 L<FindBin>
185
186 L<Getopt::Long|Getopt::Long>
187
188 =head2 ClearSCM Perl Modules
189
190 =begin man 
191
192  Clearadm
193  DateUtils
194  Display
195  TimeUtils
196  Utils
197
198 =end man
199
200 =begin html
201
202 <blockquote>
203 <a href="http://clearscm.com/php/scm_man.php?file=clearadm/lib/Clearadm.pm">Clearadm</a><br>
204 <a href="http://clearscm.com/php/scm_man.php?file=lib/DateUtils.pm">DateUtils</a><br>
205 <a href="http://clearscm.com/php/scm_man.php?file=lib/Display.pm">Display</a><br>
206 <a href="http://clearscm.com/php/scm_man.php?file=lib/TimeUtils.pm">TimeUtils</a><br>
207 <a href="http://clearscm.com/php/scm_man.php?file=lib/Utils.pm">Utils</a><br>
208 </blockquote>
209
210 =end html
211
212 =head1 BUGS AND LIMITATIONS
213
214 There are no known bugs in this script
215
216 Please report problems to Andrew DeFaria <Andrew@ClearSCM.com>.
217
218 =head1 LICENSE AND COPYRIGHT
219
220 Copyright (c) 2010, ClearSCM, Inc. All rights reserved.
221
222 =cut