Guest User

Untitled

a guest
Dec 6th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. ## Python Test
  2.  
  3. ---
  4.  
  5. ```py
  6. # python C:\Users\204097\PycharmProjects\NetMonkey\test.py
  7.  
  8. import orionsdk
  9. import requests.packages.urllib3
  10. import getpass
  11. from pprint import pprint
  12. from datetime import datetime, timedelta
  13. import pandas as pd
  14.  
  15. # begin script timer
  16. start_time = datetime.now()
  17. print("\nScript has begun at: \n" + str(start_time))
  18.  
  19.  
  20. def read_file(filename):
  21.  
  22. with open(filename) as f:
  23. return f.readlines()
  24.  
  25.  
  26. def init_orion():
  27. """ Prompts for Orion credentials and returns a SwisClient object.
  28. TODO: Need a better/more resilient way of referencing server cert.
  29. """
  30.  
  31. requests.packages.urllib3.disable_warnings()
  32.  
  33. orion_server = "Solarwinds-Orion"
  34. orion_username = "eehealth\\204097"
  35. orion_password = "Blazeblue666"
  36.  
  37. if not orion_username:
  38. default_username = getpass.getuser()
  39. print(default_username)
  40. orion_username = "eehealth\\" + default_username
  41. if not orion_password:
  42. print("pass required\n")
  43. orion_password = getpass.getpass("Orion password: ")
  44.  
  45. return orionsdk.SwisClient(orion_server, orion_username, orion_password, verify='Solarwinds-Orion.der')
  46.  
  47.  
  48. def unmanage(node, hours):
  49. """ unmanage solarwinds node by user defined hours based on DisplayName
  50. USAGE: python
  51. """
  52.  
  53. swis = init_orion()
  54.  
  55. print("\nUnmanage Test for hostname " + node + "\n")
  56. results = swis.query("SELECT NodeID, DisplayName, IPAddress FROM Orion.Nodes WHERE NodeName='" + str(node) + "'")
  57. print(results)
  58.  
  59. for row in results['results']:
  60. print("{NodeID}, {DisplayName}, {IPAddress}".format(**row))
  61.  
  62. interface_id = results["results"][0]["NodeID"]
  63. print(interface_id)
  64. netobject_id = "N:{}".format(interface_id)
  65. print(netobject_id)
  66. now = datetime.utcnow()
  67. print(now)
  68. duration = now + timedelta(hours=hours)
  69. print(duration)
  70. swis.invoke("Orion.Nodes", "Unmanage", netobject_id, now, duration, False)
  71. print("\n\n" + node + "is now unmanaged for " + str(duration) + "hours")
  72.  
  73.  
  74. def query_test_file(node):
  75. """ query all nodes in
  76. USAGE: python
  77. """
  78.  
  79. swis = init_orion()
  80.  
  81. print("Query Test:")
  82. #results = swis.query("SELECT Uri FROM Orion.Nodes WHERE NodeName='" + str(node) + "'", id=1) # set valid NodeID!
  83. results = swis.query("SELECT Uri FROM Orion.Nodes")
  84. uri = results['results'][0]['Uri']
  85. print(uri)
  86. obj = swis.read(uri)
  87. cust_obj = swis.read(uri + '/CustomProperties')
  88. obj.update(cust_obj)
  89. pprint(obj)
  90. logs_df = pd.DataFrame(obj)
  91. df_list.append(logs_df)
  92. #logs_df.to_csv("{node}_RAW.csv".format(node=node), index_label = "Property", header=[node])
  93.  
  94.  
  95.  
  96. def query_test():
  97. """ query single node
  98. USAGE: python
  99. """
  100.  
  101. swis = init_orion()
  102.  
  103. print("Query Test:")
  104. results = swis.query("SELECT Uri FROM Orion.Nodes")
  105. print(type(results))
  106. print(len(results))
  107. print(len(results['results']))
  108. for row in range(len(results['results'])):
  109. uri = results['results'][row]['Uri']
  110. print(uri)
  111. obj = swis.read(uri)
  112. cust_obj = swis.read(uri + '/CustomProperties')
  113. obj.update(cust_obj)
  114. pprint(obj)
  115. logs_df = pd.DataFrame(obj)
  116. df_list.append(logs_df)
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125. # def add_node():
  126.  
  127. unmanage("RMELROSE", 1)
  128. unmanage("WMELROSE", 1)
  129. unmanage("RMELROSEPARK", 1)
  130. unmanage("RMELROSEPARK", 1)
  131.  
  132.  
  133.  
  134.  
  135. # query_test("WJEWEL34WASH")
  136.  
  137.  
  138. df_list = []
  139. query_test()
  140. final_df = pd.concat(df_list)
  141. final_df.to_csv("solar_RAW.csv", index_label="Property")
  142.  
  143. # End Clock and print time
  144.  
  145. end_time = datetime.now()
  146. total_time = end_time - start_time
  147. print("\nTotal time for script: \n" + str(total_time))
  148. ```
Add Comment
Please, Sign In to add comment