summaryrefslogtreecommitdiffstats
path: root/plugins/nscautils.py.in
blob: 8826adeaa5cb8aa3e4fc0fa31f213821b4dd76c8 (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
# nscautils.py --utility methods to interact with Nagios NSCA
# 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 ConfigParser
import subprocess

from glusternagios import utils

__NAGIOSSERVER_CONF = "@nagiosconf@/nagios_server.conf"
__NSCA_CONF_PATH = "@nagiosconf@/send_nsca.cfg"
__NSCA_CMD_PATH = utils.CommandPath("nsca", "/usr/sbin/send_nsca")


def getNagiosServerIP():
    config = ConfigParser.ConfigParser()
    config.read(__NAGIOSSERVER_CONF)
    return config.get('NAGIOS-SERVER', 'nagios_server')


def getCurrentHostNameInNagiosServer():
    config = ConfigParser.ConfigParser()
    config.read(__NAGIOSSERVER_CONF)
    return config.get('HOST-NAME', 'hostname_in_nagios')


def getNagiosClusterName():
    config = ConfigParser.ConfigParser()
    config.read(__NAGIOSSERVER_CONF)
    return config.get('NAGIOS-DEFINTIONS', 'cluster_name')


def getProcessMonitorSleepTime():
    config = ConfigParser.ConfigParser()
    config.read(__NAGIOSSERVER_CONF)
    return config.get('HOST-CONF', 'proc-mon-sleep-time')


def send_to_nsca(hostName, serviceName, exitStatus, resultString):
    cmddata = '%s\t%s\t%s\t%s\n' % (hostName,
                                    serviceName,
                                    exitStatus,
                                    resultString)
    nagiosIP = getNagiosServerIP()
    command_send_nsca = [__NSCA_CMD_PATH.cmd, '-H', nagiosIP,
                         '-c', __NSCA_CONF_PATH]
    ret, out, err = utils.execCmd(command_send_nsca, data=cmddata)
    return ret


def send_to_nsca_subproc(hostName, serviceName, exitStatus, resultString):
    cmddata = '%s\t%s\t%s\t%s\n' % (hostName,
                                    serviceName,
                                    exitStatus,
                                    resultString)
    nagiosIP = getNagiosServerIP()
    p = subprocess.Popen(
        args=(
            __NSCA_CMD_PATH.cmd,
            '-c',  __NSCA_CONF_PATH,
            '-H', nagiosIP,
        ),
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        close_fds=True,
    )

    stdout, stderr = p.communicate(input=cmddata)
    rc = p.returncode
    return rc


def vol_service_name(volName, statusType=None):
    return "Volume %s - %s" % (statusType, volName)