summaryrefslogtreecommitdiffstats
path: root/plugins/check_proc_status.py
blob: bea18ab88c40a1c88dca436ed1de5511a76de3fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/python
# Copyright (C) 2014 Red Hat Inc
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#

import sys
import errno
import lockfile
import logging
import psutil
import time
from daemon import runner
import nscautils
import glusternagios

from glusternagios import utils
from glusternagios import glustercli


_checkProc = utils.CommandPath('check_proc',
                               '/usr/lib64/nagios/plugins/check_procs')

_glusterVolPath = "/var/lib/glusterd/vols"
_checkNfsCmd = [_checkProc.cmd, "-c", "1:", "-C", "glusterfs", "-a", "nfs"]
_checkShdCmd = [_checkProc.cmd, "-c", "1:", "-C", "glusterfs", "-a",
                "glustershd"]
_checkSmbCmd = [_checkProc.cmd, "-C", "smb"]
_checkQuotaCmd = [_checkProc.cmd, "-c", "1:", "-C", "glusterfs", "-a",
                  "quotad"]
_checkBrickCmd = [_checkProc.cmd, "-C", "glusterfsd"]
_checkGlusterdCmd = [_checkProc.cmd, "-c", "1:", "-w", "1:1", "-C", "glusterd"]
_nfsService = "Glusterfs NFS Daemon"
_shdService = "Glusterfs Self-Heal Daemon"
_smbService = "CIFS"
_brickService = "Brick Status - "
_glusterdService = "Gluster Management Daemon"
_quotadService = "Gluster Quota Daemon"


def getBrickStatus(hostName, volInfo):
    bricks = {}
    hostUuid = glustercli.hostUUIDGet()
    status = None
    for volumeName, volumeInfo in volInfo.iteritems():
        if volumeInfo['volumeStatus'] == glustercli.VolumeStatus.OFFLINE:
            continue
        for brick in volumeInfo['bricksInfo']:
            if brick.get('hostUuid') != hostUuid:
                continue
            brickPath = brick['name'].split(':')[1]
            brickService = "Brick Status - %s" % brickPath
            pidFile = brick['name'].replace(
                ":/", "-").replace("/", "-") + ".pid"
            try:
                with open("%s/%s/run/%s" % (
                        _glusterVolPath, volumeName, pidFile)) as f:
                    if psutil.pid_exists(int(f.read().strip())):
                        status = utils.PluginStatusCode.OK
                    else:
                        status = utils.PluginStatusCode.CRITICAL
            except IOError, e:
                if e.errno == errno.ENOENT:
                    status = utils.PluginStatusCode.CRITICAL
                else:
                    status = utils.PluginStatusCode.UNKNOWN
                    msg = "UNKNOWN: Brick %s: %s" % (brickPath, str(e))
            finally:
                if status == utils.PluginStatusCode.OK:
                    msg = "OK: Brick %s" % brickPath
                elif status != utils.PluginStatusCode.UNKNOWN:
                    msg = "CRITICAL: Brick %s is down" % brickPath
                bricks[brickService] = [status, msg]
    return bricks


def getNfsStatus(hostName, volInfo):
    # if nfs is already running we need not to check further
    status, msg, error = utils.execCmd(_checkNfsCmd)
    if status == utils.PluginStatusCode.OK:
        return status, msg[0] if len(msg) > 0 else ""

    # if nfs is not running and any of the volume uses nfs
    # then its required to alert the user
    for volume, volumeInfo in volInfo.iteritems():
        if volumeInfo['volumeStatus'] == glustercli.VolumeStatus.OFFLINE:
            continue
        nfsStatus = volumeInfo.get('options', {}).get('nfs.disable', 'off')
        if nfsStatus == 'off':
            msg = "CRITICAL: Process glusterfs-nfs is not running"
            status = utils.PluginStatusCode.CRITICAL
            break
    else:
        msg = "OK: No gluster volume uses nfs"
        status = utils.PluginStatusCode.OK
    return status, msg


def getSmbStatus(hostName, volInfo):
    status, msg, error = utils.execCmd(_checkSmbCmd)
    if status == utils.PluginStatusCode.OK:
        return status, msg[0] if len(msg) > 0 else ""

    # if smb is not running and any of the volume uses smb
    # then its required to alert the use
    for k, v in volInfo.iteritems():
        cifsStatus = v.get('options', {}).get('user.cifs', 'enable')
        smbStatus = v.get('options', {}).get('user.smb', 'enable')
        if cifsStatus == 'enable' and smbStatus == 'enable':
            msg = "CRITICAL: Process smb is not running"
            status = utils.PluginStatusCode.CRITICAL
            break
    else:
        msg = "OK: No gluster volume uses smb"
        status = utils.PluginStatusCode.OK
    return status, msg


def getQuotadStatus(hostName, volInfo):
    # if quota is already running we need not to check further
    status, msg, error = utils.execCmd(_checkQuotaCmd)
    if status == utils.PluginStatusCode.OK:
        return status, msg[0] if len(msg) > 0 else ""

    # if quota is not running and any of the volume uses quota
    # then the quotad process should be running in the host
    for k, v in volInfo.iteritems():
        quotadStatus = v.get('options', {}).get('features.quota', '')
        if quotadStatus == 'on':
            msg = "CRITICAL: Process quotad is not running"
            utils.PluginStatusCode.CRITICAL
            break
    else:
        msg = "OK: Quota not enabled"
        status = utils.PluginStatusCode.OK
    return status, msg


