Removed /usr/local from CDPATH
[clearscm.git] / clearadm / updatela.pl
1 #!/usr/bin/env perl
2
3 =pod
4
5 =head1 NAME $RCSfile: updatela.pl,v $
6
7 Update Load Average
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.29 $
20
21 =item Created:
22
23 Mon Dec 13 09:13:27 EST 2010
24
25 =item Modified:
26
27 $Date: 2011/06/16 15:14:52 $
28
29 =back
30
31 =head1 SYNOPSIS
32
33  Usage updatela.pl: [-u|sage] [-ve|rbose] [-deb|ug]
34                     [-host [<host>|all]]
35
36  Where:
37    -u|sage:     Displays usage
38  
39    -ve|rbose:   Be verbose
40    -deb|ug:     Output debug messages
41    
42    -host [<host>|all]: Update host or all hosts (Default: all)
43    -fs   [<fs>|all]:   Update filesystem or all (Default: all)   
44
45 =head1 DESCRIPTION
46
47 This script will record the load average of a system
48
49 =cut
50
51 use strict;
52 use warnings;
53
54 use Net::Domain qw(hostname);
55 use FindBin;
56 use Getopt::Long;
57
58 use lib "$FindBin::Bin/lib", "$FindBin::Bin/../lib";
59
60 use Clearadm;
61 use Clearexec;
62 use DateUtils;
63 use Display;
64 use Utils;
65
66 my $VERSION  = '$Revision: 1.29 $';
67   ($VERSION) = ($VERSION =~ /\$Revision: (.*) /);
68
69 my $clearadm  = Clearadm->new;
70 my $clearexec = Clearexec->new; 
71
72 my $host;
73
74 # Given a host, formulate a loadavg record
75 sub snapshotLoad($) {
76   my ($systemRef) = @_;
77
78   my %system = %{$systemRef};
79   
80   my ($status, @output);
81
82   $status = $clearexec->connectToServer(
83     $system{name}, $system{port}
84   );
85   
86   error "Unable to connect to system $system{name}:$system{port}", 1 unless $status;
87   
88   verbose "Snapshotting load on $system{name}";
89   
90   my %load = (
91     system => $system{name},
92   );
93
94   my $cmd = 'uptime';
95   
96   ($status, @output) = $clearexec->execute($cmd);
97
98   return if $status;
99
100   # Parsing uptime is odd. Sometimes we get output like
101   #
102   #  10:11:59 up 17 days, 22:11,  6 users,  load average: 1.08, 1.10, 1.10
103   #
104   # And sometimes we get output like:
105   #
106   #  10:11:15 up 23:04,  0 users,  load average: 0.00, 0.00, 0.00
107   #
108   # Notice that if the machine was up for less than a day you don't get the
109   # "x days" portion of output. There is no real controls on uptime to format
110   # the output better, so we parse for either format.
111   if ($output[0] =~ /up\s+(.+?),\s+(.+?),\s+(\d+) user.*load average:\s+(.+?),/) {
112     $load{uptime}  = "$1 $2";
113     $load{users}   = $3;
114     $load{loadavg} = "$4";
115   } elsif ($output[0] =~ /up\s+(.+?),\s+(\d+) user.*load average:\s+(.+?),/) {
116     $load{uptime}  = "$1";
117     $load{users}   = $2;
118     $load{loadavg} = "$3";
119   } else {
120     warning "Unable to parse output of uptime from $system{name}";
121     return;
122   } # if
123
124   # On Windows sytems, Cygwin's uptime does not return a loadavg at all - it
125   # returns only 0! So we have load.vbs which give us the LoadPercentage
126   if ($system{type} =~ /windows/i) {
127     my $loadvbs = 'c:/cygwin/opt/clearscm/clearadm/load.vbs';
128     $cmd = "cscript /nologo $loadvbs";
129         
130     ($status, @output) = $clearexec->execute($cmd);
131         
132     chop @output if $output[0] =~ /\r/;
133         
134     return if $status;
135           
136     $load{loadavg} = $output[0] / 100;
137   } # if
138   
139   $clearexec->disconnectFromServer;
140   
141   return %load;  
142 } # snapshotLoad
143
144 # Main
145 GetOptions(
146   'usage'   => sub { Usage },
147   'verbose' => sub { set_verbose },
148   'debug'   => sub { set_debug },
149   'host=s'  => \$host,
150 ) or Usage "Invalid parameter";
151
152 Usage 'Extraneous options: ' . join ' ', @ARGV if @ARGV;
153
154 # Announce ourselves
155 verbose "$FindBin::Script V$VERSION";
156
157 my $exit = 0;
158
159 for my $system ($clearadm->FindSystem($host)) {
160   next if $system->{active} eq 'false';
161   
162   my %load = snapshotLoad $system;
163   
164   if (%load) {
165     my ($err, $msg) = $clearadm->AddLoadavg(%load);
166   
167     error $msg, $err if $err;
168   } else {
169     error "Unable to get loadavg for system $system->{name}", 1;
170   } # if
171   
172   # Check if over threshold
173   my %notification = $clearadm->GetNotification('Loadavg');
174
175   next unless %notification;
176   
177   if ($load{loadavg} >= $system->{loadavgThreshold}) {
178     $exit = 2;
179     error YMDHMS . " System: $system->{name} "
180         . "Loadavg $load{loadavg} "
181         . "Threshold $system->{loadavgThreshold}";
182   } else {
183     $clearadm->ClearNotifications ($system->{name});
184   } # if
185
186   # Add graphs to system record
187   my ($loadavgsmall, $loadavg);
188
189   my $cmd = "plotloadavg.cgi generate=1 system=$system->{name} scaling=Hour points=24";
190
191   verbose "Generating loadavgsmall for $system->{name}";
192   my ($error, @output) = Execute("$cmd tiny=1 2>&1");
193
194   error 'Unable to generate loadavgsmall' . join("\n", @output), $error if $error;
195
196   $system->{loadavgsmall} = join '', @output;
197
198   verbose "Generating loadavg for $system->{name}";
199   ($error, @output) = Execute("$cmd 2>&1");
200   
201   error 'Unable to generate loadavg' . join("\n", @output), $error if $error;
202
203   $system->{loadavg} = join '', @output;
204
205   my ($err, $msg) = $clearadm->UpdateSystem($system->{name}, %$system);
206
207   error "Unable to udpate system record $msg", $err if $err;
208 } # for
209
210 exit $exit;
211
212 =pod
213
214 =head1 CONFIGURATION AND ENVIRONMENT
215
216 DEBUG: If set then $debug is set to this level.
217
218 VERBOSE: If set then $verbose is set to this level.
219
220 TRACE: If set then $trace is set to this level.
221
222 =head1 DEPENDENCIES
223
224 =head2 Perl Modules
225
226 L<FindBin>
227
228 L<Getopt::Long|Getopt::Long>
229
230 L<Net::Domain|Net::Domain>
231
232 =head2 ClearSCM Perl Modules
233
234 =begin man 
235
236  Clearadm
237  Clearexec
238  DateUtils
239  Display
240  Utils
241
242 =end man
243
244 =begin html
245
246 <blockquote>
247 <a href="http://clearscm.com/php/scm_man.php?file=clearadm/lib/Clearadm.pm">Clearadm</a><br>
248 <a href="http://clearscm.com/php/scm_man.php?file=clearadm/lib/Clearexec.pm">Clearexec</a><br>
249 <a href="http://clearscm.com/php/scm_man.php?file=lib/DateUtils.pm">DateUtils</a><br>
250 <a href="http://clearscm.com/php/scm_man.php?file=lib/Display.pm">Display</a><br>
251 <a href="http://clearscm.com/php/scm_man.php?file=lib/Utils.pm">Utils</a><br>
252 </blockquote>
253
254 =end html
255
256 =head1 BUGS AND LIMITATIONS
257
258 There are no known bugs in this script
259
260 Please report problems to Andrew DeFaria <Andrew@ClearSCM.com>.
261
262 =head1 LICENSE AND COPYRIGHT
263
264 Copyright (c) 2010, ClearSCM, Inc. All rights reserved.
265
266 =cut