Example: Host CPU load percentage

This process repeatedly queries the edge device CPU load and sends the resulting data to IBM Event Streams, see IBM Event Streams Opens in a new tab. Because it does not require specialized sensor hardware, it can run on any edge device.

Before performing this task, register and unregister your edge device using the https://www.ibm.com/support/knowledgecenter/SSBS6K_3.2.0/supported_environments/edge_devices/registration.html example.

This example provides a foundation for understanding the CPU load edge service example.

To achieve a more realistic scenario, this cpu2msghub example illustrates additional aspects of a typical edge services, including the following:

Deploying an instance of IBM Event Streams

Every member of your organization can share the IBM Event Streams instance. If the instance is currently deployed, obtain the access information and set the environment variables. You can deploy IBM Event Streams in IBM Cloud or IBM Cloud Private. Both situations are included in this content.

Deploying IBM Event Streams in IBM Cloud

  1. Navigate to the IBM Cloud.

  2. Click Create resource.

  3. Enter Event Streams in the search box.

  4. Select the Event Streams tile.

  5. In Event Streams, enter a service name, select a location, select a pricing plan, and click Create to provision the instance.

  6. After provisioning is complete, click the instance.

  7. To create a topic, click on the + icon, then name the instance cpu2msghub.

  8. You can either create credentials in your terminnal or obtain them if they are already created. To create credentials, click Service credentials > New credentials, and export the following environment variables:

    • MSGHUB_API_KEY (the value of api_key)
    • MSGHUB_BROKER_URL (all kafka_brokers_sasl values in a single string, separated by commas) For example, from the view credentials pane:

      
      MSGHUB_BROKER_URL="kafka04-prod02.messagehub.services.us-south.bluemix.net:9093,kafka03-prod02.messagehub.services.us-south.bluemix.net:9093,kafka01-prod02.messagehub.services.us-south.bluemix.net:9093,kafka02-prod02.messagehub.services.us-south.bluemix.net:9093,kafka05-prod02.messagehub.services.us-south.bluemix.net:9093")
      

Testing IBM Event Streams in IBM Cloud

  1. Install kafkacat (https://github.com/edenhill/kafkacat).

  2. On a terminal, enter the following to subscribe to the cpu2msghub topic:

     kafkacat -C -q -o end -f "%t/%p/%o/%k: %s\n" -b $MSGHUB_BROKER_URL -X api.version.request=true -X security.protocol=sasl_ssl -X sasl.mechanisms=PLAIN -X sasl.username=${MSGHUB_API_KEY:0:16} -X sasl.password=${MSGHUB_API_KEY:16} -t cpu2msghub
    
  3. On a second terminal, publish test content to the cpu2msghub topic to display it on the original console. For example:

     echo 'hi there' | kafkacat -P -b $MSGHUB_BROKER_URL -X api.version.request=true -X security.protocol=sasl_ssl -X sasl.mechanisms=PLAIN -X sasl.username=${MSGHUB_API_KEY:0:16} -X sasl.password=${MSGHUB_API_KEY:16} -t cpu2msghub
    

Deploying IBM Event Streams in IBM Cloud Private

  1. In the IBM Cloud Private navigation menu, click Manage > Namespaces > Create Namespace to create a new namespace that is dedicated to IBM Event Streams:

    • Set the Name to 1event-streams1 (for example)
    • Select ibm-restricted-psp for Pod Security Policy
  2. Click Catalog to install IBM Event Streams.

  3. Search for and select ibm-eventstreams-dev, then click Configure:

    • Set Helm release name to samples-event-streams (for example).
    • Select event-streams for Target namespace.
    • Select local-cluster for Target cluster.
    • Click Install.
  4. After samples-event-streams is deployed, run the following command to verify:

     helm test samples-event-streams --cleanup --tls
    
  5. In the navigation menu, click Workloads > Helm Releases > samples-event-streams > Launch, scroll, and click admin-ui-https:

  6. Click Connect to this cluster:

    • Note the Bootstrap Server and in your console export MSGHUB_BROKER_URL=<bootstrap-svr>.
    • Download the PEM certificate file and:

      export MSGHUB_CERT_FILE="<pem-file-path>"
      export MSGHUB_CERT_ENCODED="$(cat $MSGHUB_CERT_FILE | base64)"
      
    • Create and save the event streams API key and export MSGHUB_API_KEY=<api-key>.

  7. Click Toolbox to create the cpu2msghub event streams topic, scroll to IBM Event Streams command line, and click Find out more to download the event streams cloudctl plugin, and run the following command:

     cloudctl plugin install <es-plugin-file>
     cloudctl es init
     cloudctl es topic-create cpu2msghub -p 2 -r 1
     cloudctl es topics
    

Testing IBM Event Streams in IBM Cloud Private

  1. Install kafkacat (https://github.com/edenhill/kafkacat).

  2. On one terminal, enter the following to subscribe to the cpu2msghub topic:

     kafkacat -C -q -o end -f "%t/%p/%o/%k: %s\n" -b $MSGHUB_BROKER_URL -X api.version.request=true -X security.protocol=sasl_ssl -X sasl.mechanisms=PLAIN -X sasl.username=token -X sasl.password=$MSGHUB_API_KEY -X ssl.ca.location=$MSGHUB_CERT_FILE -t cpu2msghub
    
  3. On a different terminal, publish test content to the cpu2msghub topic to display it on the original console. For example:

     echo 'hi there' | kafkacat -P -b $MSGHUB_BROKER_URL -X api.version.request=true -X security.protocol=sasl_ssl -X sasl.mechanisms=PLAIN -X sasl.username=token -X sasl.password=$MSGHUB_API_KEY -X ssl.ca.location=$MSGHUB_CERT_FILE -t cpu2msghub
    

Registering your edge device

To run the cpu2msghub service example on your edge node, you must register your edge node with the IBM/pattern-ibm.cpu2msghub deployment pattern. Perform the steps in the first section in Horizon CPU To IBM Event Streams Opens in a new tab.

Additional information

The CPU example source code is also available in the Horizon GitHub repository as an example for IBM Edge Computing for Devices development. This source includes the code for all of the three services that run on the edge nodes for this example. Note that the api_key here is different from the one that you retrieved in the previous step. In this case, et your exchange user credentials in the Horizon-supported environment variable and verify it:

```
export HZN_EXCHANGE_USER_AUTH="iamapikey:<myapikey>" hzn exchange user list 
```

These services include: