Java Code Examples for oshi.hardware.NetworkIF

The following examples show how to use oshi.hardware.NetworkIF. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Flink-CEPplus   Source File: SystemResourcesCounter.java    License: Apache License 2.0 6 votes vote down vote up
public SystemResourcesCounter(Time probeInterval) {
	probeIntervalMs = probeInterval.toMilliseconds();
	checkState(this.probeIntervalMs > 0);

	setName(SystemResourcesCounter.class.getSimpleName() + " probing thread");

	cpuUsagePerProcessor = new AtomicReferenceArray<>(hardwareAbstractionLayer.getProcessor().getLogicalProcessorCount());

	NetworkIF[] networkIFs = hardwareAbstractionLayer.getNetworkIFs();
	bytesReceivedPerInterface = new long[networkIFs.length];
	bytesSentPerInterface = new long[networkIFs.length];
	receiveRatePerInterface = new AtomicLongArray(networkIFs.length);
	sendRatePerInterface = new AtomicLongArray(networkIFs.length);
	networkInterfaceNames = new String[networkIFs.length];

	for (int i = 0; i < networkInterfaceNames.length; i++) {
		networkInterfaceNames[i] = networkIFs[i].getName();
	}
}
 
Example 2
Source Project: flink   Source File: SystemResourcesCounter.java    License: Apache License 2.0 6 votes vote down vote up
public SystemResourcesCounter(Time probeInterval) {
	probeIntervalMs = probeInterval.toMilliseconds();
	checkState(this.probeIntervalMs > 0);

	setName(SystemResourcesCounter.class.getSimpleName() + " probing thread");

	cpuUsagePerProcessor = new AtomicReferenceArray<>(hardwareAbstractionLayer.getProcessor().getLogicalProcessorCount());

	NetworkIF[] networkIFs = hardwareAbstractionLayer.getNetworkIFs();
	bytesReceivedPerInterface = new long[networkIFs.length];
	bytesSentPerInterface = new long[networkIFs.length];
	receiveRatePerInterface = new AtomicLongArray(networkIFs.length);
	sendRatePerInterface = new AtomicLongArray(networkIFs.length);
	networkInterfaceNames = new String[networkIFs.length];

	for (int i = 0; i < networkInterfaceNames.length; i++) {
		networkInterfaceNames[i] = networkIFs[i].getName();
	}
}
 
Example 3
Source Project: flink   Source File: SystemResourcesCounter.java    License: Apache License 2.0 6 votes vote down vote up
public SystemResourcesCounter(Time probeInterval) {
	probeIntervalMs = probeInterval.toMilliseconds();
	checkState(this.probeIntervalMs > 0);

	setName(SystemResourcesCounter.class.getSimpleName() + " probing thread");

	cpuUsagePerProcessor = new AtomicReferenceArray<>(hardwareAbstractionLayer.getProcessor().getLogicalProcessorCount());

	NetworkIF[] networkIFs = hardwareAbstractionLayer.getNetworkIFs();
	bytesReceivedPerInterface = new long[networkIFs.length];
	bytesSentPerInterface = new long[networkIFs.length];
	receiveRatePerInterface = new AtomicLongArray(networkIFs.length);
	sendRatePerInterface = new AtomicLongArray(networkIFs.length);
	networkInterfaceNames = new String[networkIFs.length];

	for (int i = 0; i < networkInterfaceNames.length; i++) {
		networkInterfaceNames[i] = networkIFs[i].getName();
	}
}
 
Example 4
Source Project: Flink-CEPplus   Source File: SystemResourcesCounter.java    License: Apache License 2.0 5 votes vote down vote up
private void calculateNetworkUsage(NetworkIF[] networkIFs) {
	checkState(networkIFs.length == receiveRatePerInterface.length());

	for (int i = 0; i < networkIFs.length; i++) {
		NetworkIF networkIF = networkIFs[i];
		networkIF.updateNetworkStats();

		receiveRatePerInterface.set(i, (networkIF.getBytesRecv() - bytesReceivedPerInterface[i]) * 1000 / probeIntervalMs);
		sendRatePerInterface.set(i, (networkIF.getBytesSent() - bytesSentPerInterface[i]) * 1000 / probeIntervalMs);

		bytesReceivedPerInterface[i] = networkIF.getBytesRecv();
		bytesSentPerInterface[i] = networkIF.getBytesSent();
	}
}
 
