本文整理汇总了Java中org.hyperic.sigar.NetFlags类的典型用法代码示例。如果您正苦于以下问题:Java NetFlags类的具体用法?Java NetFlags怎么用?Java NetFlags使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
NetFlags类属于org.hyperic.sigar包,在下文中一共展示了NetFlags类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getIfaces
import org.hyperic.sigar.NetFlags; //导入依赖的package包/类
public static NetInterfaceConfig[] getIfaces() throws SigarException {
_init();
String[] ifaces = sigar.getNetInterfaceList();
List<NetInterfaceConfig> l1 = new ArrayList<NetInterfaceConfig>();
for (int i = 0; i < ifaces.length; i++) {
NetInterfaceConfig cfg = sigar.getNetInterfaceConfig(ifaces[i]);
if (NetFlags.LOOPBACK_ADDRESS.equals(cfg.getAddress()) || (cfg.getFlags() & NetFlags.IFF_LOOPBACK) != 0
|| NetFlags.NULL_HWADDR.equals(cfg.getHwaddr())) {
continue;
}
l1.add(cfg);
}
return l1.toArray(new NetInterfaceConfig[l1.size()]);
}
示例2: get
import org.hyperic.sigar.NetFlags; //导入依赖的package包/类
public SystemMetrics get() {
try {
final SystemMetrics.Builder metrics = new SystemMetrics.Builder();
metrics.cpuPerc(sigar.getCpuPerc());
metrics.mem(sigar.getMem());
metrics.tcp(sigar.getTcp());
metrics.tcpEstablishedCount(getCount(NetFlags.TCP_ESTABLISHED));
metrics.tcpIdleCount(getCount(NetFlags.TCP_IDLE));
metrics.tcpCloseCount(getCount(NetFlags.TCP_CLOSE));
metrics.tcpClosingCount(getCount(NetFlags.TCP_CLOSING));
metrics.tcpCloseWaitCount(getCount(NetFlags.TCP_CLOSE_WAIT));
metrics.tcpTimeWaitCount(getCount(NetFlags.TCP_TIME_WAIT));
metrics.tcpBoundCount(getCount(NetFlags.TCP_BOUND));
return metrics.build();
} catch (final SigarException e) {
LOG.error("Could not get system metrics from Sigar.", e);
return new SystemMetrics.Builder().build();
}
}
示例3: formatAddress
import org.hyperic.sigar.NetFlags; //导入依赖的package包/类
private String formatAddress(int proto, String ip,
long portnum, int max) {
String port = formatPort(proto, portnum);
String address;
if (NetFlags.isAnyAddress(ip)) {
address = "*";
}
else if (isNumeric) {
address = ip;
}
else {
try {
address = InetAddress.getByName(ip).getHostName();
} catch (UnknownHostException e) {
address = ip;
}
}
max -= port.length() + 1;
if (address.length() > max) {
address = address.substring(0, max);
}
return address + ":" + port;
}
示例4: outputStats
import org.hyperic.sigar.NetFlags; //导入依赖的package包/类
private void outputStats(int flags) throws SigarException {
if ((flags & NetFlags.CONN_TCP) != 0) {
println("Tcp:");
try {
outputTcpStats();
} catch (SigarException e) {
println(" " + e);
}
}
}
示例5: getNetStats
import org.hyperic.sigar.NetFlags; //导入依赖的package包/类
public static List<JSON> getNetStats() throws SigarException {
_init();
int flags = NetFlags.CONN_CLIENT | NetFlags.CONN_SERVER | NetFlags.CONN_TCP | NetFlags.CONN_UDP
| NetFlags.CONN_PROTOCOLS;
NetConnection[] nn = sigar.getNetConnectionList(flags);
List<JSON> l1 = new ArrayList<JSON>();
for (NetConnection n : nn) {
if (n.getRemotePort() == 0 && n.getLocalPort() == 0)
continue;
JSON jo = JSON.create().append("remoteaddress", n.getRemoteAddress())
.append("remoteport", n.getRemotePort()).append("localaddress", n.getLocalAddress())
.append("localport", n.getLocalPort()).append("recvqueue", n.getReceiveQueue())
.append("sendqueue", n.getSendQueue()).append("state", n.getState()).append("type", n.getType())
.append("typename", n.getTypeString());
if (n.getType() != NetFlags.CONN_UDP) {
jo.append("statename", n.getStateString());
}
try {
long pid = sigar.getProcPort(n.getType(), n.getLocalPort());
if (pid != 0) {
ProcState p = sigar.getProcState(pid);
if (p != null) {
jo.append("pid", pid + "/" + p.getName());
}
}
} catch (Exception e) {
// ignore
log.warn("get pid error, type=" + n.getType() + ", port=" + n.getLocalPort());
}
l1.add(jo);
}
return l1;
}
示例6: output
import org.hyperic.sigar.NetFlags; //导入依赖的package包/类
public void output(String name) throws SigarException {
NetInterfaceConfig ifconfig =
this.sigar.getNetInterfaceConfig(name);
long flags = ifconfig.getFlags();
String hwaddr = "";
if (!NetFlags.NULL_HWADDR.equals(ifconfig.getHwaddr())) {
hwaddr = " HWaddr " + ifconfig.getHwaddr();
}
if (!ifconfig.getName().equals(ifconfig.getDescription())) {
println(ifconfig.getDescription());
}
println(ifconfig.getName() + "\t" +
"Link encap:" + ifconfig.getType() +
hwaddr);
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();
}
println("\t" +
"inet addr:" + ifconfig.getAddress() +
ptp + //unlikely
bcast +
" Mask:" + ifconfig.getNetmask());
println("\t" +
NetFlags.getIfFlagsString(flags) +
" MTU:" + ifconfig.getMtu() +
" Metric:" + ifconfig.getMetric());
try {
NetInterfaceStat ifstat =
this.sigar.getNetInterfaceStat(name);
println("\t" +
"RX packets:" + ifstat.getRxPackets() +
" errors:" + ifstat.getRxErrors() +
" dropped:" + ifstat.getRxDropped() +
" overruns:" + ifstat.getRxOverruns() +
" frame:" + ifstat.getRxFrame());
println("\t" +
"TX packets:" + ifstat.getTxPackets() +
" errors:" + ifstat.getTxErrors() +
" dropped:" + ifstat.getTxDropped() +
" overruns:" + ifstat.getTxOverruns() +
" carrier:" + ifstat.getTxCarrier());
println("\t" + "collisions:" +
ifstat.getTxCollisions());
long rxBytes = ifstat.getRxBytes();
long txBytes = ifstat.getTxBytes();
println("\t" +
"RX bytes:" + rxBytes +
" (" + Sigar.formatSize(rxBytes) + ")" +
" " +
"TX bytes:" + txBytes +
" (" + Sigar.formatSize(txBytes) + ")");
} catch (SigarException e) {
}
println("");
}
示例7: getFlags
import org.hyperic.sigar.NetFlags; //导入依赖的package包/类
public static int getFlags(String[] args, int flags) {
int proto_flags = 0;
isNumeric = false;
wantPid = false;
isStat = false;
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 's':
isStat = 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:
System.err.println("unknown option");
}
}
}
if (proto_flags != 0) {
flags &= ~NetFlags.CONN_PROTOCOLS;
flags |= proto_flags;
}
return flags;
}
示例8: output
import org.hyperic.sigar.NetFlags; //导入依赖的package包/类
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<connections.length; i++) {
NetConnection conn = connections[i];
String proto = conn.getTypeString();
String state;
if (conn.getType() == NetFlags.CONN_UDP) {
state = "";
}
else {
state = conn.getStateString();
}
ArrayList items = new ArrayList();
items.add(proto);
items.add(formatAddress(conn.getType(),
conn.getLocalAddress(),
conn.getLocalPort(),
LADDR_LEN));
items.add(formatAddress(conn.getType(),
conn.getRemoteAddress(),
conn.getRemotePort(),
RADDR_LEN));
items.add(state);
String process = null;
if (wantPid &&
//XXX only works w/ listen ports
(conn.getState() == NetFlags.TCP_LISTEN))
{
try {
long pid =
this.sigar.getProcPort(conn.getType(),
conn.getLocalPort());
if (pid != 0) { //XXX another bug
String name =
this.sigar.getProcState(pid).getName();
process = pid + "/" + name;
}
} catch (SigarException e) {
}
}
if (process == null) {
process = "";
}
items.add(process);
printf(items);
}
}
示例9: run
import org.hyperic.sigar.NetFlags; //导入依赖的package包/类
@Override
public void run()
{
if(pid == -1)
{
// get current process PID
pid = sigar.getPid();
}
if(interfaceName == null)
{
try
{
NetInterfaceConfig ifConfig = sigar.getNetInterfaceConfig();
if(!NetFlags.NULL_HWADDR.equals(ifConfig.getHwaddr()))
{
interfaceName = ifConfig.getName();
}
} catch(SigarException ex)
{
logger.error(ex.getMessage(), ex);
}
}
logger.info("Running monitoring cycle...");
running = true;
try
{
while(running)
{
collectMetrics();
sleep(period);
}
}
catch(Exception e)
{
running = false;
}
logger.info("Finished monitoring cycle at " + System.currentTimeMillis());
writeStatsToFile();
}