sigar/bindings/dotnet/examples/Ifconfig.cs

102 lines
3.1 KiB
C#
Raw Normal View History

/*
* Copyright (c) 2004-2005 Hyperic, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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);
2005-12-07 01:08:14 +08:00
long flags = ifconfig.Flags;
2004-09-18 06:52:18 +08:00
String hwaddr = "";
if (!Sigar.NULL_HWADDR.Equals(ifconfig.Hwaddr)) {
hwaddr = " HWaddr " + ifconfig.Hwaddr;
}
println(ifconfig.Name + "\t" +
2005-12-07 01:01:54 +08:00
"Link encap:" + ifconfig.Type +
2004-09-18 06:52:18 +08:00
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);
2005-12-07 01:08:14 +08:00
long rxBytes = ifstat.RxBytes;
long txBytes = ifstat.TxBytes;
2004-09-18 06:52:18 +08:00
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);
}
}
}