Removed /usr/local from CDPATH
[clearscm.git] / clearadm / discovery.pl
1 #!/usr/bin/env perl
2
3 =pod
4
5 =head1 NAME $RCSfile: discovery.pl,v $
6
7 Update System
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.1 $
20
21 =item Created:
22
23 Mon Dec 13 09:13:27 EST 2010
24
25 =item Modified:
26
27 $Date: 2011/01/07 20:48:22 $
28
29 =back
30
31 =head1 SYNOPSIS
32
33  Usage updatesystem.pl: [-u|sage] [-ve|rbose] [-deb|ug]
34                         [-b|roadcastTime <seconds>]
35
36  Where:
37    -u|sage:       Displays usage
38  
39    -ve|rbose:     Be verbose
40    -deb|ug:       Output debug messages
41    
42    -broadcastA|ddr <ip>:      Broadcast IP (Default: Current subnet)
43    -broadcastT|ime <seconds>: Number of sends to wait for responses to broadcast
44                               (Default: 10 seconds)
45
46 =head1 DESCRIPTION
47
48 This script will discover systems on the local subnet and then add or update
49 them in the Clearadm database.
50
51 =cut
52
53 use strict;
54 use warnings;
55
56 use Socket;
57
58 use FindBin;
59 use Getopt::Long;
60
61 use lib "$FindBin::Bin/lib", "$FindBin::Bin/../lib";
62
63 use Clearadm;
64 use Display;
65 use OSDep;
66 use Utils;
67
68 my $VERSION  = '$Revision: 1.1 $';
69   ($VERSION) = ($VERSION =~ /\$Revision: (.*) /);
70
71 my $clearadm = Clearadm->new;
72
73 my $broadcastTime = 10;
74
75 sub discover($) {
76   my ($broadcast) = @_;
77   
78   my $startTime = time;
79
80   my %hosts;
81
82   verbose "Performing discovery (for $broadcastTime seconds)...";
83
84   while (<$broadcast>) {
85     display "Received line: $_";
86     if (/from (\S+) \((.+)\)/) {
87       my $hostname = $1;
88       my $ip       = $2;
89
90       # Remove domain
91       $hostname =~ s/(\w+)\..*/$1/;
92
93       unless ($hosts{$ip}) {
94         verbose "Received response from ($ip): $hostname";
95         $hosts{$ip} = $hostname;
96       } # unless
97     } # if
98   
99     last if (time() - $startTime) > $broadcastTime;
100   } # while
101
102   verbose "$broadcastTime seconds has elapsed - discovery complete";
103
104   return %hosts
105 } # discover
106
107 # Main
108 my $broadcastAddress = inet_ntoa(INADDR_BROADCAST);
109
110 GetOptions(
111   usage             => sub { Usage },
112   verbose           => sub { set_verbose },
113   debug             => sub { set_debug },
114   'broadcastTime=s' => \$broadcastTime,
115   'broadcastAddr=s' => \$broadcastAddress,  
116 ) or Usage "Invalid parameter";
117
118 Usage 'Extraneous options: ' . join ' ', @ARGV if @ARGV;
119
120 # Announce ourselves
121 verbose "$FindBin::Script V$VERSION";
122
123 my $broadcastCmd = 'ping ';
124
125 if ($ARCHITECTURE eq 'solaris') {
126   $broadcastCmd .= '-s ';
127 } else {
128   $broadcastCmd .= '-b ';
129 } # if
130
131 $broadcastCmd .= "$broadcastAddress 2>&1";
132   
133 my $pid = open my $broadcast, '-|', $broadcastCmd
134   or error "Unable to do $broadcastCmd", 1;
135
136 my %hosts = discover $broadcast;
137
138 kill TERM => $pid;
139
140 close $broadcast;
141
142 my $nbrHosts = scalar keys %hosts;  
143
144 verbose_nolf "Found $nbrHosts host";
145 verbose_nolf 's' if $nbrHosts != 1;
146 verbose      " on subnet $broadcastAddress";
147
148 for (sort values %hosts) {
149   my $verbose = get_verbose() ? '-verbose' : '';
150   
151   my ($status, @output) = Execute "updatesystem.pl -host $_ $verbose";
152
153   error "Unable to update host $_ (Status: $status)\n" . join ("\n", @output), 1 if $status;
154     
155   verbose join "\n", @output;
156 } # for
157
158 =pod
159
160 =head1 CONFIGURATION AND ENVIRONMENT
161
162 DEBUG: If set then $debug is set to this level.
163
164 VERBOSE: If set then $verbose is set to this level.
165
166 TRACE: If set then $trace is set to this level.
167
168 =head1 DEPENDENCIES
169
170 =head2 Perl Modules
171
172 L<FindBin>
173
174 L<Getop::Long|Getopt::Long>
175
176 L<Socket>
177
178 =head2 ClearSCM Perl Modules
179
180 =begin man 
181
182  Clearadm
183  Display
184  Utils
185
186 =end man
187
188 =begin html
189
190 <blockquote>
191 <a href="http://clearscm.com/php/scm_man.php?file=clearadm/lib/Clearadm.pm">Clearadm</a><br>
192 <a href="http://clearscm.com/php/scm_man.php?file=lib/Display.pm">Display</a><br>
193 <a href="http://clearscm.com/php/scm_man.php?file=lib/Utils.pm">Utils</a><br>
194 </blockquote>
195
196 =end html
197
198 =head1 BUGS AND LIMITATIONS
199
200 There are no known bugs in this script
201
202 Please report problems to Andrew DeFaria <Andrew@ClearSCM.com>.
203
204 =head1 LICENSE AND COPYRIGHT
205
206 Copyright (c) 2010, ClearSCM, Inc. All rights reserved.
207
208 =cut