Search in sources :

Example 1 with TsharkException

use of com.att.aro.core.exception.TsharkException in project VideoOptimzer by attdevsupport.

the class AROServiceImpl method analyzeFile.

/**
 * Launches an analysis of a traceFile with the results populating an
 * AROTraceData object
 *
 * @param requests
 *            list of BestPracticeType bestPractices to analyze
 * @param traceFile
 *            path to a pcap trace file, usually traffic.cap
 * @param profile
 *            device profile used as a model of the device when analyzing
 *            trace data
 * @param filter
 *            used for filtering information from a trace analysis based on
 *            a specified time range and set of ApplicationSelection
 *            objects.
 * @return AROTraceData object
 * @throws IOException
 *             if trace file not found
 */
@Override
public AROTraceData analyzeFile(List<BestPracticeType> requests, String traceFile, Profile profile, AnalysisFilter filter) throws IOException {
    AROTraceData data = new AROTraceData();
    try {
        PacketAnalyzerResult result = packetanalyzer.analyzeTraceFile(traceFile, profile, filter);
        if (result == null) {
            data.setError(ErrorCodeRegistry.getTraceFileNotAnalyzed());
        } else {
            if (result.getTraceresult().getAllpackets().size() == 0) {
                // we set on purpose
                data.setError(ErrorCodeRegistry.getUnRecognizedPackets());
                data.setSuccess(false);
            } else {
                List<AbstractBestPracticeResult> bestPractices = analyze(result, requests);
                bestPractices.addAll(createEmptyResults());
                data.setAnalyzerResult(result);
                data.setBestPracticeResults(bestPractices);
                data.setSuccess(true);
            }
        }
    } catch (TsharkException ex) {
        data.setError(ErrorCodeRegistry.getWiresharkError());
    }
    return data;
}
Also used : AbstractBestPracticeResult(com.att.aro.core.bestpractice.pojo.AbstractBestPracticeResult) TsharkException(com.att.aro.core.exception.TsharkException) PacketAnalyzerResult(com.att.aro.core.packetanalysis.pojo.PacketAnalyzerResult) AROTraceData(com.att.aro.core.pojo.AROTraceData)

Example 2 with TsharkException

use of com.att.aro.core.exception.TsharkException in project VideoOptimzer by attdevsupport.

the class AROServiceImpl method analyzeDirectory.

/**
 * Launches an analysis of a trace directory with the results populating an
 * AROTraceData object.
 * <p>
 * Other trace files depend on capture method, platform and version of
 * device.
 * </p>
 *
 * @param requests
 *            list of BestPracticeType bestPractices to analyze
 * @param traceDirectory
 *            path to a trace directory, usually contains traffic.cap
 * @param profile
 *            device profile used as a model of the device when analyzing
 *            trace data
 * @param filter
 *            used for filtering information from a trace analysis based on
 *            a specified time range and set of ApplicationSelection
 *            objects.
 * @return AROTraceData object
 * @throws IOException
 *             if trace file not found
 */
@Override
public AROTraceData analyzeDirectory(List<BestPracticeType> requests, String traceDirectory, Profile profile, AnalysisFilter filter) throws IOException {
    AROTraceData data = new AROTraceData();
    PacketAnalyzerResult result = null;
    if (!filemanager.fileExist(traceDirectory + Util.FILE_SEPARATOR + "traffic.cap")) {
        data.setError(ErrorCodeRegistry.getTrafficFileNotFound());
        data.setSuccess(false);
    } else {
        try {
            result = packetanalyzer.analyzeTraceDirectory(traceDirectory, profile, filter);
        } catch (FileNotFoundException ex) {
            data.setError(ErrorCodeRegistry.getTraceDirNotFound());
            return data;
        } catch (TsharkException ex) {
            data.setError(ErrorCodeRegistry.getWiresharkError());
            return data;
        }
        if (result == null) {
            data.setError(ErrorCodeRegistry.getTraceDirectoryNotAnalyzed());
            data.setSuccess(false);
        } else {
            if (result.getTraceresult() == null) {
                // we set this on purpose
                data.setSuccess(false);
                data.setError(ErrorCodeRegistry.getUnRecognizedPackets());
            } else if (result.getTraceresult().getAllpackets() == null || result.getTraceresult().getAllpackets().size() == 0) {
                data.setSuccess(false);
                data.setError(ErrorCodeRegistry.getPacketsNotFound());
            } else {
                List<AbstractBestPracticeResult> bestPractices = analyze(result, requests);
                bestPractices.addAll(createEmptyResults());
                data.setAnalyzerResult(result);
                data.setBestPracticeResults(bestPractices);
                data.setSuccess(true);
            }
        }
    }
    return data;
}
Also used : FileNotFoundException(java.io.FileNotFoundException) TsharkException(com.att.aro.core.exception.TsharkException) ArrayList(java.util.ArrayList) List(java.util.List) PacketAnalyzerResult(com.att.aro.core.packetanalysis.pojo.PacketAnalyzerResult) AROTraceData(com.att.aro.core.pojo.AROTraceData)

