Skip to main content

Example of How to Use the Status API – Get Sensor Status For All Sensors In A Group

Updated this week

The User Experience Insight (UXI) Sensor Status API allows you fetch the status of a given sensor. You can find documentation for the Sensor Status API in the OpenAPI specification.

Prerequisites

You will need to create a personal API client for the User Experience Insight service manager on the Greenlake Cloud Platform. See the detailed instructions on creating your personal API client in the Greenlake documentation.

For this specific example to work, you will also need the group ID and sensors need to be directly assigned to the group. You can get the group ID from the API /networking-uxi/v1alpha1/groups.

Example Scenario

In this example, suppose we have a group and we want to see the status of all the sensors that are directly assigned to that group. To determine:

1. Whether the sensor is currently online.

2. Whether the sensor is currently testing.

3. Whether the sensor has any ongoing issues.

APIs

Here are the APIs you will need:

  • /networking-uxi/v1alpha1/groups

  • /networking-uxi/v1alpha1/sensor-group-assignments

  • /networking-uxi/v1alpha1/sensors/{id}/status

Putting it together

Here is a Python 3 example of this in action. This example uses Python requests to onboard a sensor into a group.

This example does the following:

1. Generate a temporary access token using the credentials of the personal API client.

2. Fetch all the sensors assigned to a specific group.

3. Fetch the status of each sensor in that group.

**WARNING**

This example is for demonstration purposes only. This example lacks robust error handling and optimizations necessary for a production environment. Do not use the example code in a production environment.

from typing import Any  # noqa: INP001

import requests
from oauthlib.oauth2 import BackendApplicationClient
from requests.auth import HTTPBasicAuth
from requests_oauthlib import OAuth2Session

# Global constants
AUTH_TOKEN_URL = "https://sso.common.cloud.hpe.com/as/token.oauth2" # noqa: S105
UXI_API_BASE_URL = "https://api.capenetworks.com/networking-uxi/v1alpha1"

# Customer specific constants
CLIENT_ID = "<CLIENT_ID>"
CLIENT_SECRET = "<CLIENT_SECRET>" # noqa: S105

# Example specific constants

# The ID of the group that holds the sensors of interest.
# This can be retrieved from the API: /networking-uxi/v1alpha1/groups.
GROUP_ID = "<GROUP_ID>" # eg: 9bfded8bd7cb


def main() -> None: # noqa: D103
api_token = get_api_token()
sensors = get_sensors_assigned_to_group(api_token=api_token)
get_sensor_statuses(api_token=api_token, sensors=sensors)


def get_api_token() -> str: # noqa: D103
client = BackendApplicationClient(CLIENT_ID)
oauth = OAuth2Session(client=client)
auth = HTTPBasicAuth(CLIENT_ID, CLIENT_SECRET)
token = oauth.fetch_token(token_url=AUTH_TOKEN_URL, auth=auth)

return token["access_token"]


def get_sensors_assigned_to_group(api_token: str) -> list[str]: # noqa: D103
sensors: list[str] = []
next_cursor = None

# This while loop exists to keep making requests until all data for the specified resource is fetched.
while True:
try:
response = requests.get( # noqa: S113
f"{UXI_API_BASE_URL}/sensor-group-assignments",
headers={
"Authorization": f"Bearer {api_token}",
"Content-Type": "application/json",
},
params={"next": next_cursor} if next_cursor else {},
)
response.raise_for_status()
response_body: dict[str, Any] = response.json()
response_items: list[dict[str, Any]] = response_body.get("items", [])
next_cursor = response_body.get("next")

sensors.extend(
[item["sensor"]["id"] for item in response_items if item["group"]["id"] == GROUP_ID]
)

if not next_cursor or not response_items:
break
except requests.exceptions.HTTPError as e:
print(f"Failed to get sensors for group '{GROUP_ID}': {e.response.text}") # noqa: T201
break

return sensors


def get_sensor_statuses(api_token: str, sensors: list[str]) -> None: # noqa: D103
for sensor_id in sensors:
try:
response = requests.get( # noqa: S113
f"{UXI_API_BASE_URL}/sensors/{sensor_id}/status",
headers={
"Authorization": f"Bearer {api_token}",
"Content-Type": "application/json",
},
)
response.raise_for_status()

response_body: dict[str, Any] = response.json()
print(f"Sensor status for'{sensor_id}'") # noqa: T201
print(f"\tis_online: {response_body['isOnline']}") # noqa: T201
print(f"\tis_testing: {response_body['isTesting']}") # noqa: T201
print(f"\tissues: {response_body['issues']}") # noqa: T201
except requests.exceptions.HTTPError as e:
print(f"Failed to get sensor status for '{sensor_id}': {e.response.text}") # noqa: T201


if __name__ == "__main__":
main()

Did this answer your question?