6 Replies Latest reply on Aug 10, 2017 12:05 PM by Steven Klassen

    Python SDK - Unmanage/Remanage a node

    centirc-jim

      Hi everyone,

       

      We have a process for deploying code to web servers.  When we put the web server into 'maintenance' mode to do the code deploy, we'd also like to unmanage the node in SolarWinds as well.  I'm not sure if I've run into a bug or if I'm simply doing something wrong.  Anyway I hope some of the great folks on the formus can see what's going on.

       

      I'm using v1.10 of the SDK and Python 2.7.something

       

      As you'll see in the end, the error I'm getting is, "TypeError: unbound method _json_serial() must be called with SwisClient instance as first argument (got datetime instance instead)".

       

       

      Here's the very basic script, it's mainly from the SwisClient.py example

      import requests

      import json

      from getpass import getpass

      from datetime import datetime, timedelta

      import SwisClient

       

      npm_server = "myOrionServer.local"

      username = raw_input("Username: ")

      password = getpass("Password: ")

      swis = SwisClient(npm_server,username,password)

      node = 'myNode.local'

      results = swis.query("SELECT NodeID from Orion.Nodes WHERE NodeName='" + node + "'")

      interfaceId = results["results"][0]["NodeID"]

      netObjectId = "N:{}".format(interfaceId)

      now = datetime.utcnow()

      tomorrow = now + timedelta(days=1)

      swis.invoke("Orion.Nodes", "Unmanage", netObjectId, now, tomorrow, False)

       

      Here's the obvsucated values for results, interfaceId, netObjectId, now, tomorrow:

       

      In [261]: results

      Out[261]: {u'results': [{u'NodeID': 4526}]}

       

      In [262]: interfaceId

      Out[262]: 4526

       

      In [263]: netObjectId

      Out[263]: 'N:4526'

       

      In [265]: now

      Out[265]: datetime.datetime(2015, 6, 10, 20, 34, 46, 345759)

       

      In [266]: tomorrow

      Out[266]: datetime.datetime(2015, 6, 11, 20, 34, 46, 345759)

       

      -=-=-=-=-=-  So everything looks good so far -=-=-=-=-=-

       

      Here's where I get an error

      In [267]: swis.invoke("Orion.Nodes", "Unmanage", netObjectId, now, tomorrow, False)

      ---------------------------------------------------------------------------

      TypeError                                 Traceback (most recent call last)

      <ipython-input-267-70d4084b09c9> in <module>()

      ----> 1 swis.invoke("Orion.Nodes", "Unmanage", netObjectId, now, tomorrow, False)

       

      <ipython-input-5-0266593e63b0> in invoke(self, entity, verb, *args)

            8

            9     def invoke(self, entity, verb, *args):

      ---> 10         return self._req("POST", "Invoke/%s/%s" % (entity, verb), args).json()

           11

           12     def create(self, entity, **properties):

       

      <ipython-input-5-0266593e63b0> in _req(self, method, frag, data)

           31     def _req(self, method, frag, data=None):

           32         return requests.request(method, self.url + frag,

      ---> 33             data=json.dumps(data, default=SwisClient._json_serial),

           34             verify=False,

           35             auth=self.credentials,

       

      /usr/lib/python2.7/json/__init__.pyc in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, encoding, default, sort_keys, **kw)

          248         check_circular=check_circular, allow_nan=allow_nan, indent=indent,

          249         separators=separators, encoding=encoding, default=default,

      --> 250         sort_keys=sort_keys, **kw).encode(obj)

          251

          252

       

      /usr/lib/python2.7/json/encoder.pyc in encode(self, o)

          205         # exceptions aren't as detailed.  The list call should be roughly

          206         # equivalent to the PySequence_Fast that ''.join() would do.

      --> 207         chunks = self.iterencode(o, _one_shot=True)

          208         if not isinstance(chunks, (list, tuple)):

          209             chunks = list(chunks)

       

      /usr/lib/python2.7/json/encoder.pyc in iterencode(self, o, _one_shot)

          268                 self.key_separator, self.item_separator, self.sort_keys,

          269                 self.skipkeys, _one_shot)

      --> 270         return _iterencode(o, 0)

          271

          272 def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,

       

      TypeError: unbound method _json_serial() must be called with SwisClient instance as first argument (got datetime instance instead)

       

       

       

      Sooooo, I'm not awesome at Python, but it seems like I maybe should have the Node's Uri in there somewhere?  I don't know, this is my first time writing for SolarWinds in Python, I'd much prefer to use PowerShell, but the 'calling server' is Linux. 

       

       

      -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

      On a side note, a dictionary containing a list of dictionaries.. that's just plain evil.. I still can't wrap my head around the 'pythonic' way to get at the key, values pairs for the dictionary contained in the list.

       

       

       

       

      Thanks,

      Jim