Example 3 with TsharkException

use of com.att.aro.core.exception.TsharkException in project VideoOptimzer by attdevsupport.

the class PacketReaderLibraryImpl method readPacket.

@Override
public void readPacket(String packetfile, IPacketListener listener) throws IOException {
    // windows dependency only
    if (Util.isWindowsOS()) {
        checkNativeLib();
    }
    FileDetails fileDetails = parseFileDetails(packetfile);
    if (!fileDetails.isPcap() && !fileDetails.isPcapNG()) {
        LOGGER.error("Not a valid pcap file " + packetfile);
        return;
    }
    long start = System.currentTimeMillis();
    // Update system property org.pcap4j.af.inet6 to correct value based on original OS which captured pcap/pcapng file
    identifyOSInfo(packetfile);
    if (fileDetails.isPcap) {
        PcapHandle handle = null;
        try {
            handle = Pcaps.openOffline(packetfile);
        } catch (PcapNativeException e) {
            LOGGER.error("Error while reading pcap file " + packetfile, e);
            return;
        } catch (Exception ee) {
            LOGGER.error("Error unknow " + packetfile, ee);
            return;
        }
        try {
            int currentPacketNumber = 0;
            int totalPacketReads = 0;
            while (true) {
                try {
                    ++currentPacketNumber;
                    Packet pcap4jPacket = handle.getNextPacketEx();
                    // Get last packet's capture timestamp
                    Timestamp timestamp = handle.getTimestamp();
                    com.att.aro.core.packetreader.pojo.Packet packet = translatePcap4jPacket(timestamp.getTime() / 1000, timestamp.getNanos() / 1000, pcap4jPacket);
                    ++totalPacketReads;
                    listener.packetArrived(null, packet);
                } catch (EOFException e) {
                    LOGGER.info(String.format("Finished reading total %d packets out of %d packets for pcap file %s", totalPacketReads, currentPacketNumber - 1, packetfile));
                    break;
                } catch (Exception ex) {
                    LOGGER.debug("Error while reading packet number " + currentPacketNumber, ex);
                }
            }
        } catch (Exception e) {
            LOGGER.error("Error while reading pcap file " + packetfile, e);
        } finally {
            handle.close();
        }
    } else {
        TSharkConfirmationImpl tsharkConfirmation = new TSharkConfirmationImpl(externalProcessRunner);
        if (!tsharkConfirmation.checkTsharkVersion()) {
            throw new TsharkException("Wireshark is either not installed on your machine or its not in your path.");
        }
        handlePCAPNGFile(packetfile, listener);
    }
    LOGGER.info("Time to read pcap file in ms: " + (System.currentTimeMillis() - start));
}
Also used : TCPPacket(com.att.aro.core.packetreader.pojo.TCPPacket) IcmpV6CommonPacket(org.pcap4j.packet.IcmpV6CommonPacket) IcmpV4CommonPacket(org.pcap4j.packet.IcmpV4CommonPacket) UdpPacket(org.pcap4j.packet.UdpPacket) IPPacket(com.att.aro.core.packetreader.pojo.IPPacket) UnknownPacket(org.pcap4j.packet.UnknownPacket) Packet(org.pcap4j.packet.Packet) UDPPacket(com.att.aro.core.packetreader.pojo.UDPPacket) TcpPacket(org.pcap4j.packet.TcpPacket) IllegalPacket(org.pcap4j.packet.IllegalPacket) TSharkConfirmationImpl(com.att.aro.core.util.TSharkConfirmationImpl) TsharkException(com.att.aro.core.exception.TsharkException) PcapNativeException(org.pcap4j.core.PcapNativeException) Timestamp(java.sql.Timestamp) TsharkException(com.att.aro.core.exception.TsharkException) PcapNativeException(org.pcap4j.core.PcapNativeException) EOFException(java.io.EOFException) IOException(java.io.IOException) PcapHandle(org.pcap4j.core.PcapHandle) EOFException(java.io.EOFException)

