2 Replies Latest reply on May 18, 2016 6:50 AM by e.solonin

    Trap/alert variable transfer to Python script

    e.solonin

      Hello all,

      I got some question with execution of external program via alert\trap action.

      So our test scenario:

      - an trap comes from device to Trap console (for example bfd session down)

      - it triggers rules for this trap, one of this rules is to execute external program.

      - this external program is a python script, which (for simplification of test) changes custom property for interface trap came from.

       

       

      In general case with hardcoded NodeID and Inrerface index it works fine.

      Trap rule points to the path to script: C:\script1bfd.bat

       

      bat file looks like this:

      @echo off

      start "C:\Program Files\Python35\python.exe" C:\custompropertyBFD %*

       

      And python is slightly modified version of sample provided on orionsdk-python/custom_property_update.py at master · solarwinds/orionsdk-python · GitHub

      ===========================================================

      import requests
      from orionsdk import SwisClient
      
      
      
      
      def main():
          npm_server = 'localhost'
          username = 'Admin'
          password = ''
          
      
      
          swis = SwisClient(npm_server, username, password)
          print("Custom Property Update Test:")
          results = swis.query(
              "SELECT Uri FROM Orion.NPM.Interfaces where NodeID =@id and Index = @Index",
              id=2107,Index=33)  
          print (results)
          uri = results['results'][0]['Uri']
      
      
          swis.update(uri + '/CustomProperties',BFD='Down')
          obj = swis.read(uri + '/CustomProperties')
          print (obj)
      
      
         
      
      
      requests.packages.urllib3.disable_warnings()
      
      
      
      
      if __name__ == '__main__':
          main()
      

       

      So with hardcoded NodeID and Interface index it works fine.

       

       

      But in the real world there is no use for hardcoded Node and Interface Index)

      It's necessary to pull this data from trap\alert via variables.

      Node\device can be specified by NodeID or IP address variable and interface index can be specified in this case by trap variable ${vbData5} , which corresponds to IntIndex in trap message.

      All of this variables (for test) were inserted in body of email notification for trap\alert and all this variables return correct values.

       

       

      So here comes the question how to transfer this variables to a python script and how to call it in the body of a script.

       

      I tried something like this:

      path to the script: C:\script1bfd.bat ${NodeID}  or like this "C:\Program Files\Python35\python.exe" C:\custompropertyBFD.py ${NodeID}

      and script body:

      import requests
      from orionsdk import SwisClient
      
      
      
      
      def main():
          npm_server = 'localhost'
          username = 'Admin'
          password = ''
          nodeid = ${NodeID} # or like this nodeid = {NodeID}
      
      
          swis = SwisClient(npm_server, username, password)
          print("Custom Property Update Test:")
          results = swis.query(
              "SELECT Uri FROM Orion.NPM.Interfaces where NodeID =@id and Index = @Index",
              id=nodeid ,Index=33)  
          print (results)
          uri = results['results'][0]['Uri']
      
      
          swis.update(uri + '/CustomProperties',BFD='Down')
          obj = swis.read(uri + '/CustomProperties')
          print (obj)
      
      
         
      
      
      requests.packages.urllib3.disable_warnings()
      
      
      
      
      if __name__ == '__main__':
          main()
      

       

      But it doesnt work this way.

      My knowledge in python is not very deep, so I assume I do something wrong.

      So maybe someone knows correct syntax to transfer NPM variable to python script and syntax to call this variable in the body of the script, or can point me the right sources of information.

       

       

      Thanks in advance for your help and replies.