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 . 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:
-
Querying dynamic edge device data
-
Analyzing edge device data (for example,
cpu2msghub
calculates a window average of the CPU load) -
Sending processed data to a central data ingest service
-
Automates the acquisition of event stream credentials to securely authenticate data transfer
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
-
Navigate to the IBM Cloud.
-
Click Create resource.
-
Enter
Event Streams
in the search box. -
Select the Event Streams tile.
-
In Event Streams, enter a service name, select a location, select a pricing plan, and click Create to provision the instance.
-
After provisioning is complete, click the instance.
-
To create a topic, click on the + icon, then name the instance
cpu2msghub
. -
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
-
Install
kafkacat
(https://github.com/edenhill/kafkacat). -
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
-
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
-
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
-
Click Catalog to install IBM Event Streams.
-
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.
- Set Helm release name to
-
After
samples-event-streams
is deployed, run the following command to verify:helm test samples-event-streams --cleanup --tls
-
In the navigation menu, click Workloads > Helm Releases > samples-event-streams > Launch, scroll, and click admin-ui-https:
-
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 exportMSGHUB_API_KEY=<api-key>
.
- Note the Bootstrap Server and in your console export
-
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
-
Install
kafkacat
(https://github.com/edenhill/kafkacat). -
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
-
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 .
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:
-
The cpu service that provides the CPU load percentage data as a REST service on a local private Docker network. For more information, see Horizon CPU Percent Service .
-
The gps service that provides location information from GPS hardware, if available or from a location that is provided at registration. The service can also estimate the information by using the IP address when the other sources are not provided. The location information is provided as a REST service on a local private Docker network. For more information, see Horizon GPS Service .
-
The cpu2msghub service that uses the REST APIs that are provided by the other two services. This service sends the combined data to an IBM Event Streams Kafka broker in the IBM Cloud. For more information about the service, see Horizon CPU To IBM Event Streams Service .
-
For more information about the IBM Event Streams, see IBM Event Streams, see IBM Event Streams .