Host CPU load percentage example (cpu2evtstreams)

Host CPU load percentage is an example deployment pattern that consumes CPU load percentage data and makes the available through IBM Event Streams.

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

Before performing this task, register and unregister by performing the steps in Install the Horizon agent on your edge device and register it with the hello world example

To gain experience with a more realistic scenario, this cpu2evtstreams example illustrates additional aspects of a typical edge services, including the following:

Deploying an instance of IBM Event Streams

Before deploying the cpu2evtstreams edge service you need an instance of IBM Event Streams running in the cloud to receive its data. Every member of your organization can share one 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 cpu2evtstreams.

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

     export EVTSTREAMS_API_KEY="<the value of api_key>"
     export EVTSTREAMS_BROKER_URL="<all kafka_brokers_sasl values in a single string, separated by commas>"
    

For example, from the view credentials pane:

export EVTSTREAMS_BROKER_URL=broker-4-x7ztkttrm44911kc.kafka.svc01.us-south.eventstreams.cloud.ibm.com:9093,broker-3-x7ztkttrm44911kc.kafka.svc01.us-south.eventstreams.cloud.ibm.com:9093,broker-2-x7ztkttrm44911kc.kafka.svc01.us-south.eventstreams.cloud.ibm.com:9093,broker-0-x7ztkttrm44911kc.kafka.svc01.us-south.eventstreams.cloud.ibm.com:9093,broker-1-x7ztkttrm44911kc.kafka.svc01.us-south.eventstreams.cloud.ibm.com:9093,broker-5-x7ztkttrm44911kc.kafka.svc01.us-south.eventstreams.cloud.ibm.com: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 cpu2evtstreams topic:

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

     echo 'hi there' | kafkacat -P -b $EVTSTREAMS_BROKER_URL -X api.version.request=true -X security.protocol=sasl_ssl -X sasl.mechanisms=PLAIN -X sasl.username=token -X sasl.password=$EVTSTREAMS_API_KEY -t cpu2evtstreams
    

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 EVTSTREAMS_BROKER_URL=<bootstrap-svr>.
    • Download the PEM certificate file and:

      export EVTSTREAMS_CERT_FILE="<pem-file-path>"
      export EVTSTREAMS_CERT_ENCODED="$(cat $EVTSTREAMS_CERT_FILE | base64)" (on Mac)
      export EVTSTREAMS_CERT_ENCODED="$(cat $EVTSTREAMS_CERT_FILE | base64 -w 0)" (on Linux)
      

      Note: Select the command based on MacOS or Linux.

    • Create and save the event streams API key and export EVTSTREAMS_API_KEY=<api-key>.

  7. Click Toolbox to create the cpu2evtstreams 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 cpu2evtstreams -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 cpu2evtstreams topic:

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

     echo 'hi there' | kafkacat -P -b $EVTSTREAMS_BROKER_URL -X api.version.request=true -X security.protocol=sasl_ssl -X sasl.mechanisms=PLAIN -X sasl.username=token -X sasl.password=$EVTSTREAMS_API_KEY -X ssl.ca.location=$EVTSTREAMS_CERT_FILE -t cpu2evtstreams
    

Registering your edge device

To run the cpu2evtstreams service example on your edge node, you must register your edge node with the IBM/pattern-ibm.cpu2evtstreams 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 available in the Open Horizon examples repository Opens in a new tab as an example for IBM Edge Computing for Devices edge service development. This source includes the code for all three of the services that run on the edge node for this example:

What to do next

If you want to deploy your own software to an edge node, you must create your own edge services, and associated deployment pattern or deployment policy. For more information, see Developing edge services with IBM Edge Computing for Devices.