|
From: <do...@hy...> - 2007-03-25 02:42:38
|
Author: dougm Date: 2007-03-24 18:42:01 -0800 (Sat, 24 Mar 2007) New Revision: 3874 URL: http://svn.hyperic.org/?view=rev&root=Hyperic+HQ&revision=3874 Added: trunk/plugins/system/src/org/hyperic/hq/plugin/system/NetConnectionData.java trunk/plugins/system/src/org/hyperic/hq/plugin/system/NetstatData.java Modified: trunk/plugins/system/src/org/hyperic/hq/plugin/system/SystemLiveDataPlugin.java Log: add netstat livedata command Added: trunk/plugins/system/src/org/hyperic/hq/plugin/system/NetConnectionData.java =================================================================== --- trunk/plugins/system/src/org/hyperic/hq/plugin/system/NetConnectionData.java (rev 0) +++ trunk/plugins/system/src/org/hyperic/hq/plugin/system/NetConnectionData.java 2007-03-25 02:42:01 UTC (rev 3874) @@ -0,0 +1,164 @@ +/* + * NOTE: This copyright does *not* cover user programs that use HQ + * program services by normal system calls through the application + * program interfaces provided as part of the Hyperic Plug-in Development + * Kit or the Hyperic Client Development Kit - this is merely considered + * normal use of the program, and does *not* fall under the heading of + * "derived work". + * + * Copyright (C) [2004, 2005, 2006], Hyperic, Inc. + * This file is part of HQ. + * + * HQ is free software; you can redistribute it and/or modify + * it under the terms version 2 of the GNU General Public License as + * published by the Free Software Foundation. This program is distributed + * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + */ +//XXX move this class to sigar +package org.hyperic.hq.plugin.system; + +import java.net.InetAddress; +import java.net.UnknownHostException; + +import org.hyperic.sigar.NetConnection; +import org.hyperic.sigar.NetFlags; +import org.hyperic.sigar.NetServices; +import org.hyperic.sigar.Sigar; +import org.hyperic.sigar.SigarException; + +public class NetConnectionData { + + private NetConnection _conn; + private boolean _isNumeric; + private String _proto; + private int _maxLocalAddrLen = -1; + private int _maxRemoteAddrLen = -1; + private long _processPid = -1; + private String _processName; + + public NetConnectionData(NetConnection conn, + boolean isNumeric) { + _conn = conn; + _isNumeric = isNumeric; + _proto = conn.getTypeString(); + } + + public void setMaxLocalAddrLen(int len) { + _maxLocalAddrLen = len; + } + + public void setMaxRemoteAddrLen(int len) { + _maxRemoteAddrLen = len; + } + + public String getProtocol() { + return _proto; + } + + public String getFormattedPort(long port) { + if (port == 0) { + return "*"; + } + if (!_isNumeric) { + String service = + NetServices.getName(_proto, port); + if (service != null) { + return service; + } + } + + return String.valueOf(port); + } + + public String getFormattedAddress(String ip, + long port, + int max) { + + String fport = getFormattedPort(port); + String address; + + if (NetFlags.isAnyAddress(ip)) { + address = "*"; + } + else if (_isNumeric) { + address = ip; + } + else { + try { + address = InetAddress.getByName(ip).getHostName(); + } catch (UnknownHostException e) { + address = ip; + } + } + + if (max != -1) { + max -= fport.length() + 1; + if (address.length() > max) { + address = address.substring(0, max); + } + } + + return address + ":" + fport; + } + + public String getFormattedLocalAddress() { + return getFormattedAddress(_conn.getLocalAddress(), + _conn.getLocalPort(), + _maxLocalAddrLen); + } + + public String getFormattedRemoteAddress() { + return getFormattedAddress(_conn.getRemoteAddress(), + _conn.getRemotePort(), + _maxRemoteAddrLen); + } + + public String getFormattedState() { + if (_conn.getType() == NetFlags.CONN_UDP) { + return ""; + } + else { + return _conn.getStateString(); + } + } + + public long getProcessPid() { + return _processPid; + } + + public String getProcessName() { + return _processName; + } + + public String getFormattedProcessName() { + if (_processPid == -1) { + return ""; + } + return _processPid + "/" + _processName; + } + + public void lookupProcessInfo(Sigar sigar) { + if (_conn.getState() != NetFlags.TCP_LISTEN) { + return; + } + try { + long pid = + sigar.getProcPort(_conn.getType(), + _conn.getLocalPort()); + if (pid != 0) { //XXX another bug + _processPid = pid; + _processName = + sigar.getProcState(pid).getName(); + } + } catch (SigarException e) { + } + } +} Added: trunk/plugins/system/src/org/hyperic/hq/plugin/system/NetstatData.java =================================================================== --- trunk/plugins/system/src/org/hyperic/hq/plugin/system/NetstatData.java (rev 0) +++ trunk/plugins/system/src/org/hyperic/hq/plugin/system/NetstatData.java 2007-03-25 02:42:01 UTC (rev 3874) @@ -0,0 +1,181 @@ +/* + * NOTE: This copyright does *not* cover user programs that use HQ + * program services by normal system calls through the application + * program interfaces provided as part of the Hyperic Plug-in Development + * Kit or the Hyperic Client Development Kit - this is merely considered + * normal use of the program, and does *not* fall under the heading of + * "derived work". + * + * Copyright (C) [2004, 2005, 2006], Hyperic, Inc. + * This file is part of HQ. + * + * HQ is free software; you can redistribute it and/or modify + * it under the terms version 2 of the GNU General Public License as + * published by the Free Software Foundation. This program is distributed + * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + */ +//XXX move this class to sigar +package org.hyperic.hq.plugin.system; + +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.List; + +import org.hyperic.sigar.NetConnection; +import org.hyperic.sigar.NetFlags; +import org.hyperic.sigar.Sigar; +import org.hyperic.sigar.SigarException; + +public class NetstatData { + public static final String LABEL_PROTO = "Proto"; + public static final String LABEL_LADDR = "Local Address"; + public static final String LABEL_RADDR = "Foreign Address"; + public static final String LABEL_STATE = "State"; + + private boolean _isNumeric = false; + private boolean _wantPid = false; + private int _flags = + NetFlags.CONN_CLIENT | NetFlags.CONN_PROTOCOLS; + private List _connections; + + public NetstatData() {} + + public void populate(Sigar sigar) throws SigarException { + _connections = new ArrayList(); + NetConnection[] connections = + sigar.getNetConnectionList(_flags); + + for (int i=0; i<connections.length; i++) { + NetConnectionData data = + new NetConnectionData(connections[i], _isNumeric); + + if (_wantPid) { + data.lookupProcessInfo(sigar); + } + + _connections.add(data); + } + } + + public List getConnections() { + return _connections; + } + + public boolean isNumeric() { + return _isNumeric; + } + + public void setIsNumeric(boolean isNumeric) { + _isNumeric = isNumeric; + } + + public boolean wantPid() { + return _wantPid; + } + + public void setWantPid(boolean wantPid) { + _wantPid = wantPid; + } + + public int getFlags() { + return _flags; + } + + public void setFlags(int flags) { + _flags = flags; + } + + public void setFlags(String flags) { + setFlags(new String[] { flags }); + } + + public void setFlags(String[] args) { + int proto_flags = 0; + + for (int i=0; i<args.length; i++) { + String arg = args[i]; + int j = 0; + + while (j<arg.length()) { + switch (arg.charAt(j++)) { + case '-': + continue; + case 'l': + _flags &= ~NetFlags.CONN_CLIENT; + _flags |= NetFlags.CONN_SERVER; + break; + case 'a': + _flags |= NetFlags.CONN_SERVER | NetFlags.CONN_CLIENT; + break; + case 'n': + _isNumeric = true; + break; + case 'p': + _wantPid = true; + break; + case 't': + proto_flags |= NetFlags.CONN_TCP; + break; + case 'u': + proto_flags |= NetFlags.CONN_UDP; + break; + case 'w': + proto_flags |= NetFlags.CONN_RAW; + break; + case 'x': + proto_flags |= NetFlags.CONN_UNIX; + break; + default: + } + } + } + + if (proto_flags != 0) { + _flags &= ~NetFlags.CONN_PROTOCOLS; + _flags |= proto_flags; + } + } + + public void print(PrintStream out) { + final String header = + LABEL_PROTO + "\t" + + LABEL_LADDR + "\t" + + LABEL_RADDR + "\t" + + LABEL_STATE; + out.println(header); + + List connections = getConnections(); + for (int i=0; i<connections.size(); i++) { + NetConnectionData data = + (NetConnectionData)connections.get(i); + + String conn = + data.getProtocol() + "\t" + + data.getFormattedLocalAddress() + "\t" + + data.getFormattedRemoteAddress() + "\t" + + data.getFormattedState() + "\t" + + data.getFormattedProcessName(); + + out.println(conn); + } + } + + public static void main(String[] args) throws Exception { + Sigar sigar = new Sigar(); + NetstatData data = new NetstatData(); + if (args.length != 0) { + data.setFlags(args); + } + data.populate(sigar); + data.print(System.out); + sigar.close(); + } +} Modified: trunk/plugins/system/src/org/hyperic/hq/plugin/system/SystemLiveDataPlugin.java =================================================================== --- trunk/plugins/system/src/org/hyperic/hq/plugin/system/SystemLiveDataPlugin.java 2007-03-25 00:08:05 UTC (rev 3873) +++ trunk/plugins/system/src/org/hyperic/hq/plugin/system/SystemLiveDataPlugin.java 2007-03-25 02:42:01 UTC (rev 3874) @@ -40,13 +40,15 @@ private static final String CMD_CPUPERC = "cpuperc"; private static final String CMD_FILESYSTEM = "filesystem"; private static final String CMD_TOP = "top"; + private static final String CMD_NETSTAT = "netstat"; private static final String _COMMANDS[] = { CMD_CPUINFO, CMD_CPU, CMD_CPUPERC, CMD_FILESYSTEM, - CMD_TOP + CMD_TOP, + CMD_NETSTAT }; public Object getData(String command, ConfigResponse config) @@ -67,6 +69,14 @@ String filter = config.getValue(SigarMeasurementPlugin.PTQL_CONFIG); return TopData.gather(sigar, filter); + } else if (command.equals(CMD_NETSTAT)) { + NetstatData data = new NetstatData(); + String flags = config.getValue("netstat.flags"); + if (flags != null) { + data.setFlags(flags); + } + data.populate(sigar); + return data; } else { throw new PluginException("Unknown command '" + command + "'"); } |