Example 5
Source Project: flink   Source File: SystemResourcesCounter.java    License: Apache License 2.0 5 votes vote down vote up
private void calculateNetworkUsage(NetworkIF[] networkIFs) {
	checkState(networkIFs.length == receiveRatePerInterface.length());

	for (int i = 0; i < networkIFs.length; i++) {
		NetworkIF networkIF = networkIFs[i];
		networkIF.updateNetworkStats();

		receiveRatePerInterface.set(i, (networkIF.getBytesRecv() - bytesReceivedPerInterface[i]) * 1000 / probeIntervalMs);
		sendRatePerInterface.set(i, (networkIF.getBytesSent() - bytesSentPerInterface[i]) * 1000 / probeIntervalMs);

		bytesReceivedPerInterface[i] = networkIF.getBytesRecv();
		bytesSentPerInterface[i] = networkIF.getBytesSent();
	}
}
 
Example 6
Source Project: garmadon   Source File: NetworkStatistics.java    License: Apache License 2.0 5 votes vote down vote up
NetworkStatistics() {
    super(NETWORK_HEADER);
    for (NetworkIF nic : new SystemInfo().getHardware().getNetworkIFs()) {
        if (nic.getBytesSent() == 0 && nic.getBytesRecv() == 0) {
            // nothing happens on this nic since boot
            continue;
        }
        nics.add(new NicInfo(nic));
    }
}
 
Example 7
Source Project: garmadon   Source File: NetworkStatistics.java    License: Apache License 2.0 5 votes vote down vote up
NicInfo(NetworkIF nic) {
    this.nic = nic;
    this.recvProp = nic.getName() + NETWORK_RECV_SUFFIX;
    this.sentProp = nic.getName() + NETWORK_SENT_SUFFIX;
    this.pktRecvProp = nic.getName() + NETWORK_PKT_RECV_SUFFIX;
    this.pktSentProp = nic.getName() + NETWORK_PKT_SENT_SUFFIX;
    this.errInProp = nic.getName() + NETWORK_ERRORS_IN_SUFFIX;
    this.errOutProp = nic.getName() + NETWORK_ERRORS_OUT_SUFFIX;
    this.previousRx = nic.getBytesRecv();
    this.previousTx = nic.getBytesSent();
    this.previousPktRx = nic.getPacketsRecv();
    this.previousPktTx = nic.getPacketsSent();
    this.previousErrIn = nic.getInErrors();
    this.previousErrOut = nic.getOutErrors();
}
 
Example 8
Source Project: LagMonitor   Source File: NetworkCommand.java    License: MIT License 5 votes vote down vote up
private void displayInterfaceInfo(CommandSender sender, NetworkIF networkInterface) {
    sendMessage(sender, "Name", networkInterface.getName());
    sendMessage(sender, "    Display", networkInterface.getDisplayName());
    sendMessage(sender, "    MAC", networkInterface.getMacaddr());
    sendMessage(sender, "    MTU", String.valueOf(networkInterface.getMTU()));
    sendMessage(sender, "    IPv4", Arrays.toString(networkInterface.getIPv4addr()));
    sendMessage(sender, "    IPv6", Arrays.toString(networkInterface.getIPv6addr()));
    sendMessage(sender, "    Speed", String.valueOf(networkInterface.getSpeed()));

    String receivedBytes = LagUtils.readableBytes(networkInterface.getBytesRecv());
    String sentBytes = LagUtils.readableBytes(networkInterface.getBytesSent());
    sendMessage(sender, "    Received", receivedBytes);
    sendMessage(sender, "    Sent", sentBytes);
}
 
