Initial commit
[clearscm.git] / clearadm / discovery.pl
1 #!/usr/bin/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: 30 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 Utils;
66
67 my $VERSION  = '$Revision: 1.1 $';
68   ($VERSION) = ($VERSION =~ /\$Revision: (.*) /);
69
70 my $clearadm = Clearadm->new;
71
72 my $broadcastTime = 10;
73
74 sub discover ($) {
75   my ($broadcast) = @_;
76   
77   my $startTime = time;
78
79   my %hosts;
80
81   verbose "Performing discovery (for $broadcastTime seconds)...";
82
83   while (<$broadcast>) {
84     if (/from (.*):/) {
85       my $ip       = $1;
86       my $hostname = gethostbyaddr (inet_aton ($ip), AF_INET);
87      
88        unless ($hosts{$ip}) {
89          verbose "Received response from ($ip): $hostname";
90          $hosts{$ip} = $hostname;
91        } # unless
92     } # if
93   
94     last
95       if (time () - $startTime) > $broadcastTime;
96   } # while
97
98   verbose "$broadcastTime seconds has elapsed - discovery complete";
99
100   return %hosts
101 } # discover
102
103 # Main
104 my $broadcastAddress = inet_ntoa (INADDR_BROADCAST);
105
106 GetOptions (
107   usage             => sub { Usage },
108   verbose           => sub { set_verbose },
109   debug             => sub { set_debug },
110   'broadcastTime=s' => \$broadcastTime,
111   'broadcastAddr=s' => \$broadcastAddress,  
112 ) or Usage "Invalid parameter";
113
114 Usage 'Extraneous options: ' . join ' ', @ARGV
115   if @ARGV;
116
117 # Announce ourselves
118 verbose "$FindBin::Script V$VERSION";
119
120 my $broadcastCmd = "ping -b $broadcastAddress 2>&1";
121
122 my $pid = open my $broadcast, '-|', $broadcastCmd
123   or error "Unable to do $broadcastCmd", 1;
124
125 my %hosts = discover $broadcast;
126
127 kill TERM => $pid;
128
129 close $broadcast;
130
131 my $nbrHosts = scalar keys %hosts;  
132
133 verbose_nolf "Found $nbrHosts host";
134 verbose_nolf 's' if $nbrHosts != 1;
135 verbose      " on subnet $broadcastAddress";
136
137 foreach (sort values %hosts) {
138   my $verbose = get_verbose () ? '-verbose' : '';
139   
140   my ($status, @output) = Execute "updatesystem.pl -host $_ $verbose";
141
142   error "Unable to update host $_ (Status: $status)\n"
143       . join ("\n", @output), 1
144     if $status;
145     
146   verbose join "\n", @output;
147 } # foreach
148
149 =pod
150
151 =head1 CONFIGURATION AND ENVIRONMENT
152
153 DEBUG: If set then $debug is set to this level.
154
155 VERBOSE: If set then $verbose is set to this level.
156
157 TRACE: If set then $trace is set to this level.
158
159 =head1 DEPENDENCIES
160
161 =head2 Perl Modules
162
163 L<FindBin>
164
165 L<Getop::Long|Getopt::Long>
166
167 L<Socket>
168
169 =head2 ClearSCM Perl Modules
170
171 =begin man 
172
173  Clearadm
174  Display
175  Utils
176
177 =end man
178
179 =begin html
180
181 <blockquote>
182 <a href="http://clearscm.com/php/cvs_man.php?file=clearadm/lib/Clearadm.pm">Clearadm</a><br>
183 <a href="http://clearscm.com/php/cvs_man.php?file=lib/Display.pm">Display</a><br>
184 <a href="http://clearscm.com/php/cvs_man.php?file=lib/Utils.pm">Utils</a><br>
185 </blockquote>
186
187 =end html
188
189 =head1 BUGS AND LIMITATIONS
190
191 There are no known bugs in this script
192
193 Please report problems to Andrew DeFaria <Andrew@ClearSCM.com>.
194
195 =head1 LICENSE AND COPYRIGHT
196
197 Copyright (c) 2010, ClearSCM, Inc. All rights reserved.
198
199 =cut