Chapter 4. Show General, Device, NetIf Example

Table of Contents

Imports
Setup
Get Methods
Get Local Method 1
Get Local Method 2
Get Remote Method 1
Print General
Print DevInfo
Print Method

It's time for a more complex example which we will call "Show General, Device, NetIf". In this example we will show how to specify more parameters to tell the API what specific data we want and how to use the resulting data.

This example should be called with a single command line argument of LocalMethod1, LocalMethod2, or RemoteMethod1. Each of these names triggers a different example method of retrieving the same classes of data from either the local system or a remote system.

Here is the full listing

/*
 * Example of how to call SysInfo java API to retrieve data.
 * The code in this file can be freely used.
 */
package example1;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.magnicomp.sysinfo.v2.SysInfo;
import com.magnicomp.sysinfo.v2.core.DevInfo;
import com.magnicomp.sysinfo.v2.core.NetIf;
import com.magnicomp.sysinfo.v2.core.NetIfAddr;
import com.magnicomp.sysinfo.v2.core.SysInfoCore2;
import com.magnicomp.sysinfo.v2.core.TypeAddrtype;
import com.magnicomp.sysinfo.v2.core.TypeString;
import com.magnicomp.sysinfo.common.SysInfoException;

public class ShowGeneralDeviceNetIf {
	public static void main(String[] args) {
		// Variable which will contain SysInfo data
		SysInfoCore2		sysInfo = null;
		// Top level directory where SysInfo is installed.
		String 				sysInfoDir = "/opt/sysinfo";
		// List of classes to retrieve
		String 				classes = "general,device,netif";
		// Hostname of remote system to get data from
		String 				remoteHostName = "dune";

		if (args.length <= 0) {
			System.out.printf("First argument must be one of LocalMethod1, LocalMethod2, RemoteMethod1\n");
			System.exit(1);
		}
		String method = args[0];
		if (method.equalsIgnoreCase("LocalMethod1")) {
			/*
			 * Retrieve the SysInfo data specified by classes.
			 */
			sysInfo = getLocalMethod1(sysInfoDir, classes);
		} else if (method.equalsIgnoreCase("LocalMethod2")) {
			/*
			 * Retrieve the SysInfo data specified by classes.
			 * This does the same thing as LocalMethod1.
			 */
			sysInfo = getLocalMethod2(sysInfoDir, classes);
		} else if (method.equalsIgnoreCase("RemoteMethod1")) {
			/*
			 * Retrieve the SysInfo data specified by classes
			 * from host remoteHostName.
			 */
			sysInfo = getRemoteMethod1(remoteHostName, classes);
		}

		// Print some of the General class data
		print("GENERAL", sysInfo.getGeneral().getHostName());
		print("GENERAL", sysInfo.getGeneral().getOsName());
		print("GENERAL", sysInfo.getGeneral().getOsVersion());
		print("GENERAL", sysInfo.getGeneral().getOsDistName());
		print("GENERAL", sysInfo.getGeneral().getOsDistVersion());
		print("GENERAL", sysInfo.getGeneral().getSystemModel());
		print("GENERAL", sysInfo.getGeneral().getSystemManufacturerFull());

		/*
		 * Print some of the Device data attributes
		 * for all the devices found.
		 */
		List<DevInfo> devList = sysInfo.getDevInfos();
		Iterator<DevInfo> devIter = devList.iterator();
		while (devIter.hasNext()) {
			DevInfo devInfo = devIter.next();
			System.out.printf("\n");
			print("DEVICE", devInfo.getName());
			print("DEVICE", devInfo.getVendor());
			print("DEVICE", devInfo.getModel());
			print("DEVICE", devInfo.getSerial());
			print("DEVICE", devInfo.getCapacity());
		}
		
		/*
		 * Print some of the NetIf data attributes
		 * for all the network interfaces found.
		 */
		List<NetIf> ifList = sysInfo.getNetIves();
		Iterator<NetIf> ifIter = ifList.iterator();
		while (ifIter.hasNext()) {
			NetIf nif = ifIter.next();
			System.out.printf("\n");
			print("NETIF", nif.getName());
			print("NETIF", nif.getMACaddr());
			print("NETIF", nif.getSpeed());
			List<NetIfAddr> addrList = nif.getAddresses();
			Iterator<NetIfAddr> addrIter = addrList.iterator();
			while (addrIter.hasNext()) {
				NetIfAddr addr = addrIter.next();
				printAddr("NETIF ADDR", addr.getAddrType());
				print("NETIF ADDR", addr.getHostAddr());
				print("NETIF ADDR", addr.getNetMask());
				print("NETIF ADDR", addr.getBroadAddr());
			}
		}
	}

