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 if $status;
92 # Real file systems start with "/"
93 my @fs = grep { /^\// } @output;
95 # Also add lines that start with rpool (This is for Solaris zones
96 push @fs, grep { /^rpool/ } @output;
101 if (/^(\S+)\s+(\S+).+?(\S+)$/) {
104 $filesystem{system} = $system{name};
105 $filesystem{filesystem} = $1;
106 $filesystem{fstype} = $2;
107 $filesystem{mount} = $3;
109 push @filesystems, \%filesystem;
116 sub GatherSysInfo(;%) {
119 # Set name if not currently set
120 $system{name} = $host unless $system{name};
122 my ($status, @output);
124 $system{port} ||= $port;
126 # Connect to clearexec server
127 $status = $clearexec->connectToServer($system{name}, $system{port});
130 warning "Unable to connect to $system{name}:$port";
135 my $cmd = 'uname -a';
137 ($status, @output) = $clearexec->execute($cmd);
139 error "Unable to execute '$cmd' - $!", $status . join("\n". @output) if $status;
141 $system{os} = $output[0];
143 $system{clearagent} = 1;
147 ($status, @output) = $clearexec->execute($cmd);
149 error "Unable to execute '$cmd' - $!", $status . join("\n". @output) if $status;
151 # TODO: Need to handle this better
152 if ($output[0] =~ /sunos/i) {
153 $system{type} = 'Unix';
154 } elsif ($output[0] =~ /cygwin/i) {
155 $system{type} = 'Windows';
157 $system{type} = 'Linux';
163 sub AddFilesystems(%) {
168 for (GetFilesystems %system) {
169 my %filesystem = %{$_};
171 my %oldfilesystem = $clearadm->GetFilesystem(
173 $filesystem{filesystem}
176 if (%oldfilesystem) {
177 verbose "Updating filesystem $filesystem{system}:$filesystem{filesystem}";
179 ($err, $msg) = $clearadm->UpdateFilesystem(
181 $filesystem{filesystem},
185 error 'Unable to update filesystem '
186 . "$filesystem{system}:$filesystem{filesystem}" if $err;
188 verbose 'Adding filesystem '
189 . "$filesystem{system}:$filesystem{filesystem}";
191 ($err, $msg) = $clearadm->AddFilesystem(%filesystem);
193 error 'Unable to add filesystem '
194 . "$filesystem{system}:$filesystem{filesystem}" if $err;
204 verbose "Adding newhost $system";
206 my %system = GatherSysInfo;
208 # If GatherSysInfo was able to connect to clearagent it will set this field
209 my $clearagent = delete $system{clearagent};
211 my ($err, $msg) = $clearadm->AddSystem(%system);
213 return ($err, $msg) if $err;
216 return AddFilesystems %system;
222 sub UpdateSystem(%) {
227 %system = GatherSysInfo(%system);
229 # If GatherSysInfo was able to connect to clearagent it will set this field
230 my $clearagent = delete $system{clearagent};
232 return ($err, $msg) unless $clearagent;
234 verbose "Updating existing host $system{name}";
236 ($err, $msg) = $clearadm->UpdateSystem($system{name}, %system);
238 return ($err, $msg) if $err;
240 ($err, $msg) = AddFilesystems %system;
242 $clearexec->disconnectFromServer;
249 $port = $Clearexec::CLEAROPTS{CLEAREXEC_PORT};
252 'usage' => sub { Usage },
253 'verbose' => sub { set_verbose },
254 'debug' => sub { set_debug },
255 'delete' => \$delete,
258 ) or Usage "Invalid parameter";
260 Usage 'Extraneous options: ' . join ' ', @ARGV if @ARGV;
263 error "Must specify -host if you specify -delete", 1 unless $host;
267 verbose "$FindBin::Script V$VERSION";
272 display_nolf "Delete host $host (y/N):";
274 my $answer = <STDIN>;
276 if ($answer =~ /(y|yes)/i) {
277 ($err, $msg) = $clearadm->DeleteSystem($host);
280 error "No host named $host in database";
282 error "Unable to delete $host" . $msg, $err;
284 verbose "Deleted host $host";
287 display "Host $host not deleted";
290 if ($host eq 'all') {
291 for ($clearadm->FindSystem) {
294 ($err, $msg) = UpdateSystem(%system);
296 error "Unable to update host $system{name}\n$msg", $err if $err;
299 my %system = $clearadm->GetSystem($host);
302 ($err, $msg) = UpdateSystem(%system);
304 ($err, $msg) = AddSystem($host);
308 my $errmsg = 'Unable to ';
309 $errmsg .= %system ? 'update' : 'add';
310 $errmsg .= " host $host\$msg";
312 error "Unable to add host $host\n$msg", $err;
319 =head1 CONFIGURATION AND ENVIRONMENT
321 DEBUG: If set then $debug is set to this level.
323 VERBOSE: If set then $verbose is set to this level.
325 TRACE: If set then $trace is set to this level.
333 L<Getop::Long|Getopt::Long>
335 L<Sys::Hostname|Sys::Hostname>
337 =head2 ClearSCM Perl Modules
351 <a href="http://clearscm.com/php/scm_man.php?file=clearadm/lib/Clearadm.pm">Clearadm</a><br>
352 <a href="http://clearscm.com/php/scm_man.php?file=clearadm/lib/Clearexec.pm">Clearexec</a><br>
353 <a href="http://clearscm.com/php/scm_man.php?file=lib/Display.pm">Display</a><br>
354 <a href="http://clearscm.com/php/scm_man.php?file=lib/Utils.pm">Utils</a><br>
359 =head1 BUGS AND LIMITATIONS
361 There are no known bugs in this script
363 Please report problems to Andrew DeFaria <Andrew@ClearSCM.com>.
365 =head1 LICENSE AND COPYRIGHT
367 Copyright (c) 2010, ClearSCM, Inc. All rights reserved.