Starting from here, we start to deal with big data using distributed systems. Before dealing with big data, there is a need for a scenario, otherwise the technology tools cannot embed reality and the value will be reduced. The scene I met should still be more universal, so you can play it out in my scene, familiarize yourself with the process and methods, and then transform it into your own scene and environment.
Scenario: Within a large company, data transfer between the terminal and the various business systems is carried out over the network. For monitoring purposes, you need to get all the packets on the network and see if the packets contain certain keywords. If some keywords are included, it proves that some kind of operation is taking place between the terminal and the business system. The system records these actions for real-time display or statistical use.
This is actually the collection and recording of "Behavioral Data", which is a typical big data processing scenario.
Extension, the use of the technology and tools used to load the scene on the Internet or app, you can not change any business system, the user does not perceive the situation, the user's behavior data collected and used to form user data. Of course, can also be through the "buried point" way, but change the business system do not spend money, can save a little bit.
Access to network data. Network data is obtained through the "Mirror Port" of the network device. The settings of the mirror Port can let the network management Help, the general can be managed switches can do, Cisco, China Three, Maples and Calder These are no problem. By acquiring network data through the mirror port, you can get all the data without the awareness of the various business systems and users. Of course, if the company or the system in the transmission of the use of HTTPS encryption means, this will not be able to. Generally, however, companies rarely encrypt their network transmissions.
The structure is as follows (the painting is really touching):
The Mirror Port (that is, the listening port) to a server RJ45 network card, without the IP address, the server network card can receive all the data transmitted on the network. For example, eth1 is the network card that accesses the mirror port.
With Tcpdump-i eth1, you can see some of the data transferred over the network. Through the source address, the destination address, you can probably determine whether the company's data.
The use of tcpdump is very complex, online a lot of methods introduced, here do not explain, directly use up. In general, the complete Tcpdump packet contains many segments, each with a logo, and so on. Such as:
You can see that the network package contains a JSON, which is the data that is transferred over the network.
But these data obtained directly through tcpdump are not suitable for direct disposal to distributed systems. Why is it?
Because distributed processing is the parallel processing of multiple devices, it achieves the purpose of extending data processing capability. Just said, the complete tcpdump packet contains a lot of segments, assuming that the packet is A,A1 is the first paragraph, A2 is the second paragraph. It is no problem to combine the A1,A2 with the distributed system and make the results at the same time. But the reality is, A1 than A2 first processing, first received a result A1, can not get a meaningful result. Worse, if at the same time come out a B1, put a1,b1 together to get a more wrong result.
Therefore, in order to use the distributed system to process tcpdump data, it is necessary to throw a complete packet to the distributed processing system to ensure the integrity and correspondence of the data packet and computing result. That is, the A1 data packet in the calculation, the results obtained is the A1 calculation.
Use the tcpdump command:
Tcpdump-anvtttt-s 0-i eth1 tcp[20:2]=0x4745 or tcp[20:2]=0x4854 or tcp[20:2]=0x504f|sed '/IP (tos/s/^/<<interval >>/'
In particular, tcp[20:2]=0x4745 or tcp[20:2]=0x4854 or tcp[20:2]=0x504f is the initial filtering of the tcpdump packets, only the HTTP packets, other such as ARP, RTP packets, and so on do not need.
After using a command, the data obtained by tcpdump will be marked with a <<interval>> on the first line, marking the beginning of the packet and ending at the next <<interval>>. This makes it possible for the next step to use Java to clearly mark all line breaks.
The results are as follows:
Two <<interval>> is a complete tcpdump packet. In combination with Java, a complete tcpdump packet is integrated into a data that can be processed by a distributed data processing platform. is actually a simple ETL process.
Data cleansing of tcpdump packets using Java and loading into Kafka
1. Create a new MAVEN project using Eclipse, named Shell
2, using Pom.xml to manage the dependency package, mainly use the Kafka dependency package.
<Dependencies><!--Kafka -<Dependency><groupId>Org.apache.kafka</groupId><Artifactid>kafka_2.11</Artifactid><version>0.10.1.1</version></Dependency><Dependency><groupId>Org.apache.kafka</groupId><Artifactid>Kafka-clients</Artifactid><version>0.10.1.1</version></Dependency><Dependency><groupId>Org.apache.maven.plugins</groupId><Artifactid>Maven-resources-plugin</Artifactid><version>2.3</version></Dependency></Dependencies>
View Code
3, set a topic in the Kafka. This Kafka configuration and the configuration of topic later, not difficult. If you do not know how to do it, you can write in a Java program in a local text file. This is a homework, let's do it by ourselves.
4. Clean the data between two <<interval>> in each tcpdump package. The main one is to replace the newline character with the "|" Symbols, clear out other unwanted symbols. Put the clean data into the s_right variable and wait for it to be sent. After cleaning another piece of data, after cleaning the second data, the data in the S_right is moved forward to the S_left and sent to the Kafka, the second data is placed in the s_right waiting to be sent.
PackageShell;ImportJava.io.*;ImportJava.text.SimpleDateFormat;Importjava.util.Properties;Importjava.util.concurrent.ExecutionException;Importjava.util.Date;ImportJava.util.Calendar;//import kafka.producer.*;//import Kafka.serializer.StringEncoder;//import Kafka.javaapi.producer.Producer;//import Kafka.producer.Partitioner;//import Kafka.producer.ProducerConfig;ImportOrg.apache.kafka.clients.producer.KafkaProducer;ImportOrg.apache.kafka.clients.producer.Producer;ImportOrg.apache.kafka.clients.producer.ProducerRecord; Public classRunshell {//private static Kafka.javaapi.producer.producer<integer, string> producer//= null; Public Static voidMain (string[] args)throwsIOException, Interruptedexception, executionexception {String shell_file_dir, shell_file; Shell_file_dir= "/hadoop/sh/"; //Put the tcpdump command in an SH file and write the file address in shell_file so that Java//The program can invoke the Tcpdump command.Shell_file = "./" + "gettcpdump.sh"; String s=NULL; String S_left= "", S_right = ""; intRunningstatus = 0; Properties Props=NewProperties (); Props.put ("Bootstrap.servers", "Your Kafka address and port"); Props.put ("ACKs", "1"); Props.put ("Retries", 0); Props.put ("Linger.ms", 0); Props.put ("Buffer.memory", 33554432); Props.put ("Key.serializer", "Org.apache.kafka.common.serialization.StringSerializer"); Props.put ("Value.serializer", "Org.apache.kafka.common.serialization.StringSerializer"); //placing the props configuration in the Kafkaproducer<string, string> Producer =NewKafkaproducer<string, string>(props); //set the topic of KafkaString TOPIC = "Netrawdata"; Processbuilder PB=NewProcessbuilder (Shell_file); Pb.directory (NewFile (Shell_file_dir)); Process P=Pb.start (); BufferedReader Stdinput=NewBufferedReader (NewInputStreamReader (P.getinputstream ())); BufferedReader Stderror=NewBufferedReader (NewInputStreamReader (P.getinputstream ())); while((s = stdinput.readline ())! =NULL) { if(S.startswith ("<<interval>>") ) {S_left=S_right; S_right= S.substring (12); if(S_left! = "") {S_left= S_left.replaceall ("\ T", "" "); S_left= S_left.replaceall ("+", "" "); Producer.send (NewProducerrecord<string, string>(TOPIC, S_left)). get (); System.out.println (S_left); } } Else{s_right= S_right + "|" +s; } } while((s = stderror.readline ())! =NULL) {System.out.println (s); } Try{runningstatus=p.waitfor (); if(S_right! = "") {S_right= S_right.replaceall ("\ T", "" "); S_right= S_right.replaceall ("+", "" "); S_right= S_right.replaceall ("\ n", "" "); Producer.send (NewProducerrecord<string, string>(TOPIC, S_right)). get (); System.out.println (S_right); Producer.close (); } } Catch(interruptedexception E1) {//TODO auto-generated Catch blockE1.printstacktrace (); } }}View Code
Tcptump use------get raw data from the network using Java and tcpdump