5 =head1 NAME $RCSfile: updatesystem.pl,v $
15 Andrew DeFaria <Andrew@ClearSCM.com>
23 Mon Dec 13 09:13:27 EST 2010
27 $Date: 2012/11/09 06:44:38 $
33 Usage updatesystem.pl: [-u|sage] [-ve|rbose] [-deb|ug]
34 [-del|ete -h|ost <host>]
37 -u|sage: Displays usage
40 -deb|ug: Output debug messages
43 -h|ost <host>: Host to operate on (Default: Current host)
44 -p|ort <port>: Clearexec port to connect to
48 This script will add/update the system to the Clearadm database. You can also
49 delete a system from the Clearadm database.
61 use lib "$FindBin::Bin/lib", "$FindBin::Bin/../lib";
68 my $VERSION = '$Revision: 1.17 $';
69 ($VERSION) = ($VERSION =~ /\$Revision: (.*) /);
71 my $clearadm = Clearadm->new;
72 my $clearexec = Clearexec->new;
74 my ($delete, $host, $port);
76 sub GetFilesystems(%) {
79 # TODO: Unix/Linux systems often vary as to what parameters df supports. The
80 # -P is to intended to make this POSIX standard. Need to make sure this works
81 # on other systems (i.e. Solaris, HP-UX, Redhat, etc.).
82 my $cmd = $system{type} eq 'Windows'
84 : $system{type} eq 'Unix' # I think I need to add a Solaris type
85 ? '/usr/xpg4/bin/df -l -P'
88 my ($status, @output) = $clearexec->execute ($cmd);
90 error "Unable to execute $cmd - $! (Status: $status)\n" . join ("\n". @output), $status
93 # Real file systems start with "/"
94 my @fs = grep { /^\// } @output;
96 # Also add lines that start with rpool (This is for Solaris zones
97 push @fs, grep { /^rpool/ } @output;
102 if (/^(\S+)\s+(\S+).+?(\S+)$/) {
105 $filesystem{system} = $system{name};
106 $filesystem{filesystem} = $1;
107 $filesystem{fstype} = $2;
108 $filesystem{mount} = $3;
110 push @filesystems, \%filesystem;
117 sub GatherSysInfo (;%) {
120 # Set name if not currently set
121 $system{name} = $host
122 unless $system{name};
124 my ($status, @output);
126 $system{port} ||= $port;
128 # Connect to clearexec server
129 $status = $clearexec->connectToServer ($system{name}, $system{port});
132 warning "Unable to connect to $system{name}:$port";
137 my $cmd = 'uname -a';
139 ($status, @output) = $clearexec->execute ($cmd);
141 error "Unable to execute '$cmd' - $!", $status . join ("\n". @output)
144 $system{os} = $output[0];
146 $system{clearagent} = 1;
150 ($status, @output) = $clearexec->execute ($cmd);
152 error "Unable to execute '$cmd' - $!", $status . join ("\n". @output)
155 # TODO: Need to handle this better
156 if ($output[0] =~ /sunos/i) {
157 $system{type} = 'Unix';
158 } elsif ($output[0] =~ /cygwin/i) {
159 $system{type} = 'Windows';
161 $system{type} = 'Linux';
167 sub AddFilesystems (%) {
172 for (GetFilesystems %system) {
173 my %filesystem = %{$_};
175 my %oldfilesystem = $clearadm->GetFilesystem (
177 $filesystem{filesystem}
180 if (%oldfilesystem) {
181 verbose "Updating filesystem $filesystem{system}:$filesystem{filesystem}";
183 ($err, $msg) = $clearadm->UpdateFilesystem (
185 $filesystem{filesystem},
189 error 'Unable to update filesystem '
190 . "$filesystem{system}:$filesystem{filesystem}"
193 verbose 'Adding filesystem '
194 . "$filesystem{system}:$filesystem{filesystem}";
196 ($err, $msg) = $clearadm->AddFilesystem (%filesystem);
198 error 'Unable to add filesystem '
199 . "$filesystem{system}:$filesystem{filesystem}"
210 verbose "Adding newhost $system";
212 my %system = GatherSysInfo;
214 # If GatherSysInfo was able to connect to clearagent it will set this field
215 my $clearagent = delete $system{clearagent};
217 my ($err, $msg) = $clearadm->AddSystem (%system);
223 return AddFilesystems %system;
229 sub UpdateSystem (%) {
234 %system = GatherSysInfo (%system);
236 # If GatherSysInfo was able to connect to clearagent it will set this field
237 my $clearagent = delete $system{clearagent};
239 return ($err, $msg) unless $clearagent;
241 verbose "Updating existing host $system{name}";
243 ($err, $msg) = $clearadm->UpdateSystem ($system{name}, %system);
245 return ($err, $msg) if $err;
247 ($err, $msg) = AddFilesystems %system;
249 $clearexec->disconnectFromServer;
256 $port = $Clearexec::CLEAROPTS{CLEAREXEC_PORT};
259 'usage' => sub { Usage },
260 'verbose' => sub { set_verbose },
261 'debug' => sub { set_debug },
262 'delete' => \$delete,
265 ) or Usage "Invalid parameter";
267 Usage 'Extraneous options: ' . join ' ', @ARGV
271 error "Must specify -host if you specify -delete", 1
276 verbose "$FindBin::Script V$VERSION";
281 display_nolf "Delete host $host (y/N):";
283 my $answer = <STDIN>;
285 if ($answer =~ /(y|yes)/i) {
286 ($err, $msg) = $clearadm->DeleteSystem ($host);
289 error "No host named $host in database";
291 error "Unable to delete $host" . $msg, $err;
293 verbose "Deleted host $host";
296 display "Host $host not deleted";
299 if ($host eq 'all') {
300 for ($clearadm->FindSystem) {
303 ($err, $msg) = UpdateSystem (%system);
305 error "Unable to update host $system{name}\n$msg", $err
309 my %system = $clearadm->GetSystem ($host);
312 ($err, $msg) = UpdateSystem (%system);
314 ($err, $msg) = AddSystem ($host);
318 my $errmsg = 'Unable to ';
319 $errmsg .= %system ? 'update' : 'add';
320 $errmsg .= " host $host\$msg";
322 error "Unable to add host $host\n$msg", $err;
329 =head1 CONFIGURATION AND ENVIRONMENT
331 DEBUG: If set then $debug is set to this level.
333 VERBOSE: If set then $verbose is set to this level.
335 TRACE: If set then $trace is set to this level.
343 L<Getop::Long|Getopt::Long>
345 L<Sys::Hostname|Sys::Hostname>
347 =head2 ClearSCM Perl Modules
361 <a href="http://clearscm.com/php/scm_man.php?file=clearadm/lib/Clearadm.pm">Clearadm</a><br>
362 <a href="http://clearscm.com/php/scm_man.php?file=clearadm/lib/Clearexec.pm">Clearexec</a><br>
363 <a href="http://clearscm.com/php/scm_man.php?file=lib/Display.pm">Display</a><br>
364 <a href="http://clearscm.com/php/scm_man.php?file=lib/Utils.pm">Utils</a><br>
369 =head1 BUGS AND LIMITATIONS
371 There are no known bugs in this script
373 Please report problems to Andrew DeFaria <Andrew@ClearSCM.com>.
375 =head1 LICENSE AND COPYRIGHT
377 Copyright (c) 2010, ClearSCM, Inc. All rights reserved.