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

NodeCreate   A

Complexity

Total Complexity 12

Size/Duplication

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

1 Method

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