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

NodeCreate   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 100
Duplicated Lines 0 %
Metric Value
dl 0
loc 100
rs 10
wmc 11

1 Method

Rating   Name   Duplication   Size   Complexity  
F run() 0 99 11
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
        node_id = re.search('(\d+)$', orion_data).group(0)
79
        results['node_id'] = node_id
80
81
        self.logger.info("Created Orion Node: {}".format(results['node_id']))
82
83
        pollers_to_add = {
84
            'N.Details.SNMP.Generic': True,
85
            'N.Uptime.SNMP.Generic': True,
86
            'N.Cpu.SNMP.HrProcessorLoad': True,
87
            'N.Memory.SNMP.NetSnmpReal': True,
88
            'N.AssetInventory.Snmp.Generic': True,
89
            'N.Topology_Layer3.SNMP.ipNetToMedia': False,
90
            'N.Routing.SNMP.Ipv4CidrRoutingTable': False
91
        }
92
93
        if status == 'icmp':
94
            pollers_to_add['N.Status.ICMP.Native'] = True
95
            pollers_to_add['N.Status.SNMP.Native'] = False
96
            pollers_to_add['N.ResponseTime.ICMP.Native'] = True
97
            pollers_to_add['N.ResponseTime.SNMP.Native'] = False
98
        elif status == 'snmp':
99
            pollers_to_add['N.Status.ICMP.Native'] = False
100
            pollers_to_add['N.Status.SNMP.Native'] = True
101
            pollers_to_add['N.ResponseTime.ICMP.Native'] = False
102
            pollers_to_add['N.ResponseTime.SNMP.Native'] = True
103
104
        pollers = []
105
        for p in pollers_to_add:
106
            pollers.append({
107
                'PollerType': p,
108
                'NetObject': 'N:{}'.format(node_id),
109
                'NetObjectType': 'N',
110
                'NetObjectID': node_id,
111
                'Enabled': pollers_to_add[p]
112
            })
113
114
        for poller in pollers:
115
            # self.logger.info(
116
            #    "Adding poller type: {} with status {}... ".format(
117
            #        poller['PollerType'], poller['Enabled'])
118
            #    )
119
            response = self.create('Orion.Pollers', **poller)
120
            # self.logger.info("Done: {}".format(response))
121
122
        return results
123