Using Telnet to Collect Performance Data

Download this manual as a PDF file

Some network devices expose useful pieces of performance or configuration data via a telnet interface. Telnet may be the only method to access some information, such as configuration data.

Snippets provide a convenient way to gather data via telnet, and then present, report and alert against the results.

In this example, telnet is used to connect to a Dell Ethernet switch and collect CPU statistics. From the command line, an interaction with the switch looks like this:

> telnet 192.168.1.4

Trying 192.168.1.4...

Connected to 192.168.1.4.

Escape character is '^]'.

User Name:admin

Password:************

Dell-5324b# show cpu utilization

CPU utilization service is on.

CPU utilization

---------------

five seconds:8% ;one minute:8% ;five minutes:4%

Dell-5324b# exit

Connection closed by foreign host.

>

From the command line interface access attempt, we can see that to log in, the switch presents a "User Name:" prompt, followed by a "Password:" prompt. Once logged in, commands may be submitted at the # prompt. When CPU utilization is requested, the results come back on one line showing utilization for three different collection intervals. Finally, "exit" terminates the telnet session.

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:

  • Go to the Dynamic Applications Manager page (System > Manage > Dynamic Applications).
  • Select the Actions button, then Create New Dynamic Application. The Create New Application page is displayed.
  • Supply values in the following fields:
    • Application Name. The name of the Dynamic Application. This example is called "Snippet Telnet 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.
  • 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.
  • Select the Save button.

Because collection objects are 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 "CPU Collection".
    • 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

The snippet code for this example will use a regular expression to extract specific CPU values from the CPU response line in the switch. The collection objects will specify the command to execute (show cpu utilization) and the specific CPU value to be extracted using a regular expression for the collection object. The command to execute the specific CPU value to collect for each object will be defined in the Snippet Arguments field, separated by a pipe ('|').

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 first object, which will collect the five second CPU data:
    • Object Name. The name of the collection object. Enter "CPU Five Second" in this field.
    • Snippet Arguments. The arguments to pass to the snippet. Enter "show cpu utilization|five second:" in this field.
    • Class Type. The collected values for this collection object will be incrementing numeric values from which a delta must be taken. Select 1 Performance Counter in this field.
    • Snippet. There is only one snippet for this Dynamic Application. Select CPU Collection in this field.
  • This example does not have specific requirements for the other collection object settings. You can leave the remaining fields set to the default values.
  • Select the Save button, then select the Reset button to clear the values you entered.
  • Repeat steps two, three, and four to create a collection object for the one minute CPU collection, using the following values:
    • Object Name. Enter "CPU 1 Minute".
    • Snippet Arguments. The arguments to pass to the snippet. Enter "show cpu utilization|one minute:" in this field.
    • Class Type. The collected values for this collection object will be incrementing numeric values from which a delta must be taken. Select 1 Performance Counter in this field.
    • Snippet. There is only one snippet for this Dynamic Application. Select CPU Collection in this field.
  • Repeat steps two, three, and four to create a collection object for the five minute CPU collection, using the following values:
    • Object Name. Enter "CPU Five Minute".
    • Snippet Arguments. The arguments to pass to the snippet. Enter "show cpu utilization|five minute:" in this field.
    • Class Type. The collected values for this collection object will be incrementing numeric values from which a delta must be taken. Select 1 Performance Counter in this field.
    • Snippet. There is only one snippet for this Dynamic Application. Select CPU Collection in this field.

Snippet Code

This section walks through all the snippet code used in this example.

First, the telnetlib module is imported, which will be used to make the telnet connection to the switch. The regular expression module is also imported, which will be used to parse the performance data from the response:

import telnetlib

import re

The self.cred_details dictionary is used to make a connection to the switch:

host = self.cred_details['cred_host']

user = self.cred_details['cred_user']

password = self.cred_details['cred_pwd']

tn = telnetlib.Telnet(host)

tn.read_until("User Name:")

tn.write(user + "\n")

tn.read_until("Password:")

tn.write(password + "\n")

Like the snippet code in Example 1 and Example 2, this snippet iterates through the self.oids dictionary:

for group, oid_group in self.oids.iteritems():

