SolarWinds & Python – Extracting Standard Data from the Orion.Nodes table via the SolarWinds Information Service (SWIS) API

By 22nd May 2019SolarWinds, SWIS, SWQL, Uncategorised

The SolarWinds Information Service (SWIS) is a data access layer for the Orion product family that provides a hybrid of object-oriented and relational features. It has its own SQL-like language called SolarWinds Query Language (SWQL).

The purpose of this post if demonstrate the data that can be extracted using SWIS and SWQL with the orionsdk for Python, as a quick and easy way to obtain granular data on your estate.

Solarwinds Orion SDK Schema – http://solarwinds.github.io/OrionSDK/schema/index.html

Full Code
 
  1. __author__ = "Mitch Dawson"
  2. __contact__ = "info@ucdevops.com"
  3. __copyright__ = "Copyright (C) 2017 - 2021 Ucdevops All Rights Reserved"
  4. __license__ = "GPLv3"
  5. """
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see http://www.gnu.org/licenses.
  16. """
  17. import requests
  18. from orionsdk import SwisClient
  19. from requests.packages.urllib3.exceptions import InsecureRequestWarning
  20. import csv
  21. # Disable Insecure HTTPS Certificate Warnings.
  22. requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
  23. # Define the Solarwinds Server Host.
  24. server = "solarwindsHost"
  25. # Define the API User Credential.
  26. user = "apiuser"
  27. # Define the API User Password Credential.
  28. pwd = "apipassword"
  29. # Build our SWQl Query.
  30. cisco_nodes_query = """
  31. SELECT
  32. n.Vendor,n.MachineType, n.NodeDescription,
  33. n.IPAddress, n.Caption, n.Status
  34. FROM Orion.Nodes n
  35. WHERE n.Vendor = 'Cisco'
  36. ORDER BY n.IPAddress
  37. """
  38. def query_nodes(query, file_name):
  39.     # Create our SwisClient Object.
  40.     swis = SwisClient(server, user, pwd)
  41.     # Initiate our Query and store the results.
  42.     node_results = swis.query(query)["results"]
  43.     # Create a file object.
  44.     file = open(file_name + ".csv", "w", newline='')
  45.     # Create our CSV Fields.
  46.     fields = node_results[0].keys()
  47.     # Create our CSV.Dictwriter Object.
  48.     writer = csv.DictWriter(file, fieldnames=fields)
  49.     # Write the headers to the csv file.
  50.     writer.writeheader()
  51.     # Write the results to the csv file.
  52.     writer.writerows(node_results)
  53.     # Optionally Print out the results to the terminal.
  54.     for node in node_results:
  55.         print("*" * 100)
  56.         for k, v in node.items():
  57.             print(k, "==", v)
  58. query_nodes(cisco_nodes_query, "cisco_nodes")



Example Output

Output
 
  1. ****************************************************************************************************
  2. Vendor == Cisco
  3. MachineType == Cisco 4431 ISR
  4. NodeDescription == Cisco IOS Software, ISR Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 15.4(3)S4, RELEASE SOFTWARE (fc3)
  5. Technical Support: http://www.cisco.com/techsupport
  6. Copyright (c) 1986-2015 by Cisco Systems, Inc.
  7. Compiled Mon 05-Oct-15 11:24 by mcpre
  8. IPAddress == 10.10.10.1
  9. Caption == dr-ir4431.net
  10. Status == 1
Mitch

Author Mitch

I am an independent IT Consultant with specialities in Software Development, Enterprise Unified Communication, Network and Security platforms. In addition to my day to day work, I develop bespoke applications and I hope that through ucdevops.com I can build relationships with clients, business partners and fellow engineers by providing solutions to complex problems through the use of programming.

More posts by Mitch
"