Initial commit
[clearscm.git] / clearadm / updatela.pl
1 #!/usr/bin/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 foreach 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
180     unless %notification;
181   
182   if ($load{loadavg} >= $$system{loadavgThreshold}) {
183     $exit = 2;
184     error YMDHMS . " System: $$system{name} "
185         . "Loadavg $load{loadavg} "
186         . "Threshold $$system{loadavgThreshold}";
187   } else {
188     $clearadm->ClearNotifications ($$system{name});
189   } # if
190 } # foreach
191
192 exit $exit;
193
194 =pod
195
196 =head1 CONFIGURATION AND ENVIRONMENT
197
198 DEBUG: If set then $debug is set to this level.
199
200 VERBOSE: If set then $verbose is set to this level.
201
202 TRACE: If set then $trace is set to this level.
203
204 =head1 DEPENDENCIES
205
206 =head2 Perl Modules
207
208 L<FindBin>
209
210 L<Getopt::Long|Getopt::Long>
211
212 L<Net::Domain|Net::Domain>
213
214 =head2 ClearSCM Perl Modules
215
216 =begin man 
217
218  Clearadm
219  Clearexec
220  DateUtils
221  Display
222  Utils
223
224 =end man
225
226 =begin html
227
228 <blockquote>
229 <a href="http://clearscm.com/php/cvs_man.php?file=clearadm/lib/Clearadm.pm">Clearadm</a><br>
230 <a href="http://clearscm.com/php/cvs_man.php?file=clearadm/lib/Clearexec.pm">Clearexec</a><br>
231 <a href="http://clearscm.com/php/cvs_man.php?file=lib/DateUtils.pm">DateUtils</a><br>
232 <a href="http://clearscm.com/php/cvs_man.php?file=lib/Display.pm">Display</a><br>
233 <a href="http://clearscm.com/php/cvs_man.php?file=lib/Utils.pm">Utils</a><br>
234 </blockquote>
235
236 =end html
237
238 =head1 BUGS AND LIMITATIONS
239
240 There are no known bugs in this script
241
242 Please report problems to Andrew DeFaria <Andrew@ClearSCM.com>.
243
244 =head1 LICENSE AND COPYRIGHT
245
246 Copyright (c) 2010, ClearSCM, Inc. All rights reserved.
247
248 =cut