A Random Number Generator Dynamic Application

Download this manual as a PDF file

This example describes the development of a Performance Snippet Dynamic Application that generates a random number within a specified range. The results are then presented in a graph. For simplicity, this example does not use credentials or network connections.

Use the following menu options to navigate the SL1 user interface:

  • To view a pop-out list of menu options, click the menu icon ().
  • To view a page containing all of the menu options, click the Advanced menu icon ().

Creating the Dynamic Application

To create this example Dynamic Application, perform the following steps:

  1. Go to the Dynamic Applications Manager page (System > Manage > Dynamic Applications).
  2. In the Dynamic Applications Manager page, select the Actions button, then select Create New Dynamic Application. The Create New Application page is displayed.
  3. Supply values in the following fields:
    • Application Name. The name of the Dynamic Application. This example is called "Snippet Random Number Example".
    • Application Type. This example is a Snippet Performance Dynamic Application. Select Snippet Performance in this field.
    • Poll Frequency. To see data as quickly as possible, select Every 1 Minute in this field.
  4. This example does not have specific requirements for the other settings defined in this page. You can leave the remaining fields set to the default values.
  5. Select the Save button.

Because each collection object is assigned a specific snippet, you must create the container for the snippet code before creating the collection objects for this Dynamic Application. To create a container for the snippet code, perform the following steps:

  • Select the Snippets tab.
  • Supply values in the following fields:
    • Snippet Name. The name of the snippet. The snippet in this example is called "Generate Random Number".
    • Active State. To ensure that the snippet code is run by SL1, select Enabled in this field.
    • Snippet Code. Leave this field blank. You will add the snippet code later in this example.
  • Select the Save button.

Creating the Collection Objects

Each collection object defines a nuermic range by specifying a high value and low value within which the random number must fall. This example will include two collection objects:

  • Random 17-20. The snippet will generate random numbers between 17 and 20.
  • Random 30-50. The snippet will generate random numbers between 30 and 50.

For each collection object, the argument will specify the range the random number must fall within for that collection object.

To create the collection objects for this Dynamic Application, perform the following steps:

  • Select the Collections tab in the Dynamic Application Editor pane.
  • Supply values in the following fields to create the Random 17-20 object:
    • Object Name. The name of the collection object. Enter "Random 17-20" in this field.
    • Snippet Arguments. The arguments to pass to the snippet. Enter "17 20" in this field.
    • Class Type. The collected values for this collection object will be numeric values that can go up or down. Select 4 Performance Gauge in this field.
    • Snippet. There is only one snippet for this Dynamic Application. Select Generate Random Number in this field.
  • This example does not have specific requirements for the other fieldss. You can accept the default values for the remaining fields.
  • Select the Save button, then select the Reset button to clear the values you entered.
  • Repeat steps two, three, and four for the Random 30-50 collection object using the following values:
    • Object Name. Enter "Random 30-50".
    • Snippet Arguments. Enter "30 50".
    • Class Type. Select 4 Performance Gauge.
    • Snippet. Select Generate Random Number.

Snippet Code

When the snippet in this Dynamic Application begins execution, the items to be collected are in the result_handler dictionary. The result_handler dictionary includes the following:

  • For each collection object argument (the "oid" value), the result_handler dictionary includes a key. The key has the has the same name as the argument.  The argument is defined in the Snippet Arguments field for each collection object.
  • For each key, there is a dictionary of collection object attributes. Each attribute is a tuple that contains two values: an index followed by a result string.
  • Often the only attribute of interest are the result_handler dictionary keys themselves.

The result_handler dictionary for this Dynamic Application has the following structure, with the Object Name and Snippet Argument values highlighted in blue:

{

'30 50': {'prime': 0, 'error_msg': '', 'enum': '', 'name': 'Random 30-50',

'oid': '30 50', 'string_type': 0, 'class': 4, 'wm_walk_length': '', 'oid_time': '0', 'result': '', 'oid_type': 9, 'factor': '', 'trend_col': '', 'monitor_config': 0},

'17 20': {'prime': 0, 'error_msg': '', 'enum': '', 'name': 'Random 17-20',

'oid': '17 20', 'string_type': 0, 'class': 4, 'wm_walk_length': '', 'oid_time': '0', 'result': '', 'oid_type': 9, 'factor': '', 'trend_col': '', 'monitor_config': 0}

}

