view release on metacpan or search on metacpan
lib/App/Netsync.pm view on Meta::CPAN
foreach my $node (@nodes) {
# Establish a connection to the node.
my ($session,$info) = App::Netsync::SNMP::Info $node->{'ip'};
if (defined $info) {
$node->{'session'} = $session;
$node->{'info'} = $info;
}
else { # Otherwise, consider it inactive.
note ($config{'NodeLog'},node_string ($node).' inactive');
say node_string ($node).' inactive' if $config{'Verbose'};
next;
}
# Retrieve the serials of devices at the node.
my $serial2if2ifName = device_interfaces ($node->{'info'}->vendor,$node->{'session'});
if (defined $serial2if2ifName) {
my @serials = keys %$serial2if2ifName;
note ($config{'NodeLog'},node_string ($node).' '.join (' ',@serials));
node_initialize ($node,$serial2if2ifName);
$serial_count += @serials;
}
else { # Otherwise, consider the device unrecognized.
note ($config{'NodeLog'},node_string ($node).' unrecognized');
say node_string ($node).' unrecognized' if $config{'Verbose'};
next;
}
# Show the user what's been found if necessary.
node_dump $node if $config{'Verbose'};
}
return $serial_count;
}
lib/App/Netsync.pm view on Meta::CPAN
return undef;
}
# Initialize output table headers.
my $fields = $config{'DeviceField'}.','.$config{'InterfaceField'};
$fields .= ','.join (',',sort @{$config{'InfoFields'}});
my %inputs = (
'DB' => sub {
my %drivers = DBI->installed_drivers;
say $_ foreach values %drivers; exit; #XXX debug
unless (defined $config{'DB'}) {
warn 'A database has not been configured.';
return undef;
}
# Connect to the database.
my $DSN = 'dbi:'.$config{'DBMS'};
$DSN .= ':host='.$config{'Server'};
$DSN .= ';port='.$config{'Port'};
$DSN .= ';database='.$config{'Database'};
lib/App/Netsync.pm view on Meta::CPAN
print "\b"x$config{'DeviceOrder'};
printf ('%'.$config{'DeviceOrder'}.'d',$successful_update_count);
}
}
}
else { # Log a failed update.
note ($config{'UpdateLog'},$note.' error: '.$error);
++$failed_update_count;
if ($config{'Verbose'}) {
say interface_string ($interface).' failed';
say ((' 'x$config{'Indent'}).$error);
}
}
}
}
}
# Show the user how many nodes have been updated if necessary.
unless ($config{'Quiet'}) {
print $successful_update_count if $config{'Verbose'};
print ' successful';
lib/App/Netsync/Configurator.pm view on Meta::CPAN
=item F<foobar.pl>
#!/usr/bin/env perl
use feature 'say';
use App::Netsync::Configurator;
configurate 'foobar.ini';
say App::Netsync::Configurator::config('testGroup','fooSetting');
say (App::Netsync::Configurator::config('testGroup','bazSetting'))[2];
say {App::Netsync::Configurator::config('testGroup')}->{'barSetting'};
=back
$ perl foobar.pl
> barValue
> baz
> bazValue
=cut
lib/App/Netsync/Configurator.pm view on Meta::CPAN
my ($file,$overrides,$defaults) = @_;
$file //= '/etc/'.$SCRIPT.'/'.$SCRIPT.'.ini';
$overrides //= {};
$defaults //= {};
{
open (my $ini,'<',$file);
my $parser = Config::Simple->new($file);
my $syntax = $parser->guess_syntax($ini);
unless (defined $syntax and $syntax eq 'ini') {
say 'The configuration file "'.$file.'" is malformed.';
return undef;
}
close $ini;
}
$config{$_} = $defaults->{$_} foreach keys %$defaults;
{
my %imports;
Config::Simple->import_from($file,\%imports);
lib/App/Netsync/Configurator.pm view on Meta::CPAN
prints the current configuration (use sparingly)
I<Note: configurate needs to be run first!>
=cut
sub dump {
warn 'too many arguments' if @_ > 0;
say $_.' = '.($config{$_} // 'undef') foreach sort keys %config;
}
=head1 AUTHOR
David Tucker, C<< <dmtucker at ucsc.edu> >>
=head1 BUGS
Please report any bugs or feature requests to C<bug-netsync at rt.cpan.org>, or through
lib/App/Netsync/Network.pm view on Meta::CPAN
my %node;
$node{'ip'} = '10.0.0.1';
$node{'hostname'} = 'host1';
$node{'session'} = App::Netsync::SNMP::Session $node{'ip'};
$node{'info'} = App::Netsync::SNMP::Info $session;
my $serial2if2ifName = device_interfaces ($node{'info'}->vendor,$node{'session'});
node_initialize (\%node,$serial2if2ifName);
say node_string \%node;
node_dump \%node;
# or
device_initialize (\%node,$_,$serial2if2ifName->{$_}) foreach keys $serial2if2ifName;
foreach my $serial (keys %{$node{'devices'}}) {
my $device = $node{'devices'}{$serial};
say device_string $device;
device_dump $device;
}
# or
foreach my $serial (keys %serial2if2ifName) {
$node->{'devices'}{$serial} = \%device;
my $device = $node{'devices'}{$serial};
$device->{'node'} = $node;
my $if2ifName = $serial2if2ifName{$serial};
interface_initialize ($device,$if2ifName->{$_},$_) foreach keys %$if2ifName;
}
foreach my $serial (keys %{$node{'devices'}}) {
my $device = $node{'devices'}{$serial};
foreach my $ifName (keys %{$device->{'interfaces'}}) {
my interface = device->{'interfaces'}{$ifName};
say interface_string $interface;
interface_dump $interface;
}
}
my %nodes;
$nodes{'10.0.0.1'} = \%node;
$nodes{'10.0.0.2'}{'ip'} = '10.0.0.2';
$nodes{'10.0.0.3'}{'ip'} = '10.0.0.3';
$nodes{'10.0.0.4'}{'ip'} = '10.0.0.4';
lib/App/Netsync/Network.pm view on Meta::CPAN
=back
=cut
sub node_dump {
warn 'too few arguments' if @_ < 1;
my (@nodes) = @_;
foreach my $node (@nodes) {
say node_string $node;
my $device_count = 0;
if (defined $node->{'devices'}) {
$device_count = scalar keys %{$node->{'devices'}};
my ($identified_device_count,$interface_count,$identified_interface_count) = (0,0,0);
foreach my $serial (keys %{$node->{'devices'}}) {
my $device = $node->{'devices'}{$serial};
++$identified_device_count if $device->{'identified'};
next unless defined $device->{'interfaces'};
lib/App/Netsync/Network.pm view on Meta::CPAN
print "\n";
print ((' 'x$config{'Indent'}).$interface_count.' interface');
print 's' if $interface_count > 1;
print ' ('.$identified_interface_count.' identified)' if $identified_interface_count > 0;
print "\n";
}
if (defined $node->{'info'}) {
my $info = $node->{'info'};
if ($device_count == 1) {
#say ((' 'x$config{'Indent'}).$info->class);
say ((' 'x$config{'Indent'}).$info->vendor.' '.$info->model);
say ((' 'x$config{'Indent'}).$info->serial);
}
}
}
say scalar (@nodes).' nodes' if @nodes > 1;
}
=head2 device_dump
prints a device structure
B<Arguments>
I<( @devices )>
lib/App/Netsync/Network.pm view on Meta::CPAN
=back
=cut
sub device_dump {
warn 'too few arguments' if @_ < 1;
my (@devices) = @_;
foreach my $device (@devices) {
say device_string $device;
if (defined $device->{'identified'}) {
say ((' 'x$config{'Indent'}).(($device->{'identified'}) ? 'identified' : 'unidentified'));
}
if (defined $device->{'interfaces'}) {
my $interface_count = scalar keys %{$device->{'interfaces'}};
my $identified_interface_count = 0;
foreach my $ifName (keys %{$device->{'interfaces'}}) {
my $interface = $device->{'interfaces'}{$ifName};
++$identified_interface_count if $interface->{'identified'};
}
print ((' 'x$config{'Indent'}).$interface_count.' interface');
print 's' if $interface_count > 1;
say ' ('.$identified_interface_count.' identified)';
}
}
say scalar (@devices).' devices' if @devices > 1;
}
=head2 interface_dump
prints an interface structure
B<Arguments>
I<( @interfaces )>
lib/App/Netsync/Network.pm view on Meta::CPAN
=back
=cut
sub interface_dump {
warn 'too few arguments' if @_ < 1;
my (@interfaces) = @_;
foreach my $interface (@interfaces) {
say interface_string $interface;
if (defined $interface->{'identified'}) {
say ((' 'x$config{'Indent'}).(($interface->{'identified'}) ? 'identified' : 'unidentified'));
}
if (defined $interface->{'info'}) {
foreach my $field (sort keys %{$interface->{'info'}}) {
print ((' 'x$config{'Indent'}).$field.': ');
say (($interface->{'info'}{$field} =~ /[\S]+/) ? $interface->{'info'}{$field} : '(empty)');
}
}
}
say scalar (@interfaces).' interfaces' if @interfaces > 1;
}
################################################################################
lib/App/Netsync/Scribe.pm view on Meta::CPAN
sub note {
warn 'too few arguments' if @_ < 2;
warn 'too many arguments' if @_ > 4;
my ($file,$note,$stamp,$mode) = @_;
$stamp //= 1;
$mode //= '>>';
open ($files{$file},$mode,$file) unless defined $files{$file};
print {$files{$file}} timestamp.' ' if $stamp;
say {$files{$file}} $note;
return 1;
}
END {
foreach my $file (keys %files) {
close $files{$file} if defined $files{$file};
}
}
script/netsync view on Meta::CPAN
$Getopt::Std::STANDARD_HELP_VERSION = 1;
$| = 1;
$config{'Options'} = 'c:p:Dm:d:au';
$config{'Arguments'} = '[nodes]';
}
sub VERSION_MESSAGE {
say 'Perl v'.$];
say $SCRIPT.' '.$VERSION;
say 'App::Netsync '.$App::Netsync::VERSION;
say 'App::Netsync::Configurator '.$App::Netsync::Configurator::VERSION;
say 'App::Netsync::Network '.$App::Netsync::Network::VERSION;
say 'App::Netsync::Scribe '.$App::Netsync::Scribe::VERSION;
say 'App::Netsync::SNMP '.$App::Netsync::SNMP::VERSION;
say '[Core Modules]';
say ' File::Basename v'.$File::Basename::VERSION;
say ' Getopt::Std v'.$Getopt::Std::VERSION;
say ' Pod::Usage v'.$Pod::Usage::VERSION;
say ' POSIX v'.$POSIX::VERSION;
say ' Scalar::Util v'.$Scalar::Util::VERSION;
say '[CPAN Modules]';
say ' Config::Simple v'.$Config::Simple::VERSION;
say ' DBI v'.$DBI::VERSION;
say ' Net::DNS v'.$Net::DNS::VERSION;
say ' SNMP v'.$SNMP::VERSION;
say ' SNMP::Info v'.$SNMP::Info::VERSION;
say ' Text::CSV v'.$Text::CSV::VERSION;
}
sub HELP_MESSAGE {
my $opts = $config{'Options'};
$opts =~ s/://g;
pod2usage({
'-message' => $SCRIPT.' [-'.$opts.'] '.$config{'Arguments'},
'-exitval' => 0,
'-verbose' => 0,
script/netsync view on Meta::CPAN
HELP_MESSAGE and exit if $opts{'h'};
VERSION_MESSAGE and exit if $opts{'V'};
$config{'Verbose'} = (defined $opts{'q'}) ? 0 : $opts{'v'} // 0;
$config{'Quiet'} = $opts{'q'} // 0;
{ # Read the configuration file.
$config{'File'} = $opts{'c'} // '/etc/'.$SCRIPT.'/'.$SCRIPT.'.ini';
print 'configuring (using '.$config{'File'}.')...' unless $config{'Quiet'};
my %conf = App::Netsync::Configurator::configurate ($config{'File'});
$config{$_} = $conf{$_} foreach keys %conf;
say ' done' unless $config{'Quiet'};
}
$config{'ProbeLevel'} = $opts{'p'} // 0;
unless ($config{'ProbeLevel'} =~ /^[0-2]$/) {
warn 'Invalid ProbeLevel';
HELP_MESSAGE and exit 1;
}
$config{'Probe1Cache'} //= '/var/cache/'.$SCRIPT.'/active.txt';
$config{'Probe2Cache'} //= '/var/cache/'.$SCRIPT.'/synced.csv';