1#!/usr/sbin/dtrace -s
2/*
3 * udpstat.d - print UDP statistics. Uses DTrace.
4 *
5 * This prints UDP statistics every second, retrieved from the MIB provider.
6 *
7 * 25-Jul-2005, ver 0.70	(first release)
8 *
9 * USAGE:	udpstat.d
10 *
11 * FIELDS:
12 *		UDP_out		UDP datagrams sent
13 *		UDP_outErr	UDP datagrams errored on send
14 *		UDP_in		UDP datagrams received
15 *		UDP_inErr	UDP datagrams undeliverable
16 *		UDP_noPort	UDP datagrams received to closed ports
17 *
18 * The above UDP statistics are documented in the mib2_udp struct
19 * in the /usr/include/inet/mib2.h file; and also in the mib provider
20 * chapter of the DTrace Guide, http://docs.sun.com/db/doc/817-6223.
21 *
22 * COPYRIGHT: Copyright (c) 2005 Brendan Gregg.
23 *
24 * CDDL HEADER START
25 *
26 *  The contents of this file are subject to the terms of the
27 *  Common Development and Distribution License, Version 1.0 only
28 *  (the "License").  You may not use this file except in compliance
29 *  with the License.
30 *
31 *  You can obtain a copy of the license at Docs/cddl1.txt
32 *  or http://www.opensolaris.org/os/licensing.
33 *  See the License for the specific language governing permissions
34 *  and limitations under the License.
35 *
36 * CDDL HEADER END
37 *
38 * 25-Jul-2005  Brendan Gregg   Created this.
39 */
40
41#pragma D option quiet
42
43/*
44 * Declare Globals
45 */
46dtrace:::BEGIN
47{
48	UDP_in = 0; UDP_out = 0;
49	UDP_inErr = 0; UDP_outErr = 0; UDP_noPort = 0;
50	LINES = 20; line = 0;
51}
52
53/*
54 * Print Header
55 */
56profile:::tick-1sec { line--; }
57
58profile:::tick-1sec
59/line <= 0 /
60{
61	printf("%11s %11s %11s %11s %11s\n",
62	    "UDP_out", "UDP_outErr", "UDP_in", "UDP_inErr", "UDP_noPort");
63
64	line = LINES;
65}
66
67/*
68 * Save Data
69 */
70mib:::udpInDatagrams	{ UDP_in += arg0;	}
71mib:::udpOutDatagrams	{ UDP_out += arg0;	}
72mib:::udpInErrors	{ UDP_inErr += arg0;	}
73mib:::udpInCksumErrs	{ UDP_inErr += arg0;	}
74mib:::udpOutErrors	{ UDP_outErr += arg0;	}
75mib:::udpNoPorts	{ UDP_noPort += arg0;	}
76
77/*
78 * Print Output
79 */
80profile:::tick-1sec
81{
82	printf("%11d %11d %11d %11d %11d\n",
83	    UDP_out, UDP_outErr, UDP_in, UDP_inErr, UDP_noPort);
84
85	/* clear values */
86	UDP_out		= 0;
87	UDP_outErr	= 0;
88	UDP_in		= 0;
89	UDP_inErr	= 0;
90	UDP_noPort	= 0;
91}
92