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' ? 'df -TP' : 'df -l -TP';
84 my ($status, @output) = $clearexec->execute ($cmd);
86 error "Unable to execute uname -a - $!", $status . join ("\n". @output)
89 # Real file systems start with "/"
90 @output = grep { /^\// } @output;
95 if (/^(\S+)\s+(\S+).+?(\S+)$/) {
98 $filesystem{system} = $system{name};
99 $filesystem{filesystem} = $1;
100 $filesystem{fstype} = $2;
101 $filesystem{mount} = $3;
103 push @filesystems, \%filesystem;
110 sub GatherSysInfo (;%) {
113 # Set name if not currently set
114 $system{name} = $host
115 unless $system{name};
117 my ($status, @output);
119 $system{port} ||= $port;
121 # Connect to clearexec server
122 $status = $clearexec->connectToServer ($system{name}, $system{port});
125 warning "Unable to connect to $system{name}:$port";
130 my $cmd = 'uname -a';
132 ($status, @output) = $clearexec->execute ($cmd);
134 error "Unable to execute '$cmd' - $!", $status . join ("\n". @output)
137 $system{os} = $output[0];
139 $system{clearagent} = 1;
143 ($status, @output) = $clearexec->execute ($cmd);
145 error "Unable to execute '$cmd' - $!", $status . join ("\n". @output)
148 # TODO: Need to handle this better
149 $system{type} = $output[0] =~ /cygwin/i ? 'Windows' : $output[0];
154 sub AddFilesystems (%) {
159 foreach (GetFilesystems %system) {
160 my %filesystem = %{$_};
162 my %oldfilesystem = $clearadm->GetFilesystem (
164 $filesystem{filesystem}
167 if (%oldfilesystem) {
168 verbose "Updating filesystem $filesystem{system}:$filesystem{filesystem}";
170 ($err, $msg) = $clearadm->UpdateFilesystem (
172 $filesystem{filesystem},
176 error 'Unable to update filesystem '
177 . "$filesystem{system}:$filesystem{filesystem}"
180 verbose 'Adding filesystem '
181 . "$filesystem{system}:$filesystem{filesystem}";
183 ($err, $msg) = $clearadm->AddFilesystem (%filesystem);
185 error 'Unable to add filesystem '
186 . "$filesystem{system}:$filesystem{filesystem}"
197 verbose "Adding newhost $system";
199 my %system = GatherSysInfo;
201 # If GatherSysInfo was able to connect to clearagent it will set this field
202 my $clearagent = delete $system{clearagent};
204 my ($err, $msg) = $clearadm->AddSystem (%system);
210 return AddFilesystems %system;
216 sub UpdateSystem (%) {
221 %system = GatherSysInfo (%system);
223 # If GatherSysInfo was able to connect to clearagent it will set this field
224 my $clearagent = delete $system{clearagent};
226 return ($err, $msg) unless $clearagent;
228 verbose "Updating existing host $system{name}";
230 ($err, $msg) = $clearadm->UpdateSystem ($system{name}, %system);
232 return ($err, $msg) if $err;
234 ($err, $msg) = AddFilesystems %system;
236 $clearexec->disconnectFromServer;
243 $port = $Clearexec::CLEAROPTS{CLEAREXEC_PORT};
246 'usage' => sub { Usage },
247 'verbose' => sub { set_verbose },
248 'debug' => sub { set_debug },
249 'delete' => \$delete,
252 ) or Usage "Invalid parameter";
254 Usage 'Extraneous options: ' . join ' ', @ARGV
258 error "Must specify -host if you specify -delete", 1
263 verbose "$FindBin::Script V$VERSION";
268 display_nolf "Delete host $host (y/N):";
270 my $answer = <STDIN>;
272 if ($answer =~ /(y|yes)/i) {
273 ($err, $msg) = $clearadm->DeleteSystem ($host);
276 error "No host named $host in database";
278 error "Unable to delete $host" . $msg, $err;
280 verbose "Deleted host $host";
283 display "Host $host not deleted";
286 if ($host eq 'all') {
287 foreach ($clearadm->FindSystem) {
290 ($err, $msg) = UpdateSystem (%system);
292 error "Unable to update host $system{name}\n$msg", $err
296 my %system = $clearadm->GetSystem ($host);
299 ($err, $msg) = UpdateSystem (%system);
301 ($err, $msg) = AddSystem ($host);
305 my $errmsg = 'Unable to ';
306 $errmsg .= %system ? 'update' : 'add';
307 $errmsg .= " host $host\$msg";
309 error "Unable to add host $host\n$msg", $err;
316 =head1 CONFIGURATION AND ENVIRONMENT
318 DEBUG: If set then $debug is set to this level.
320 VERBOSE: If set then $verbose is set to this level.
322 TRACE: If set then $trace is set to this level.
330 L<Getop::Long|Getopt::Long>
332 L<Sys::Hostname|Sys::Hostname>
334 =head2 ClearSCM Perl Modules
348 <a href="http://clearscm.com/php/scm_man.php?file=clearadm/lib/Clearadm.pm">Clearadm</a><br>
349 <a href="http://clearscm.com/php/scm_man.php?file=clearadm/lib/Clearexec.pm">Clearexec</a><br>
350 <a href="http://clearscm.com/php/scm_man.php?file=lib/Display.pm">Display</a><br>
351 <a href="http://clearscm.com/php/scm_man.php?file=lib/Utils.pm">Utils</a><br>
356 =head1 BUGS AND LIMITATIONS
358 There are no known bugs in this script
360 Please report problems to Andrew DeFaria <Andrew@ClearSCM.com>.
362 =head1 LICENSE AND COPYRIGHT
364 Copyright (c) 2010, ClearSCM, Inc. All rights reserved.