· 5 years ago · May 17, 2020, 04:02 AM
1import re
2import requests
3import getpass
4import colorama
5from pprint import pprint
6from colorama import Fore, Back
7# Must install OrionSDK to use this
8# pip3 install orionsdk
9from orionsdk import SwisClient
10
11def main():
12 colorama.init(autoreset=True)
13 # Gather info from user on SolarWinds login information
14 npm_server = input('SolarWinds Server IP: ')
15 username = input('SolarWinds Username: ')
16 password = getpass.getpass(prompt = 'Solarwinds Password: ')
17 ip_address = input('IP Address of Node to Add: ')
18 input_page_day_time = input('Page Day Time? Enter True or False: ')
19 input_page_night_time = input('Page Night Time? Enter True or False: ')
20 # Apply to variable
21 swis = SwisClient(npm_server, username, password)
22 # Prompt user for which interfaces should be monitored on the node
23 # This can be improved
24 # Possibly add for loop here, or reference a file.
25 interfaces = input('Node Interfaces to Monitor (Separated by a space, command and space (ex. 0/9 , 0/10)): ').split(' , ')
26 #print("Adding SNMPv3 Node...")
27 props = {
28 'IPAddress': ip_address,
29 'EngineID': 2,
30 'ObjectSubType': 'SNMP',
31 'SNMPVersion': 3,
32 'SNMPV3Username': 'snmpwrite',
33 'SNMPV3PrivMethod': 'AES128',
34 'SNMPV3PrivKeyIsPwd': True,
35 'SNMPV3PrivKey': 'removed',
36 'SNMPV3AuthMethod': 'SHA1',
37 'SNMPV3AuthKeyIsPwd': True,
38 'SNMPV3AuthKey': 'removed',
39 }
40 print(Fore.CYAN + "Adding node {}... ".format(props['IPAddress']), end="")
41 results = swis.create('Orion.Nodes', **props)
42 print("DONE!")
43 # extract the nodeID from the result
44 nodeid = re.search(r'(\d+)$', results).group(0)
45 print(Fore.RED + "Node ID is: " + str(nodeid))
46 pollers_enabled = {
47 'N.Status.ICMP.Native': True,
48 'N.Status.SNMP.Native': False,
49 'N.ResponseTime.ICMP.Native': True,
50 'N.ResponseTime.SNMP.Native': False,
51 'N.Details.SNMP.Generic': True,
52 'N.Uptime.SNMP.Generic': True,
53 'N.Cpu.SNMP.HrProcessorLoad': True,
54 'N.Memory.SNMP.NetSnmpReal': True,
55 'N.AssetInventory.Snmp.Generic': True,
56 'N.Routing.SNMP.Ipv4CidrRoutingTable': True,
57 'N.RoutingNeighbor.SNMP.BGP': True,
58 'N.RoutingNeighbor.SNMP.OSPF': True,
59 'N.RoutingNeighbor.SNMP.EIGRP': True,
60 'N.Topology_Layer2.SNMP.Dot1dTpFdb': True,
61 'N.Topology_Layer3.SNMP.ipNetToMedia': True,
62 'N.EnergyWise.SNMP.Cisco': True
63 }
64 pollers = []
65 for k in pollers_enabled:
66 pollers.append(
67 {
68 'PollerType': k,
69 'NetObject': 'N:' + nodeid,
70 'NetObjectType': 'N',
71 'NetObjectID': nodeid,
72 'Enabled': pollers_enabled[k]
73 }
74 )
75 for poller in pollers:
76 print(Fore.CYAN + " Adding poller type: {} with status {}... ".format(poller['PollerType'], poller['Enabled']), end="")
77 response = swis.create('Orion.Pollers', **poller)
78 print(Fore.GREEN + "DONE!")
79
80 # Add interfaces to be monitored
81 print(Fore.CYAN + 'Discovering/Adding Interfaces...')
82 results = swis.invoke('Orion.NPM.Interfaces', 'DiscoverInterfacesOnNode', str(nodeid))
83 # use the results['DiscoveredInterfaces'] for all interfaces
84 # or get a subset of interfaces using a comprehension like below
85 ###
86 # This section takes the input value for 'interfaces' and splits it on the comma
87 # Then uses this as a list for this to iterate through and apply the interfaces
88 eth_only = [
89 x for x
90 in results['DiscoveredInterfaces']
91 if x['Caption'].endswith(tuple(interfaces))
92 ]
93 pprint(eth_only)
94 results2 = swis.invoke(
95 'Orion.NPM.Interfaces',
96 'AddInterfacesOnNode',
97 nodeid, # use a valid nodeID! - References the nodeid var defined in the first part of the script
98 eth_only,
99 'AddDefaultPollers')
100 pprint(results2)
101 print(Fore.CYAN + 'Adding Hardware Health Sensor')
102 # Sets hardware health to true with "N:{}".format(nodeid) - otherwise this would be disabled
103 results = swis.invoke('Orion.HardwareHealth.HardwareInfoBase', 'EnableHardwareHealth', "N:{}".format(nodeid), 9)
104
105 # Add assignment group
106 print(Fore.CYAN + 'Setting Assignment Group...')
107 query_node = swis.query("SELECT Uri FROM Orion.Nodes WHERE NodeID=" + str(nodeid))
108 assignment_group = query_node['results'][0]['Uri']
109 swis.update(assignment_group + '/CustomProperties', AssignmentGroup = "ITSM Network Support")
110 assign_group_result = swis.read(assignment_group + '/CustomProperties')
111 pprint(assign_group_result)
112
113 # Begin adding custom properties for PagerDuty
114
115 # Set pagerduty API key
116 print(Fore.CYAN + 'Adding PagerDuty API Key...')
117 query_node = swis.query("SELECT Uri FROM Orion.Nodes WHERE NodeID=" +str(nodeid))
118 pager_api_key = query_node['results'][0]['Uri']
119 swis.update(pager_api_key + '/CustomProperties', PagerDuty_Int_Key = "ee6a3b3de0f34a8f83d50dcd90f6f5f2")
120 pager_api_result = swis.read(pager_api_key + '/CustomProperties')
121 pprint(pager_api_result)
122
123 # Set page day time
124 print(Fore.CYAN + 'Setting Page Day Time Value...')
125 query_node = swis.query("SELECT Uri FROM Orion.Nodes WHERE NodeID=" +str(nodeid))
126 pager_day_time = query_node['results'][0]['Uri']
127 swis.update(pager_day_time + '/CustomProperties', Page_Daytime = str(input_page_day_time))
128 pager_day_time_result = swis.read(pager_day_time + '/CustomProperties')
129 pprint(pager_day_time_result)
130
131 # Set page night time
132 print(Fore.CYAN + 'Setting Page Night Time Value...')
133 query_node = swis.query("SELECT Uri FROM Orion.Nodes WHERE NodeID=" +str(nodeid))
134 pager_night_time = query_node['results'][0]['Uri']
135 swis.update(pager_night_time + '/CustomProperties', Page_Nighttime = str(input_page_night_time))
136 pager_night_time_result = swis.read(pager_night_time + '/CustomProperties')
137 pprint(pager_night_time_result)
138
139 # Add to NCM
140 print(Fore.CYAN + 'Adding Node to NCM...')
141 add_ncm = swis.invoke('Cirrus.Nodes', 'AddNodeToNCM', str(nodeid))
142 pprint(add_ncm)
143
144 # End of script
145
146 print(Fore.GREEN + "-" * 80)
147 print(Fore.GREEN + "Done.")
148 print(Fore.GREEN + "-" * 80)
149# Disables HTTPS errors if any
150requests.packages.urllib3.disable_warnings()
151if __name__ == '__main__':