#!perl
our
(
$SCRIPT
,
$VERSION
);
our
%config
;
BEGIN {
(
$SCRIPT
) = ($0);
(
$VERSION
) = (3.00);
$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
.
' v'
.
$VERSION
;
say
'App::Netsync v'
.
$App::Netsync::VERSION
;
say
'App::Netsync::Configurator v'
.
$App::Netsync::Configurator::VERSION
;
say
'App::Netsync::Network v'
.
$App::Netsync::Network::VERSION
;
say
'App::Netsync::Scribe v'
.
$App::Netsync::Scribe::VERSION
;
say
'App::Netsync::SNMP v'
.
$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,
});
}
INIT {
my
%opts
;
$config
{
'Options'
} =
'hVvq'
.
$config
{
'Options'
};
HELP_MESSAGE and
exit
1
unless
getopts (
$config
{
'Options'
},\
%opts
);
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;
{
$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'
;
chomp
(
$config
{
'NodeFile'
} = (
defined
$opts
{
'D'
}) ?
'DNS'
: <>);
$config
{
'Match'
} =
$opts
{
'm'
} //
'[^.]+'
;
$config
{
'DataFile'
} =
$opts
{
'd'
} //
'DB'
;
$config
{
'Automatch'
} =
$opts
{
'a'
} // 0;
$config
{
'Update'
} =
$opts
{
'u'
} // 0;
App::Netsync::configure({
%{App::Netsync::Configurator::config(
'Netsync'
)},
'Quiet'
=>
$config
{
'Quiet'
},
'Verbose'
=>
$config
{
'Verbose'
},
},
App::Netsync::Configurator::config(
'DNS'
),
App::Netsync::Configurator::config(
'SNMP'
),
App::Netsync::Configurator::config(
'DB'
),
);
}
{
my
$nodes
= App::Netsync::discover(
$config
{
'NodeFile'
},
$config
{
'Match'
});
if
(
$config
{
'ProbeLevel'
} == 1) {
foreach
my
$ip
(
sort
keys
%$nodes
) {
note (
$config
{
'Probe1Cache'
},
$nodes
->{
$ip
}{
'RFC1035'
},0,
'>'
);
}
exit
;
}
App::Netsync::identify(
$nodes
,
$config
{
'DataFile'
},
$config
{
'Automatch'
});
if
(
$config
{
'ProbeLevel'
} == 2) {
my
$Netsync
= App::Netsync::Configurator::config(
'Netsync'
);
my
$fields
=
$Netsync
->{
'DeviceField'
}.
','
.
$Netsync
->{
'InterfaceField'
};
$fields
.=
','
.
join
(
','
,
sort
@{
$Netsync
->{
'InfoFields'
}});
note (
$config
{
'Probe2Cache'
},
$fields
,0,
'>'
);
foreach
my
$ip
(
sort
keys
%$nodes
) {
my
$node
=
$nodes
->{
$ip
};
foreach
my
$serial
(
sort
keys
%{
$node
->{
'devices'
}}) {
my
$device
=
$node
->{
'devices'
}{
$serial
};
foreach
my
$ifName
(
sort
keys
%{
$device
->{
'interfaces'
}}) {
my
$interface
=
$device
->{
'interfaces'
}{
$ifName
};
my
$note
=
$serial
.
','
.
$ifName
;
foreach
my
$field
(
sort
@{
$Netsync
->{
'InfoFields'
}}) {
$note
.=
','
.(
$interface
->{
'info'
}{
$field
} //
''
);
}
note (
$config
{
'Probe2Cache'
},
$note
,0);
}
}
}
exit
;
}
App::Netsync::update
$nodes
if
$config
{
'Update'
};
exit
;
}
1;