def getShdStatus(hostName, volInfo):
    status, msg, error = utils.execCmd(_checkShdCmd)
    if status == utils.PluginStatusCode.OK:
        return status, msg[0] if len(msg) > 0 else ""

    hostUuid = glustercli.hostUUIDGet()
    for volumeName, volumeInfo in volInfo.iteritems():
        if volumeInfo['volumeStatus'] == glustercli.VolumeStatus.OFFLINE:
            continue
        if hasBricks(hostUuid, volumeInfo['bricksInfo']) and \
           int(volumeInfo['replicaCount']) > 1:
            status = utils.PluginStatusCode.CRITICAL
            msg = "CRITICAL: Gluster Self Heal Daemon not running"
            break
    else:
        msg = "OK: Process Gluster Self Heal Daemon"
        status = utils.PluginStatusCode.OK
    return status, msg


def hasBricks(hostUuid, bricks):
    for brick in bricks:
        if brick['hostUuid'] == hostUuid:
            return True
    return False


class App():
    def __init__(self):
        self.stdin_path = '/dev/null'
        self.stdout_path = '/dev/tty'
        self.stderr_path = '/dev/null'
        self.pidfile_path = '/var/run/glusterpmd.pid'
        self.pidfile_timeout = 5

    def run(self):
        hostName = nscautils.getCurrentHostNameInNagiosServer()
        sleepTime = int(nscautils.getProcessMonitorSleepTime())
        glusterdStatus = None
        nfsStatus = None
        smbStatus = None
        shdStatus = None
        quotaStatus = None
        brickStatus = {}
        while True:
            if not hostName:
                hostName = nscautils.getCurrentHostNameInNagiosServer()
                if not hostName:
                    logger.warn("Hostname is not configured")
                    time.sleep(sleepTime)
                    continue
            status, msg, error = utils.execCmd(_checkGlusterdCmd)
            if status != glusterdStatus or \
                    status == utils.PluginStatusCode.CRITICAL:
                glusterdStatus = status
                msg = msg[0] if len(msg) > 0 else ""
                nscautils.send_to_nsca(hostName, _glusterdService, status, msg)

            # Get the volume status only if glusterfs is running to avoid
            # unusual delay
            if status != utils.PluginStatusCode.OK:
                logger.warn("Glusterd is not running")
                time.sleep(sleepTime)
                continue

            try:
                volInfo = glustercli.volumeInfo()
            except glusternagios.glustercli.GlusterCmdFailedException:
                logger.error("failed to find volume info")
                time.sleep(sleepTime)
                continue

            status, msg = getNfsStatus(hostName, volInfo)
            if status != nfsStatus or \
                    status == utils.PluginStatusCode.CRITICAL:
                nfsStatus = status
                nscautils.send_to_nsca(hostName, _nfsService, status, msg)

            status, msg = getSmbStatus(hostName, volInfo)
            if status != smbStatus or \
                    status == utils.PluginStatusCode.CRITICAL:
                smbStatus = status
                nscautils.send_to_nsca(hostName, _smbService, status, msg)

            status, msg = getShdStatus(hostName, volInfo)
            if status != shdStatus or \
                    status == utils.PluginStatusCode.CRITICAL:
                shdStatus = status
                nscautils.send_to_nsca(hostName, _shdService, status, msg)

            status, msg = getQuotadStatus(hostName, volInfo)
            if status != quotaStatus or \
                    status == utils.PluginStatusCode.CRITICAL:
                quotaStatus = status
                nscautils.send_to_nsca(hostName, _quotadService, status, msg)

            brick = getBrickStatus(hostName, volInfo)
            # brickInfo contains status, and message
            for brickService, brickInfo in brick.iteritems():
                if brickInfo[0] != brickStatus.get(brickService, [None])[0] \
                   or brickInfo[0] == utils.PluginStatusCode.CRITICAL:
                    brickStatus[brickService] = brickInfo
                    nscautils.send_to_nsca(hostName, brickService,
                                           brickInfo[0], brickInfo[1])
            time.sleep(sleepTime)

if __name__ == '__main__':
    app = App()
    logger = logging.getLogger("GlusterProcLog")
    logger.setLevel(logging.INFO)
    formatter = logging.Formatter(
        "%(asctime)s - %(name)s - %(levelname)s - %(message)s")
    handler = logging.FileHandler("/var/log/glusterpmd.log")
    handler.setFormatter(formatter)
    logger.addHandler(handler)

    daemonRunner = runner.DaemonRunner(app)
    daemonRunner.daemon_context.files_preserve = [handler.stream]
    try:
        daemonRunner.do_action()
    except lockfile.LockTimeout:
        logger.error("failed to aquire lock")
    except runner.DaemonRunnerStopFailureError:
        logger.error("failed to get the lock file")
    sys.exit(utils.PluginStatusCode.OK)