Completed
Pull Request — master (#463)
by
unknown
02:30
created

OrionStatus.run()   D

Complexity

Conditions 8

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 44
rs 4
cc 8
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
from st2actions.runners.pythonrunner import Action
19
from orionsdk import SwisClient
20
21
22
class OrionStatus(Action):
23
    def run(self, node, platform):
24
        """
25
        Query Solarwinds Orion.
26
        """
27
28
        # Set up the results
29
        results = {}
30
        results['status'] = None
31
        results['color'] = None
32
33
        try:
34
            swis = SwisClient(self.config['orion'][platform]['host'],
35
                              self.config['orion'][platform]['user'],
36
                              self.config['orion'][platform]['password'])
37
        except KeyError:
38
            raise ValueError("Orion host details not in the config.yaml")
39
40
        orion_data = swis.query(
41
            "SELECT Status FROM Orion.Nodes WHERE Caption=@node",
42
            node=node)
43
44
        if len(orion_data['results']) != 0:
45
            raw_status = orion_data['results'][0]['Status']
46
47
            if raw_status == 0:
48
                results['status'] = "Unknown"
49
                results['color'] = "grey"
50
            elif raw_status == 1:
51
                results['status'] = "Up"
52
                results['color'] = "good"
53
            elif raw_status == 2:
54
                results['status'] = "Down"
55
                results['color'] = "danger"
56
            elif raw_status == 3:
57
                results['status'] = "Warning"
58
                results['color'] = "warning"
59
            elif raw_status == 14:
60
                results['status'] = "Critical"
61
                results['color'] = "danger"
62
        else:
63
            ### Log error here...
64
            raise ValueError("Node not found")
65
66
        return results
67