udpsnoop.d revision 14ea49401f3c8c61422aefbda43809e275f60c6c
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#!/usr/sbin/dtrace -s
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync/*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * udpsnoop - snoop UDP network packets by process.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Written using DTrace udp Provider.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * This analyses UDP network packets and prints the responsible PID plus
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * standard details such as IP address and port. This captures traffic
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * from existing and newly created UDP connections. It can help identify
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * which processes are causing UDP traffic.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * CDDL HEADER START
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * The contents of this file are subject to the terms of the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Common Development and Distribution License (the "License").
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * You may not use this file except in compliance with the License.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * or http://www.opensolaris.org/os/licensing.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * See the License for the specific language governing permissions
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * and limitations under the License.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * When distributing Covered Code, include this CDDL HEADER in each
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * If applicable, add the following below this CDDL HEADER, with the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * fields enclosed by brackets "[]" replaced with your own identifying
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * information: Portions Copyright [yyyy] [name of copyright owner]
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * CDDL HEADER END
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync/*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Portions Copyright 2010 Brendan Gregg
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#pragma D option quiet
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#pragma D option switchrate=10hz
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncdtrace:::BEGIN
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync{
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync printf("%6s %6s %15s:%-5s %15s:%-5s %6s\n",
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "TIME", "PID", "LADDR", "PORT", "RADDR", "PORT", "BYTES");
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncudp:::send
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync{
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync printf("%6d %6d %15s:%-5d -> %15s:%-5d %6d\n",
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync timestamp/1000, args[1]->cs_pid, args[2]->ip_saddr,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync args[4]->udp_sport, args[2]->ip_daddr, args[4]->udp_dport,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync args[4]->udp_length);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncudp:::receive
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync{
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync printf("%6d %6d %15s:%-5d <- %15s:%-5d %6d\n",
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync timestamp/1000, args[1]->cs_pid, args[2]->ip_daddr,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync args[4]->udp_dport, args[2]->ip_saddr, args[4]->udp_sport,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync args[4]->udp_length);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}