1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.miloss.fgsms.osagent;
17
18 import java.util.ArrayList;
19 import java.util.List;
20 import org.miloss.fgsms.services.interfaces.common.NetworkAdapterInfo;
21 import org.hyperic.sigar.NetInterfaceConfig;
22 import org.hyperic.sigar.SigarException;
23 import org.hyperic.sigar.cmd.Shell;
24
25
26
27
28 public class NetInfo extends SigarCommandBase implements Closable{
29
30 public NetInfo(Shell shell) {
31 super(shell);
32 }
33
34 public NetInfo() {
35 super();
36 }
37
38
39 public List<NetworkAdapterInfo> GetNetworkInfo() throws SigarException {
40 List<NetworkAdapterInfo> info = new ArrayList<NetworkAdapterInfo>();
41 String[] names = this.sigar.getNetInterfaceList();
42 NetworkAdapterInfo d = null;
43 for (int i = 0; i < names.length; i++) {
44 d = new NetworkAdapterInfo();
45 NetInterfaceConfig config = this.sigar.getNetInterfaceConfig(names[i]);
46
47 try {
48 org.hyperic.sigar.NetInfo netinfo = this.sigar.getNetInfo();
49 d.setDefaultGateway(netinfo.getDefaultGateway());
50 d.getDns().add(netinfo.getPrimaryDns());
51 d.getDns().add(netinfo.getSecondaryDns());
52 } catch (Exception ex) {
53 ex.printStackTrace();
54 d.setDefaultGateway("unavailable");
55 }
56 d.setSubnetMask(config.getNetmask());
57 d.setAdapterName(config.getName());
58 d.setAdapterDescription(config.getDescription());
59 d.setMtu(config.getMtu());
60 d.setMac(config.getHwaddr());
61 d.getIp().add(config.getAddress());
62
63 info.add(d);
64 }
65
66
67
68 return info;
69 }
70
71
72
73 private boolean ContainsMax(List<NetworkAdapterInfo> info, String mac) {
74 for (int i = 0; i < info.size(); i++) {
75 if (info.get(i).getMac().equalsIgnoreCase(mac)) {
76 return true;
77 }
78 }
79 return false;
80 }
81 @Override
82 public void close() throws Exception{
83 if (sigar != null) {
84 sigar.close();
85 sigar = null;
86 }
87 }
88 @Override
89 protected void finalize() throws Throwable
90 {
91
92 if (sigar != null) {
93 System.out.println("WARN,. finalize called without closing sigar first"+ this.getClass().getCanonicalName());
94 sigar.close();
95 }
96 super.finalize();
97 }
98
99
100 }