summaryrefslogtreecommitdiffstats
path: root/plugins/check_proc_status.py
blob: 31825f1d89ae090c2ed40abe0f45435778976f64 (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
#!/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 lockfile
import logging
import time
from daemon import runner
from logging import handlers
import nscautils
import check_proc_util
import glusternagios

from glusternagios import utils
from glusternagios import glustercli


_nfsService = "NFS"
_shdService = "Self-Heal"
_smbService = "SMB"
_brickService = "Brick - %s"
_glusterdService = "Gluster Management"
_quotadService = "Quota"
_ctdbdService = "CTDB"
checkIdeSmartCmdPath = utils.CommandPath(
    'check_ide_smart', '/usr/lib64/nagios/plugins/check_ide_smart')


def getBrickStatus(volInfo):
    bricks = {}
    hostUuid = glustercli.hostUUIDGet()
    for volumeName, volumeInfo in volInfo.iteritems():
        for brick in volumeInfo['bricksInfo']:
            if brick.get('hostUuid') != hostUuid:
                continue
            brickPath = brick['name'].split(':')[1]
            if volumeInfo['volumeStatus'] == glustercli.VolumeStatus.OFFLINE:
                status = utils.PluginStatusCode.CRITICAL
                msg = "CRITICAL: Brick %s is down" % brickPath
            else:
                status, msg = check_proc_util.getBrickStatus(volumeName,
                                                             brick['name'])
            brickService = _brickService % brickPath
            bricks[brickService] = [status, msg]
    return bricks


class Status():
    def __init__(self, code=None, message=None):
        self.code = code
        self.message = message

    def isStatusChanged(self, code, message):
        if (self.code, self.message) != (code, message) or \
           code == utils.PluginStatusCode.CRITICAL:
            self.code = code
            self.message = message
            return True
        return False


class App():
    def __init__(self):
        self.stdin_path = '/dev/null'
        self.stdout_path = '/dev/null'
        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 = Status()
        nfsStatus = Status()
        smbStatus = Status()
        shdStatus = Status()
        quotaStatus = Status()
        ctdbStatus = Status()
        brickStatus = {}
        while True:
            if not hostName:
                hostName = nscautils.getCurrentHostNameInNagiosServer()
                if not hostName:
                    logger.warn("Hostname is not configured")
                    time.sleep(sleepTime)
                    continue
            status, msg = check_proc_util.getGlusterdStatus()
            if glusterdStatus.isStatusChanged(status, msg):
                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 = check_proc_util.getNfsStatus(volInfo)
            if nfsStatus.isStatusChanged(status, msg):
                nscautils.send_to_nsca(hostName, _nfsService, status, msg)

            status, msg = check_proc_util.getSmbStatus(volInfo)
            if smbStatus.isStatusChanged(status, msg):
                nscautils.send_to_nsca(hostName, _smbService, status, msg)

            status, msg = check_proc_util.getCtdbStatus(smbStatus, nfsStatus)
            if ctdbStatus.isStatusChanged(status, msg):
                nscautils.send_to_nsca(hostName, _ctdbdService, status, msg)

            status, msg = check_proc_util.getShdStatus(volInfo)
            if shdStatus.isStatusChanged(status, msg):
                nscautils.send_to_nsca(hostName, _shdService, status, msg)

            status, msg = check_proc_util.getQuotadStatus(volInfo)
            if quotaStatus.isStatusChanged(status, msg):
                nscautils.send_to_nsca(hostName, _quotadService, status, msg)

            brick = getBrickStatus(volInfo)
            # brickInfo contains status, and message
            for brickService, brickInfo in brick.iteritems():
                if brickInfo != brickStatus.get(brickService, [None]) \
                   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 = handlers.TimedRotatingFileHandler(
        "/var/log/glusterpmd.log", 'midnight')
    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)