Example 9
Source Project: kieker   Source File: NetworkUtilizationSampler.java    License: Apache License 2.0 5 votes vote down vote up
private NetworkStatistic getCurrentNetworkStatistic(final ITimeSource timesource, final NetworkIF networkIF)
		throws SigarException {
	final long currentTimestamp = timesource.getTime();
	// final NetInterfaceStat interfaceStat =
	// this.sigar.getNetInterfaceStat(interfaceName);
	final long speed = networkIF.getSpeed();
	final long txBytes = networkIF.getBytesSent();
	// final long txCarrier = interfaceStat.getTxCarrier();
	final long txCarrier = 0;
	// final long txCollisions = interfaceStat.getTxCollisions();
	final long txCollisions = 0;
	// final long txDropped = interfaceStat.getTxDropped();
	final long txDropped = 0;
	final long txErrors = networkIF.getOutErrors();
	// final long txOverruns = interfaceStat.getTxOverruns();
	final long txOverruns = 0;
	final long txPackets = networkIF.getPacketsSent();
	final long rxBytes = networkIF.getBytesRecv();
	// final long rxDropped = interfaceStat.getRxDropped();
	final long rxDropped = 0;
	final long rxErrors = networkIF.getInErrors();
	// final long rxFrame = interfaceStat.getRxFrame();
	final long rxFrame = 0;
	// final long rxOverruns = interfaceStat.getRxOverruns();
	final long rxOverruns = 0;
	final long rxPackets = networkIF.getPacketsRecv();

	return new NetworkStatistic(currentTimestamp, speed, txBytes, txCarrier, txCollisions, txDropped, txErrors,
			txOverruns, txPackets, rxBytes, rxDropped, rxErrors, rxFrame, rxOverruns, rxPackets);
}
 
Example 10
Source Project: LagMonitor   Source File: NetworkCommand.java    License: MIT License 5 votes vote down vote up
private void displayInterfaceInfo(CommandSender sender, NetworkIF networkInterface) {
    sendMessage(sender, "Name", networkInterface.getName());
    sendMessage(sender, "    Display", networkInterface.getDisplayName());
    sendMessage(sender, "    MAC", networkInterface.getMacaddr());
    sendMessage(sender, "    MTU", String.valueOf(networkInterface.getMTU()));
    sendMessage(sender, "    IPv4", Arrays.toString(networkInterface.getIPv4addr()));
    sendMessage(sender, "    IPv6", Arrays.toString(networkInterface.getIPv6addr()));
    sendMessage(sender, "    Speed", String.valueOf(networkInterface.getSpeed()));

    String receivedBytes = LagUtils.readableBytes(networkInterface.getBytesRecv());
    String sentBytes = LagUtils.readableBytes(networkInterface.getBytesSent());
    sendMessage(sender, "    Received", receivedBytes);
    sendMessage(sender, "    Sent", sentBytes);
}
 
Example 11
Source Project: flink   Source File: SystemResourcesCounter.java    License: Apache License 2.0 5 votes vote down vote up
private void calculateNetworkUsage(NetworkIF[] networkIFs) {
	checkState(networkIFs.length == receiveRatePerInterface.length());

	for (int i = 0; i < networkIFs.length; i++) {
		NetworkIF networkIF = networkIFs[i];
		networkIF.updateNetworkStats();

		receiveRatePerInterface.set(i, (networkIF.getBytesRecv() - bytesReceivedPerInterface[i]) * 1000 / probeIntervalMs);
		sendRatePerInterface.set(i, (networkIF.getBytesSent() - bytesSentPerInterface[i]) * 1000 / probeIntervalMs);

		bytesReceivedPerInterface[i] = networkIF.getBytesRecv();
		bytesSentPerInterface[i] = networkIF.getBytesSent();
	}
}
 
