Print UDP statistics

Problem

Customer wanted to see UDP statistics of both sending and receiving sides. They wanted to know how many UDP datagrams are dropped. netstat -su does not show how many datagrams are dropped when sending. iptraf does not show any statistics on datagram loss.

Scripts

# udpstat.stp
# Copyright (C) 2006 Red Hat, Inc., Eugene Teo <eteo@redhat.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#

global udp_out, udp_outerr, udp_in, udp_inerr, udp_noport, lines

probe begin {
        lines = 20
        printf("%11s  %10s  %10s  %10s  %10s\n",
                "UDP_out", "UDP_outErr", "UDP_in", "UDP_inErr", "UDP_noPort")
}

probe kernel.function("udp_sendmsg").return
{
        $return >= 0 ? udp_out++ : udp_outerr++
}

probe kernel.function("udp_queue_rcv_skb").return
{
        $return == 0 ? udp_in++ : udp_inerr++
}

probe kernel.function("icmp_send")
{
        /* icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); */
        if ($type == 3 && $code == 3) {
                /* IPPROTO_UDP = 17 */
                if ($skb_in->nh->iph->protocol == 17)
                        udp_noport++
        }
}

probe timer.ms(1000)
{
        if (lines-- <= 0) {
                printf("%11s  %10s  %10s  %10s  %10s\n",
                        "UDP_out", "UDP_outErr", "UDP_in", "UDP_inErr", "UDP_noPort")
                lines = 20
        }
        printf("%11d  %10d  %10d  %10d  %10d\n",
                udp_out, udp_outerr, udp_in, udp_inerr, udp_noport)
}

Output

The output format is modelled after the dtrace toolkit's udpstat.d script.

[eteo@kerndev ~]$ ./udpstat.stp
    UDP_out  UDP_outErr      UDP_in   UDP_inErr  UDP_noPort
          0           0           0           0           0
          0           0           0           0           0
          4           0           0           0           0
          5           0           0           0           0
          5           0           0           0           0
          6           0           1           0           0
          7           0           1           0           0
          7           0           1           0           0
          7           0           1           0           2
          7           0           1           0           2
          8           0           1           0           2
          9           0           2           0           5
         10           0           2           0           6
         11           0           2           0           6
         15           0           5           0           6
         19           1           9           0           6
         19           1          10           0           6
         19           1          10           0           6
         19           1          10           0           6
         19           1          10           0           6
    UDP_out  UDP_outErr      UDP_in   UDP_inErr  UDP_noPort
         21           1          10           0           6
         26           1          14           0           6
         33           1          22           0           6
         37           1          26           0           6

Lessons

With this script, we can find out how many UDP datagrams have been sent and received, and how many are dropped. SystemTap gives you the flexibility to write scripts that do what you need.


WarStories

None: WSUDPStatistics (last edited 2008-01-10 19:47:29 by localhost)