Added caching to clearadm
[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
87     unless $status;
88   
89   verbose "Snapshotting load on $system{name}";
90   
91   my %load = (
92     system => $system{name},
93   );
94
95   my $cmd = 'uptime';
96   
97   ($status, @output) = $clearexec->execute ($cmd);
98
99   return
100     if $status;
101
102   # Parsing uptime is odd. Sometimes we get output like
103   #
104   #  10:11:59 up 17 days, 22:11,  6 users,  load average: 1.08, 1.10, 1.10
105   #
106   # And sometimes we get output like:
107   #
108   #  10:11:15 up 23:04,  0 users,  load average: 0.00, 0.00, 0.00
109   #
110   # Notice that if the machine was up for less than a day you don't get the
111   # "x days" portion of output. There is no real controls on uptime to format
112   # the output better, so we parse for either format.
113   if ($output[0] =~ /up\s+(.+?),\s+(.+?),\s+(\d+) user.*load average:\s+(.+?),/) {
114     $load{uptime}  = "$1 $2";
115     $load{users}   = $3;
116     $load{loadavg} = "$4";
117   } elsif ($output[0] =~ /up\s+(.+?),\s+(\d+) user.*load average:\s+(.+?),/) {
118     $load{uptime}  = "$1";
119     $load{users}   = $2;
120     $load{loadavg} = "$3";
121   } else {
122     warning "Unable to parse output of uptime from $system{name}";
123     return;
124   } # if
125
126   # On Windows sytems, Cygwin's uptime does not return a loadavg at all - it
127   # returns only 0! So we have load.vbs which give us the LoadPercentage
128   if ($system{type} =~ /windows/i) {
129     my $loadvbs = 'c:/cygwin/opt/clearscm/clearadm/load.vbs';
130     $cmd = "cscript /nologo $loadvbs";
131         
132     ($status, @output) = $clearexec->execute ($cmd);
133         
134     chop @output if $output[0] =~ /\r/;
135         
136     return
137       if $status;
138           
139     $load{loadavg} = $output[0] / 100;
140   } # if
141   
142   $clearexec->disconnectFromServer;
143   
144   return %load;  
145 } # snapshotLoad
146
147 # Main
148 GetOptions (
149   'usage'   => sub { Usage },
150   'verbose' => sub { set_verbose },
151   'debug'   => sub { set_debug },
152   'host=s'  => \$host,
153 ) or Usage "Invalid parameter";
154
155 Usage 'Extraneous options: ' . join ' ', @ARGV
156   if @ARGV;
157
158 # Announce ourselves
159 verbose "$FindBin::Script V$VERSION";
160
161 my $exit = 0;
162
163 for my $system ($clearadm->FindSystem ($host)) {
164   next if $system->{active} eq 'false';
165   
166   my %load = snapshotLoad $system;
167   
168   if (%load) {
169     my ($err, $msg) = $clearadm->AddLoadavg (%load);
170   
171     error $msg, $err if $err;
172   } else {
173     error "Unable to get loadavg for system $system->{name}", 1;
174   } # if
175   
176   # Check if over threshold
177   my %notification = $clearadm->GetNotification ('Loadavg');
178
179   next unless %notification;
180   
181   if ($load{loadavg} >= $system->{loadavgThreshold}) {
182     $exit = 2;
183     error YMDHMS . " System: $system->{name} "
184         . "Loadavg $load{loadavg} "
185         . "Threshold $system->{loadavgThreshold}";
186   } else {
187     $clearadm->ClearNotifications ($system->{name});
188   } # if
189
190   # Add graphs to system record
191   my ($loadavgsmall, $loadavg);
192
193   my $cmd = "plotloadavg.cgi generate=1 system=$system->{name} scaling=Hour points=24";
194
195   verbose "Generating loadavgsmall for $system->{name}";
196   my ($error, @output) = Execute("$cmd tiny=1 2>&1");
197
198   error 'Unable to generate loadavgsmall' . join("\n", @output), $error if $error;
199
200   $system->{loadavgsmall} = join '', @output;
201
202   verbose "Generating loadavg for $system->{name}";
203   ($error, @output) = Execute("$cmd 2>&1");
204   
205   error 'Unable to generate loadavg' . join("\n", @output), $error if $error;
206
207   $system->{loadavg} = join '', @output;
208
209   my ($err, $msg) = $clearadm->UpdateSystem($system->{name}, %$system);
210
211   error "Unable to udpate system record $msg", $err if $err;
212 } # for
213
214 exit $exit;
215
216 =pod
217
218 =head1 CONFIGURATION AND ENVIRONMENT
219
220 DEBUG: If set then $debug is set to this level.
221
222 VERBOSE: If set then $verbose is set to this level.
223
224 TRACE: If set then $trace is set to this level.
225
226 =head1 DEPENDENCIES
227
228 =head2 Perl Modules
229
230 L<FindBin>
231
232 L<Getopt::Long|Getopt::Long>
233
234 L<Net::Domain|Net::Domain>
235
236 =head2 ClearSCM Perl Modules
237
238 =begin man 
239
240  Clearadm
241  Clearexec
242  DateUtils
243  Display
244  Utils
245
246 =end man
247
248 =begin html
249
250 <blockquote>
251 <a href="http://clearscm.com/php/scm_man.php?file=clearadm/lib/Clearadm.pm">Clearadm</a><br>
252 <a href="http://clearscm.com/php/scm_man.php?file=clearadm/lib/Clearexec.pm">Clearexec</a><br>
253 <a href="http://clearscm.com/php/scm_man.php?file=lib/DateUtils.pm">DateUtils</a><br>
254 <a href="http://clearscm.com/php/scm_man.php?file=lib/Display.pm">Display</a><br>
255 <a href="http://clearscm.com/php/scm_man.php?file=lib/Utils.pm">Utils</a><br>
256 </blockquote>
257
258 =end html
259
260 =head1 BUGS AND LIMITATIONS
261
262 There are no known bugs in this script
263
264 Please report problems to Andrew DeFaria <Andrew@ClearSCM.com>.
265
266 =head1 LICENSE AND COPYRIGHT
267
268 Copyright (c) 2010, ClearSCM, Inc. All rights reserved.
269
270 =cut