Hi perl monks, I wrote a script to perform the following task. It grabs the ACKs lines from the dhcp log file using tail. It takes the ip of the recently connected device and performs a snmp query. Then it will append this info to a plain text record. So far it is working, here is the code:
#!/usr/bin/perl -w use strict; use File::Tail; use Cwd qw(); my $path = Cwd::cwd(); my $community = 'public'; my $snmp_bsid = '.1.3.6.1.4.1.2700.1.1.11.0'; my $bsid; my $file = File::Tail->new( name =>'/var/log/messages', interval => 1, maxinterval => 1, resetafter=> 5, ); while (defined(my $line=$file->read)) { print $line,"\n"; if ($line =~ /^(.*) dns02cor dhcpd: DHCPACK on ([0-9\. +]+) to ([[:xdigit:]:]+).* ([0-9\.]+)/) { write_register($1,$2,$3); } } sub write_register { my ($date,$client_ip,$client_imsi) = @_; $output=qx(snmpget -v2c -t1 -c $community $client_ip $snmp_bsid 2> +&1); my @result=split(/:/,$output); if ($result[3]){ $bsid=$result[3]; $bsid=~s/ //g; $bsid=~s/\n//g; } system 'echo "'.$date.','.$client_ip.','.$client_imsi.','.$bsi +d.'," >>'.$path.'/register_list.txt'; } 1;
The weird thing is when I list the output register: Take a look: Apr 12 20:56:54,186.183.101.72,77:10:00:02:06:85,77777851CFC7106,
Apr 12 20:57:06,186.183.104.175,77:10:00:03:38:56,,
Apr 12 20:57:08,186.183.106.13,77:10:00:01:92:00,77777851CFC8901,
Apr 12 20:57:11,186.183.101.196,77:10:00:01:99:35,,
Apr 12 20:57:11,186.183.105.204,77:10:00:01:93:30,,77777851CFC9A02,

As you can see there are some empty bsid fields,but if I manually execute the snmpget query from the command line to those ips, I can bring the value without any problem. I would like to know if you have some suggestion for me in order to improve my scrip. I also think that due to the ACKs rate (4 or 5 per secs)I should consider implementing some fork technique ... well. I hope to hear your opinions, Regards, Leandro.

In reply to snmpget is missing some values by leostereo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.