Before the end of a successful collection, the snippet code must assign the result of the collection to result_handler. The result for each collection object must be a list of tuples. Each result tuple contains two values: an index followed by a result string. The index can be any scalar type, but no two tuples can have the same index value. There are two methods for updating the result value for a collection object using the result_handler dictionary:

  • Assign a value directly to the key for that collection object. Remember that the key for a collection object is the value in the Snippet Arguments field.
  • Update the result value for all the collection objects at the same time by using the update() function of result_handler. The results parameter passed to the function must be a dictionary that has the same keys as result_handler. Each key in the supplied dictionary must reference that value to be set as the result value for that collection object.

This example uses the first method to return results.

The following steps walk through each section of code in this example:

    Import the random module, which is needed to generate random numbers:

    import random

    Loop through the keys in result_handler. This will allow the snippet to operate on the arguments for each collection object in turn:

    for collection in result_handler.iterkeys():

    The low and high range values are extracted from the collection object. The snippet then validates the range values. The COLLECTION_PROBLEM and PROBLEM_STR variables are used to report invalid values:

    range_values = collection.split()

    if len(range_values) != 2 or not range_values[0].isdigit() or not range_values[1].isdigit:

    COLLECTION_PROBLEM = True

    PROBLEM_STR = "App:%s, Snippet:%s, Invalid collection value:%s" % (self.app_id, req_id, collection)

    continue

    low = int(range_values[0])

    high = int(range_values[1])

    If COLLECTION_PROBLEM is TRUE at the end of collection, the value of PROBLEM_STR will be used to generate an event for the associated device.

    The snippet then generates the random number and assigns a list that contains a single tuple to the collection object key in result_handler. In this case there is only one value being returned for each collection object, so the index is fixed as the value 0:

    rnum = str(random.randint(low, high))

    result_handler[collection] = [(0, rnum),]

Using the Dynamic Application

For performance Dynamic Applications, SL1 automatically creates presentation objects that correspond to each collection object. To add the snippet code for this example to the example Dynamic Application you created, perform the following steps:

  • Select the Snippets tab in the Dynamic Application Editor pane.
  • Select the wrench icon () for the "Generate Random Number" snippet.
  • Insert the snippet code in the Snippet Code field. A full listing of the snippet code is included in the last section.
  • Select the Save button.

Perform the following steps to align this Dynamic Application to a device:

  • Go to the Device Manager page (Devices > Device Manager).
  • Select the wrench icon () for the device you want to align the Dynamic Application with. The Device Properties page is displayed.
  • Select the Collections tab. The Dynamic Application Collections page is displayed.
  • Select the Action menu and choose Add Dynamic Application.
  • In the Dynamic Application Alignment modal page, select our example Dynamic Application, Snippet Random Number Example. Select Default SNMP Credential.
  • Select the Save button. The page refreshes, and the Snippet Random Number Example Dynamic Application is displayed in the list of Dynamic Applications.

The first collected value will be stored within one minute. To view collected data for this Dynamic Application:

  • Go to the Device Manager page (Devices > Device Manager).
  • Select the graph icon () for the device you aligned the Dynamic Application with. The Device Summary page is displayed.
  • Select the Performance tab. The Device Performance page is displayed.
  • Select Snippet Random Number Example in the left Navbar, then select one of the two presentations for this Dynamic Application. A performance graph will be displayed.

Full Snippet Code Listing

import random

for collection in result_handler.iterkeys():

range_values = collection.split()

if len(range_values) != 2 or not range_values[0].isdigit() or not range_values[1].isdigit:

COLLECTION_PROBLEM = True

PROBLEM_STR = "App:%s, Snippet:%s, Invalid collection value:%s" % (self.app_id, req_id, collection)

continue

low = int(range_values[0])

high = int(range_values[1])

rnum = str(random.randint(low, high))

result_handler[collection] = [(0, rnum),]