5 =head1 NAME $RCSfile: updatela.pl,v $
15 Andrew DeFaria <Andrew@ClearSCM.com>
23 Mon Dec 13 09:13:27 EST 2010
27 $Date: 2011/06/16 15:14:52 $
33 Usage updatela.pl: [-u|sage] [-ve|rbose] [-deb|ug]
37 -u|sage: Displays usage
40 -deb|ug: Output debug messages
42 -host [<host>|all]: Update host or all hosts (Default: all)
43 -fs [<fs>|all]: Update filesystem or all (Default: all)
47 This script will record the load average of a system
54 use Net::Domain qw(hostname);
58 use lib "$FindBin::Bin/lib", "$FindBin::Bin/../lib";
66 my $VERSION = '$Revision: 1.29 $';
67 ($VERSION) = ($VERSION =~ /\$Revision: (.*) /);
69 my $clearadm = Clearadm->new;
70 my $clearexec = Clearexec->new;
74 # Given a host, formulate a loadavg record
75 sub snapshotLoad ($) {
78 my %system = %{$systemRef};
80 my ($status, @output);
82 $status = $clearexec->connectToServer (
83 $system{name}, $system{port}
86 error "Unable to connect to system $system{name}:$system{port}", 1
89 verbose "Snapshotting load on $system{name}";
92 system => $system{name},
97 ($status, @output) = $clearexec->execute ($cmd);
102 # Parsing uptime is odd. Sometimes we get output like
104 # 10:11:59 up 17 days, 22:11, 6 users, load average: 1.08, 1.10, 1.10
106 # And sometimes we get output like:
108 # 10:11:15 up 23:04, 0 users, load average: 0.00, 0.00, 0.00
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";
116 $load{loadavg} = "$4";
117 } elsif ($output[0] =~ /up\s+(.+?),\s+(\d+) user.*load average:\s+(.+?),/) {
118 $load{uptime} = "$1";
120 $load{loadavg} = "$3";
122 warning "Unable to parse output of uptime from $system{name}";
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";
132 ($status, @output) = $clearexec->execute ($cmd);
134 chop @output if $output[0] =~ /\r/;
139 $load{loadavg} = $output[0] / 100;
142 $clearexec->disconnectFromServer;
149 'usage' => sub { Usage },
150 'verbose' => sub { set_verbose },
151 'debug' => sub { set_debug },
153 ) or Usage "Invalid parameter";
155 Usage 'Extraneous options: ' . join ' ', @ARGV
159 verbose "$FindBin::Script V$VERSION";
163 for my $system ($clearadm->FindSystem ($host)) {
164 next if $system->{active} eq 'false';
166 my %load = snapshotLoad $system;
169 my ($err, $msg) = $clearadm->AddLoadavg (%load);
171 error $msg, $err if $err;
173 error "Unable to get loadavg for system $system->{name}", 1;
176 # Check if over threshold
177 my %notification = $clearadm->GetNotification ('Loadavg');
179 next unless %notification;
181 if ($load{loadavg} >= $system->{loadavgThreshold}) {
183 error YMDHMS . " System: $system->{name} "
184 . "Loadavg $load{loadavg} "
185 . "Threshold $system->{loadavgThreshold}";
187 $clearadm->ClearNotifications ($system->{name});
190 # Add graphs to system record
191 my ($loadavgsmall, $loadavg);
193 my $cmd = "plotloadavg.cgi generate=1 system=$system->{name} scaling=Hour points=24";
195 verbose "Generating loadavgsmall for $system->{name}";
196 my ($error, @output) = Execute("$cmd tiny=1 2>&1");
198 error 'Unable to generate loadavgsmall' . join("\n", @output), $error if $error;
200 $system->{loadavgsmall} = join '', @output;
202 verbose "Generating loadavg for $system->{name}";
203 ($error, @output) = Execute("$cmd 2>&1");
205 error 'Unable to generate loadavg' . join("\n", @output), $error if $error;
207 $system->{loadavg} = join '', @output;
209 my ($err, $msg) = $clearadm->UpdateSystem($system->{name}, %$system);
211 error "Unable to udpate system record $msg", $err if $err;
218 =head1 CONFIGURATION AND ENVIRONMENT
220 DEBUG: If set then $debug is set to this level.
222 VERBOSE: If set then $verbose is set to this level.
224 TRACE: If set then $trace is set to this level.
232 L<Getopt::Long|Getopt::Long>
234 L<Net::Domain|Net::Domain>
236 =head2 ClearSCM Perl Modules
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>
260 =head1 BUGS AND LIMITATIONS
262 There are no known bugs in this script
264 Please report problems to Andrew DeFaria <Andrew@ClearSCM.com>.
266 =head1 LICENSE AND COPYRIGHT
268 Copyright (c) 2010, ClearSCM, Inc. All rights reserved.