summaryrefslogtreecommitdiffstats
path: root/plugins/discovery.py
diff options
context:
space:
mode:
authorRamesh Nachimuthu <rnachimu@redhat.com>2014-03-28 17:48:54 +0530
committerBala.FA <barumuga@redhat.com>2014-04-29 10:21:37 +0530
commit05278f31d54d89ef0a68e94af389d66a3c3b6f74 (patch)
tree933d5ece03e544b190a5fac9a97d5b647b41d748 /plugins/discovery.py
parentb0f3cfc18011f149e1ce4b885fd47a6828a3c2ca (diff)
AutoDiscovery: Auto discovery for gluster entities
Basic plugin which will discover all the basic gluster entities and creates nagios configuration. Change-Id: I71f05dec9bcce74969db300393f7f7c178161dba Signed-off-by: Ramesh Nachimuthu <rnachimu@redhat.com> Reviewed-on: https://code.engineering.redhat.com/gerrit/22100 Reviewed-by: Sahina Bose <sabose@redhat.com>
Diffstat (limited to 'plugins/discovery.py')
-rwxr-xr-xplugins/discovery.py138
1 files changed, 138 insertions, 0 deletions
diff --git a/plugins/discovery.py b/plugins/discovery.py
new file mode 100755
index 0000000..3fd5573
--- /dev/null
+++ b/plugins/discovery.py
@@ -0,0 +1,138 @@
+#!/usr/bin/python
+# discovery.py Nagios plugin to discover Gluster entities using NRPE
+# 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 argparse
+import commands
+import json
+import datetime
+import re
+from config_generator import GlusterNagiosConfManager
+
+#from glusternagios import utils
+from constants import DEFAULT_AUTO_CONFIG_DIR
+from constants import HOST_TEMPLATE_DIR
+from constants import HOST_TEMPLATE_NAME
+from constants import NRPE_PATH
+from constants import NAGIOS_COMMAND_FILE_PATH
+
+
+def excecNRPECommand(command):
+ """
+ This function executes NRPE command and return the result
+ """
+ status = commands.getoutput(command)
+ return status
+
+
+def discoverhostdetails(host, args):
+ hostparamsdict = {}
+ command = NRPE_PATH + " -H " + host + " -c discoverhostparams"
+ hostparams = excecNRPECommand(command)
+ #convert to dictionary
+ try:
+ hostparamsdict = json.loads(hostparams)
+ except Exception, e:
+ e.args += (hostparams,)
+ raise
+ return hostparamsdict
+
+
+def discoverlogicalcomponents(host):
+ componentlist = []
+ command = NRPE_PATH + " -H " + host + " -c discoverlogicalcomponents"
+ components = excecNRPECommand(command)
+ try:
+ componentlist = json.loads(components)
+ except Exception, e:
+ e.args += (components,)
+ #print e.args
+ raise
+ return componentlist
+
+
+def discovercluster(args):
+ """
+
+ :rtype : None
+ """
+ ipPat = re.compile("^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$")
+ clusterdata = {}
+ #Discover the logical components
+ componentlist = discoverlogicalcomponents(args.hostip)
+ #Discover the peers
+ command = NRPE_PATH + " -H " + args.hostip + " -c discoverpeers"
+ hosts = excecNRPECommand(command)
+ hostlist = json.loads(hosts)
+
+ #Add the ip address of the root node to the peer list
+ #to generate the configuration
+ hostlist.append({"hostip": args.hostip})
+ for host in hostlist:
+ if(ipPat.match(host['hostip'])):
+ host.update(discoverhostdetails(host['hostip'], args))
+ #Get the list of bricks for this host and add to dictionary
+ host['bricks'] = \
+ [brick for brick in componentlist
+ if brick["hostip"] == host['hostip']]
+ clusterdata['hosts'] = hostlist
+ clusterdata['volumes'] =\
+ [volume for volume in componentlist
+ if volume["srvctype"] == "volume"]
+ clusterdata['name'] = args.cluster
+ return clusterdata
+
+
+def parse_input():
+ parser = argparse.ArgumentParser(description="Gluster Auto Discover Tool")
+ parser.add_argument('-c', '--cluster', action='store', dest='cluster',
+ type=str, required=True, help='Cluster name')
+ parser.add_argument('-H', '--hostip', action='store', dest='hostip',
+ type=str, required=True, help='Host IP')
+ parser.add_argument('-d', '--configdir', action='store', dest='configDir',
+ type=str, required=False,
+ help='Configuration directory '
+ 'where output files will be written')
+ args = parser.parse_args()
+ return args
+
+
+def getConfigManager(args):
+ configDir = DEFAULT_AUTO_CONFIG_DIR
+ if args.configDir is not None:
+ configDir = args.configDir
+ configManager = GlusterNagiosConfManager(
+ configDir, HOST_TEMPLATE_DIR, HOST_TEMPLATE_NAME)
+ return configManager
+
+
+def __restartNagios():
+ now = datetime.datetime.now()
+ cmdStr = "[%s] RESTART_PROGRAM\n" % (now)
+ with open(NAGIOS_COMMAND_FILE_PATH, "w") as f:
+ f.write(cmdStr)
+
+
+if __name__ == '__main__':
+ args = parse_input()
+ clusterdata = discovercluster(args)
+ configManager = getConfigManager(args)
+ clusterConfing = configManager.generateNagiosConfigFromGlusterCluster(
+ clusterdata)
+ print " Cluster configurations re-synced successfully from host %s" % \
+ (args.hostip)
+ __restartNagios()