	/**
	 * Get SysInfoCore2 data via the SysInfo API from the local system.
	 * This is a simple method which uses one API
	 * method call to specify the classes to retrieve.
	 * @param sysInfoDir
	 * @param classes Comma separated list of SysInfo classes
	 * @return
	 */
	private static SysInfoCore2 getLocalMethod1(String sysInfoDir, String classes) {
		SysInfoCore2		sysInfo = null;
		
		try {
			SysInfo si = new SysInfo(sysInfoDir);
			// Specify what SysInfo data classes we should retrieve
			si.setDataClasses(classes);
			// Get the SysInfo object with data populated
			sysInfo = si.get();
		} catch (SysInfoException e) {
			System.err.printf("SysInfo Exception: %s", e.traceBack());
			System.exit(1);
		}

		return sysInfo;
	}
	
	/**
	 * Get SysInfoCore2 data via the SysInfo API from the local system.
	 * 
	 * This method passes command line arguments to the SysInfo API
	 * which specify what data to retrieve.  It does the same thing
	 * as getMethod1 but it shows how any SysInfo CLI option can
	 * be specified via the API.
	 * 
	 * @param sysInfoDir
	 * @param classes Comma separated list of SysInfo classes
	 * @return
	 */
    private static SysInfoCore2 getLocalMethod2(String sysInfoDir, String classes) {
		SysInfoCore2		sysInfo = null;
		
		try {
			/*
			 * Create arguments to give SysInfo.  See the mcsysinfocli
			 * Reference Manual for details on all valid options.
			 */
			List<String> siArgs = new ArrayList<String>();
			siArgs.add("--class");
			// Tell SysInfo what data classes to populate
			siArgs.add(classes);
			// Create SysInfo object
			SysInfo si = new SysInfo(sysInfoDir, siArgs);
			// OPTIONAL: Enable debugging
			si.setDebug(true);
			// Get the SysInfo object with data populated
			sysInfo = si.get();
		} catch (SysInfoException e) {
			System.err.printf("SysInfo Exception: %s", e.traceBack());
			System.exit(1);
		}

		return sysInfo;
	}
	
	/**
	 * This method retrieves the SysInfo Data Classes specified in
	 * classes from a remote system with a hostname of hostName.
	 *
	 * @param hostName Name of host to retrieve SysInfo data from
	 * @param classes Comma separated list of SysInfo classes
	 * @return
	 */
    private static SysInfoCore2 getRemoteMethod1(String hostName, String classes) {
		SysInfoCore2		sysInfo = null;

		try {
			SysInfo si = new SysInfo(null, hostName);
			// Specify what SysInfo data classes we should retrieve
			si.setDataClasses(classes);
			// Get the SysInfo object with data populated
			sysInfo = si.get();
		} catch (SysInfoException e) {
			System.err.printf("SysInfo Exception: %s", e.traceBack());
			System.exit(1);
		}

		return sysInfo;		
	}
	
	private static void print(String prefix, TypeString info) {
		if (info == null)
			return;
		System.out.printf("%s %s=\"%s\"\n", prefix, 
				info.getLabel(), info.getValue());
	}
	
	private static void printAddr(String prefix, TypeAddrtype info) {
		if (info == null)
			return;
		System.out.printf("%s %s=\"%s\"\n", prefix, 
				info.getLabel(), info.getValue());
	}
}

Imports

The imports section of this example contains:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.magnicomp.sysinfo.v2.SysInfo;
import com.magnicomp.sysinfo.v2.core.DevInfo;
import com.magnicomp.sysinfo.v2.core.SysInfoCore2;
import com.magnicomp.sysinfo.v2.core.TypeString;
import com.magnicomp.sysinfo.common.SysInfoException;

The import com.magnicomp.sysinfo.v2.core.DevInfo class is used to later iterate through a list of DevInfo objects. The import com.magnicomp.sysinfo.v2.core.TypeString class is used to allow for some generic handling of the returned data later discussed in the print method.