![]() |
Table Of Contents
Example 1 (Python-Suds Client)
Example 2 (Python-Suds Client)
Example 3 (Python-Suds Client)
Using the search-client Script
Search API
This chapter describes the Search API:
•
Using the search-client Script
Overview
All search interfaces return a queryId and a queryStatus, in addition to the actual search results. The queryId may be used later for retrieving more results from the same search. The queryStatus contains an integer status code and a string message about the code.
The search API provides the following interfaces:
•
DeviceQueryResult searchDevices(String query, List<String> fieldNames, String queryId, Long count, Long offset)
•
GroupQueryResult getGroups(String groupType)
•
DeviceDetailQueryResult getDeviceDetails(String query, List<String> deviceIds, String queryId, Long count, Long offset)
•
MetricHistoryQueryResult getMetricHistory(String query, List<String> deviceIds, Date startTime, Date endTime, List<String> metricIds, String rollupInterval, String rollupFunction, String queryId, Long count, Long offset)
Using the Search API
In your CG-NMS NB API client application, use this CG-NMS server URL to access the Search API WSDL:
http://<server_address>/nbapi/search?wsdl
These are examples of client applications using the Search API:
•
Example 1 (Python-Suds Client)
•
Example 2 (Python-Suds Client)
•
Example 3 (Python-Suds Client)
Example 1 (Python-Suds Client)
from suds.client import Clientimport logginglogging.basicConfig(level=logging.INFO)logging.getLogger('suds.client').setLevel(logging.INFO)url = "http://localhost/nbapi/search?wsdl"cl = Client(url, username='root', password='Tree123!')globalMetrics = ['uptime']cgmeshMetrics = ['meshHops','meshLinkCost','meshPathCost','meshRssi','meshReverseRssi','meshRxSpeed','mesh TxSpeed']cgr1kStatus = ['doorStatus','numBBU','battery0Level','battery0Runtime','battery1Level','battery1Runtime' ,'battery2Level','battery2Runtime']cgr1kMetrics = ['chassisTemp','ethernetTxDrops','ethernetRxSpeed','ethernetTxSpeed']cgr1kCellIfMetrics = ['cellularRSSI','cellularRxSpeed','cellularTxSpeed']cgr1kWiMAXIfMetrics = ['wimaxRSSI','wimaxRxSpeed','wimaxTxSpeed']cgr1kMeshIfMetrics = ['meshEndpointCount','meshRxSpeed','meshTxSpeed']# search FAN router devicesdevices = []result = cl.service.searchDevices('deviceType:cgr1000 status:down',['lng','lat','ip'],'',1000,0)#print resultif result.queryStatus == "SUCCEEDED":print "eid,lng,lat,ip,"for d in result.devices:devices.append(d.eid)print str(d.eid)+",",for f in d.fields.entry:print str(f.value)+",",print ""Example 2 (Python-Suds Client)
from suds.client import Clientimport logginglogging.basicConfig(level=logging.INFO)logging.getLogger('suds.client').setLevel(logging.INFO)url = "http://localhost/nbapi/search?wsdl"cl = Client(url, username='root', password='Tree123!')#print cl# search mesh end devicesdevices = []result = cl.service.searchDevices('deviceType:cgmesh',['lng','lat','ip'],'',10,0)#print resultif result.queryStatus == "SUCCEEDED":print "eid,lng,lat,ip,"for d in result.devices:devices.append(d.eid)print str(d.eid)+",",for f in d.fields.entry:print str(f.value)+",",print ""globalMetrics = ['uptime']cgmeshMetrics = ['meshHops','meshLinkCost','meshPathCost','meshRssi','meshReverseRssi','meshRxSpeed','mesh TxSpeed']cgr1kStatus = ['doorStatus','numBBU','battery0Level','battery0Runtime','battery1Level','battery1Runtime' ,'battery2Level','battery2Runtime']cgr1kMetrics = ['chassisTemp','ethernetTxDrops','ethernetRxSpeed','ethernetTxSpeed']cgr1kCellIfMetrics = ['cellularRSSI','cellularRxSpeed','cellularTxSpeed']cgr1kWiMAXIfMetrics = ['wimaxRSSI','wimaxRxSpeed','wimaxTxSpeed']cgr1kMeshIfMetrics = ['meshEndpointCount','meshRxSpeed','meshTxSpeed']# get device metric historyresult = cl.service.getMetricHistory('status:up',[str(devices[0]),str(devices[1]),str(devices[2])], '2012-04-01 00:00:00','2012-04-01 23:59:59',cgmeshMetrics,'day','avg','',2,0)#print resultif (result.queryStatus == "SUCCEEDED"):try:result.metricValuesexcept:print "no metrics returned"else:print "eid,metric,value,timestamp,"for m in result.metricValues:print str(m.eid), ",", str(m.metricId),",", str(m.value),",", str(m.timestamp)# search FAN router devicesdevices = []result = cl.service.searchDevices('deviceType:cgr1000',['lng','lat','ip'],'',10,0)#print resultif result.queryStatus == "SUCCEEDED":print "eid,lng,lat,ip,"for d in result.devices:devices.append(d.eid)print str(d.eid)+",",for f in d.fields.entry:print str(f.value)+",",print ""# get device metric historyfor f in devices:result = cl.service.getMetricHistory('status:up',f,'2012-04-01 00:00:00','2012-04-01 23:59:59',cgr1kMetrics+cgr1kCellIfMetrics+cgr1kWiMAXIfMetrics+cgr1kMeshIfMetrics,'day','av g','',0,0)#print resultif (result.queryStatus == "SUCCEEDED"):try:result.metricValuesexcept:print "no metrics returned"else:print "eid,metric,value,timestamp,"for m in result.metricValues:print str(m.eid), ",", str(m.metricId),",", str(m.value),",", str(m.timestamp)Example 3 (Python-Suds Client)
import sysfrom suds.client import Clientimport logginglogging.basicConfig(level=logging.INFO)logging.getLogger('suds.client').setLevel(logging.DEBUG)url = "http://localhost/nbapi/search?wsdl"cl = Client(url, username='root', password='Tree123!')# get config groupsgroups = ['DeviceType','Status','ConfigGroup','FirmwareGroup','TunnelProvisioningGroup','Label']for g in groups:result = cl.service.getGroups(g)#print resultif result.queryStatus == "SUCCEEDED":print gfor r in result.groups:print r.name,if (r.properties != None and r.properties != ""):for p in r.properties.entry:if (p.key == "description" or p.key == "deviceType"):print p.value,if r.memberCount != None:print "x " + str(r.memberCount) + ",",print ""print ""# search devicesdevices = []result = cl.service.searchDevices('status:up',['lng','lat','ip'],'',10,0)#print resultif result.queryStatus == "SUCCEEDED":print "eid,lng,lat,ip,"for d in result.devices:devices.append(d.eid)print str(d.eid)+",",for f in d.fields.entry:print str(f.value)+",",print ""# fetch mesh endpoint node device detailsmetrics = ("uptime","nodeLocalTime","meshHops","meshRssi","meshReverseRssi","meshLinkCost","meshPath Cost","meshRxSpeed","meshTxSpeed","meshTxDrops")properties = ("sn","deviceType","pid","lng","lat","alt","geoHash","mapLevel","lastHeard","status","conf igInSync","runningFirmwareVersion","hardwareId","vid","certSN","certL","certO","certCN","c ertST","certOU","certC")devices = []result = cl.service.searchDevices('deviceType:cgmesh','','',10,0)if result.queryStatus == "SUCCEEDED":for d in result.devices:devices.append(d.eid)print devicesif len(devices) != 0:fn = "./cgmeshDevDetails.csv"f = open(fn, 'w')#fetch device detailsprint >>f, "eid,configGroup,firmwareGroup,",for p in properties:print >>f, p+",",for m in metrics:print >>f, m+",",print >>f, "interfaces"for d in devices:result = cl.service.getDeviceDetails('',d,'',0,0)#print resultif result.queryStatus == "SUCCEEDED":print >>f, d+","+result.deviceDetails[0].configGroup+","+result.deviceDetails[0].firmwareGroup+",",for p in properties:for j in result.deviceDetails[0].properties.entry:if j.key == p:try:j.valueexcept:passelse:if j.value != "null":print >>f, j.value,print >>f, ",",for m in metrics:for j in result.deviceDetails[0].metrics.entry:if j.key == m:print >>f, j.value,print >>f, ",",for i in result.deviceDetails[0].interfaces:print >>f, i.name,print >>f, ""# fetch FAN router detailsmetrics = ["uptime","chassisTemp","ethernetTxSpeed","ethernetRxSpeed","ethernetTxDrops","meshEndpoin tCount","meshRxSpeed","meshTxSpeed"]properties = ["sn","deviceType","pid","lng","lat","alt","geoHash","mapLevel","lastHeard","status","door Status","bbuPresent","numBBU","configInSync","runningFirmwareVersion","hardwareId","vid"," certSN","certL","certO","certCN","certST","certOU","certC","meshPanidConfig","meshLinkKeyR efresh","meshLinkKeyExpiration","meshPrefixConfig","powerSource","ip","hostname","domainNa me","meshPrefixLengthConfig","meshPrefix","meshPrefixLength","meshAddressConfig","meshAddr ess"]devices = []result = cl.service.searchDevices('deviceType:cgr1000','','',10,0)#print resultif result.queryStatus == "SUCCEEDED":for d in result.devices:devices.append(d.eid)print devicesif len(devices) != 0:fn = "./cgr1kDevDetails.csv"f = open(fn, 'w')#fetch device detailsprint >>f, "eid,configGroup,firmwareGroup,",for p in properties:print >>f, p+",",for m in metrics:print >>f, m+",",print >>f, "interfaces"for d in devices:result = cl.service.getDeviceDetails('',d,'',0,0)#print resultif result.queryStatus == "SUCCEEDED":print >>f, d+","+result.deviceDetails[0].configGroup+","+result.deviceDetails[0].firmwareGroup+",",for p in properties:for j in result.deviceDetails[0].properties.entry:if j.key == p:try:j.valueexcept:passelse:if j.value != "null":print >>f, j.value,print >>f, ",",for m in metrics:for j in result.deviceDetails[0].metrics.entry:if j.key == m:print >>f, j.value,print >>f, ",",for i in result.deviceDetails[0].interfaces:print >>f, i.name,print >>f, ""Using the search-client Script
The CG-NMS distribution provides a Search API client (search-client script) contained in the package cgms-tools-version.x86_64.rpm. The client itself is wrapped by a shell script (/opt/cgms-tools/bin/search-client), which you can use directly for communicating with the CG-NMS API.
[root@localhost bin]# ./search-client
usage:./search-client device <endpoint URL> <query> <field names> <count> <offset>./search-client device <endpoint URL> <query Id>./search-client deviceDetail <endpoint URL> <query> <count> <offset>./search-client deviceDetail <endpoint URL> <query Id>./search-client deviceDetail <endpoint URL> <device eid list>./search-client group <endpoint URL> <group type>./search-client metricHistory <endpoint URL> <query> <startTime> <endTime> <field names> <rollupInterval> <rollupFunction> <count> <offset>./search-client metricHistory <endpoint URL> <queryId>./search-client metricHistory <endpoint URL> <device eid list> <startTime> <endTime> <field names> <rollupInterval> <rollupFunction>The search-client script defines four commands:
Search API Methods
searchDevices
The searchDevices call enables the client to provide a search query string (using the query language) and a list of device details (properties or metrics) to return.
searchDevices SOAP XML Request Format
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sear="http://search.nbapi.cgms.cisco.com/"><soapenv:Header/><soapenv:Body><sear:searchDevices><!--Optional:--><query>deviceType:cgr1000</query><!--Zero or more repetitions:--><fieldNames></fieldNames><!--Optional:--><queryId></queryId><!--Optional:--><count>10</count><!--Optional:--><offset>0</offset></sear:searchDevices> </soapenv:Body></soapenv:Envelope>Parameters
Table 7-2 searchDevices Parameters
Parameter Type Descriptionquery
string
Search query string.
fieldNames
string
List of property or metric names to return in the results for each device. For more information about those properties and metrics, see
Property Field Names for All Devices, page 1-4 andqueryId
string
Query ID returned by the call.
If available results for the query is more than count, the caller can use the returned queryId to call the same API repeatedly. When queryId is provided, all other parameters are ignored.
count
integer
Number of results to be retrieved.
offset
integer
Position of the first result.
Results
The searchDevices call returns a list of device details, described in Table 7-3.
Table 7-3 searchDevices Results
Name Type Descriptioneid
string
Device ID.
fields
map<String.String>
A list of fieldname-value pairs.
key
long
A long value associated with the device.
getGroups
The getGroups call enables the client to retrieve the following:
•
List of groups for a specific group type within the system
•
Current number of members in each group
•
Properties assigned to the group
Group types can be one of the following strings:
•
deviceType
•
status
•
label
•
subnet
•
config
•
firmware
getGroups SOAP XML Request Format
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sear="http://search.nbapi.cgms.cisco.com/"><soapenv:Header/><soapenv:Body><sear:getGroups><!--Optional:--><groupType>devicetype</groupType></sear:getGroups></soapenv:Body></soapenv:Envelope>Parameters
Results
This method returns a list of groups, described in Table 7-5.
Table 7-5 GetGroups Results
Name Type Descriptionname
string
Name of the group.
memberCount
integer
Number of devices in the group.
properties
Map<String, String>
Property name-value pairs.
getDeviceDetails
The getDeviceDetails call enables the client to retrieve the following:
•
All device properties
•
Current metrics
•
Assigned labels
•
Interface information
•
Addressing information
•
Routing information about a device
Devices can be retrieved by specifying a query or by specifying a list.
getDeviceDetails SOAP XML Request Format
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sear="http://search.nbapi.cgms.cisco.com/"><soapenv:Header/><soapenv:Body><sear:getDeviceDetails><!--Optional:--><query>far_test2</query><!--Zero or more repetitions:--><deviceIds>+JSJ1522003G</deviceIds><!--Optional:--><queryId></queryId><!--Optional:--><count>5</count><!--Optional:--><offset>0</offset></sear:getDeviceDetails></soapenv:Body></soapenv:Envelope>Parameters
Results
This interface returns a list of device details, described in Table 7-7.
Note
For detailed information on Interface, Route, Asset, and Address, see the Cisco 1000 Series Connected Grid Router software configuration guides, at www.cisco.com/go/cgr1000-docs.
getMetricHistory
The getMetricHistory call enables the client to retrieve the historical metric values saved in the CG-NMS database. In the call, you can specify:
•
Single device
•
List of devices
•
Query that returns devices
You can also specify a rollup interval from the following:
•
none
•
hour
•
day
And a rollup function from the following:
•
avg
•
max
•
min
getMetricHistory SOAP XML Request Format
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sear="http://search.nbapi.cgms.cisco.com/"><soapenv:Header/><soapenv:Body><sear:getMetricHistory><!--Optional:--><query>deviceType:cgr1000</query><!--Zero or more repetitions:--><deviceIds>far_test3</deviceIds><!--Optional:--><startTime>null</startTime><!--Optional:--><endTime>null</endTime><!--Zero or more repetitions:--><metricIds>1</metricIds><!--Optional:--><rollupInterval>none</rollupInterval><!--Optional:--><rollupFunction>min</rollupFunction><!--Optional:--><queryId></queryId><!--Optional:--><count>5</count><!--Optional:--><offset>1</offset></sear:getMetricHistory></soapenv:Body></soapenv:Envelope>Parameters
Results
This interface returns a list of metric values, as defined in Table 7-9.