Skip to content

Commit

Permalink
Merge pull request #399 from fyb007/yongbiaofeng
Browse files Browse the repository at this point in the history
Monitor network card information
  • Loading branch information
zxy0728 authored Sep 14, 2018
2 parents 224efd9 + 55043c2 commit 46cb891
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ public void handleClientOut(HeartBeatEvent data) {
}
ni.putInfo(InfoType.OS, "io.disk", str);

// netcatd info, include name,ip,Bcast and Mask
ni.putInfo(InfoType.OS, "netcard", NetworkHelper.getNetCardInfo());

// java version
ni.putInfo(InfoType.OS, "java.ver", System.getProperty("java.version"));
// java vm name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,7 @@ var mvcObj={
"os.cpu.freemem":{key:'info/os.cpu.freemem'},
"os.conn.cur":{key:'info/os.conn.cur'},
"os.io.disk":{key:'info/os.io.disk'},
"os.netcard":{key:'info/os.netcard'},
"os.java.vm":{key:'info/os.java.vm'},
"os.java.ver":{key:'info/os.java.ver'},
"os.java.home":{key:'info/os.java.home'},
Expand Down Expand Up @@ -1882,6 +1883,9 @@ var mvcObj={
" <div class=\"kv\">" +
" <span class=\"kvField\">磁盘</span><span>:</span>"+this.formatter.disk(resultObj) +
" </div>" +
" <div class=\"kv\">" +
" <span class=\"kvField\">网卡</span><span>:</span>"+this.formatter.netcard(resultObj) +
" </div>" +
" <div class=\"kv\">" +
" <span class=\"kvField\">标签</span><span>:</span>"+resultObj["node.tags"] +
" </div>" +
Expand Down Expand Up @@ -1931,6 +1935,30 @@ var mvcObj={

return sb.toString();
},
netcard:function(resultObj) {

var netcardStr = resultObj["os.netcard"];
var netcards=eval("("+netcardStr+")");
var sb=new StringBuffer();

for(netcard in netcards) {
sb.append("<div class='kvField'>"+"<span class='kvSubField' style='display:inline-block;width:130px;'>"+netcard+"</span>:");
var i=0;
for(ip in netcards[netcard]){
var ipInfo="<span class='kvSubValue'>"+ip+"</span>";
var mask="<span class='kvSubValue'>"+netcards[netcard][ip]["mask"]+"</span>";
var bcast="<span class='kvSubValue'>"+netcards[netcard][ip]["bcast"]+"</span>";

if(i!=0){
sb.append("<div class='kvField'>"+"<span class='kvSubField' style='display:inline-block;width:130px;'></span> ");
}
i++;
sb.append("&nbsp;IP "+ipInfo+",&nbsp;子网掩码 "+mask+",&nbsp;广播地址 "+bcast+"</div>");
}
}

return sb.toString();
},
feature:function(f,nodeObj) {
var fts=eval("("+f+")");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,4 +448,10 @@ public static List<InetAddress> getAllIP() {

return hni.getIPs();
}

public static String getNetCardInfo() {

HostNewworkInfo hni = new HostNewworkInfo();
return JSONHelper.toString(hni.getNetCardInfo());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package com.creditease.uav.helpers.network;

import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
Expand Down Expand Up @@ -131,4 +132,64 @@ public String getNetCardName(String ipString) {

return null;
}

/**
* @return {wlan0={101.254.182.34={bcast=101.254.182.255, mask=255.255.254.0}}}
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public Map getNetCardInfo() {

Map netCardInfos = new HashMap<String, Map<String, Map<String, String>>>();
Enumeration<NetworkInterface> netInterfaces;
try {
netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = netInterfaces.nextElement();
// if (ni.isVirtual()) {
// continue;
// }

Map netCardInfo = new HashMap<String, Map<String, String>>();
List<InterfaceAddress> addresses = ni.getInterfaceAddresses();
for (InterfaceAddress ia : addresses) {
if (ia.getAddress().isLoopbackAddress() || ia.getAddress().getHostAddress().indexOf(":") != -1) {
continue;
}

Map ipInfo = new HashMap<String, String>();
ipInfo.put("bcast", ia.getBroadcast().getHostAddress());
ipInfo.put("mask", NetmaskLengthToNetmask(ia.getNetworkPrefixLength()));

netCardInfo.put(ia.getAddress().getHostAddress(), ipInfo);
}

if (!netCardInfo.isEmpty()) {
netCardInfos.put(ni.getName(), netCardInfo);
}
}
}
catch (SocketException e) {
// ignore
}

return netCardInfos;
}

// only support ipv4
public String NetmaskLengthToNetmask(int length) {

if (length < 0 || length > 32) {
return "";
}

int netmask = 0xFFFFFFFF << (32 - length);
try {
return InetAddress.getByAddress(new byte[] { (byte) (netmask >>> 24), (byte) (netmask >>> 16 & 0xFF),
(byte) (netmask >>> 8 & 0xFF), (byte) (netmask & 0xFF) }).getHostAddress();
}
catch (UnknownHostException e) {
return "";
}
}

}

0 comments on commit 46cb891

Please sign in to comment.