Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import requests
import json
from getpass import getpass
"""
A more friendly, bug-fixed version of the Python sample included with
Solarwinds SDK v1.8
Make sure to set a valid nodeID in line 50 before using!
"""
class SwisClient:
def __init__(self, hostname, username, password):
self.url = "https://%s:17778/SolarWinds/InformationService/v3/Json/" % (hostname)
self.credentials = (username, password)
def query(self, query, **params):
return self._req("POST", "Query", {'query': query, 'parameters': params}).json()
def invoke(self, entity, verb, *args):
return self._req("POST", "Invoke/%s/%s" % (entity, verb), args).json()
def create(self, entity, **properties):
return self._req("POST", "Create/" + entity, properties).json()
def read(self, uri):
return self._req("GET", uri).json()
def update(self, uri, **properties):
self._req("POST", uri, properties)
def delete(self, uri):
self._req("DELETE", uri)
def _req(self, method, frag, data=None):
return requests.request(method, self.url + frag,
data=json.dumps(data),
verify=False,
auth=self.credentials,
headers={'Content-Type': 'application/json'})
def samplecode(npm_server,username,password):
swis = SwisClient(npm_server,username,password)
print "Invoke Test:"
aliases = swis.invoke("Metadata.Entity", "GetAliases", "SELECT B.Caption FROM Orion.Nodes B")
print aliases
print "Query Test:"
results = swis.query("SELECT Uri FROM Orion.Nodes WHERE NodeID=@id", id=245) # set valid NodeID!
uri = results['results'][0]['Uri']
print uri
print "Custom Property Update Test:"
swis.update(uri + "/CustomProperties", city="Austin")
obj = swis.read(uri + "/CustomProperties")
print obj
pollerUri = swis.create("Orion.Pollers", PollerType="just testing",
NetObject="N:" + str(obj["NodeID"]), NetObjectType="N", NetObjectID=obj["NodeID"])
print pollerUri
print "Deleting Custom Property...."
swis.delete(pollerUri)
def main():
npm_server = raw_input("IP address of NPM Server: ")
username = raw_input("Username: ")
password = getpass("Password: ")
samplecode(npm_server,username,password)
if __name__ == "__main__":
main()
@leinhard

This comment has been minimized.

Copy link

commented Apr 16, 2014

I am honor to meet you,
Really amazing, how can you do with python language so expert.

From Thwack
http://thwack.solarwinds.com/thread/64457
tdanner introduce me to here :-)

@leinhard

This comment has been minimized.

Copy link

commented Apr 16, 2014

It is working well :-)

if name == "main":
main()

IP address of NPM Server: prugio
Username: admin
Warning: Password input may be echoed.
Password:
Invoke Test:
{u'B': u'Orion.Nodes'}
Query Test:
swis://PRUGIO./Orion/Orion.Nodes/NodeID=2
Custom Property Update Test:
{u'City': u'Austin', u'DisplayName': None, u'Description': None, u'NodeID': 2, u'Comments': None, u'Uri': None, u'Department': None, u'InstanceType': u'Orion.NodesCustomProperties'}
swis://PRUGIO./Orion/Orion.Pollers/PollerID=58
Deleting Custom Property....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.