Highlighted
Absent Member.. Absent Member..
Absent Member..
3349 views

NNMi API - using Python and WS-I filters

Hello,

 

We are using python frequently in our compagny to automate certain processes and came  across the following article describing a way to interface with the NNMi 9 WS-I API:

 

http://24x7x0.wordpress.com/2011/07/07/use-python-to-talk-to-nnmi/

 

The example works great only I'm struggling with using filters. I would like to for example query the API to return a node with a certain name. I read up on the WS-I Filter Definition chapter in the Developers Toolkit guide but couldn't figure out the proper syntax for the filter.

 

I've tried several things e.g:

 

"name=HOST01"

"EXISTS {name=HOST01"}"

"node.name=HOST01"

"{customAttribute.name=name}{customAttribute.value=HOST01}"

 

but unfortunately it seems anything I use just dumps the full node table (actually only 1000 records as maxObjects='1000' by default which i also don't know how to change 🙂 )

 

The developers guide does show a few examples in Java but I'm not sure how to translate it.

 

(Java example)

// EXIST clause

Condition condition1 = new Condition();

condition1.setName("customAttribute.name");

condition1.setOperator(Operator.EQ);

condition1.setValue("test");

Condition condition2 = new Condition();

condition2.setName("customAttribute.value");

condition2.setOperator(Operator.EQ);

condition2.setValue("0");

Filter[] subFilters = new Filter[]{condition1, condition2};

Filter existFilter = new Expression();

existFilter.setOperator(BooleanOperator.AND);

existFilter.setSubFilters(subFilters);

Filter[] existSubFilters = new Filter[]{existFilter};

Expression exist = new Expression();

exist.setOperator(BooleanOperator.EXISTS);

exist.setSubFilters(existSubFilters);

 

 

Kind regards

 

 

 

Tags (2)
0 Likes
5 Replies
Highlighted
Absent Member.. Absent Member..
Absent Member..

Re: NNMi API - using Python and WS-I filters

Hi Guys,

 

After some browsing and fiddling around I finally managed to create a proper filter. It came down to my lack of understanding of using SOAP i guess.. Anyway here's a quick python function to get a specific host from NNMi using python and suds:

 

 

from suds.client import Client

from suds.transport.http import HttpAuthenticated

 

user = "myNNMiUser"

pw = "myNNMipw"

 

= HttpAuthenticated(username=user, password=pw)

url_node = 'http://nms.yourdomain.com/NodeBeanService/NodeBean?wsdl'

 

def getNode(hostname):

  client = Client(url_node, transport=t)

 

  filter = client.factory.create('ns3:condition')

  filter.name="name"

  filter.operator="EQ"

  filter.value=hostname

 

  # print filter

  node = client.service.getNodes(filter)

 

  if node:

    for i in node.item[:]:

      print str(i)

  else:

    print "No node with hostname " + hostname + " found.."

 

 

Cheers,..

Tags (2)
Highlighted
Absent Member.. Absent Member..
Absent Member..

Re: NNMi API - using Python and WS-I filters

To expand on the filter answer just a bit, here is the python way that seems to work with filter expressions. Here I am looking for registered incidents related to interface down on a particular system:

filter1 = client.factory.create('ns2:condition')
filter1.name="lifecycleState"
filter1.operator="EQ"
filter1.value="com.hp.nms.incident.lifecycle.Registered"

filter2 = client.factory.create('ns2:condition')
filter2.name="name"
filter2.operator="EQ"
filter2.value="InterfaceDown"

filter3 = client.factory.create('ns2:condition')
filter3.name="sourceNodeName"
filter3.operator="EQ"
filter3.value="my-node"

filter = client.factory.create('ns2:expression')
filter.operator = "AND"
filter.subFilters = [filter1, filter2, filter3]

incidents = client.service.getIncidents(filter)

I'm new to this, so let me know if there is a better way to do it.

 

0 Likes
Highlighted
New Member.

Re: NNMi API - using Python and WS-I filters

Changing the maxObjects:

filter = client.factory.create('ns3:constraint')
filter.name="maxObjects"
filter.value=2000
allNodes = client.service.getNodes(filter)

0 Likes
Highlighted
Contributor.. Contributor..
Contributor..

Re: NNMi API - using Python and WS-I filters

Hi,

When I try this I get an error. Here is the code:

def getNode():

client = Client(url_node, transport=t)

filter = client.factory.create('ns3:condition')

filter.name = "longName"
filter.operator.value="EQ"
filter.value= "GAS-DATA-SHB"
node = client.service.getNodes(filter)
cnt = client.service.getNodeCount("*")
print(cnt)
print(len(node))
if node:
for i in node.item[:]:
print(str(i))

else:
return False

 

And here is the output:

Traceback (most recent call last):
File "nnmi_puller.py", line 40, in <module>
main()
File "nnmi_puller.py", line 37, in main
print(getNode())# kotc-wlc-prim
File "nnmi_puller.py", line 25, in getNode
node = client.service.getNodes(filter)
File "C:\Users\c9091\AppData\Local\Programs\Python\Python38-32\lib\site-packages\suds\client.py", line 559, in __call__
return client.invoke(args, kwargs)
File "C:\Users\c9091\AppData\Local\Programs\Python\Python38-32\lib\site-packages\suds\client.py", line 612, in invoke
soapenv = binding.get_message(self.method, args, kwargs)
File "C:\Users\c9091\AppData\Local\Programs\Python\Python38-32\lib\site-packages\suds\bindings\binding.py", line 120, in get_message
content = self.bodycontent(method, args, kwargs)
File "C:\Users\c9091\AppData\Local\Programs\Python\Python38-32\lib\site-packages\suds\bindings\rpc.py", line 56, in bodycontent
p = self.mkparam(method, pd, value)
File "C:\Users\c9091\AppData\Local\Programs\Python\Python38-32\lib\site-packages\suds\bindings\binding.py", line 289, in mkparam
return marshaller.process(content)
File "C:\Users\c9091\AppData\Local\Programs\Python\Python38-32\lib\site-packages\suds\mx\core.py", line 60, in process
self.append(document, content)
File "C:\Users\c9091\AppData\Local\Programs\Python\Python38-32\lib\site-packages\suds\mx\core.py", line 73, in append
self.appender.append(parent, content)
File "C:\Users\c9091\AppData\Local\Programs\Python\Python38-32\lib\site-packages\suds\mx\appender.py", line 102, in append
appender.append(parent, content)
File "C:\Users\c9091\AppData\Local\Programs\Python\Python38-32\lib\site-packages\suds\mx\appender.py", line 243, in append
Appender.append(self, child, cont)
File "C:\Users\c9091\AppData\Local\Programs\Python\Python38-32\lib\site-packages\suds\mx\appender.py", line 182, in append
self.marshaller.append(parent, content)
File "C:\Users\c9091\AppData\Local\Programs\Python\Python38-32\lib\site-packages\suds\mx\core.py", line 72, in append
if self.start(content):
File "C:\Users\c9091\AppData\Local\Programs\Python\Python38-32\lib\site-packages\suds\mx\literal.py", line 87, in start
raise TypeNotFound(content.tag)
suds.TypeNotFound: Type not found: 'name'

 

Any idea?

0 Likes
Highlighted
Contributor.. Contributor..
Contributor..

Re: NNMi API - using Python and WS-I filters

FWIW, When I switched to Python2 script worked without issue.

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.