8.4. Integrating sFlowTrend-Pro with other applications

sFlowTrend-Pro includes an interface which can be used to control it from other applications. The intention of this capability is to allow an application, such as a switch element manager, to change the view within sFlowTrend-Pro to match the context of the external application. For example, if an element manager which has been integrated with sFlowTrend-Pro is being used to configure a switch, then the user could automatically make sFlowTrend-Pro display traffic information for that switch.

This part of the sFlowTrend-Pro help is intended for developers, who are working on integrating other applications with sFlowTrend-Pro.

8.4.1. How integration works

When sFlowTrend-Pro runs, it automatically starts a web server, listening on TCP port 8087 (by default - you can change it with the advanced configuration properties, see Section 8.3, “Custom configuration”). When specific URLs are visited at this address, sFlowTrend-Pro will respond by taking an appropriate action. Several URLs are provided by default with sFlowTrend-Pro, which perform basic functions. These are actually implemented in JavaScript, which allows more complex control to be implemented relatively easily, if desired.

The URLs use standard http GET (or query) parameters, to pass information to sFlowTrend-Pro. For example, the URL to change the switch and switch interface currently being viewed in sFlowTrend-Pro, to 10.1.2.3 and 49, respectively, is http://localhost:8087/selectIfIndex.js?agent=10.1.2.3&ifIndex=49 . The intention is that such URLs would be visited by an external application, which directly controls sFlowTrend-Pro. However, to test and demonstrate some of the URLs available, you can point a web browser directly at http://localhost:8087 (on the system running sFlowTrend-Pro). This will display a web page which allows different controls to be tried out.

Note that for security reasons, by default sFlowTrend-Pro only will respond to web requests from localhost (which, of course, means that sFlowTrend-Pro must be running on the same system as the application or web browser that is trying to access sFlowTrend-Pro). This can be changed through the use of the advanced configuration properties (see Section 8.3, “Custom configuration”).

8.4.2. Default URLs available

The following URLs are provided for easy integration. In most cases, these are sufficient, but if more flexibility is required custom URLs (see Section 8.4.3, “Creating custom URLs”) can be generated. With the default URLs, if the request is successful, then the URL will return the string "OK". If it fails, then the string "FAIL -", followed by an error message (where possible), is returned. These strings can be used by an application to understand the success of a request.

http://localhost:8087/selectAgent.js?agent=10.1.2.3
Changes sFlowTrend-Pro to view switch 10.1.2.3. If this switch is known by sFlowTrend-Pro, then it will be displayed in the main user interface. If it cannot be selected, either because the IP address of the switch is invalid, or because sFlowTrend-Pro does not know about the switch, then the string "FAIL -" is returned, with an error message.
http://localhost:8087/selectIfIndex.js?agent=10.1.2.3&ifIndex=49"
Changes sFlowTrend-Pro to view switch 10.1.2.3, and interface 49. If this switch and interface is known by sFlowTrend-Pro, then a chart with both switch and interface will be displayed. If the interface is not known, then just the switch will be selected (and "all" interfaces shown).
http://localhost:8087/selectTab.js?tab=trends
Displays the 'Trends' tab in the user interface, for the current switch and interface. Instead of 'trends', 'interfaces' can also be specified, to display the 'Interfaces' tab. If an illegal tab is specified, then a list of valid ones will be returned.
http://localhost:8087/selectChart.js?chart=topSources
Displays the specified chart (Top Sources in this example), for the current switch and interface. If an illegal chart is specified, then a list of valid ones will be returned (currently utilization, counters, topSources, topDestinations, topSrcVLANs, topDestVLANs, topPairs, topFlows, topInterVLANPairs, topConnections, topServers, topClients, topProtocols, topL2Broadcasts, topL3Multicasts, topSrcsByNumDests, topDestsByNumSrcs and topDestProtocolsByNumPairs).

8.4.3. Creating custom URLs

sFlowTrend-Pro comes with some web pages pre-packaged into the application. However, these can be overridden, or new ones generated, if specific customization is desired. By default, the document root is the directory html in the sFlowTrend-Pro data directory. This will need to be created before it can be used. Files placed in this directory will form available URLs, in the customary fashion for a web server. Files with the extension html will be served as html files, and those with the extension .js will be interpreted as JavaScript scripts.

If html files are used with the sFlowTrend-Pro web server, these will be served up in the normal way, but will not affect the operation of sFlowTrend-Pro. JavaScript must be used to control sFlowTrend-Pro. To use JavaScript to generate a URL, first create the file (eg html/test.js, within the sFlowTrend-Pro data directory). The content of this file should be standard JavaScript 1.6, with some extensions to allow control of sFlowTrend-Pro. The available extensions are:

Object sFlowTrend

The object sFlowTrend is the main interface to the running application. It has several properties defined:

selectAgent(string agentIP)
sets the currently selected agent to agentIP.
selectIFIndex(string agentIP, int ifIndex)
sets the currently selected agent to agentIP, and the current interface to ifIndex.
selectTab(string tabName)
selects the currently display user interface tab.
selectChart(string chartName)
selects the currently displayed chart.
string[] agents
a property containing an array of the switch IP addresses that sFlowTrend-Pro is aware of. This is read-only.
string[] agentHostnames
a property containing an array of the switch DNS names (when resolveable) that sFlowTrend-Pro is aware of. This is read-only.
int maximumAgents
a property which reflects the maximum number of agents that sFlowTrend-Pro is configured to monitor (this will be -1 for an unlimited number). This is read-only.
document (class Document)

Provides a means for JavaScript to write to the current html document. Anything written to document will be returned from this URL. Methods available are:

write(string s)
Writes s to the document.
writeln(string s)
Writes s to the document, followed by a new line.
string mimeType
a property which can be read and written to, which reflects the mime type of the current document.
include(string file)
includes (and evaluates) the JavaScript file. The file should be specified relative to the current file, or absolutely with respect to document root. All properties defined prior to including the file will be available to the included file, and all defined with the included file will be available in the remainder of the including file.
getVars
getVars is an Object, with properties set for each get variable passed in through the URL. For example, if the URL was of the form http://localhost:8087/test.js?key=value , then getVars.key will contain value,

8.4.3.1. Custom URL example

This is a simple example, which is actually the JavaScript for the selectAgent page.

var pretty = getVars.pretty == "true";
if (pretty) {
    pageName = "sFlowTrend select agent";
    include("../inc/header.js");
}
document.writeln(sFlowTrend.selectAgent(getVars.agent));
if (pretty) {
    document.writeln("<br>");
    include("../inc/footer.js");
}
                    

In this example, we first look for the get variable pretty. We set a boolean variable to true if the get variable is set to the string true. This is used to produce nicer formatted html; for the case of integrating with an external application, it is best to leave this unset, as the result of the URL will be easier to parse. If pretty is set, then we include a header file, which just provides a the standard HTML headers, and a style sheet. Likewise, at the end, we include a footer file if pretty is true.

The real work is in the line document.writeln(sFlowTrend.selectAgent(getVars.agent)); . This calls the selectAgent method with the get variable agent, passed in through the URL, and then writes the result to document.