|
1
|
|
|
# Licensed to the StackStorm, Inc ('StackStorm') under one or more |
|
2
|
|
|
# contributor license agreements. See the NOTICE file distributed with |
|
3
|
|
|
# this work for additional information regarding copyright ownership. |
|
4
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0 |
|
5
|
|
|
# (the "License"); you may not use this file except in compliance with |
|
6
|
|
|
# the License. You may obtain a copy of the License at |
|
7
|
|
|
# |
|
8
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
|
9
|
|
|
# |
|
10
|
|
|
# Unless required by applicable law or agreed to in writing, software |
|
11
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
|
12
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13
|
|
|
# See the License for the specific language governing permissions and |
|
14
|
|
|
# limitations under the License. |
|
15
|
|
|
|
|
16
|
|
|
import time |
|
17
|
|
|
|
|
18
|
|
|
from st2actions.runners.pythonrunner import Action |
|
19
|
|
|
from orionsdk import SwisClient |
|
20
|
|
|
|
|
21
|
|
|
from lib.node import OrionNode |
|
22
|
|
|
from lib.utils import send_user_error, is_ip |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
class OrionBaseAction(Action): |
|
26
|
|
|
def __init__(self, config): |
|
27
|
|
|
super(OrionBaseAction, self).__init__(config) |
|
28
|
|
|
|
|
29
|
|
|
self.client = None |
|
30
|
|
|
|
|
31
|
|
|
if "orion" not in self.config: |
|
32
|
|
|
raise ValueError("Orion host details not in the config.yaml") |
|
33
|
|
|
|
|
34
|
|
|
def connect(self, platform): |
|
35
|
|
|
""" |
|
36
|
|
|
Connect to an Orion platform from the packs config.yaml. |
|
37
|
|
|
""" |
|
38
|
|
|
if platform is None: |
|
39
|
|
|
try: |
|
40
|
|
|
platform = self.config['defaults']['platform'] |
|
41
|
|
|
except IndexError: |
|
42
|
|
|
send_user_error("No default Orion platform.") |
|
43
|
|
|
raise ValueError("No default Orion platform.") |
|
44
|
|
|
|
|
45
|
|
|
self.logger.debug("Connecting to Orion platform: {}".format(platform)) |
|
46
|
|
|
|
|
47
|
|
|
try: |
|
48
|
|
|
self.client = SwisClient( |
|
49
|
|
|
self.config['orion'][platform]['host'], |
|
50
|
|
|
self.config['orion'][platform]['user'], |
|
51
|
|
|
self.config['orion'][platform]['password']) |
|
52
|
|
|
except KeyError: |
|
53
|
|
|
raise ValueError("Orion host details not in the config.yaml") |
|
54
|
|
|
|
|
55
|
|
|
return platform |
|
56
|
|
|
|
|
57
|
|
|
def get_node(self, node): |
|
58
|
|
|
""" |
|
59
|
|
|
Get an OrionNode object |
|
60
|
|
|
""" |
|
61
|
|
|
|
|
62
|
|
|
orion_node = OrionNode() |
|
63
|
|
|
|
|
64
|
|
|
if is_ip(node): |
|
65
|
|
|
query_for_where = "IPAddress" |
|
66
|
|
|
else: |
|
67
|
|
|
query_for_where = "Caption" |
|
68
|
|
|
|
|
69
|
|
|
swql = """SELECT NodeID, Uri, IPAddress, Caption |
|
70
|
|
|
FROM Orion.Nodes |
|
71
|
|
|
WHERE {}=@query_on""".format(query_for_where) |
|
72
|
|
|
kargs = {'query_on': node} |
|
73
|
|
|
data = self.query(swql, **kargs) |
|
74
|
|
|
|
|
75
|
|
|
if not 'results' in data: |
|
76
|
|
|
msg = "No results from Orion: {}".format(data) |
|
77
|
|
|
self.logger.info(msg) |
|
78
|
|
|
raise Exception(msg) |
|
79
|
|
|
|
|
80
|
|
|
if len(data['results']) == 1: |
|
81
|
|
|
try: |
|
82
|
|
|
orion_node.npm_id = data['results'][0]['NodeID'] |
|
83
|
|
|
orion_node.uri = data['results'][0]['Uri'] |
|
84
|
|
|
orion_node.ip_address = data['results'][0]['IPAddress'] |
|
85
|
|
|
orion_node.caption = data['results'][0]['Caption'] |
|
86
|
|
|
except IndexError: |
|
87
|
|
|
pass |
|
88
|
|
|
elif len(data['results']) >= 2: |
|
89
|
|
|
self.logger.debug( |
|
90
|
|
|
"Muliple Nodes match '{}' Caption: {}".format( |
|
91
|
|
|
node, data)) |
|
92
|
|
|
raise ValueError("Muliple Nodes match '{}' Caption".format( |
|
93
|
|
|
node)) |
|
94
|
|
|
|
|
95
|
|
|
if orion_node.npm: |
|
96
|
|
|
swql = """SELECT NodeID |
|
97
|
|
|
FROM Cirrus.Nodes |
|
98
|
|
|
WHERE CoreNodeID=@CoreNodeID""" |
|
99
|
|
|
kargs = {'CoreNodeID': orion_node.npm_id} |
|
100
|
|
|
data = self.query(swql, **kargs) |
|
101
|
|
|
|
|
102
|
|
|
# Don't raise an exception if this fails. |
|
103
|
|
|
# The platform may not haev NCM installed. |
|
104
|
|
|
if not 'results' in data: |
|
105
|
|
|
msg = "No results from Orion NCM: {}".format(data) |
|
106
|
|
|
self.logger.info(msg) |
|
107
|
|
|
elif len(data['results']) == 1: |
|
108
|
|
|
try: |
|
109
|
|
|
orion_node.ncm_id = data['results'][0]['NodeID'] |
|
110
|
|
|
except IndexError: |
|
111
|
|
|
pass |
|
112
|
|
|
|
|
113
|
|
|
return orion_node |
|
114
|
|
|
|
|
115
|
|
|
def query(self, swql, **kargs): |
|
116
|
|
|
""" |
|
117
|
|
|
Run SWQL against the Orion Platform. |
|
118
|
|
|
""" |
|
119
|
|
|
return self.client.query(swql, **kargs) |
|
120
|
|
|
|
|
121
|
|
|
def invoke(self, entity, verb, *args): |
|
122
|
|
|
""" |
|
123
|
|
|
Run an Invoke against the Orion Platform. |
|
124
|
|
|
""" |
|
125
|
|
|
return self.client.invoke(entity, verb, *args) |
|
126
|
|
|
|
|
127
|
|
|
def create(self, entity, **kargs): |
|
128
|
|
|
""" |
|
129
|
|
|
Run an Create against the Orion Platform. |
|
130
|
|
|
""" |
|
131
|
|
|
return self.client.create(entity, **kargs) |
|
132
|
|
|
|
|
133
|
|
|
def read(self, uri): |
|
134
|
|
|
""" |
|
135
|
|
|
Run an Read against the Orion Platform. |
|
136
|
|
|
""" |
|
137
|
|
|
return self.client.read(uri) |
|
138
|
|
|
|
|
139
|
|
|
def update(self, uri, **kargs): |
|
140
|
|
|
""" |
|
141
|
|
|
Run an Update against the Orion Platform. |
|
142
|
|
|
""" |
|
143
|
|
|
return self.client.update(uri, **kargs) |
|
144
|
|
|
|
|
145
|
|
|
def delete(self, uri): |
|
146
|
|
|
""" |
|
147
|
|
|
Run an Delete of an URI against the Orion Platform. |
|
148
|
|
|
""" |
|
149
|
|
|
return self.client.delete(uri) |
|
150
|
|
|
|
|
151
|
|
|
def get_snmp_community(self, community, std_community): |
|
152
|
|
|
""" |
|
153
|
|
|
Return the correct SNMP comminity to use. |
|
154
|
|
|
""" |
|
155
|
|
|
if community is not None: |
|
156
|
|
|
return community |
|
157
|
|
|
elif std_community is not None: |
|
158
|
|
|
try: |
|
159
|
|
|
return self.config['defaults']['snmp'][std_community] |
|
160
|
|
|
except KeyError: |
|
161
|
|
|
raise ValueError("Invalid standard community") |
|
162
|
|
|
elif std_community is None: |
|
163
|
|
|
raise ValueError("Need one of community or std_community") |
|
164
|
|
|
|
|
165
|
|
|
def get_snmp_cred_id(self, community): |
|
166
|
|
|
""" |
|
167
|
|
|
Look up an SNMP community in the config and then look up |
|
168
|
|
|
the Orion ID for the Credential. |
|
169
|
|
|
""" |
|
170
|
|
|
|
|
171
|
|
|
# Check if community is a know standard, otherwise |
|
172
|
|
|
# use it as the community. |
|
173
|
|
|
try: |
|
174
|
|
|
name = self.get_snmp_community(None, community) |
|
175
|
|
|
except ValueError: |
|
176
|
|
|
name = community |
|
177
|
|
|
|
|
178
|
|
|
swql = """SELECT ID FROM Orion.Credential |
|
179
|
|
|
WHERE CredentialType=@CredentialType and Name=@name""" |
|
180
|
|
|
|
|
181
|
|
|
kargs = {'CredentialType': |
|
182
|
|
|
'SolarWinds.Orion.Core.Models.Credentials.SnmpCredentialsV2', |
|
183
|
|
|
'name': name} |
|
184
|
|
|
orion_data = self.query(swql, **kargs) |
|
185
|
|
|
|
|
186
|
|
|
if len(orion_data['results']) == 1: |
|
187
|
|
|
return orion_data['results'][0]['ID'] |
|
188
|
|
|
else: |
|
189
|
|
|
raise ValueError( |
|
190
|
|
|
"Failed to lookup community in Orion.Credential!") |
|
191
|
|
|
|
|
192
|
|
|
def get_engine_id(self, poller): |
|
193
|
|
|
""" |
|
194
|
|
|
Takes a poller name (or primary) and returns the EngineID for |
|
195
|
|
|
the poller. |
|
196
|
|
|
|
|
197
|
|
|
Raises: ValueError on an invaild poller. |
|
198
|
|
|
|
|
199
|
|
|
Returns: The EngineID (int) |
|
200
|
|
|
""" |
|
201
|
|
|
|
|
202
|
|
|
if poller == "primary": |
|
203
|
|
|
return 1 |
|
204
|
|
|
else: |
|
205
|
|
|
swql = """SELECT EngineID, ServerName, IP, ServerType |
|
206
|
|
|
FROM Orion.Engines |
|
207
|
|
|
WHERE ServerName=@poller""" |
|
208
|
|
|
kargs = {'poller': poller} |
|
209
|
|
|
data = self.query(swql, **kargs) |
|
210
|
|
|
|
|
211
|
|
|
if len(data['results']) == 1: |
|
212
|
|
|
return data['results'][0]['EngineID'] |
|
213
|
|
|
else: |
|
214
|
|
|
send_user_error("Invalid poller name") |
|
215
|
|
|
raise ValueError("Invalid poller name") |
|
216
|
|
|
|
|
217
|
|
|
def get_ncm_transfer_results(self, transfer_id, sleep_delay=10): |
|
218
|
|
|
""" |
|
219
|
|
|
Gets the completed (waits until finished). NCM job transfer status |
|
220
|
|
|
from Orion. |
|
221
|
|
|
|
|
222
|
|
|
Retruns: The completed status. |
|
223
|
|
|
""" |
|
224
|
|
|
ts = {} |
|
225
|
|
|
while True: |
|
226
|
|
|
swql = """SELECT TransferID, NodeID, Action, RequestedConfigType, |
|
227
|
|
|
RequestedScript, RequestedReboot, ConfigID, TransferProtocol, |
|
228
|
|
|
Status, ErrorMessage, DeviceOutput, DateTime, UserName |
|
229
|
|
|
FROM NCM.TransferResults |
|
230
|
|
|
WHERE TransferID=@transfer_id""" |
|
231
|
|
|
kargs = {'transfer_id': transfer_id} |
|
232
|
|
|
|
|
233
|
|
|
transfer_data = self.query(swql, **kargs) |
|
234
|
|
|
status = transfer_data['results'][0]['Status'] |
|
235
|
|
|
|
|
236
|
|
|
if status == 1: |
|
237
|
|
|
time.sleep(sleep_delay) |
|
238
|
|
|
elif status == 2: |
|
239
|
|
|
ts['status'] = "Complete" |
|
240
|
|
|
break |
|
241
|
|
|
elif status == 3: |
|
242
|
|
|
ts['status'] = "Error" |
|
243
|
|
|
break |
|
244
|
|
|
else: |
|
245
|
|
|
ts['status'] = "Unknown" |
|
246
|
|
|
break |
|
247
|
|
|
|
|
248
|
|
|
ts['RequestedScript'] = transfer_data['results'][0]['RequestedScript'] |
|
249
|
|
|
ts['RequestedReboot'] = transfer_data['results'][0]['RequestedReboot'] |
|
250
|
|
|
ts['ErrorMessage'] = transfer_data['results'][0]['ErrorMessage'] |
|
251
|
|
|
ts['DeviceOutput'] = transfer_data['results'][0]['DeviceOutput'] |
|
252
|
|
|
ts['UserName'] = transfer_data['results'][0]['UserName'] |
|
253
|
|
|
|
|
254
|
|
|
return ts |
|
255
|
|
|
|