#!/usr/bin/perl

use Sys::Syslog qw(:standard :macros setlogsock);

sub get_dropped {
    open(STAT, '/usr/bin/netstat -s -p udp|');
    while (<STAT>) {
        next unless /(\d+) dropped due to full socket buffers/;
        return $1;
    }
}

openlog('netstat.pl', 'ndelay', LOG_USER);
setlogsock('unix');

my $dropped = get_dropped();

for (;;) {
    my $cur_dropped = get_dropped();
    if ($cur_dropped > $dropped) {
        syslog(LOG_WARNING, "%d udp packets dropped due to full socket buffers\n", $cur_dropped - $dropped);
        $dropped = $cur_dropped;
    }
    sleep 2;
}