Example 12
Source Project: LagMonitor   Source File: NativeSaveTask.java    License: MIT License 4 votes vote down vote up
@Override
public void run() {
    Instant currentTime = Instant.now();
    int timeDiff = (int) Duration.between(lastCheck, currentTime).getSeconds();

    int mcReadDiff = 0;
    int mcWriteDiff = 0;

    TrafficReader trafficReader = plugin.getTrafficReader();
    if (trafficReader != null) {
        int mcRead = LagUtils.byteToMega(trafficReader.getIncomingBytes().longValue());
        mcReadDiff = getDifference(mcRead, lastMcRead, timeDiff);
        lastMcRead = mcRead;

        int mcWrite = LagUtils.byteToMega(trafficReader.getOutgoingBytes().longValue());
        mcWriteDiff = getDifference(mcWrite, lastMcWrite, timeDiff);
        lastMcWrite = mcWrite;
    }

    int totalSpace = LagUtils.byteToMega(plugin.getNativeData().getTotalSpace());
    int freeSpace = LagUtils.byteToMega(plugin.getNativeData().getFreeSpace());

    //4 decimal places -> Example: 0.2456
    float freeSpacePct = round((freeSpace * 100 / (float) totalSpace), 4);

    int diskReadDiff = 0;
    int diskWriteDiff = 0;
    int netReadDiff = 0;
    int netWriteDiff = 0;

    Optional<SystemInfo> systemInfo = plugin.getNativeData().getSystemInfo();
    if (systemInfo.isPresent()) {
        NetworkIF[] networkIfs = systemInfo.get().getHardware().getNetworkIFs();
        if (networkIfs.length > 0) {
            NetworkIF networkInterface = networkIfs[0];

            int netRead = LagUtils.byteToMega(networkInterface.getBytesRecv());
            netReadDiff = getDifference(netRead, lastNetRead, timeDiff);
            lastNetRead = netRead;

            int netWrite = LagUtils.byteToMega(networkInterface.getBytesSent());
            netWriteDiff = getDifference(netWrite, lastNetWrite, timeDiff);
            lastNetWrite = netWrite;
        }

        Path root = Paths.get(".").getRoot();
        Optional<OSProcess> optProcess = plugin.getNativeData().getProcess();
        if (root != null && optProcess.isPresent()) {
            OSProcess process = optProcess.get();
            String rootFileSystem = root.toAbsolutePath().toString();

            int diskRead = LagUtils.byteToMega(process.getBytesRead());
            diskReadDiff = getDifference(diskRead, lastDiskRead, timeDiff);
            lastDiskRead = diskRead;

            int diskWrite = LagUtils.byteToMega(process.getBytesWritten());
            diskWriteDiff = getDifference(diskWrite, lastDiskWrite, timeDiff);
            lastDiskWrite = diskWrite;
        }
    }

    lastCheck = currentTime;
    storage.saveNative(mcReadDiff, mcWriteDiff, freeSpace, freeSpacePct, diskReadDiff, diskWriteDiff
            , netReadDiff, netWriteDiff);
}
 
Example 13
Source Project: LagMonitor   Source File: NetworkCommand.java    License: MIT License 4 votes vote down vote up
private void displayNetworkInfo(CommandSender sender, SystemInfo systemInfo) {
    displayGlobalNetworkInfo(sender, systemInfo.getOperatingSystem().getNetworkParams());
    for (NetworkIF networkInterface : systemInfo.getHardware().getNetworkIFs()) {
        displayInterfaceInfo(sender, networkInterface);
    }
}
 
