Unit 3: Monitoring CPU Load


By extracting PC system data at specified time intervals, you can monitor the CPU load and log CPU load information if the CPU load exceeds the set threshold (for example, 50%).

In this unit, you will add the function of monitoring the CPU load to the program that is used in Unit 2.


  1. Declare the function monitor() for monitoring the CPU load.

    public static void main(String[] args) throws Exception {
        connect();
        updateAttribute();
        monitor();
    }
    
  2. Program the monitor() function to upload the PC system data to EnOS Cloud according to the specified interval (for example, every 10 seconds) and monitor the CPU load (for example, cpu_load > 0.2).

    public static void monitor() throws Exception {
        while (true) {
            Map<String, Object> systemInfo = collectDeviceInfo();
            postMeasurepoint(systemInfo);
    
            double cpu_load = (double) systemInfo.get("cpu_used");
            if (cpu_load > 0.2) {
                System.out.println("[Warning] CPU load: " + cpu_load);
            }
    
            Thread.sleep(interval * 1000);
        }
    }
    
  3. Compile and run the program for device connection, data ingestion, and CPU load monitoring.

    import java.util.HashMap;
    import java.util.Map;
    
    import com.envisioniot.enos.iot_mqtt_sdk.core.ConnCallback;
    import com.envisioniot.enos.iot_mqtt_sdk.core.MqttClient;
    import com.envisioniot.enos.iot_mqtt_sdk.message.upstream.tsl.*;
    import oshi.hardware.HardwareAbstractionLayer;
    import oshi.software.os.OperatingSystem;
    
    public class Sample {
    
        private static final String uri = "tcp://{host}:{port}";
        private static final String productKey = "product_key";
        private static final String deviceKey = "device_key";
        private static final String deviceSecret = "device_secret";
    
        private static MqttClient client;
        private static int interval = 5; // 10s
    
        public static void main(String[] args) throws Exception {
            connect();
            updateAttribute();
            monitor();
        }
    
        // Device connection initialization
        public static void connect() {
            System.out.println("start connect with callback ... ");
            try {
                client = new MqttClient(uri, productKey, deviceKey, deviceSecret);
                client.getProfile().setConnectionTimeout(60).setAutoReconnect(true);
                client.connect(new ConnCallback() {
                    @Override
                    public void connectComplete(boolean reconnect) {
                        System.out.println("connect success");
                    }
    
                    @Override
                    public void connectLost(Throwable cause) {
                        System.out.println("onConnectLost");
                    }
    
                    @Override
                    public void connectFailed(Throwable cause) {
                        System.out.println("onConnectFailed : " + cause);
                    }
                });
            } catch (Throwable var1) {
            }
            System.out.println("connect result :" + client.isConnected());
        }
    
        // Ingesting PC system and hardware data
        public static Map<String, Object> collectDeviceInfo() {
            oshi.SystemInfo si = new oshi.SystemInfo();
            HardwareAbstractionLayer hal = si.getHardware();
            OperatingSystem os = si.getOperatingSystem();
    
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("system", os.toString());
            data.put("model", hal.getComputerSystem().getManufacturer() + " " + hal.getComputerSystem().getModel());
            data.put("cpu_core", hal.getProcessor().getLogicalProcessorCount());
            data.put("cpu_used", hal.getProcessor().getSystemCpuLoad());
            data.put("mem_total", hal.getMemory().getTotal());
            data.put("mem_used", hal.getMemory().getAvailable());
            data.put("cpu_used_average", hal.getProcessor().getSystemLoadAverage());
            data.put("cpu_temperature", hal.getSensors().getCpuTemperature());
    
            return data;
        }
    
        // Updating PC attributes with the ingested system and hardware data
        public static void updateAttribute() {
            Map<String, Object> deviceInfo = collectDeviceInfo();
            System.out.println("Computer info: " + deviceInfo);
            AttributeUpdateRequest request = AttributeUpdateRequest.builder()
                    .setQos(1)
                    .addAttribute("system", deviceInfo.get("system"))
                    .addAttribute("model", deviceInfo.get("model"))
                    .addAttribute("cpu_core", deviceInfo.get("cpu_core"))
                    .addAttribute("mem_total", deviceInfo.get("mem_total"))
                    .build();
            System.out.println(">>> Update Attribute: " + request);
    
            try {
                AttributeUpdateResponse resp = client.publish(request);
                System.out.println("<-- " + resp);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        // Uploading PC system data to EnOS Cloud
        public static void postMeasurepoint(Map<String, Object> systemInfo) {
            MeasurepointPostRequest request = MeasurepointPostRequest.builder()
                    .setQos(0)
                    .addMeasurePoint("cpu_used", Double.parseDouble(systemInfo.get("cpu_used").toString()) + 0.0)
                    .addMeasurePoint("mem_used", systemInfo.get("mem_used"))
                    .build();
            System.out.println(">>> Post Measurepoint: " + request);
    
            try {
                MeasurepointPostResponse resp = client.publish(request);
                System.out.println("<-- " + resp);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        // Monitoring the CPU load
        public static void monitor() throws Exception {
            while (true) {
                Map<String, Object> systemInfo = collectDeviceInfo();
                postMeasurepoint(systemInfo);
    
                double cpu_load = (double) systemInfo.get("cpu_used");
                if (cpu_load > 0.2) {
                    System.out.println("[Warning] CPU load: " + cpu_load);
                }
    
                Thread.sleep(interval * 1000);
            }
        }
    }
    
  4. Check the running result of the program. The program will return the following result if the CPU load exceeds the threshold.

    [Warning] CPU load: 1.0
    

Next Unit


Unit 4: Controlling Data Upload Interval