add network info printing

This commit is contained in:
kimchy 2010-05-20 21:10:53 +03:00
parent a8a4bbc30e
commit 3e0f854c88
9 changed files with 151 additions and 9 deletions

View File

@ -36,6 +36,7 @@
<w>freqs</w> <w>freqs</w>
<w>hpux</w> <w>hpux</w>
<w>hyperic</w> <w>hyperic</w>
<w>ifconfig</w>
<w>indices</w> <w>indices</w>
<w>inet</w> <w>inet</w>
<w>infos</w> <w>infos</w>
@ -52,6 +53,7 @@
<w>linefeeds</w> <w>linefeeds</w>
<w>loopback</w> <w>loopback</w>
<w>lucene</w> <w>lucene</w>
<w>mcast</w>
<w>memcached</w> <w>memcached</w>
<w>metadata</w> <w>metadata</w>
<w>metadatas</w> <w>metadatas</w>

View File

@ -41,4 +41,8 @@ public class JmxOsProbe extends AbstractComponent implements OsProbe {
stats.timestamp = System.currentTimeMillis(); stats.timestamp = System.currentTimeMillis();
return stats; return stats;
} }
@Override public String ifconfig() {
return "NA";
}
} }

View File

@ -27,4 +27,6 @@ public interface OsProbe {
OsInfo osInfo(); OsInfo osInfo();
OsStats osStats(); OsStats osStats();
String ifconfig();
} }

View File

@ -48,4 +48,8 @@ public class OsService extends AbstractComponent {
public OsStats stats() { public OsStats stats() {
return probe.osStats(); return probe.osStats();
} }
public String ifconfig() {
return probe.ifconfig();
}
} }

View File

@ -126,4 +126,85 @@ public class SigarOsProbe extends AbstractComponent implements OsProbe {
return stats; return stats;
} }
@Override public String ifconfig() {
Sigar sigar = sigarService.sigar();
StringBuilder sb = new StringBuilder();
try {
for (String ifname : sigar.getNetInterfaceList()) {
NetInterfaceConfig ifconfig = null;
try {
ifconfig = sigar.getNetInterfaceConfig(ifname);
} catch (SigarException e) {
sb.append(ifname + "\t" + "Not Avaialbe [" + e.getMessage() + "]");
}
long flags = ifconfig.getFlags();
String hwaddr = "";
if (!NetFlags.NULL_HWADDR.equals(ifconfig.getHwaddr())) {
hwaddr = " HWaddr " + ifconfig.getHwaddr();
}
if (!ifconfig.getName().equals(ifconfig.getDescription())) {
sb.append(ifconfig.getDescription()).append('\n');
}
sb.append(ifconfig.getName() + "\t" + "Link encap:" + ifconfig.getType() + hwaddr).append('\n');
String ptp = "";
if ((flags & NetFlags.IFF_POINTOPOINT) > 0) {
ptp = " P-t-P:" + ifconfig.getDestination();
}
String bcast = "";
if ((flags & NetFlags.IFF_BROADCAST) > 0) {
bcast = " Bcast:" + ifconfig.getBroadcast();
}
sb.append("\t" +
"inet addr:" + ifconfig.getAddress() +
ptp + //unlikely
bcast +
" Mask:" + ifconfig.getNetmask()).append('\n');
sb.append("\t" +
NetFlags.getIfFlagsString(flags) +
" MTU:" + ifconfig.getMtu() +
" Metric:" + ifconfig.getMetric()).append('\n');
try {
NetInterfaceStat ifstat = sigar.getNetInterfaceStat(ifname);
sb.append("\t" +
"RX packets:" + ifstat.getRxPackets() +
" errors:" + ifstat.getRxErrors() +
" dropped:" + ifstat.getRxDropped() +
" overruns:" + ifstat.getRxOverruns() +
" frame:" + ifstat.getRxFrame()).append('\n');
sb.append("\t" +
"TX packets:" + ifstat.getTxPackets() +
" errors:" + ifstat.getTxErrors() +
" dropped:" + ifstat.getTxDropped() +
" overruns:" + ifstat.getTxOverruns() +
" carrier:" + ifstat.getTxCarrier()).append('\n');
sb.append("\t" + "collisions:" +
ifstat.getTxCollisions()).append('\n');
long rxBytes = ifstat.getRxBytes();
long txBytes = ifstat.getTxBytes();
sb.append("\t" +
"RX bytes:" + rxBytes +
" (" + Sigar.formatSize(rxBytes) + ")" +
" " +
"TX bytes:" + txBytes +
" (" + Sigar.formatSize(txBytes) + ")").append('\n');
} catch (SigarException e) {
}
}
return sb.toString();
} catch (SigarException e) {
return "NA";
}
}
} }

View File

