Tcpdump-dumptrafficonanetwork: ip-based traffic statistics. Hardware: difficulties in school cluster: 1. what is the packet loss rate when a high-speed Nic (gigabit or IB) is used and the NIC is fully loaded? 2. how is the cpu usage of the program 3. large log file scheme and analysis: 1. packet capture can be implemented in a variety of ways, such as using the libpcap library and using the zero copy method.
Tcpdump-Dump traffic on a network
Problem:Based onIpStatisticsTraffic.
Hardware: school cluster
Difficulties:
1. whenUseHigh-speed Nic (gigabit or IB) packet loss rate when the NIC is full
2. what is the cpu usage of the program?
3. the log file is too large.
Solution and analysis:
1. there are multiple packet capture methods, such as using the libpcap library, using the zero copy method,UsePF_RING interface, directUseSystem functions
I read some articles and tested the netsniff-ng program (Based onZero copy), I think the zero copy method has the highest efficiency and the lowest packet loss rate. But I
When testing tcpdump, it is found that the efficiency is also high (the network card is almost full during the test), and there is almost no packet loss, and because of its convenienceUseOther
Tcpdump is the final factor.
2. test the cpu usage of tcpdump during packet capture: about 4%
3. tcpdump because each packet is recorded, although we can limit the number of bytes of each packet record (-s option), due to the full load of the network card,
Therefore, the number of packages is extremely large. As follows, 748168 packets have been captured in 60 seconds, resulting in extremely large pcap files.
Number of packages: 748168
File size: 58394582 bytes
Data size: 3900660165 bytes
Capture duration: 59.963470 seconds
Therefore, IUse-G option: Record one pcap file per minute andStatisticsDelete the data and the pcap file. That is, every minute
Generate the following file (the time interval can be changed as needed)
File name:/public/home/wangvsa/software/tcp-audit/2012-07-16, 12: 54: 54. pcapFile encapsulation: Ethernet
Number of packages: 748168
File size: 58394582 bytes
Data size: 3900660165 bytes
Capture duration: 59.963470 seconds
Start time: Mon Jul 16 12:54:53 2012 End time: Mon Jul 16 532012 Data rate: 65050607.75 bytes/s Data rate: 520404861.98 bits/sIPv4 total packet: 748037 source ip port dir dest ip port protoflow 10. 10. 10. 61: 9090> 10. 10. 10. 49: 27884 tcp 1793 0. 0.0. 0: 68> 255.255.255.255: 67 UDP 5310 10. 10. 10. 49:-> 10. 10. 10. 62:-ICMP 234 10. 10. 10. 49: 22> 10. 10. 10. 61: 47051 tcp 117124 10. 10. 10. 48: 38906> 10. 10. 10. 49: 16797tcp 44288974 10. 10. 10. 48: 64468> 10. 10. 10. 49: 45094 tcp171 10. 10. 10. 61: 47051> 10. 10. 10. 49: 22 tcp 5406 10. 10.10. 49: 27884> 10. 10. 10. 61: 9090 tcp 2112 10. 10. 10. 49: 45094> 10. 10. 10. 48: 64468 tcp 108 10. 10. 10. 62:-> 10.10. 10. 49:-ICMP 234 10. 10. 10. 49: 16797> 10. 10. 10. 48: 38906 tcp 3856230180
Specific implementation:
1. CompileStatisticsScript (if high efficiency requirements can be written into the C language), a total of two files tcp-audit.sh and
Tcp-reduce.sh. The tcp-audit.sh is called in the tcp-reduce.sh and should not be called directly.
See tcp-audit.sh. this file reads content from ready.txt. each row isStatisticsThe name of the pcap file.
Discuss the ready.txt file.
#! /Bin/bash
# Tcp-audit.sh
# Read the ready.txt file and read a row each time
# Pass its content as a parameter to the tcp-reduce.sh
# Delete the row after execution
DIR = "/home/wangchen/shell_ex /"
While read line
Do
# Tcp-reduce.sh processing
$ DIR/tcp-reduce.sh $ {line}
# Delete the pcap file
Rm $ DIR/$ {line}
# Delete this row
Sed-I '1D '$ DIR/ready.txt
Done <$ DIR/ready.txt
Look at the tcp-reduce.sh, this script completes allStatisticsOperation and output to the log file. In this scriptUseGrep and sed commands
After the output of the pcap file is convenient, the awk programUse. We still use the capinfos program to provide some analysis data.
Note: The tcpdump and capinfos versions are different. you must modify the parameters. For example, capinfos-x uses-I in earlier versions.
#! /Bin/bash
# Tcp-reduce.sh
DIR = "/home/wangchen/shell_ex"
# Construct the log file name based on the pcap file name (named by Time)
Tmp1 = $ *
Tmp2 = ($ {tmp1 //./})
LOG_FILE_NAME =$ {tmp2 []} ". log"
# Output file information, that is, network card information
Capinfos-a-c-d-e-E-s-u-x-y $ DIR/$ * >$ DIR/$ LOG_FILE_NAME
# Generate readable text from the tcpdump file
# Filtered and replaced by grep and sed and handed over to awk
#
UseThe awk language processes data rows,
Statistics Based onIp
TrafficInformation
Tcpdump-q-n-e-t-r $ DIR/$ *\
| Grep "IPv4" | sed's/, // g; s/> // g; s/: // g '\
| Awk'
BEGIN {
Printf ("\ n ")
}
{
# $: Packet length
# $: Source ip. port
# $: Destination ip. port
# $: Proto
# Array_flow is an array that records each direction
Traffic
# Its subscript is the data transmission direction (string)
Count ++
Way = $ "." $ "." $
For (x in arr_total_flow)
{
If (way = x)
{
Arr_total_flow [way] + = $
Next
}
}
Arr_total_flow [way] = $
}
END {
Printf ("\ nIPv4 total packet: % d \ n", NR)
Printf ("% 16 s % 6 s % 6 s % 19 s % 6s \ tproto \ tflow \ n", "source ip", "port", "dir ", "dest ip", "port ")
For (x in arr_total_flow)
{
# Formatting Output
Split (x, chunks ,".")
If (chunks [] = "ICMP ")
{
Printf ("% 3 s. % 3 s. % 3 s. % 3 s: % 6 s> % 3 s. % 3 s. % 3 s. % 3 s: % 6s \ t % s \ t % d \ n ",
Chunks [], chunks [], chunks [], chunks [], "-", # Source Address: Port
Chunks [], chunks [], chunks [], chunks [], "-", # destination address: Port
Chunks [], arr_total_flow [x]) # total protocol
Traffic
}
Else
{
Printf ("% 3 s. % 3 s. % 3 s. % 3 s: % 6 s> % 3 s. % 3 s. % 3 s. % 3 s: % 6s \ t % s \ t % d \ n ",
Chunks [], chunks [], chunks [], chunks [], chunks [], # Source Address: Port
Chunks [], chunks [], chunks [], chunks [], chunks [], # destination address: Port
Chunks [], arr_total_flow [x]) # total protocol
Traffic
}
}
}
'> $ DIR/$ LOG_FILE_NAME
2. the next step is how to run it. first, run the tcpdump packet capture program with the root permission.UseThe command is as follows:
Sudo cpdump-I eth3-s-G-z./tcp-add.sh-w % F, % T. pcap
Parameter Analysis:-I eth3 captures packets on eth3;-s 84 each packet records a maximum of 84 bytes;-G 60 opens a file record every 60 seconds;
-Z./tcp-add.sh with-GUse, Execute the./tcp-add.sh script each time after a file is recorded (this script will be described later)
-W % F, % T. pcap output. the file name consists of time. % F indicates the year, month, and day, and % T indicates the hour, minute, and second, for example, 2012-07-16, 12: 54: 54. pcap
Let's take a look at this tcp-add.sh. this script is very simple. just write the pcapfile name that has just been recorded to the ready.txt file.
#! /Bin/bash
# As the-z parameter of tcpdump
Use
# Add the stored pcapfile name to ready.txt for processing
Echo $ *> ready.txt
In this example, a pcapfile is generated every minute and its name is written to the ready.txt file.
3. we will use cron to regularly execute ourStatisticsTask. Run the following command after enabling the crond service:
Run this command to open a file, add the following to the last line, save and exit
*/***/Home/wangchen/shell_ex/tcp-audit.sh
This indicates that a tcp-audit.sh script is executed every two minutes (at will.
This function is basically implemented and practical in the cluster.
Of course, the above scheme is just an exercise I have done in the lab, and there are certainly many shortcomings. I hope the experts can give me some advice.