Created
December 13, 2011 07:49
-
-
Save Surgo/1471128 to your computer and use it in GitHub Desktop.
Example of Zabbix script for MySQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import argparse | |
import subprocess | |
try: | |
from lxml import etree | |
except ImportError: | |
try: | |
import xml.etree.cElementTree as etree | |
except ImportError: | |
try: | |
import xml.etree.ElementTree as etree | |
except ImportError: | |
try: | |
import cElementTree as etree | |
except ImportError: | |
import elementtree.ElementTree as etree | |
SERVICE_NAME = 'mysql' | |
def _check_service(func): | |
def __check_service(*args, **kwargs): | |
try: | |
res = subprocess.check_output(['which', SERVICE_NAME, ]) | |
return func(res.strip(), *args, **kwargs) | |
except (subprocess.CalledProcessError) as e: | |
sys.exit("Cannot locate '%s'. Please check installation.\n" % SERVICE_NAME) | |
return __check_service | |
@_check_service | |
def _call(service, query, user=None, passwd=None): | |
args = [service, '--xml', | |
user and '-u%s' % user, (user and passwd) and '-p%s' % passwd, | |
'-e', '%s;' % query, ] | |
try: | |
res = subprocess.check_output([arg for arg in args if arg]) | |
return etree.fromstring(res.strip()) | |
except (subprocess.CalledProcessError) as e: | |
sys.exit("Failed to call '%s'.\n" % ' '.join(args)) | |
except (etree.ParseError) as e: | |
sys.exit("Failed to parse response. %s" % e) | |
def get_global_statuses(entry, user=None, passwd=None): | |
resultset = _call('SHOW GLOBAL STATUS', user=user, passwd=passwd) | |
resultset.tag == 'resultset' or sys.exit("Cannot get 'resultset'") | |
for row in [row for row in resultset.findall('.//row') if row.tag == 'row']: | |
fields = row.getchildren() | |
if len(fields) != 2: | |
continue | |
variable_name, value = fields | |
if variable_name.text.lower() == entry.lower(): | |
return '%s' % value.text | |
sys.exit("Failed to get entry '%s'" % entry) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='Set mysql user and password.') | |
parser.add_argument('entry', help='Entry') | |
parser.add_argument('-u', '--user', help='MySQL user') | |
parser.add_argument('-p', '--passwd', help='MySQL password') | |
args = parser.parse_args() | |
entry, user, passwd = args.entry, args.user, args.passwd | |
sys.stdout.write(get_global_statuses(entry)) |
Author
Surgo
commented
Dec 13, 2011
$ cat /etc/zabbix/zabbix_agentd.conf
...
UserParameter=mysql.innodb_buffer_pool_reads,/path/to/mon_mysql.py innodb_buffer_pool_reads
...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment