Dear BrowserUk; I made some minor modifications to test it but can not make it run yet.
I wrote:
#!/usr/bin/perl -slw
use strict;
use threads;
use Thread::Queue;
our $T = 10; ## 10 walkers; adjust to suit.
sub listener {
my( $Qout ) = @_;
require 'IO::Socket::INET::Daemon'; ## Requiring here means othe
+r threads don't carry the redundant weight
my $host = new IO::Socket::INET::Daemon(
host => '172.24.3.208', port => 7777, timeout => 20,
callback => { data =>
sub {
my ($io, $host) = @_;
chomp( my $line = <$io> );
return 0 unless $line;
$Qout->enqueue( $line ); ## send work to listener #
+#-> aren't going to the walker ?
return !0;
}
},
);
$host->run;
return;
}
sub walker {
my( $Qin, $Qout ) = @_;
while( $Qin->dequeue ) { ## receive work from listener
my( $type, $ip, $mac, $bsid, $datecode ) = split( ',', $_ );
$mac =~ tr[-][]d;
my $output = qx( snmpwalk -v2c -t1 -c public $ip .1.3.6.1.4.1.
+9885.9885.1.2.0 2>&1 );
my( $rssi, $error ) = 0;
if( $output eq "Timeout: No Response from $ip" ) {
$error = 'SNMP not responding. Upgrade firmware';
}
else {
my @result = split( /:/, $output );
$rssi = $result[ 3 ];
$rssi =~ tr[ \n][]d;
if( $rssi < -100 ) {
$rssi = $rssi / 100;
}
$rssi = int( $rssi );
}
$Qout->enqueue( join $;, $mac, $ip, $bsid, $rssi ); ## Send
+data items to DBI as one scalar
}
}
use enum qw[ IN DBI ];
my @Qs = map Thread::Queue->new(), 1 .. 2; # set up two Qs
## start the listener thread
my $tListener = threads->create( \&listener, $Qs[ IN ] ); ## One for t
+he listener to send work to the walkers
## start 10 walkers
my @walkers = map{ threads->create( \&walker, @Qs[ IN, DBI ] ) } 1 ..
+$T; ## And one for the walkers to forward data for adding to the db
require DBI; ## Avoid loading DBI into threads
my $dbh = DBI->connect("DBI:mysql:database=cpe_info;host=172.24.3.207;
+port=3306","account_process","neting.!" );
my $sth = $dbh->prepare("INSERT INTO cpe_info(mac,ip,bsid,rssi) VALUE
+S( ?, ?, ?, ? ) ON DUPLICATE KEY UPDATE ip = ?, bsid = ?, rssi = ?");
## process data produced by walkers
while( $Qs[DBI]->dequeue ) {
my( $mac, $ip, $bsid, $rssi ) = split $;; ##
+Retrieve individual data
$sth->execute( $mac, $ip, $bsid, $rssi, $ip, $bsid, $rssi ); ##
+bind and execute
}
$dbh->disconnect();
It yields:
./lines_dispacher_and_consumer_threads.pl
Thread 1 terminated abnormally: Can't locate IO::Socket::INET::Daemon
+in @INC (@INC contains: /root/perl5/lib/perl5/5.16.3/x86_64-linux-thr
+ead-multi /root/perl5/lib/perl5/5.16.3 /root/perl5/lib/perl5/x86_64-l
+inux-thread-multi /root/perl5/lib/perl5 /usr/local/lib64/perl5 /usr/l
+ocal/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor
+_perl /usr/lib64/perl5 /usr/share/perl5 .) at ./lines_dispacher_and_c
+onsumer_threads.pl line 10.
Can't call method "connect" without a package or object reference at .
+/lines_dispacher_and_consumer_threads.pl line 65.
Perl exited with active threads:
10 running and unjoined
1 finished and unjoined
0 running and detached
Very strange , IO::Socket::INET::Daemon is at /usr/local/share/perl5 folder , I have used it with no problem.
The other error is about the dbh=DBI->connect line ... also ver strange.
I have been playing around with both modules declaration statements but still can not make it work ... I will keep trying.
Regards.
Leandro.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.