1*12507SAlan.Maguire@Sun.COM #!/usr/sbin/dtrace -s 2*12507SAlan.Maguire@Sun.COM /* 3*12507SAlan.Maguire@Sun.COM * udpsnoop - snoop UDP network packets by process. 4*12507SAlan.Maguire@Sun.COM * Written using DTrace udp Provider. 5*12507SAlan.Maguire@Sun.COM * 6*12507SAlan.Maguire@Sun.COM * This analyses UDP network packets and prints the responsible PID plus 7*12507SAlan.Maguire@Sun.COM * standard details such as IP address and port. This captures traffic 8*12507SAlan.Maguire@Sun.COM * from existing and newly created UDP connections. It can help identify 9*12507SAlan.Maguire@Sun.COM * which processes are causing UDP traffic. 10*12507SAlan.Maguire@Sun.COM * 11*12507SAlan.Maguire@Sun.COM * CDDL HEADER START 12*12507SAlan.Maguire@Sun.COM * 13*12507SAlan.Maguire@Sun.COM * The contents of this file are subject to the terms of the 14*12507SAlan.Maguire@Sun.COM * Common Development and Distribution License (the "License"). 15*12507SAlan.Maguire@Sun.COM * You may not use this file except in compliance with the License. 16*12507SAlan.Maguire@Sun.COM * 17*12507SAlan.Maguire@Sun.COM * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 18*12507SAlan.Maguire@Sun.COM * or http://www.opensolaris.org/os/licensing. 19*12507SAlan.Maguire@Sun.COM * See the License for the specific language governing permissions 20*12507SAlan.Maguire@Sun.COM * and limitations under the License. 21*12507SAlan.Maguire@Sun.COM * 22*12507SAlan.Maguire@Sun.COM * When distributing Covered Code, include this CDDL HEADER in each 23*12507SAlan.Maguire@Sun.COM * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 24*12507SAlan.Maguire@Sun.COM * If applicable, add the following below this CDDL HEADER, with the 25*12507SAlan.Maguire@Sun.COM * fields enclosed by brackets "[]" replaced with your own identifying 26*12507SAlan.Maguire@Sun.COM * information: Portions Copyright [yyyy] [name of copyright owner] 27*12507SAlan.Maguire@Sun.COM * 28*12507SAlan.Maguire@Sun.COM * CDDL HEADER END 29*12507SAlan.Maguire@Sun.COM */ 30*12507SAlan.Maguire@Sun.COM /* 31*12507SAlan.Maguire@Sun.COM * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 32*12507SAlan.Maguire@Sun.COM * 33*12507SAlan.Maguire@Sun.COM * Portions Copyright 2010 Brendan Gregg 34*12507SAlan.Maguire@Sun.COM */ 35*12507SAlan.Maguire@Sun.COM 36*12507SAlan.Maguire@Sun.COM #pragma D option quiet 37*12507SAlan.Maguire@Sun.COM #pragma D option switchrate=10hz 38*12507SAlan.Maguire@Sun.COM 39*12507SAlan.Maguire@Sun.COM dtrace:::BEGIN 40*12507SAlan.Maguire@Sun.COM { 41*12507SAlan.Maguire@Sun.COM printf("%6s %6s %15s:%-5s %15s:%-5s %6s\n", 42*12507SAlan.Maguire@Sun.COM "TIME", "PID", "LADDR", "PORT", "RADDR", "PORT", "BYTES"); 43*12507SAlan.Maguire@Sun.COM } 44*12507SAlan.Maguire@Sun.COM 45*12507SAlan.Maguire@Sun.COM udp:::send 46*12507SAlan.Maguire@Sun.COM { 47*12507SAlan.Maguire@Sun.COM printf("%6d %6d %15s:%-5d -> %15s:%-5d %6d\n", 48*12507SAlan.Maguire@Sun.COM timestamp/1000, args[1]->cs_pid, args[2]->ip_saddr, 49*12507SAlan.Maguire@Sun.COM args[4]->udp_sport, args[2]->ip_daddr, args[4]->udp_dport, 50*12507SAlan.Maguire@Sun.COM args[4]->udp_length); 51*12507SAlan.Maguire@Sun.COM } 52*12507SAlan.Maguire@Sun.COM 53*12507SAlan.Maguire@Sun.COM udp:::receive 54*12507SAlan.Maguire@Sun.COM { 55*12507SAlan.Maguire@Sun.COM printf("%6d %6d %15s:%-5d <- %15s:%-5d %6d\n", 56*12507SAlan.Maguire@Sun.COM timestamp/1000, args[1]->cs_pid, args[2]->ip_daddr, 57*12507SAlan.Maguire@Sun.COM args[4]->udp_dport, args[2]->ip_saddr, args[4]->udp_sport, 58*12507SAlan.Maguire@Sun.COM args[4]->udp_length); 59*12507SAlan.Maguire@Sun.COM } 60