forked from ddurieux/fusioninventory-agent-task-snmpquery
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fusioninventory-netinventory
executable file
·159 lines (127 loc) · 3.28 KB
/
fusioninventory-netinventory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/perl
use strict;
use warnings;
use lib './lib';
use English qw(-no_match_vars);
use Getopt::Long;
use Pod::Usage;
use XML::TreePP;
use FusionInventory::Agent::Task::NetInventory;
use FusionInventory::Agent::Logger;
my $options = {
community => 'public'
};
GetOptions(
$options,
'model=s',
'host=s',
'community=s',
'debug+',
'help',
) or pod2usage(-verbose => 0);
pod2usage(-verbose => 0, -exitval => 0) if $options->{help};
pod2usage(
-message => "no model given, aborting\n", -verbose => 0
) unless $options->{model};
pod2usage(
-message => "invalid file '$options->{model}', aborting\n", -verbose => 0
) unless -f $options->{model};
pod2usage(
-message => "no host given, aborting\n", -verbose => 0
) unless $options->{host};
my $model = loadModel($options->{model});
my $type =
$model->{TYPE} eq 'Printer' ? 'PRINTER' :
$model->{TYPE} eq 'NetworkEquipment' ? 'NETWORKING' :
undef ;
my $inventory = FusionInventory::Agent::Task::NetInventory->new(
target => FusionInventory::Agent::Task::NetInventory::Target->new(),
logger => FusionInventory::Agent::Logger->new(
debug => $options->{debug}
)
);
$inventory->{options} = {
NAME => 'SNMPQUERY',
PARAM => [
{
PID => 1,
THREADS_QUERY => 1
}
],
DEVICE => [
{
TYPE => $type,
IP => $options->{host},
AUTHSNMP_ID => 1,
MODELSNMP_ID => 1
}
],
MODEL => [ $model ],
AUTHENTICATION => [
{
ID => 1,
COMMUNITY => $options->{community},
}
]
};
$inventory->{client} =
FusionInventory::Agent::Task::NetInventory::Client->new();
$inventory->{deviceid} = 'foo';
$inventory->run();
sub loadModel {
my ($file) = @_;
my $model = XML::TreePP->new()->parsefile($file)->{model};
my @get = map {
{
OID => $_->{oid},
OBJECT => $_->{mapping_name},
VLAN => $_->{vlan},
}
} grep {
$_->{dynamicport} == 0
} @{$model->{oidlist}->{oidobject}};
my @walk = map {
{
OID => $_->{oid},
OBJECT => $_->{mapping_name},
VLAN => $_->{vlan},
}
} grep {
$_->{dynamicport} == 1
} @{$model->{oidlist}->{oidobject}};
return {
ID => 1,
NAME => $model->{name},
TYPE => $model->{type},
GET => \@get,
WALK => \@walk
}
}
package FusionInventory::Agent::Task::NetInventory::Client;
sub new {
my ($class) = @_;
return bless {}, $class;
}
sub send {
my ($self, %params) = @_;
print $params{message}->getContent();
}
package FusionInventory::Agent::Task::NetInventory::Target;
sub new {
my ($class) = @_;
return bless {}, $class;
}
sub getUrl {
my ($self, %params) = @_;
return undef;
}
__END__
=head1 NAME
fusioninventory-netinventory - Network inventory from command line
=head1 SYNOPSIS
fusioninventory-netinventory [options] [--host <host>] [--model <model>]
Options:
-h --help this menu
--host host host to inventorize
--model model XML model file
--community community string (default: public)