Example 14
Source Project: kieker   Source File: NetworkUtilizationSampler.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void sample(final IMonitoringController monitoringController) throws SigarException {
	if (!monitoringController.isMonitoringEnabled() || !monitoringController.isProbeActivated(SignatureFactory.createNetworkUtilizationSignature())) {
		return;
	}
	final NetworkIF[] networkInterfaces = this.hardwareAbstractionLayer.getNetworkIFs();
	for (final NetworkIF networkIF : networkInterfaces) {

		final String interfaceName = networkIF.getName();
		final ITimeSource timesource = monitoringController.getTimeSource();
		final TimeUnit timeUnit = timesource.getTimeUnit();
		final NetworkStatistic currentNetworkStatistic = this.getCurrentNetworkStatistic(timesource, networkIF);

		if (!this.networkStatisticMap.containsKey(interfaceName)) {
			this.networkStatisticMap.putIfAbsent(interfaceName, currentNetworkStatistic);
		} else {
			final NetworkStatistic lastObservedNetworkStatistic = this.networkStatisticMap.get(interfaceName);
			final long timeDifference = currentNetworkStatistic.getTimestamp()
					- lastObservedNetworkStatistic.getTimestamp();
			if (timeDifference <= 0) {
				throw new IllegalStateException(
						"Timestamp of new observation should be strictly larger than the previous one.");
			}
			final long txBytesDifference = currentNetworkStatistic.getTxBytes()
					- lastObservedNetworkStatistic.getTxBytes();
			final long txCarrierDifference = currentNetworkStatistic.getTxCarrier()
					- lastObservedNetworkStatistic.getTxCarrier();
			final long txCollisionsDifference = currentNetworkStatistic.getTxCollisions()
					- lastObservedNetworkStatistic.getTxCollisions();
			final long txDroppedDifference = currentNetworkStatistic.getTxDropped()
					- lastObservedNetworkStatistic.getTxDropped();
			final long txErrorsDifference = currentNetworkStatistic.getTxErrors()
					- lastObservedNetworkStatistic.getTxErrors();
			final long txOverrunsDifference = currentNetworkStatistic.getTxOverruns()
					- lastObservedNetworkStatistic.getTxOverruns();
			final long txPacketsDifference = currentNetworkStatistic.getTxPackets()
					- lastObservedNetworkStatistic.getTxPackets();

			final long rxBytesDifference = currentNetworkStatistic.getRxBytes()
					- lastObservedNetworkStatistic.getRxBytes();
			final long rxDroppedDifference = currentNetworkStatistic.getRxDropped()
					- lastObservedNetworkStatistic.getRxDropped();
			final long rxErrorsDifference = currentNetworkStatistic.getRxErrors()
					- lastObservedNetworkStatistic.getRxErrors();
			final long rxFrameDifference = currentNetworkStatistic.getRxFrame()
					- lastObservedNetworkStatistic.getRxFrame();
			final long rxOverrunsDifference = currentNetworkStatistic.getRxOverruns()
					- lastObservedNetworkStatistic.getRxOverruns();
			final long rxPacketsDifference = currentNetworkStatistic.getRxPackets()
					- lastObservedNetworkStatistic.getRxPackets();

			final double txBytesPerSecond = txBytesDifference
					/ (double) TimeUnit.SECONDS.convert(timeDifference, timeUnit);
			final double txCarrierPerSecond = txCarrierDifference
					/ (double) TimeUnit.SECONDS.convert(timeDifference, timeUnit);
			final double txCollisionsPerSecond = txCollisionsDifference
					/ (double) TimeUnit.SECONDS.convert(timeDifference, timeUnit);
			final double txDroppedPerSecond = txDroppedDifference
					/ (double) TimeUnit.SECONDS.convert(timeDifference, timeUnit);
			final double txErrorsPerSecond = txErrorsDifference
					/ (double) TimeUnit.SECONDS.convert(timeDifference, timeUnit);
			final double txOverrunsPerSecond = txOverrunsDifference
					/ (double) TimeUnit.SECONDS.convert(timeDifference, timeUnit);
			final double txPacketsPerSecond = txPacketsDifference
					/ (double) TimeUnit.SECONDS.convert(timeDifference, timeUnit);

			final double rxBytesPerSecond = rxBytesDifference
					/ (double) TimeUnit.SECONDS.convert(timeDifference, timeUnit);
			final double rxDroppedPerSecond = rxDroppedDifference
					/ (double) TimeUnit.SECONDS.convert(timeDifference, timeUnit);
			final double rxErrorsPerSecond = rxErrorsDifference
					/ (double) TimeUnit.SECONDS.convert(timeDifference, timeUnit);
			final double rxFramePerSecond = rxFrameDifference
					/ (double) TimeUnit.SECONDS.convert(timeDifference, timeUnit);
			final double rxOverrunsPerSecond = rxOverrunsDifference
					/ (double) TimeUnit.SECONDS.convert(timeDifference, timeUnit);
			final double rxPacketsPerSecond = rxPacketsDifference
					/ (double) TimeUnit.SECONDS.convert(timeDifference, timeUnit);

			final NetworkUtilizationRecord r = new NetworkUtilizationRecord(currentNetworkStatistic.getTimestamp(),
					monitoringController.getHostname(), interfaceName, currentNetworkStatistic.getSpeed(),
					txBytesPerSecond, txCarrierPerSecond, txCollisionsPerSecond, txDroppedPerSecond,
					txErrorsPerSecond, txOverrunsPerSecond, txPacketsPerSecond, rxBytesPerSecond,
					rxDroppedPerSecond, rxErrorsPerSecond, rxFramePerSecond, rxOverrunsPerSecond,
					rxPacketsPerSecond);
			monitoringController.newMonitoringRecord(r);

			this.networkStatisticMap.put(interfaceName, currentNetworkStatistic);
		}
	}
}
 