for obj_id, oid_detail in oid_group.iteritems():

if oid_detail['oid_type'] != snippet_id:

continue

The snippet expects the arguments for each collection object to be the command to run and the value to parse from the results separated by a pipe ('|'). If the collection object the snippet is currently operating on is not in this format, the snippet generates an alert, then iterates to the next collection object:

cmd_l = oid_detail['oid'].split('|')

if len(cmd_l) != 2:

self.internal_alerts.append((518, "App:%s, Snippet:%s, Invalid collection value:%s (must be a command and a match string)" % \

(self.app_id, req_id, oid_detail['oid'])))

continue

If the collection object has valid arguments, the command is executed, then the result is parsed:

command = cmd_l[0]

match_val = cmd_l[1]

 

tn.read_until("#")

tn.write(command + "\n")

tn.read_until("-----------\r\n")

result = tn.read_until("\r\n")

 

match_regex = "(?<=%s).*?[0-9]{1,}" % (match_val)

match_obj = re.search(match_regex, result)

If a match is found, the result is stored:

if match_obj:

oid_detail['result'] = [(0, match_obj.group()),]

Using the Dynamic Application

For performance Dynamic Applications, presentation objects that correspond to each collection object are automatically created. To add the snippet code 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 "CPU Collection" 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.

To use the example Dynamic Application, you must first create a Basic/Snippet credential to align with the Dynamic Application. Perform the following steps to create Basic/Snippet credential for a Dell Ethernet switch:

  • Go to the Credential Management page (System > Manage > Credentials).
  • Select the Create button, then select Basic/Snippet Credential. The Credential Editor page is displayed.
  • Supply values in the following fields:
    • Profile Name. Enter a name for the credential.
    • Hostname/IP. Specifies the hostname or IP address to connect to. If you are aligning this Dynamic Application to a Dell Ethernet switch modeled as a device in your system, enter "%D" in this field to use the IP address of the aligned device. If you are not aligning this Dynamic Application to a Dell Ethernet switch modeled as a device in your system, enter the hostname or IP address of your Dell Ethernet switch in this field.
    • Port. Specifies the port to connect to. Enter "23" in this field to use the default port for telnet.
    • Timeout. This field is not used by the snippet code. You can leave this field blank.
    • Username. Specifies the username to use when connecting to the switch. Enter the username for a user on your switch in this field.
    • Password. Specifies the password to use when connecting to the switch. In this field, enter the password for the username you entered in the Username field.
  • Select the Save button.

To align this Dynamic Application to a device, perform the following steps:

If you entered "%D" in the Hostname/IP field for your credential, you must align the Dynamic Application to a Dell Ethernet switch.

  • Go to the Device Manager page (Registry > 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, Dell Switch CPU Snippet. Select the Basic/Snippet credential you created for your Dell Ethernet switch.
  • Select the Save button. The page refreshes, and the Snippet Telnet 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 (Registry > 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 Telnet Example in the left Navbar, then select one of the three presentations for this Dynamic Application. A performance graph is displayed.

Full Snippet Code Listing

import telnetlib

import re

host = self.cred_details['cred_host']

user = self.cred_details['cred_user']

password = self.cred_details['cred_pwd']

tn = telnetlib.Telnet(host)

tn.read_until("User Name:")

tn.write(user + "\n")

tn.read_until("Password:")

tn.write(password + "\n")

for group, oid_group in self.oids.iteritems():

for obj_id, oid_detail in oid_group.iteritems():

if oid_detail['oid_type'] != snippet_id:

continue

cmd_l = oid_detail['oid'].split('|')

if len(cmd_l) != 2:

self.internal_alerts.append((518, "App:%s, Snippet:%s, Invalid collection value:%s (must be a command and a match string)" % \

(self.app_id, req_id, oid_detail['oid'])))

continue

command = cmd_l[0]

match_val = cmd_l[1]

tn.read_until("#")

tn.write(command + "\n")

tn.read_until("-----------\r\n")

result = tn.read_until("\r\n")

match_regex = "(?<=%s).*?[0-9]{1,}" % (match_val)

match_obj = re.search(match_regex, result)

if match_obj:

oid_detail['result'] = [(0, match_obj.group()),]