HomeGuidesAPI Reference
GuidesAPI ReferenceGitHubAirheads Developer CommunityLog In

StackStorm with AOS-CX Actions and Websocket Sensor

In the AOS-CX pack we have actions that allow us to retrieve information from an AOS-CX device. We can use those actions in combination with our PortSensor to retrieve real-time device statistics in the event that an interface goes down on our AOS-CX device.

PortSensor Configuration

The AOS-CX PortSensor uses a websocket connection to subscribe to a list of interfaces and retrieve their admin status upon a change. To specify which interfaces we want to monitor and on which device, we have to define our aoscx configuration file in StackStorm. StackStorm configuration files are located in /opt/stackstorm/configs/ and typically have the format of packName.yaml. Here's an example configuration file in which we've defined the device's IP, REST-API login information, and the interfaces we want to monitor:

credentials:
  admin:
    username: admin
    password: admin

device:
  ip_address: 10.100.206.188
  hostname: 8320-CX-188
  credentials: admin
  interface:
    - 1/1/1
    - 1/1/23
    - 1/1/14
  proxy:
    http: None
    https: None

Once the configuration file contains the necessary information, run this StackStorm commands to reload configuration files in that path to ensure StackStorm has the contents:
st2ctl reload --register-configs

Define Rule, Trigger, and Action

Now that the PortSensor is monitoring the switch, we need to define the criteria for our trigger and the action to retrieve all interfaces' statuses. In the rule below, we're specifying the trigger to be our aoscx.port_status and the criteria is when that value contains 'down' meaning the interface is shutdown. Then we're using the aoscx.get_link_status action to retrieve the interfaces of the device specified:

---
name: port_sensor_rule
pack: "aoscx"
description: Using the AOS-CX Websocket PortSensor, retrieves the admin status of all interfaces on an AOS-CX switch when an interface goes down.
enabled: true

trigger:
  type: aoscx.port_status

criteria:
  trigger.admin:
     pattern: "down"
     type: "icontains"

action:
    ref: "aoscx.get_link_status"
    parameters:
        ip: "10.100.206.188"
        username: "admin"		# REST-API Login username
        password: "admin"		# REST-API Login password

Once the rule is defined, we can add it into StackStorm with the following st2 command:
st2 rule create port_sensor_rule.yaml

You should see the following output if the rule was created successfully:

administrator@StackStorm-Ubuntu:~/stackstorm-aoscx/rules$ st2 rule create port_sensor_rule.yaml
+---------------+--------------------------------------------------------------+
| Property      | Value                                                        |
+---------------+--------------------------------------------------------------+
| id            | 5f6c9e3d99f71335767c235a                                     |
| name          | port_sensor_rule                                             |
| pack          | aoscx                                                        |
| description   | Using the AOS-CX Websocket PortSensor, retrieves the admin   |
|               | status of all interfaces on an AOS-CX switch when an         |
|               | interface goes down.                                         |
| action        | {                                                            |
|               |     "ref": "aoscx.get_link_status",                          |
|               |     "parameters": {                                          |
|               |         "ip": "10.100.206.188",                              |
|               |         "username": "admin",                                 |
|               |         "password": "admin"                                  |
|               |     }                                                        |
|               | }                                                            |
| context       | {                                                            |
|               |     "user": "st2admin"                                       |
|               | }                                                            |
| criteria      | {                                                            |
|               |     "trigger.admin": {                                       |
|               |         "pattern": "down",                                   |
|               |         "type": "icontains"                                  |
|               |     }                                                        |
|               | }                                                            |
| enabled       | True                                                         |
| metadata_file |                                                              |
| ref           | aoscx.port_sensor_rule                                       |
| tags          |                                                              |
| trigger       | {                                                            |
|               |     "type": "aoscx.port_status",                             |
|               |     "parameters": {},                                        |
|               |     "ref": "aoscx.port_status"                               |
|               | }                                                            |
| type          | {                                                            |
|               |     "ref": "standard",                                       |
|               |     "parameters": {}                                         |
|               | }                                                            |
| uid           | rule:aoscx:port_sensor_rule                                  |
+---------------+--------------------------------------------------------------+