Completed
Pull Request — master (#466)
by
unknown
02:37
created

NodeCreate   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 113
Duplicated Lines 0 %
Metric Value
dl 0
loc 113
rs 10
wmc 13

1 Method

Rating   Name   Duplication   Size   Complexity  
F run() 0 112 13
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 re
17
18
from lib.actions import OrionBaseAction
19
20
21
class NodeCreate(OrionBaseAction):
22
    def run(self,
23
            node,
24
            ip_address,
25
            platform,
26
            poller,
27
            mon_protocol,
28
            std_community,
29
            community,
30
            status):
31
        """
32
        Create an node in an Orion monitoring platform.
33
        """
34
        results = {}
35
36
        # Sort out which platform & poller to create the node on.
37
        if platform is None:
38
            try:
39
                platform = self.config['defaults']['platform']
40
            except IndexError:
41
                self.send_user_error("No default Orion platform.")
42
                raise ValueError("No default Orion platform.")
43
44
        self.logger.info("Connecting to Orion platform: {}".format(platform))
45
        self.connect(platform)
46
        results['platform'] = platform
47
48
        if self.node_exists(node, ip_address):
49
            self.logger.error(
50
                "Node ({}) or IP ({}) already in Orion platform: {}".format(
51
                    node,
52
                    ip_address,
53
                    platform)
54
            )
55
56
            self.send_user_error("Node and/or IP is already in Orion!")
57
            raise Exception("Node and/or IP already exists!")
58
        else:
59
            self.logger.info(
60
                "Checking node ({}) is not on Orion platform: {}".format(
61
                    node,
62
                    platform)
63
            )
64
65
        # engineID if happens to be None, default to the primary.
66
        if poller is not None:
67
            engineID = self.get_engine_id(poller)
68
        else:
69
            engineID = 1
70
71
        kargs = {'Caption': node,
72
                 'EngineID': engineID,
73
                 'IPAddress': ip_address
74
                 }
75
76
        if mon_protocol == "snmpv2":
77
            kargs['ObjectSubType'] = "SNMP"
78
            kargs['SNMPVersion'] = 2
79
80
        if community is not None:
81
            kargs['Community'] = community
82
        elif std_community is not None:
83
            kargs['Community'] = self.config['defaults']['snmp'][std_community]
84
        elif std_community is None:
85
            raise ValueError("Need one of community or std_community")
86
87
        self.logger.info("Creating Orion Node: {}".format(kargs))
88
        orion_data = self.create('Orion.Nodes', **kargs)
89
90
        node_id = re.search('(\d+)$', orion_data).group(0)
91
        results['node_id'] = node_id
92
93
        self.logger.info("Created Orion Node: {}".format(results['node_id']))
94
95
        pollers_to_add = {
96
            'N.Details.SNMP.Generic': True,
97
            'N.Uptime.SNMP.Generic': True,
98
            'N.Cpu.SNMP.HrProcessorLoad': True,
99
            'N.Memory.SNMP.NetSnmpReal': True,
100
            'N.AssetInventory.Snmp.Generic': True,
101
            'N.Topology_Layer3.SNMP.ipNetToMedia': True,
102
            'N.Routing.SNMP.Ipv4CidrRoutingTable': False
103
        }
104
105
        if status == 'icmp':
106
            pollers_to_add['N.Status.ICMP.Native'] = True
107
            pollers_to_add['N.Status.SNMP.Native'] = False
108
            pollers_to_add['N.ResponseTime.ICMP.Native'] = True
109
            pollers_to_add['N.ResponseTime.SNMP.Native'] = False
110
        elif status == 'snmp':
111
            pollers_to_add['N.Status.ICMP.Native'] = False
112
            pollers_to_add['N.Status.SNMP.Native'] = True
113
            pollers_to_add['N.ResponseTime.ICMP.Native'] = False
114
            pollers_to_add['N.ResponseTime.SNMP.Native'] = True
115
116
        pollers = []
117
        for p in pollers_to_add:
118
            pollers.append({
119
                'PollerType': p,
120
                'NetObject': 'N:{}'.format(node_id),
121
                'NetObjectType': 'N',
122
                'NetObjectID': node_id,
123
                'Enabled': pollers_to_add[p]
124
            })
125
126
        for poller in pollers:
127
            response = self.create('Orion.Pollers', **poller)
128
            self.logger.info("Added {} ({}) poller: {}".format(
129
                poller['PollerType'],
130
                poller['Enabled'],
131
                response))
132
133
        return results
134