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

NodeCreate   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 107
Duplicated Lines 0 %
Metric Value
dl 0
loc 107
rs 10
wmc 12

1 Method

Rating   Name   Duplication   Size   Complexity  
F run() 0 106 12
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
            platform,
25
            ip_address,
26
            engineID,
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
        kargs = {'Caption': node,
66
                 'EngineID': engineID,
67
                 'IPAddress': ip_address
68
                 }
69
70
        if mon_protocol == "snmpv2":
71
            kargs['ObjectSubType'] = "SNMP"
72
            kargs['SNMPVersion'] = 2
73
74
        if community is not None:
75
            kargs['Community'] = community
76
        elif std_community is not None:
77
            kargs['Community'] = self.config['defaults']['snmp'][std_community]
78
        elif std_community is None:
79
            raise ValueError("Need one of community or std_community")
80
81
        self.logger.info("Creating Orion Node: {}".format(kargs))
82
        orion_data = self.create('Orion.Nodes', **kargs)
83
84
        node_id = re.search('(\d+)$', orion_data).group(0)
85
        results['node_id'] = node_id
86
87
        self.logger.info("Created Orion Node: {}".format(results['node_id']))
88
89
        pollers_to_add = {
90
            'N.Details.SNMP.Generic': True,
91
            'N.Uptime.SNMP.Generic': True,
92
            'N.Cpu.SNMP.HrProcessorLoad': True,
93
            'N.Memory.SNMP.NetSnmpReal': True,
94
            'N.AssetInventory.Snmp.Generic': True,
95
            'N.Topology_Layer3.SNMP.ipNetToMedia': True,
96
            'N.Routing.SNMP.Ipv4CidrRoutingTable': False
97
        }
98
99
        if status == 'icmp':
100
            pollers_to_add['N.Status.ICMP.Native'] = True
101
            pollers_to_add['N.Status.SNMP.Native'] = False
102
            pollers_to_add['N.ResponseTime.ICMP.Native'] = True
103
            pollers_to_add['N.ResponseTime.SNMP.Native'] = False
104
        elif status == 'snmp':
105
            pollers_to_add['N.Status.ICMP.Native'] = False
106
            pollers_to_add['N.Status.SNMP.Native'] = True
107
            pollers_to_add['N.ResponseTime.ICMP.Native'] = False
108
            pollers_to_add['N.ResponseTime.SNMP.Native'] = True
109
110
        pollers = []
111
        for p in pollers_to_add:
112
            pollers.append({
113
                'PollerType': p,
114
                'NetObject': 'N:{}'.format(node_id),
115
                'NetObjectType': 'N',
116
                'NetObjectID': node_id,
117
                'Enabled': pollers_to_add[p]
118
            })
119
120
        for poller in pollers:
121
            response = self.create('Orion.Pollers', **poller)
122
            self.logger.info("Added {} ({}) poller: {}".format(
123
                poller['PollerType'],
124
                poller['Enabled'],
125
                response))
126
127
        return results
128