Completed
Pull Request — master (#487)
by
unknown
02:48
created

StartDiscovery   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 116
rs 10
wmc 17

1 Method

Rating   Name   Duplication   Size   Complexity  
F run() 0 115 17
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
from lib.actions import OrionBaseAction
17
18
19
class StartDiscovery(OrionBaseAction):
20
    def run(self,
21
            name,
22
            platform,
23
            poller,
24
            snmp_communities,
25
            nodes=None,
26
            subnets=None,
27
            ip_ranges=None,
28
            no_icmp_only=True,
29
            auto_import=False):
30
        """
31
        Create and Start Discovery process in Orion.
32
33
        Returns:
34
         - ProfileID that was created (or error from Orion).
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
        # Set BulkList to how Orion likes this to be empty
51
        BulkList = None
52
        if nodes is not None:
53
            BulkList = []
54
            for node in nodes:
55
                BulkList.append({'Address': node})
56
57
            if ip_ranges is not None or subnets is not None:
58
                raise ValueError("Only set one of nodes, ip_ranges or subnets!")
59
60
        # Set IpRanges how Orion likes this to be empty
61
        IpRanges = []
62
        if ip_ranges is not None:
63
            for ip_range in ip_ranges:
64
                (start_ip, end_ip) = ip_range.split(':')
65
                IpRanges.append({'StartAddress': start_ip,
66
                                 'EndAddress': end_ip})
67
68
            if BulkList is not None or subnets is not None:
69
                raise ValueError("Only set one of nodes, ip_ranges or subnets!")
70
71
        # Set Subnets how Orion likes this to be empty
72
        Subnets = None
73
        if subnets is not None:
74
            Subnets = []
75
            for subnet in subnets:
76
                (SubnetIP, SubnetMask) = subnet.split('/')
77
                Subnets.append({'SubnetIP': SubnetIP,
78
                                'SubnetMask': SubnetMask})
79
80
            if BulkList is not None or IpRanges is not None:
81
                raise ValueError("Only set one of nodes, ip_ranges or subnets!")
82
83
        CredID_order = 1
84
        CredIDs = []
85
        for snmp in snmp_communities:
86
            CredIDs.append(
87
                {'CredentialID': self.get_snmp_cred_id(snmp),
88
                 'Order': CredID_order}
89
            )
90
            CredID_order += 1
91
92
        CorePluginConfiguration = self.invoke('Orion.Discovery',
93
                                              'CreateCorePluginConfiguration',
94
                                              {'BulkList': BulkList,
95
                                               'IpRanges': IpRanges,
96
                                               'Subnets': Subnets,
97
                                               'Credentials': CredIDs,
98
                                               'WmiRetriesCount': 0,
99
                                               'WmiRetryIntervalMiliseconds':
100
                                               1000})
101
102
        # engineID if happens to be None, default to the primary (aka 1).
103
        if poller is not None:
104
            engineID = self.get_engine_id(poller)
105
        else:
106
            engineID = 1
107
108
        self.logger.info(
109
            "Adding '{}' Discovery profile to Orion Platform {}".format(
110
                name, platform))
111
112
        disco = self.invoke('Orion.Discovery', 'StartDiscovery',
113
                            {
114
                                'Name': name,
115
                                'EngineId': engineID,
116
                                'JobTimeoutSeconds': 3600,
117
                                'SearchTimeoutMiliseconds': 2000,
118
                                'SnmpTimeoutMiliseconds': 2000,
119
                                'SnmpRetries': 4,
120
                                'RepeatIntervalMiliseconds': 1800,
121
                                'SnmpPort': 161,
122
                                'HopCount': 0,
123
                                'PreferredSnmpVersion': 'SNMP2c',
124
                                'DisableIcmp': no_icmp_only,
125
                                'AllowDuplicateNodes': False,
126
                                'IsAutoImport': auto_import,
127
                                'IsHidden': False,
128
                                'PluginConfigurations': [
129
                                    {'PluginConfigurationItem':
130
                                     CorePluginConfiguration}
131
                                ]
132
                            })
133
134
        return disco
135