#!/usr/bin/perl
#
# Copyright 1999-2014 Opmantek Limited (www.opmantek.com)
#
# ALL CODE MODIFICATIONS MUST BE SENT TO CODE@OPMANTEK.COM
#
# This file is part of Network Management Information System ("NMIS").
#
# NMIS is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# NMIS is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with NMIS (most likely in a file named LICENSE).
# If not, see
#
# For further information on NMIS or for a license other than GPL please see
# www.opmantek.com or email contact@opmantek.com
#
# User group details:
# http://support.opmantek.com/users/
#
# *****************************************************************************
use strict;
our $VERSION = "1.0.0";
use FindBin;
use lib "$FindBin::Bin/../lib";
use POSIX qw();
use File::Basename;
use File::Spec;
use Data::Dumper;
use Time::Local; # report stuff - fixme needs rework!
use Time::HiRes;
use Data::Dumper;
# this imports the LOCK_ *constants (eg. LOCK_UN, LOCK_EX), also the stat modes
use Fcntl qw(:DEFAULT :flock :mode);
use Errno qw(EAGAIN ESRCH EPERM);
use NMISNG;
use NMISNG::Log;
use NMISNG::Outage;
use NMISNG::Util;
use NMISNG::rrdfunc;
use NMISNG::Sys;
use NMISNG::Notify;
use Compat::NMIS;
if ( @ARGV == 1 && $ARGV[0] eq "--version" )
{
print "version=$NMISNG::VERSION\n";
exit 0;
}
my $thisprogram = basename($0);
my $usage = "Usage: $thisprogram [option=value...]
* act=show_node node=NODE_NAME what=WHAT
where what:
* configuration
* catchall
* model
* interface
* inventory
* ALL
\n";
die $usage if ( !@ARGV || $ARGV[0] =~ /^-(h|\?|-help)$/ );
my $Q = NMISNG::Util::get_args_multi(@ARGV);
my $wantverbose = (NMISNG::Util::getbool($Q->{verbose}));
my $wantquiet = NMISNG::Util::getbool($Q->{quiet});
my $customconfdir = $Q->{dir}? $Q->{dir}."/conf" : undef;
my $C = NMISNG::Util::loadConfTable(dir => $customconfdir,
debug => $Q->{debug});
die "no config available!\n" if (ref($C) ne "HASH" or !keys %$C);
# log to stderr if debug is given
my $logfile = $C->{''} . "/cli.log";
my $error = NMISNG::Util::setFileProtDiag(file => $logfile) if (-f $logfile);
warn "failed to set permissions: $error\n" if ($error);
# use debug, or info arg, or configured log_level
my $logger = NMISNG::Log->new( level => NMISNG::Log::parse_debug_level(
debug => $Q->{debug} ) // $C->{log_level},
path => (defined $Q->{debug})? undef : $logfile);
# this opens a database connection
my $nmisng = NMISNG->new(
config => $C,
log => $logger,
);
# for audit logging
my ($thislogin) = getpwuid($<); # only first field is of interest
# show the daemon status
if ($Q->{act} =~ /^show_node/)
{
my $result = show_node($Q);
exit 0;
}
# Show node
sub show_node
{
my %args = @_;
my $node = $Q->{node};
my $what = $Q->{what};
die "Need a node and a what args \n" if (!$node or !$what);
my $S = NMISNG::Sys->new(nmisng => $nmisng);
my $nodeobj = $nmisng->node(name => $node);
if ($nodeobj) {
# Node configuration
if ($what =~ /configuration/ or $what =~ /ALL/) {
my $conf = $nodeobj->configuration;
separator();
print "Node configuration: \n";
separator();
print Dumper($conf);
# Node overrides
my $overrides = $nodeobj->overrides;
separator();
print "Node overrides: \n";
separator();
print Dumper($overrides);
}
# Node model
if ($what =~ /configuration/ or $what =~ /ALL/) {
# load node info and Model if name exists
$S->init(node => $nodeobj, snmp => 0);
my $mdl = $S->mdl;
separator();
print "Node model: \n";
separator();
print Dumper($mdl);
}
# Node catchall
if ($what =~ /catchall/ or $what =~ /ALL/) {
# load node info and Model if name exists
my $catchall_data = $S->inventory( concept => 'catchall' )->{_data};
separator();
print "Node catchall: \n";
separator();
print Dumper($catchall_data);
}
# Node Interface
if ($what =~ /interface/ or $what =~ /ALL/) {
# load node info and Model if name exists
my $sectionIds = $S->nmisng_node->get_inventory_ids(
concept => "interface",
filter => { historic => 0 });
if (@$sectionIds)
{
separator();
print "Node interfaces: \n";
separator();
for my $sectionId (@$sectionIds)
{
my ($section, $error) = $S->nmisng_node->inventory(_id => $sectionId);
if ($error)
{
print "Failed to get inventory $sectionId: $error \n";
next;
}
my $data = $section->data();
# Show just one field
print $data->{ifDescr} . " \n";
# Or print the whole structure
#print Dumper($data);
}
}
}
# Node inventory
if ($what =~ /inventory/ or $what =~ /ALL/) {
my $concept = $Q->{concept};
die "Need a concept for interface \n" if (!$concept);
# load node info and Model if name exists
my $sectionIds = $S->nmisng_node->get_inventory_ids(
concept => $concept,
filter => { historic => 0 });
if (@$sectionIds)
{
separator();
print "Node inventory: \n";
separator();
for my $sectionId (@$sectionIds)
{
my ($section, $error) = $S->nmisng_node->inventory(_id => $sectionId);
if ($error)
{
print "Failed to get inventory $sectionId: $error \n";
next;
}
my $data = $section->data();
# Or print the whole structure
print Dumper($data);
}
}
}
} else {
print "Node $node does not exist \n";
}
}
sub separator
{
print "====================================== \n";
}