Example 4 with TsharkException

use of com.att.aro.core.exception.TsharkException in project VideoOptimzer by attdevsupport.

the class Application method runAnalyzer.

/**
 * Analyze a trace and produce a report either in json or html<br>
 *
 * <pre>
 * Required command:
 *   --analyze with path to trace directory of traffic.cap
 *   --output output file, error if missing
 *   --format html or json, if missing defaults to json
 *
 * @param context
 *            - Spring ApplicationContext
 * @param cmds
 *            - user commands
 */
void runAnalyzer(ApplicationContext context, Commands cmds) {
    String trace = cmds.getAnalyze();
    IAROService serv = context.getBean(IAROService.class);
    AROTraceData results = null;
    // analyze trace file or directory?
    OutSave outSave = prepareSystemOut();
    ImHereThread imHereThread = new ImHereThread(outSave.getOut(), Logger.getRootLogger());
    try {
        if (serv.isFile(trace)) {
            try {
                results = serv.analyzeFile(retrieveBestPractices(), trace);
            } catch (IOException | TsharkException e) {
                errln("Error occured analyzing trace, detail: " + e.getMessage());
                System.exit(1);
            }
        } else {
            try {
                results = serv.analyzeDirectory(retrieveBestPractices(), trace);
            } catch (IOException e) {
                errln("Error occured analyzing trace directory, detail: " + e.getMessage());
                System.exit(1);
            }
        }
        if (results != null && results.isSuccess()) {
            outSave = prepareSystemOut();
            if (cmds.getFormat().equals("json")) {
                if (serv.getJSonReport(cmds.getOutput(), results)) {
                    outln("Successfully produced JSON report: " + cmds.getOutput());
                } else {
                    errln("Failed to produce JSON report.");
                }
            } else {
                if (serv.getHtmlReport(cmds.getOutput(), results)) {
                    println("Successfully produced HTML report: " + cmds.getOutput());
                } else {
                    errln("Failed to produce HTML report.");
                }
            }
        } else {
            printError(results == null ? new ErrorCode() : results.getError());
        }
    } finally {
        imHereThread.endIndicator();
        while (imHereThread.isRunning()) {
            Thread.yield();
        }
        restoreSystemOut(outSave);
    }
    System.exit(0);
}
Also used : IAROService(com.att.aro.core.IAROService) OutSave(com.att.aro.console.printstreamutils.OutSave) TsharkException(com.att.aro.core.exception.TsharkException) IOException(java.io.IOException) ErrorCode(com.att.aro.core.pojo.ErrorCode) AROTraceData(com.att.aro.core.pojo.AROTraceData) ImHereThread(com.att.aro.console.printstreamutils.ImHereThread)

Aggregations

TsharkException (com.att.aro.core.exception.TsharkException)4 AROTraceData (com.att.aro.core.pojo.AROTraceData)3 PacketAnalyzerResult (com.att.aro.core.packetanalysis.pojo.PacketAnalyzerResult)2 IOException (java.io.IOException)2 ImHereThread (com.att.aro.console.printstreamutils.ImHereThread)1 OutSave (com.att.aro.console.printstreamutils.OutSave)1 IAROService (com.att.aro.core.IAROService)1 AbstractBestPracticeResult (com.att.aro.core.bestpractice.pojo.AbstractBestPracticeResult)1 IPPacket (com.att.aro.core.packetreader.pojo.IPPacket)1 TCPPacket (com.att.aro.core.packetreader.pojo.TCPPacket)1 UDPPacket (com.att.aro.core.packetreader.pojo.UDPPacket)1 ErrorCode (com.att.aro.core.pojo.ErrorCode)1 TSharkConfirmationImpl (com.att.aro.core.util.TSharkConfirmationImpl)1 EOFException (java.io.EOFException)1 FileNotFoundException (java.io.FileNotFoundException)1 Timestamp (java.sql.Timestamp)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 PcapHandle (org.pcap4j.core.PcapHandle)1 PcapNativeException (org.pcap4j.core.PcapNativeException)1