Accessing the n-Central API with Python
The n-Central API with Python
So you wanna access the n-Central API with Python? You’re gonna need even more luck than you had with Powershell!
See the Powershell article here: http://220.127.116.11/2018/09/accessing-n-central-api-powershell/
So maybe you don’t want to install Powershell Core onto a Linux box and want to access the n-Central API with a generalist language like Python. Or, maybe you want to add n-Central information into a cool dashboard like maybe LCARS or something cough cough
There is pretty much no example code anywhere on the interwebs written in Python. When I deep Googled my way into the darkness of the world wide web, I could only find one little snippet from stackoverflow of someone asking for help with their itty bit of code that doesn’t work. It was followed by the person saying they figured it out and only left a vague explanation. It took me many hours, but I was able to decipher the alien tongue known as n-Central’s implementation of SOAP, and I have come to share my revelation and keep it on my website for documentation!
I also had to update the code when n-Central created v2 of the API. This one is tested working as of 2018:
import zeep from zeep import helpers wsdl = 'https://yourncentralpage/dms2/services2/ServerEI2?wsdl' client = zeep.CachingClient(wsdl=wsdl) config_type = client.get_type('ns0:tKeyPair') configcustomer = config_type('customerID','286') configFilter = config_type('NOC_View_Status_Filter','failed') configs = [configcustomer, configFilter] devices = client.service.activeIssuesList(email@example.com', password='yourpassword', settings=configs) for device in devices: print(device)
Alright, so what the hell am I doing?
First off, we’re going to use zeep to interact with the SOAP endpoint. Supposedly, zeep is the most up to date SOAPy thing out there for Python, and it’s what the only example code I could ever find used, so we’re going with it, not suds or whatever else might be out there. Thus, we import zeep.
Then we specify the wsdl file for zeep to speak to with the zeep.CachingClient() method. Simple so far.
Remember from the Powershell lesson the stuff about key pairs? That’s right, in v2 of the API, not only did t_KeyPair turn into tKeyPair, but also the namespace turned from ns1 to ns0. Once again, Solarwinds says fuck you and your old scripts.
So anyway, zeep has no idea what a tKeyPair object is so we help define it by using get_type(). Then we can insert our keys into it. ActiveIssuesList() can take a bunch of key pairs, but in this case, I only want to see failed monitors for a specific customer, so we create two key pairs and add them together into a list. Oh yeah, the list format has changed between requiring a dictionary and a list before, so that’s more Solarwinds fuckery for you.
Alright, we can finally call activeIssuesList() and return the response into the variable devices.
From there, we can print each device that has an issue in it. Easy, right? You can go further into the individual issue by using device[‘issue’], if I remember right.
I probably don’t.