@ -31,15 +31,23 @@ public class OsUtils {
/** /**
* True iff running on Linux. * True iff running on Linux.
*/ */
public static final boolean LINUX = OS_NAME.startsWith("Linux"); public static final boolean LINUX = OS_NAME.trim().toLowerCase().startsWith("linux");
/** /**
* True iff running on Windows. * True iff running on Windows.
*/ */
public static final boolean WINDOWS = OS_NAME.startsWith("Windows"); public static final boolean WINDOWS = OS_NAME.trim().toLowerCase().startsWith("windows");
/** /**
* True iff running on SunOS. * True iff running on SunOS.
*/ */
public static final boolean SUN_OS = OS_NAME.startsWith("SunOS"); public static final boolean SOLARIS = OS_NAME.trim().toLowerCase().startsWith("sun");
/**
* True iff running on Mac.
*/
public static final boolean MAC = OS_NAME.trim().toLowerCase().startsWith("mac");
/**
* True iff running on HP.
*/
public static final boolean HP = OS_NAME.trim().toLowerCase().startsWith("hp");
private OsUtils() { private OsUtils() {

View File

@ -20,6 +20,7 @@
package org.elasticsearch.util; package org.elasticsearch.util;
import org.elasticsearch.util.collect.ImmutableSet; import org.elasticsearch.util.collect.ImmutableSet;
import org.elasticsearch.util.collect.Iterables;
import java.util.*; import java.util.*;
@ -1071,8 +1072,8 @@ public class Strings {
* @param suffix the String to end each element with * @param suffix the String to end each element with
* @return the delimited String * @return the delimited String
*/ */
public static String collectionToDelimitedString(Collection coll, String delim, String prefix, String suffix) { public static String collectionToDelimitedString(Iterable coll, String delim, String prefix, String suffix) {
if (isEmpty(coll)) { if (Iterables.isEmpty(coll)) {
return ""; return "";
} }
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
@ -1094,7 +1095,7 @@ public class Strings {
* @param delim the delimiter to use (probably a ",") * @param delim the delimiter to use (probably a ",")
* @return the delimited String * @return the delimited String
*/ */
public static String collectionToDelimitedString(Collection coll, String delim) { public static String collectionToDelimitedString(Iterable coll, String delim) {
return collectionToDelimitedString(coll, delim, "", ""); return collectionToDelimitedString(coll, delim, "", "");
} }
@ -1105,7 +1106,7 @@ public class Strings {
* @param coll the Collection to display * @param coll the Collection to display
* @return the delimited String * @return the delimited String
*/ */
public static String collectionToCommaDelimitedString(Collection coll) { public static String collectionToCommaDelimitedString(Iterable coll) {
return collectionToDelimitedString(coll, ","); return collectionToDelimitedString(coll, ",");
} }

View File

@ -19,6 +19,8 @@
package org.elasticsearch.util.network; package org.elasticsearch.util.network;
import org.elasticsearch.monitor.os.JmxOsProbe;
import org.elasticsearch.monitor.os.OsService;
import org.elasticsearch.util.MapBuilder; import org.elasticsearch.util.MapBuilder;
import org.elasticsearch.util.collect.ImmutableMap; import org.elasticsearch.util.collect.ImmutableMap;
import org.elasticsearch.util.component.AbstractComponent; import org.elasticsearch.util.component.AbstractComponent;
@ -30,6 +32,7 @@ import java.net.InetAddress;
import java.net.NetworkInterface; import java.net.NetworkInterface;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.Collection; import java.util.Collection;
import java.util.Enumeration;
/** /**
* @author kimchy (shay.banon) * @author kimchy (shay.banon)
@ -56,8 +59,42 @@ public class NetworkService extends AbstractComponent {
private volatile ImmutableMap<String, CustomNameResolver> customNameResolvers = ImmutableMap.of(); private volatile ImmutableMap<String, CustomNameResolver> customNameResolvers = ImmutableMap.of();
@Inject public NetworkService(Settings settings) { public NetworkService(Settings settings) {
this(settings, new OsService(settings, new JmxOsProbe(settings)));
}
@Inject public NetworkService(Settings settings, OsService service) {
super(settings); super(settings);
if (logger.isDebugEnabled()) {
StringBuilder netDebug = new StringBuilder("net_info");
try {
Enumeration<NetworkInterface> enum_ = NetworkInterface.getNetworkInterfaces();
String hostName = InetAddress.getLocalHost().getHostName();
netDebug.append("\nhost [").append(hostName).append("]\n");
while (enum_.hasMoreElements()) {
NetworkInterface net = enum_.nextElement();
netDebug.append(net.getName()).append('\t').append("display_name [").append(net.getDisplayName()).append("]\n");
Enumeration<InetAddress> addresses = net.getInetAddresses();
netDebug.append("\t\taddress ");
while (addresses.hasMoreElements()) {
netDebug.append("[").append(addresses.nextElement()).append("] ");
}
netDebug.append('\n');
netDebug.append("\t\tmtu [").append(net.getMTU()).append("] multicast [").append(net.supportsMulticast()).append("] ptp [").append(net.isPointToPoint())
.append("] loopback [").append(net.isLoopback()).append("] up [").append(net.isUp()).append("] virtual [").append(net.isVirtual()).append("]")
.append('\n');
}
} catch (Exception ex) {
netDebug.append("Failed to get Network Interface Info [" + ex.getMessage() + "]");
}
logger.debug(netDebug.toString());
}
if (logger.isTraceEnabled()) {
logger.trace("ifconfig\n\n" + service.ifconfig());
}
} }
public void addCustomNameResolver(String name, CustomNameResolver customNameResolver) { public void addCustomNameResolver(String name, CustomNameResolver customNameResolver) {

View File

@ -62,7 +62,6 @@ public abstract class NetworkUtils {
return System.getProperty("java.net.preferIPv4Stack") != null && System.getProperty("java.net.preferIPv4Stack").equals("true"); return System.getProperty("java.net.preferIPv4Stack") != null && System.getProperty("java.net.preferIPv4Stack").equals("true");
} }
public static InetAddress getIPv4Localhost() throws UnknownHostException { public static InetAddress getIPv4Localhost() throws UnknownHostException {
return getLocalhost(StackType.IPv4); return getLocalhost(StackType.IPv4);
} }
@ -82,6 +81,10 @@ public abstract class NetworkUtils {
return InetAddress.getByName("::1"); return InetAddress.getByName("::1");
} }
public static boolean canBindToMcastAddress() {
return OsUtils.LINUX || OsUtils.SOLARIS || OsUtils.HP;
}
/** /**
* Returns the first non-loopback address on any interface on the current host. * Returns the first non-loopback address on any interface on the current host.