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
|
#!/usr/bin/python
# server_utils.py Utility methods used by nagios-server-addons module
# 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 json
import datetime
from pynag import Model
from glusternagios import utils
import submit_external_command
from constants import NRPE_PATH
serviceCmdPath = utils.CommandPath("service", "/sbin/service", )
nrpeCmdPath = utils.CommandPath("check_nrpe", NRPE_PATH, )
def restartNagios():
now = datetime.datetime.now()
cmdStr = "[%s] RESTART_PROGRAM\n" % (now)
submit_external_command.submitExternalCommand(cmdStr)
def isNagiosRunning():
(rc, out, err) = utils.execCmd([serviceCmdPath.cmd, 'nagios', 'status'])
if rc == 0:
return True
else:
return False
def getServiceConfig(serviceDesc, hostName):
serviceConfig = Model.Service.objects.filter(
service_description=serviceDesc, host_name=hostName)
if serviceConfig:
return serviceConfig[0]
else:
return None
def getServiceConfigByHost(hostName):
serviceConfigs = Model.Service.objects.filter(host_name=hostName)
return serviceConfigs
def getHostConfigByName(hostName):
hostConfigs = Model.Host.objects.filter(host_name=hostName)
if hostConfigs:
return hostConfigs[0]
else:
return None
def getHostConfigsForCluster(clusterName):
hostgroup = getHostGroup(clusterName)
if hostgroup:
return hostgroup.get_effective_hosts()
else:
return []
def getHostGroup(name):
hostgroup = Model.Hostgroup.objects.filter(hostgroup_name=name)
if hostgroup:
return hostgroup[0]
else:
return None
def getNRPEBaseCommand(host, timeout=None):
command = NRPE_PATH + " -H " + host
if timeout is not None:
command += " -t %s" % timeout
command += " -c "
return command
def execNRPECommand(host,
command,
arguments=None,
timeout=None,
json_output=True):
nrpeCmd = getNRPEBaseCommand(host, timeout).split()
nrpeCmd.append(command)
if arguments:
nrpeCmd.append('-a')
nrpeCmd.extend(arguments)
(returncode, outputStr, err) = utils.execCmd(nrpeCmd, raw=True)
if returncode == 0:
if json_output:
try:
resultDict = json.loads(outputStr)
except Exception as e:
e.args += (outputStr,)
raise
return resultDict
else:
return outputStr
else:
print "Failed to execute NRPE command '%s' in host '%s' " \
"\nError : %s" \
"Make sure NPRE server in host '%s' is configured to accept " \
"requests from Nagios server" % (command, host, outputStr, host)
sys.exit(utils.PluginStatusCode.CRITICAL)
|