All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hyperic.sigar.cmd.Netstat Maven / Gradle / Ivy

/*
 * Copyright (c) 2006-2007 Hyperic, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.hyperic.sigar.cmd;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;

import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.NetConnection;
import org.hyperic.sigar.NetFlags;
import org.hyperic.sigar.Tcp;

/**
 * Display network connections.
 */
public class Netstat extends SigarCommandBase {

    private static final int LADDR_LEN = 20;
    private static final int RADDR_LEN = 35;

    private static final String[] HEADER = new String[] {
        "Proto",
        "Local Address",
        "Foreign Address",
        "State",
        ""
    };

    private static boolean isNumeric, wantPid, isStat;

    public Netstat(Shell shell) {
        super(shell);
    }

    public Netstat() {
        super();
    }

    protected boolean validateArgs(String[] args) {
        return true;
    }

    public String getUsageShort() {
        return "Display network connections";
    }

    //poor mans getopt.
    public static int getFlags(String[] args, int flags) {
        int proto_flags = 0;
        isNumeric = false;
        wantPid = false;
        isStat = false;

        for (int i=0; i max) {
            address = address.substring(0, max);
        }

        return address + ":" + port; 
    }

    private void outputTcpStats() throws SigarException {
        Tcp stat = this.sigar.getTcp();
        final String dnt = "    ";
        println(dnt + stat.getActiveOpens() + " active connections openings");
        println(dnt + stat.getPassiveOpens() + " passive connection openings");
        println(dnt + stat.getAttemptFails() + " failed connection attempts");
        println(dnt + stat.getEstabResets() + " connection resets received");
        println(dnt + stat.getCurrEstab() + " connections established");
        println(dnt + stat.getInSegs() + " segments received");
        println(dnt + stat.getOutSegs() + " segments send out");
        println(dnt + stat.getRetransSegs() + " segments retransmited");
        println(dnt + stat.getInErrs() + " bad segments received.");
        println(dnt + stat.getOutRsts() + " resets sent");
    }

    private void outputStats(int flags) throws SigarException {
        if ((flags & NetFlags.CONN_TCP) != 0) {
            println("Tcp:");
            try {
                outputTcpStats();
            } catch (SigarException e) {
                println("    " + e);
            }
        }
    }

    //XXX currently weak sauce.  should end up like netstat command.
    public void output(String[] args) throws SigarException {
        //default
        int flags = NetFlags.CONN_CLIENT | NetFlags.CONN_PROTOCOLS;

        if (args.length > 0) {
            flags = getFlags(args, flags);
            if (isStat) {
                outputStats(flags);
                return;
            }
        }

        NetConnection[] connections = this.sigar.getNetConnectionList(flags);
        printf(HEADER);

        for (int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy