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.Arrays;
19 import java.util.Collection;
20
21 import org.hyperic.sigar.Sigar;
22 import org.hyperic.sigar.SigarException;
23 import org.hyperic.sigar.NetInterfaceConfig;
24 import org.hyperic.sigar.NetInterfaceStat;
25 import org.hyperic.sigar.NetFlags;
26 import org.hyperic.sigar.cmd.Shell;
27
28
29
30
31 public class Ifconfig extends SigarCommandBase implements Closable {
32
33 public Ifconfig(Shell shell) {
34 super(shell);
35 }
36
37 public Ifconfig() {
38 super();
39 }
40
41 protected boolean validateArgs(String[] args) {
42 return args.length <= 1;
43 }
44
45 public String getSyntaxArgs() {
46 return "[interface]";
47 }
48
49 public String getUsageShort() {
50 return "Network interface information";
51 }
52
53 public Collection getCompletions() {
54 String[] ifNames;
55
56 try {
57 ifNames = this.proxy.getNetInterfaceList();
58 } catch (SigarException e) {
59 return super.getCompletions();
60 }
61
62 return Arrays.asList(ifNames);
63 }
64
65 public class NetworkRate {
66
67
68
69
70 long rx;
71
72
73
74 long tx;
75 }
76
77 public NetworkRate NetworkSendRate(String interfacename) throws SigarException, InterruptedException {
78 NetInterfaceStat ifstat =
79 this.sigar.getNetInterfaceStat(interfacename);
80 long rx = ifstat.getRxBytes();
81 long tx = ifstat.getTxBytes();
82 Thread.sleep(1000);
83 ifstat =
84 this.sigar.getNetInterfaceStat(interfacename);
85 rx = ifstat.getRxBytes() - rx;
86 tx = ifstat.getTxBytes() - tx;
87 NetworkRate r = new NetworkRate();
88 r.rx = rx;
89 r.tx = tx;
90 return r;
91
92 }
93
94 public void output(String name) throws SigarException {
95 NetInterfaceConfig ifconfig =
96 this.sigar.getNetInterfaceConfig(name);
97 long flags = ifconfig.getFlags();
98
99 String hwaddr = "";
100 if (!NetFlags.NULL_HWADDR.equals(ifconfig.getHwaddr())) {
101 hwaddr = " HWaddr " + ifconfig.getHwaddr();
102 }
103
104 if (!ifconfig.getName().equals(ifconfig.getDescription())) {
105 println(ifconfig.getDescription());
106 }
107
108 println(ifconfig.getName() + "\t"
109 + "Link encap:" + ifconfig.getType()
110 + hwaddr);
111
112 String ptp = "";
113 if ((flags & NetFlags.IFF_POINTOPOINT) > 0) {
114 ptp = " P-t-P:" + ifconfig.getDestination();
115 }
116
117 String bcast = "";
118 if ((flags & NetFlags.IFF_BROADCAST) > 0) {
119 bcast = " Bcast:" + ifconfig.getBroadcast();
120 }
121
122 println("\t"
123 + "inet addr:" + ifconfig.getAddress()
124 + ptp +
125 bcast
126 + " Mask:" + ifconfig.getNetmask());
127
128 println("\t"
129 + NetFlags.getIfFlagsString(flags)
130 + " MTU:" + ifconfig.getMtu()
131 + " Metric:" + ifconfig.getMetric());
132 try {
133 NetInterfaceStat ifstat =
134 this.sigar.getNetInterfaceStat(name);
135 println("\t"
136 + "RX packets:" + ifstat.getRxPackets()
137 + " errors:" + ifstat.getRxErrors()
138 + " dropped:" + ifstat.getRxDropped()
139 + " overruns:" + ifstat.getRxOverruns()
140 + " frame:" + ifstat.getRxFrame());
141
142 println("\t"
143 + "TX packets:" + ifstat.getTxPackets()
144 + " errors:" + ifstat.getTxErrors()
145 + " dropped:" + ifstat.getTxDropped()
146 + " overruns:" + ifstat.getTxOverruns()
147 + " carrier:" + ifstat.getTxCarrier());
148 println("\t" + "collisions:"
149 + ifstat.getTxCollisions());
150
151 long rxBytes = ifstat.getRxBytes();
152 long txBytes = ifstat.getTxBytes();
153
154 println("\t"
155 + "RX bytes:" + rxBytes
156 + " (" + Sigar.formatSize(rxBytes) + ")"
157 + " "
158 + "TX bytes:" + txBytes
159 + " (" + Sigar.formatSize(txBytes) + ")");
160 } catch (SigarException e) {
161 }
162
163 println("");
164 }
165
166
167
168
169
170
171 @Override
172 public void close() throws Exception {
173 if (sigar != null) {
174 sigar.close();
175 sigar = null;
176 }
177 }
178
179 @Override
180 protected void finalize() throws Throwable {
181
182 if (sigar != null) {
183 System.out.println("WARN,. finalize called without closing sigar first"+ this.getClass().getCanonicalName());
184 sigar.close();
185
186 }
187 super.finalize();
188 }
189 }