sigar/bindings/dotnet/examples/Ifconfig.cs

90 lines
2.6 KiB
C#
Raw Normal View History

2004-09-18 06:52:18 +08:00
using System;
using Hyperic.Sigar;
public class Ifconfig {
private static void println(String line) {
System.Console.WriteLine(line);
}
private static void output(Sigar sigar, String name) {
NetInterfaceConfig ifconfig =
sigar.NetInterfaceConfig(name);
ulong flags = ifconfig.Flags;
//XXX cannot assume ethernet
String encap = (flags & Sigar.IFF_LOOPBACK) > 0 ?
"Local Loopback" : "Ethernet";
String hwaddr = "";
if (!Sigar.NULL_HWADDR.Equals(ifconfig.Hwaddr)) {
hwaddr = " HWaddr " + ifconfig.Hwaddr;
}
println(ifconfig.Name + "\t" +
"Link encap:" + encap +
hwaddr);
String ptp = "";
if ((flags & Sigar.IFF_POINTOPOINT) > 0) {
ptp = " P-t-P:" + ifconfig.Destination;
}
String bcast = "";
if ((flags & Sigar.IFF_BROADCAST) > 0) {
bcast = " Bcast:" + ifconfig.Broadcast;
}
println("\t" +
"inet addr:" + ifconfig.Address +
ptp + //unlikely
bcast +
" Mask:" + ifconfig.Netmask);
println("\t" +
ifconfig.FlagsString() +
" MTU:" + ifconfig.Mtu +
" Metric:" + ifconfig.Metric);
try {
NetInterfaceStat ifstat =
sigar.NetInterfaceStat(name);
println("\t" +
"RX packets:" + ifstat.RxPackets +
" errors:" + ifstat.RxErrors +
" dropped:" + ifstat.RxDropped +
" overruns:" + ifstat.RxOverruns +
" frame:" + ifstat.RxFrame);
println("\t" +
"TX packets:" + ifstat.TxPackets +
" errors:" + ifstat.TxErrors +
" dropped:" + ifstat.TxDropped +
" overruns:" + ifstat.TxOverruns +
" carrier:" + ifstat.TxCarrier);
println("\t" + "collisions:" +
ifstat.TxCollisions);
ulong rxBytes = ifstat.RxBytes;
ulong txBytes = ifstat.TxBytes;
println("\t" +
"RX bytes:" + rxBytes +
" (" + Sigar.FormatSize(rxBytes) + ")" +
" " +
"TX bytes:" + txBytes +
" (" + Sigar.FormatSize(txBytes) + ")");
2004-09-19 01:49:18 +08:00
} catch (SigarException) { }
2004-09-18 06:52:18 +08:00
println("");
}
public static void Main() {
Sigar sigar = new Sigar();
foreach (String name in sigar.NetInterfaceList()) {
output(sigar, name);
}
}
}