blib/lib/App/Netdisco/Util/Nbtstat.pm untested

filter: Statementstmt 0.0% 0 / 88 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 160 CC 13 SCAR 45.0
CC
13
Cov
0%
_filter_nbname
156.0
store_nbt
6.0
nbtstat_resolve_async
2.0
CRAP 90.4
1 package App::Netdisco::Util::Nbtstat;
2  
3 use Dancer qw/:syntax :script/;
4 use Dancer::Plugin::DBIC 'schema';
5  
6 use App::Netdisco::Util::Node 'check_mac';
7 use App::Netdisco::AnyEvent::Nbtstat;
8 use App::Netdisco::Util::SNMPText 'decode_snmp_text';
9  
10 use base 'Exporter';
11 our @EXPORT = ();
12 our @EXPORT_OK = qw/ nbtstat_resolve_async store_nbt /;
13 our %EXPORT_TAGS = (all => \@EXPORT_OK);
14  
15 =head1 NAME
16  
17 App::Netdisco::Util::Nbtstat
18  
19 =head1 DESCRIPTION
20  
21 Helper subroutines to support parts of the Netdisco application.
22  
23 There are no default exports, however the C<:all> tag will export all
24 subroutines.
25  
26 =head1 EXPORT_OK
27  
28 =head2 nbtstat_resolve_async( $ips )
29  
30 This method uses an asynchronous AnyEvent NetBIOS node status requester
31 C<App::Netdisco::AnyEvent::Nbtstat>.
32  
33 Given a reference to an array of hashes will connects to the C<IPv4> of a
34 node and gets NetBIOS node status information.
35  
36 Returns the supplied reference to an array of hashes with MAC address,
37 NetBIOS name, NetBIOS domain/workgroup, NetBIOS user, and NetBIOS server
38 service status for addresses which responded.
39  
40 =cut
41  
42 sub nbtstat_resolve_async {
43 my $ips = shift;
44  
45 my $timeout = (setting('nbtstat_response_timeout')
46 || setting('nbtstat_timeout') || 1);
47 my $interval = setting('nbtstat_interval') || 0.02;
48  
49 my $stater = App::Netdisco::AnyEvent::Nbtstat->new(
50 timeout => $timeout,
51 interval => $interval
52 );
53  
54 # Set up the condvar
55 my $cv = AE::cv;
56 $cv->begin( sub { shift->send } );
57  
58 foreach my $hash_ref (@$ips) {
59 my $ip = $hash_ref->{'ip'};
60 $cv->begin;
61 $stater->nbtstat(
62 $ip,
63 sub {
64 my $res = shift;
65 _filter_nbname( $ip, $hash_ref, $res );
66 $cv->end;
67 }
68 );
69 }
70  
71 # Decrement the cv counter to cancel out the send declaration
72 $cv->end;
73  
74 # Wait for the resolver to perform all resolutions
75 $cv->recv;
76  
77 # Close sockets
78 undef $stater;
79  
80 return $ips;
81 }
82  
83 # filter nbt names / information
84 sub _filter_nbname {
85 my $ip = shift;
86 my $hash_ref = shift;
87 my $node_status = shift;
88  
89 my $server = 0;
90 my $nbname = '';
91 my $domain = '';
92 my $nbuser = '';
93  
94 for my $rr ( @{$node_status->{'names'}} ) {
95 my $suffix = defined $rr->{'suffix'} ? $rr->{'suffix'} : -1;
96 my $G = defined $rr->{'G'} ? $rr->{'G'} : '';
97 my $name = defined $rr->{'name'} ? $rr->{'name'} : '';
98  
99 if ( $suffix == 0 and $G eq "GROUP" ) {
100 $domain = $name;
101 }
102 if ( $suffix == 3 and $G eq "UNIQUE" ) {
103 $nbuser = $name;
104 }
105 if ( $suffix == 0 and $G eq "UNIQUE" ) {
106 $nbname = $name unless $name =~ /^IS~/;
107 }
108 if ( $suffix == 32 and $G eq "UNIQUE" ) {
109 $server = 1;
110 }
111 }
112  
113 unless ($nbname) {
114 debug sprintf ' nbtstat no computer name found for %s', $ip;
115 return;
116 }
117  
118 my $mac = $node_status->{'mac_address'} || '';
119  
120 unless ( check_mac( $mac, $ip ) ) {
121  
122 # Just assume it's the last MAC we saw this IP at.
123 my $node_ip = schema(vars->{'tenant'})->resultset('NodeIp')
124 ->search( { ip => $ip, vrf => '', -bool => 'active' },
125 { rows => 1 } )->first;
126  
127 if ( !defined $node_ip ) {
128 debug sprintf ' no MAC for %s returned by nbtstat or in DB', $ip;
129 return;
130 }
131 $mac = $node_ip->mac;
132 }
133  
134 $hash_ref->{'ip'} = $ip;
135 $hash_ref->{'mac'} = $mac;
136 $hash_ref->{'nbname'} = decode_snmp_text($nbname, undef, "nbtstat nbname");
137 $hash_ref->{'domain'} = decode_snmp_text($domain, undef, "nbtstat domain");
138 $hash_ref->{'server'} = $server;
139 $hash_ref->{'nbuser'} = decode_snmp_text($nbuser, undef, "nbtstat nbuser");
140
141 return;
142 }
143  
144 =head2 store_nbt($nb_hash_ref, $now?)
145  
146 Stores entries in C<node_nbt> table from the provided hash reference; MAC
147 C<mac>, IP C<ip>, Unique NetBIOS Node Name C<nbname>, NetBIOS Domain or
148 Workgroup C<domain>, whether the Server Service is running C<server>,
149 and the current NetBIOS user C<nbuser>.
150  
151 Adds new entry or time stamps matching one.
152  
153 Optionally a literal string can be passed in the second argument for the
154 C<time_last> timestamp, otherwise the current timestamp (C<LOCALTIMESTAMP>) is used.
155  
156 =cut
157  
158 sub store_nbt {
159 my ( $hash_ref, $now ) = @_;
160 $now ||= 'LOCALTIMESTAMP';
161  
162 schema(vars->{'tenant'})->txn_do(sub {
163 my $row = schema(vars->{'tenant'})->resultset('NodeNbt')->update_or_new(
164 { mac => $hash_ref->{'mac'},
165 ip => $hash_ref->{'ip'},
166 nbname => $hash_ref->{'nbname'},
167 domain => $hash_ref->{'domain'},
168 server => $hash_ref->{'server'},
169 nbuser => $hash_ref->{'nbuser'},
170 active => \'true',
171 time_last => \$now,
172 },
173 { key => 'primary',
174 for => 'update',
175 }
176 );
177  
178 if (! $row->in_storage) {
179 $row->set_column(time_first => \$now);
180 $row->insert;
181 }
182 });
183  
184 return;
185 }
186  
187 1;