I'm writing a program to keep track of bandwidth usage per user (on Linux, kernel 2.4.18). To do this, I'm grabbing realtime output from tcpdump (standard program) and parsing it. However, on a high traffic machine, perl just can't keep up!

Using open (piped), I'm able to pull in about 300ms per individual read from tcpdump (it's roughly the same for sysread and for just $_ = <TCPIN>).

So anyways, the question is how can I get in real time data from tcpdump, very, VERY fast?

Benchmarks are taken using Benchmark::Timer. I've tried using IPC::Open2 and Open3, but to no avail. Chatterbox people have also suggested xs'ing tcpdump source, but I'd rather stay away from C for various reasons.

As to not doing parsing in real time: I won't be able to retrieve the owner of a connection if it's not done during the time of the connection...

I'm currently getting the data in this way:
open(TCPIN, "/usr/sbin/tcpdump -n -p tcp |"); while (1) { # Method 1 die "Aiii! $!" if !sysread(TCPIN, $_, 1024); # Method 2 $_ = <TCPIN>; # Of course, both of these methods aren't used at the same time! # Parsing stuff here (this is really fast, so the problem isn't here) }
Thanks!
-Alex

In reply to Very fast reads from an external program by slifox

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.