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
|
#!/usr/bin/python
#
# config_generator.py - Nagios configuration generator for gluster
# entities. 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
#
from jinja2 import Environment, FileSystemLoader
import os
import shutil
class GlusterNagiosConfManager:
def __init__(self, configDir, configTemplateDir, hostTemplateName):
self.configDir = configDir
self.configTemplateDir = configTemplateDir
self.hostTemplateName = hostTemplateName
self.__loadJinja()
def __loadJinja(self):
self.jinjaEnv = Environment(
loader=FileSystemLoader(self.configTemplateDir))
self.hostTemplate = self.jinjaEnv.get_template(self.hostTemplateName)
def __createHostConfig(self, host):
hostConfigStr = self.hostTemplate.render(host=host)
hostConfig = {'name': host['host_name'], 'config': hostConfigStr}
return hostConfig
def createHost(self, hostName, alias, template,
address, hostGroups, checkCommand, services):
host = {}
host['host_name'] = hostName
host['alias'] = alias
host['use'] = template
host['address'] = address
if checkCommand is not None:
host['check_command'] = checkCommand
if hostGroups is not None:
host['hostgroups'] = hostGroups
if services is not None:
host['host_services'] = services
return host
def __createVolumeUtilizationService(self, volume, clusterName):
volumeService = {}
volumeService['host_name'] = clusterName
volumeService['use'] = 'gluster-service-with-graph'
serviceDesc = 'Volume Utilization - %s' % (volume['name'])
volumeService['service_description'] = serviceDesc
volumeService['_VOL_NAME'] = volume['name']
checkCommand = 'check_vol_utilization!%s!%s!70!90' % \
(clusterName, volume['name'])
volumeService['check_command'] = checkCommand
volumeService['notes'] = "Volume type : %s" % (volume['typeStr'])
return volumeService
def __createVolumeStatusService(self, volume, clusterName):
volumeService = {}
volumeService['host_name'] = clusterName
volumeService['use'] = 'gluster-service-with-graph'
serviceDesc = 'Volume Status - %s' % (volume['name'])
volumeService['service_description'] = serviceDesc
volumeService['_VOL_NAME'] = volume['name']
checkCommand = 'check_vol_utilization!%s!%s!70!90' % \
(clusterName, volume['name'])
volumeService['check_command'] = checkCommand
volumeService['notes'] = "Volume type : %s" % (volume['typeStr'])
return volumeService
def createClusterUtilizationService(self, clusterName):
service = {}
service['host_name'] = clusterName
service['use'] = 'gluster-service-with-graph'
service['service_description'] = 'Cluster Utilization'
service['check_command'] = 'check_cluster_vol_usage!80!90'
return service
def createClusterAutoConfigService(self, clusterName, hostIp):
service = {}
service['host_name'] = clusterName
service['use'] = 'generic-service'
service['service_description'] = 'Cluster Auto Config'
service['check_command'] = "gluster_auto_discovery!%s" % (hostIp)
service['check_interval'] = '1440'
return service
def createrVolumeServices(self, volumes, clusterName):
volumeServices = []
for volume in volumes:
volumeService = self.__createVolumeUtilizationService(volume,
clusterName)
volumeServices.append(volumeService)
return volumeServices
def __createBrickUtilizationService(self, brick, hostName):
brickService = {}
brickService['use'] = 'brick-service'
brickService['host_name'] = hostName
serviceDesc = "Brick-%s:%s" % (hostName, brick['brickpath'])
brickService['service_description'] = serviceDesc
brickService['display_name'] = serviceDesc
brickService['_BRICK_DIR'] = brick['brickpath']
return brickService
def __createBrickStatusService(self, brick, hostName):
brickService = {}
brickService['use'] = 'brick-service'
brickService['host_name'] = hostName
serviceDesc = "Brick-%s:%s-Status" % (hostName, brick['brickpath'])
brickService['service_description'] = serviceDesc
brickService['display_name'] = serviceDesc
brickService['_BRICK_DIR'] = brick['brickpath']
return brickService
def createBrickServices(self, host):
brickServices = []
for brick in host['bricks']:
brickService = self.__createBrickUtilizationService(
brick, host['hostip'])
brickServices.append(brickService)
return brickServices
def generateNagiosConfigFromGlusterCluster(self, cluster):
hostsConfigs = []
clusterServices = self.createrVolumeServices(
cluster.get('volumes'), cluster['name'])
# If there are volumes, then create a cluster utilization service
if cluster.get('volumes'):
clusterServices.append(self.createClusterUtilizationService(
cluster['name']))
clusterServices.append(self.createClusterAutoConfigService(
cluster['name'], cluster['hosts'][-1]['hostip']))
clusterHostConfig = self.createHost(
cluster['name'], cluster['name'], "gluster-cluster",
cluster['name'], "", "check_dummy", clusterServices)
hostsConfigs.append(clusterHostConfig)
for host in cluster['hosts']:
brickServices = self.createBrickServices(host)
hostGroups = "gluster_hosts,%s" % (cluster['name'])
hostConfig = self.createHost(
host['hostip'], host['hostip'], "gluster-host",
host['hostip'], hostGroups, "", brickServices)
hostsConfigs.append(hostConfig)
self.generateConfigFiles(hostsConfigs)
return hostsConfigs
def generateConfigFiles(self, hosts):
clusterConfig = {'name': None, 'hostConfigs': []}
clusterConfigDir = None
for host in hosts:
if host['use'] == 'gluster-cluster':
clusterConfigDir = self.configDir + "/" + host['host_name']
self.__prepareConfDir(clusterConfigDir)
clusterConfig['name'] = host['host_name']
hostConfig = self.__createHostConfig(host)
clusterConfig['hostConfigs'].append(hostConfig)
for hostConfig in clusterConfig['hostConfigs']:
self.__writeHostConfig(clusterConfigDir, hostConfig)
def __prepareConfDir(self, confDir):
if os.path.exists(confDir):
# Deleting the config dir to write new configs
shutil.rmtree(confDir)
os.mkdir(confDir)
def __writeHostConfig(self, clusterConfigDir, hostConfig):
if clusterConfigDir is None:
raise Exception("Cluster configuration directory can't None")
configFilePath = clusterConfigDir + "/" + hostConfig['name'] + ".cfg"
with open(configFilePath, 'w') as configFile:
configFile.write(hostConfig['config'])
|