GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 3b532d...9b942b )
by P.R.
02:53
created

NagiosClient.__test_daemon_is_running()   A

Complexity

Conditions 3

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
dl 0
loc 21
ccs 0
cts 10
cp 0
crap 12
rs 9.3142
1
"""
2
Enarksh
3
4
Copyright 2013-2016 Set Based IT Consultancy
5
6
Licence MIT
7
"""
8
import os
9
10
import zmq
11
12
import enarksh
0 ignored issues
show
Unused Code introduced by
The import enarksh seems to be unused.
Loading history...
13
from enarksh.C import C
14
from enarksh.Config import Config
15
from enarksh.controller.message.NagiosMessage import NagiosMessage
16
17
18
class NagiosClient:
19
    """
20
    A client for Nagios.
21
    """
22
23
    # ------------------------------------------------------------------------------------------------------------------
24
    def __init__(self, io):
25
        """
26
        Object constructor.
27
28
        :param enarksh.style.EnarkshStyle.EnarkshStyle io: The output decorator.
29
        """
30
        self.__zmq_context = None
31
        """
32
        The ZMQ context.
33
34
        :type: Context
35
        """
36
37
        self.__zmq_controller = None
38
        """
39
        The socket for communicating with the controller.
40
41
        :type: zmq.sugar.socket.Socket
42
        """
43
44
        self._io = io
45
        """
46
        The output decorator.
47
48
        :type: enarksh.style.EnarkshStyle.EnarkshStyle
49
        """
50
51
        self.__state = ''
52
        """
53
        The state for Nagios.
54
55
        :type: str
56
        """
57
58
        self.__message = ''
59
        """
60
        The message for Nagios.
61
62
        :type: str
63
        """
64
65
        self.__performance_data = ''
66
        """
67
        The performance data for Nagios.
68
69
        :type: str
70
        """
71
72
    # ------------------------------------------------------------------------------------------------------------------
73
    @staticmethod
74
    def __get_pid(daemon):
75
        """
76
        Returns the PID of a daemon of Enarksh.
77
78
        :param str daemon: The name of the daemon.
79
80
        :rtype: str|None
81
        """
82
        try:
83
            config = Config.get()
84
85
            pid_file = os.path.join(C.HOME, config.get_enarksh_lock_dir(), daemon + '.pid')
86
            with open(pid_file) as handle:
87
                pid = handle.read(1000)
88
89
            return int(pid)
90
91
        except Exception:
92
            return None
93
94
    # ------------------------------------------------------------------------------------------------------------------
95
    def __test_daemon_is_running(self, daemon):
96
        """
97
        Test whether a daemon is running.
98
99
        :param str daemon: The name of the daemon.
100
101
        :rtype: bool
102
        """
103
        pid = self.__get_pid(daemon)
104
105
        if pid is None:
106
            return False
107
108
        try:
109
            proc_file = os.path.join('/proc', str(pid), 'comm')
110
            name = open(proc_file).read(1000).strip()
111
112
            return name == daemon
113
114
        except Exception:
115
            return False
116
117
    # ------------------------------------------------------------------------------------------------------------------
118
    def __test_daemons_are_running(self):
119
        """
120
        Tests all 3 daemons are running.
121
        """
122
        # Test controller is running
123
        for daemon in ['controller', 'logger', 'spawner']:
124
            if not self.__test_daemon_is_running(daemon):
125
                if self.__message:
126
                    self.__message += ' '
127
                self.__message = '{} is not running'.format(daemon)
128
129
        if self.__message:
130
            self.__message = 'Enarksh CRITICAL - ' + self.__message
131
            self.__state = 2
132
        else:
133
            self.__message = 'Enarksh OK - '
134
            self.__state = 0
135
136
    # ------------------------------------------------------------------------------------------------------------------
137
    def __get_performance_data(self):
138
        """
139
        Retrieves performance data from the controller.
140
        """
141
        # Compose the message for the controller.
142
        message = NagiosMessage()
143
144
        # Send the message to the controller.
145
        self.__zmq_controller.send_pyobj(message)
146
147
        # Await the response from the controller.
148
        response = self.__zmq_controller.recv_pyobj()
149
150
        self.__performance_data += 'schedules={}, waiting={}, queued={}, running={}, completed={}, error={}'. \
151
            format(response['sch_count'],
152
                   response['rst_count'][C.ENK_RST_ID_WAITING],
153
                   response['rst_count'][C.ENK_RST_ID_QUEUED],
154
                   response['rst_count'][C.ENK_RST_ID_RUNNING],
155
                   response['rst_count'][C.ENK_RST_ID_COMPLETED],
156
                   response['rst_count'][C.ENK_RST_ID_ERROR])
157
158
        self.__message += 'Schedules = {}, Waiting = {}, Queued = {}, Running = {}, Completed = {}, Error = {}'. \
159
            format(response['sch_count'],
160
                   response['rst_count'][C.ENK_RST_ID_WAITING],
161
                   response['rst_count'][C.ENK_RST_ID_QUEUED],
162
                   response['rst_count'][C.ENK_RST_ID_RUNNING],
163
                   response['rst_count'][C.ENK_RST_ID_COMPLETED],
164
                   response['rst_count'][C.ENK_RST_ID_ERROR])
165
166
    # ------------------------------------------------------------------------------------------------------------------
167
    def main(self):
168
        """
169
        The main function of Nagios.
170
        """
171
        # Initialize ZMQ.
172
        self.__zmq_init()
173
174
        self.__test_daemons_are_running()
175
176
        if self.__state == 0:
177
            self.__get_performance_data()
178
179
        print(self.__message + '|' + self.__performance_data)
180
181
        return self.__state
182
183
    # ------------------------------------------------------------------------------------------------------------------
184
    def __zmq_init(self):
185
        """
186
        Initializes ZMQ.
187
        """
188
        config = Config.get()
189
190
        self.__zmq_context = zmq.Context()
191
192
        # Create socket for communicating with the controller.
193
        self.__zmq_controller = self.__zmq_context.socket(zmq.REQ)
194
        self.__zmq_controller.connect(config.get_controller_lockstep_end_point())
195
196
# ----------------------------------------------------------------------------------------------------------------------
197