1
|
|
|
#!/usr/bin/env python
|
2
|
|
|
|
3
|
|
|
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
|
4
|
|
|
# contributor license agreements. See the NOTICE file distributed with
|
5
|
|
|
# this work for additional information regarding copyright ownership.
|
6
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0
|
7
|
|
|
# (the "License"); you may not use this file except in compliance with
|
8
|
|
|
# the License. You may obtain a copy of the License at
|
9
|
|
|
#
|
10
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
|
|
#
|
12
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
13
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
|
|
# See the License for the specific language governing permissions and
|
16
|
|
|
# limitations under the License.
|
17
|
|
|
|
18
|
|
|
import re
|
19
|
|
|
|
20
|
|
|
from lib.actions import OrionBaseAction
|
21
|
|
|
|
22
|
|
|
|
23
|
|
|
class NodeCreate(OrionBaseAction):
|
24
|
|
|
def run(self,
|
25
|
|
|
node,
|
26
|
|
|
platform,
|
27
|
|
|
ip_address,
|
28
|
|
|
engineID,
|
29
|
|
|
mon_protocol,
|
30
|
|
|
std_community,
|
31
|
|
|
community,
|
32
|
|
|
status):
|
33
|
|
|
"""
|
34
|
|
|
Create an node in an Orion monitoring platform.
|
35
|
|
|
"""
|
36
|
|
|
results = {}
|
37
|
|
|
|
38
|
|
|
# Sort out which platform & poller to create the node on.
|
39
|
|
|
if platform is None:
|
40
|
|
|
try:
|
41
|
|
|
platform = self.config['defaults']['platform']
|
42
|
|
|
except IndexError:
|
43
|
|
|
raise ValueError("No default Orion platform.")
|
44
|
|
|
|
45
|
|
|
self.logger.info("Connecting to Orion platform: {}".format(platform))
|
46
|
|
|
self.connect(platform)
|
47
|
|
|
results['platform'] = platform
|
48
|
|
|
|
49
|
|
|
# Check node / ip is not already on platform.
|
50
|
|
|
self.logger.info(
|
51
|
|
|
"Checking node ({}) is not on Orion platform: {}".format(node,
|
52
|
|
|
platform))
|
53
|
|
|
|
54
|
|
|
# The API allows addition of duplicate nodes, so check and raise
|
55
|
|
|
# exception if it's already monitored (by name or IP address).
|
56
|
|
|
# FIX ME - Do check of node caption here...
|
57
|
|
|
# FIX ME - Do check of ip caption here...
|
58
|
|
|
|
59
|
|
|
kargs = {'Caption': node,
|
60
|
|
|
'EngineID': engineID,
|
61
|
|
|
'IPAddress': ip_address
|
62
|
|
|
}
|
63
|
|
|
|
64
|
|
|
if mon_protocol == "snmpv2":
|
65
|
|
|
kargs['ObjectSubType'] = "SNMP"
|
66
|
|
|
kargs['SNMPVersion'] = 2
|
67
|
|
|
|
68
|
|
|
if community is not None:
|
69
|
|
|
kargs['Community'] = community
|
70
|
|
|
elif std_community is not None:
|
71
|
|
|
kargs['Community'] = self.config['defaults']['snmp'][std_community]
|
72
|
|
|
elif std_community is None:
|
73
|
|
|
raise ValueError("Need one of community or std_community")
|
74
|
|
|
|
75
|
|
|
self.logger.info("Creating Orion Node: {}".format(kargs))
|
76
|
|
|
orion_data = self.create('Orion.Nodes', **kargs)
|
77
|
|
|
|
78
|
|
|
results['node_id'] = re.search('(\d+)$', orion_data).group(0)
|
79
|
|
|
|
80
|
|
|
self.logger.info("Created Orion Node: {}".format(results['node_id']))
|
81
|
|
|
|
82
|
|
|
return results
|
83
|
|
|
|