diff --git a/collectors/0/pxc-collector.py b/collectors/0/pxc-collector.py new file mode 100755 index 00000000..09d269fa --- /dev/null +++ b/collectors/0/pxc-collector.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python + +# This file is part of tcollector. +# Copyright (C) 2010-2016 The tcollector Authors. +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 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 Lesser +# General Public License for more details. You should have received a copy +# of the GNU Lesser General Public License along with this program. If not, +# see . + +""" +TCollector for Percona XtraDB Clusters + +It's possible to query every single 'wsrep'-Variable. +If you want to add more variables simply edit the pxcconf.py + +ATTENTION: Only tested on Debian/Ubuntu systems. +""" + +import MySQLdb as mysql +import time +import sys +import os.path +from collectors.etc import pxcconf +from collectors.lib import utils + +__author__ = "Kai Laufer" +__version__ = "1.0.1" +__email__ = "mail@kai-laufer.de" + +""" You can find these functions and additional information in etc/pxcconf.py """ +prefix = pxcconf.getPrefix() or "pxc" # Prefix for the collector, e.g. pxc -> pxc.wsrep_replicated_bytes +interval = pxcconf.getInterval() or 1 # Interval for checking MySQL statistics +galeraFile = pxcconf.getGaleraFile() or "/usr/lib/libgalera_smm.so" # Path to a galera specific file for ensuring that check won't run with a usual MySQL server. Default: "/usr/lib/libgalera_smm.so" +login = pxcconf.getUserPassword() # MySQL-User, MySQL-Password and MySQL-Host (localhost) +myMap = pxcconf.getKeyMap() or ( "wsrep_last_committed", "wsrep_replicated", "wsrep_repl_keys", "wsrep_local_commits" ) # Status variables which should be read +mysqlUser = login[0] or "root" +mysqlPasswd = login[1] or "" +mysqlHost = login[2] or "localhost" + +def getRow(): + """ Test connection """ + try: + db = mysql.connect(host=mysqlHost, user=mysqlUser, passwd=mysqlPasswd) + cursor = db.cursor() + cursor.execute("SHOW STATUS LIKE '%wsrep%'") + result = cursor.fetchall() + + except: + print "Error: unable to fetch data - Check your configuration!" + sys.exit(13) # Don't respawn collector + + db.close() + return result + +class TSDResult(object): + """ Create TSD output """ + def __init__(self, key, value, prefix, timestamp): + self.key = key + self.value = value + self.prefix = prefix + self.timestamp = timestamp + + def TSDRow(self): + return "%s.%s %s %s" % (self.prefix, self.key, self.timestamp, self.value) + +def main(): + if os.path.isfile(galeraFile) is True: + while True: + rows = getRow() + for row in rows: + timestamp = int(time.time()) + if row[0] in myMap: + result = TSDResult(row[0], row[1], prefix, timestamp) + print result.TSDRow() + time.sleep(interval) + return 0 + else: + return 2 + +if __name__ == "__main__": + sys.exit(main()) diff --git a/collectors/etc/pxcconf.py b/collectors/etc/pxcconf.py new file mode 100644 index 00000000..39b21a07 --- /dev/null +++ b/collectors/etc/pxcconf.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python + +def getUserPassword(): + return ("DBUser", "DBPassword", "DBHost") + +def getKeyMap(): + """ + You can use everything that is displayed + when you call "SHOW STATUS LIKE '%wsrep%'" + on your DB-host + """ + myMap = ( + "wsrep_last_committed", + "wsrep_replicated", + "wsrep_repl_keys", + "wsrep_local_commits", + "wsrep_received", + "wsrep_local_send_queue_avg", + "wsrep_local_recv_queue_avg" + ) + return myMap + +def getGaleraFile(): + """ + Used for ensuring that Percona XtraDB Cluster is installed + and not a common MySQL-Server + """ + return "/usr/lib/libgalera_smm.so" + +def getInterval(): + """ Interval in seconds """ + return 1 + +def getPrefix(): + return "pxc"