Example 15
Source Project: LagMonitor   Source File: NativeSaveTask.java    License: MIT License 4 votes vote down vote up
@Override
public void run() {
    Instant currentTime = Instant.now();
    int timeDiff = (int) Duration.between(lastCheck, currentTime).getSeconds();

    int mcReadDiff = 0;
    int mcWriteDiff = 0;

    TrafficReader trafficReader = plugin.getTrafficReader();
    if (trafficReader != null) {
        int mcRead = LagUtils.byteToMega(trafficReader.getIncomingBytes().longValue());
        mcReadDiff = getDifference(mcRead, lastMcRead, timeDiff);
        lastMcRead = mcRead;

        int mcWrite = LagUtils.byteToMega(trafficReader.getOutgoingBytes().longValue());
        mcWriteDiff = getDifference(mcWrite, lastMcWrite, timeDiff);
        lastMcWrite = mcWrite;
    }

    int totalSpace = LagUtils.byteToMega(plugin.getNativeData().getTotalSpace());
    int freeSpace = LagUtils.byteToMega(plugin.getNativeData().getFreeSpace());

    //4 decimal places -> Example: 0.2456
    float freeSpacePct = round((freeSpace * 100 / (float) totalSpace), 4);

    int diskReadDiff = 0;
    int diskWriteDiff = 0;
    int netReadDiff = 0;
    int netWriteDiff = 0;

    Optional<SystemInfo> systemInfo = plugin.getNativeData().getSystemInfo();
    if (systemInfo.isPresent()) {
        NetworkIF[] networkIfs = systemInfo.get().getHardware().getNetworkIFs();
        if (networkIfs.length > 0) {
            NetworkIF networkInterface = networkIfs[0];

            int netRead = LagUtils.byteToMega(networkInterface.getBytesRecv());
            netReadDiff = getDifference(netRead, lastNetRead, timeDiff);
            lastNetRead = netRead;

            int netWrite = LagUtils.byteToMega(networkInterface.getBytesSent());
            netWriteDiff = getDifference(netWrite, lastNetWrite, timeDiff);
            lastNetWrite = netWrite;
        }

        Path root = Paths.get(".").getRoot();
        Optional<OSProcess> optProcess = plugin.getNativeData().getProcess();
        if (root != null && optProcess.isPresent()) {
            OSProcess process = optProcess.get();
            String rootFileSystem = root.toAbsolutePath().toString();

            int diskRead = LagUtils.byteToMega(process.getBytesRead());
            diskReadDiff = getDifference(diskRead, lastDiskRead, timeDiff);
            lastDiskRead = diskRead;

            int diskWrite = LagUtils.byteToMega(process.getBytesWritten());
            diskWriteDiff = getDifference(diskWrite, lastDiskWrite, timeDiff);
            lastDiskWrite = diskWrite;
        }
    }

    lastCheck = currentTime;
    storage.saveNative(mcReadDiff, mcWriteDiff, freeSpace, freeSpacePct, diskReadDiff, diskWriteDiff
            , netReadDiff, netWriteDiff);
}
 
Example 16
Source Project: LagMonitor   Source File: NetworkCommand.java    License: MIT License 4 votes vote down vote up
private void displayNetworkInfo(CommandSender sender, SystemInfo systemInfo) {
    displayGlobalNetworkInfo(sender, systemInfo.getOperatingSystem().getNetworkParams());
    for (NetworkIF networkInterface : systemInfo.getHardware().getNetworkIFs()) {
        displayInterfaceInfo(sender, networkInterface);
    }
}