root / plugins / mysql / mysql_aggregate_ @ c561076a
Historique | Voir | Annoter | Télécharger (4,79 ko)
| 1 | c561076a | iborodikhin | #!/usr/bin/env python |
|---|---|---|---|
| 2 | # -*- coding: utf-8 -*- |
||
| 3 | # vim: set fileencoding=utf-8 |
||
| 4 | # |
||
| 5 | # Munin plugin to show Mysql COUNT(*) results for multiple values |
||
| 6 | # |
||
| 7 | # Copyright Igor Borodikhin |
||
| 8 | # |
||
| 9 | # License : GPLv3 |
||
| 10 | # |
||
| 11 | # parsed environment variables: |
||
| 12 | # host: hostname or ip-address of Mysql server (default - localhost) |
||
| 13 | # port: port number of Mysql server (default - 3306) |
||
| 14 | # user: username to access Mysql server (default - empty) |
||
| 15 | # password: password of Mysql user (default - empty) |
||
| 16 | # database: Mysql database name (default - empty) |
||
| 17 | # table: Mysql table name (no defaul, raises exception) |
||
| 18 | # field: field name, used in GROUP BY statement (no default, raises exception) |
||
| 19 | # where: optional where condition (without "where", default - empty) |
||
| 20 | # |
||
| 21 | # This plugin shows graphs of Mysql COUNT(*) results. |
||
| 22 | # |
||
| 23 | # ## Requirements |
||
| 24 | # This plugin requires pythons MySQLdb module which can be installed via easy_install. |
||
| 25 | # |
||
| 26 | # ## Installation |
||
| 27 | # Copy file to directory /usr/share/munin/pligins/ and create symbolic links for each table you wish to monitor. |
||
| 28 | # For example, if you wish to monitor how many users Mysql has per host create this symlink: |
||
| 29 | # |
||
| 30 | # ln -s /usr/share/munin/plugins/mysql_aggregate_ /etc/munin/plugins/mysql_aggregate_user |
||
| 31 | # |
||
| 32 | # And specify some options in munin-node.conf: |
||
| 33 | # |
||
| 34 | # [mysql_aggregate_*] |
||
| 35 | # env.host 10.216.0.141 |
||
| 36 | # env.port 3306 |
||
| 37 | # env.user root |
||
| 38 | # env.password vErYsEcReT |
||
| 39 | # env.database mysql |
||
| 40 | # env.table user |
||
| 41 | # env.field Host |
||
| 42 | # |
||
| 43 | #%# capabilities=autoconf |
||
| 44 | #%# family=contrib |
||
| 45 | |||
| 46 | import os, sys, MySQLdb, MySQLdb.cursors |
||
| 47 | |||
| 48 | progName = sys.argv[0] |
||
| 49 | |||
| 50 | # Parse environment variables |
||
| 51 | # Mysql host |
||
| 52 | if "host" in os.environ and os.environ["host"] != None: |
||
| 53 | server = os.environ["host"] |
||
| 54 | else: |
||
| 55 | server = "localhost" |
||
| 56 | |||
| 57 | # Mysql port |
||
| 58 | if "port" in os.environ and os.environ["port"] != None: |
||
| 59 | try: |
||
| 60 | port = int(os.environ["port"]) |
||
| 61 | except ValueError: |
||
| 62 | port = 3306 |
||
| 63 | else: |
||
| 64 | port = 3306 |
||
| 65 | |||
| 66 | # Mysql username |
||
| 67 | if "user" in os.environ and os.environ["user"] != None: |
||
| 68 | login = os.environ["user"] |
||
| 69 | else: |
||
| 70 | login = "" |
||
| 71 | |||
| 72 | # Mysql password |
||
| 73 | if "password" in os.environ and os.environ["password"] != None: |
||
| 74 | passw = os.environ["password"] |
||
| 75 | else: |
||
| 76 | passw = "" |
||
| 77 | |||
| 78 | # Mysql database |
||
| 79 | if "database" in os.environ and os.environ["database"] != None: |
||
| 80 | db = os.environ["database"] |
||
| 81 | else: |
||
| 82 | db = "" |
||
| 83 | |||
| 84 | # Mysql table name |
||
| 85 | if "table" in os.environ and os.environ["table"] != None: |
||
| 86 | table = os.environ["table"] |
||
| 87 | else: |
||
| 88 | raise Exception("You should provide 'env.table' in configuration file")
|
||
| 89 | # Mysql group by field |
||
| 90 | if "field" in os.environ and os.environ["field"] != None: |
||
| 91 | field = os.environ["field"] |
||
| 92 | else: |
||
| 93 | raise Exception("You should provide 'env.field' in configuration file")
|
||
| 94 | |||
| 95 | # Mysql where condition |
||
| 96 | if "where" in os.environ and os.environ["where"] != None: |
||
| 97 | where = "WHERE %s" % os.environ["where"] |
||
| 98 | else: |
||
| 99 | where = "" |
||
| 100 | |||
| 101 | # Mysql connection handler |
||
| 102 | conn = None |
||
| 103 | |||
| 104 | # Query to get field values |
||
| 105 | valuesQuery = "SELECT DISTINCT %s FROM %s %s" % (field, table, where) |
||
| 106 | # Query to get graph data |
||
| 107 | aggregateQuery = "SELECT %s, COUNT(*) FROM %s %s GROUP BY %s" % (field, table, where, field) |
||
| 108 | |||
| 109 | # Connect to mysql |
||
| 110 | try: |
||
| 111 | conn = MySQLdb.connect(host=server, user=login, passwd=passw, db=db) |
||
| 112 | cursor = conn.cursor() |
||
| 113 | except MySQLdb.Error, e: |
||
| 114 | print "Error %d: %s" % (e.args[0], e.args[1]) |
||
| 115 | sys.exit(1) |
||
| 116 | |||
| 117 | # init values tuple |
||
| 118 | values = {}
|
||
| 119 | cursor.execute(valuesQuery) |
||
| 120 | results = cursor.fetchall() |
||
| 121 | for result in results: |
||
| 122 | values[result[0]] = 0 |
||
| 123 | |||
| 124 | if len(sys.argv) == 2 and sys.argv[1] == "autoconf": |
||
| 125 | print "yes" |
||
| 126 | elif len(sys.argv) == 2 and sys.argv[1] == "config": |
||
| 127 | print "multigraph mysql_aggregate_%s" % table |
||
| 128 | print "graph_title Aggregate - %s" % table |
||
| 129 | print "graph_vlabel count(*)" |
||
| 130 | print "graph_category mysql" |
||
| 131 | print "" |
||
| 132 | |||
| 133 | for key in values.keys(): |
||
| 134 | print "%s_count.label %s" % (key.replace(".", "_"), key.replace(".", "_"))
|
||
| 135 | |||
| 136 | for key in values.keys(): |
||
| 137 | print "" |
||
| 138 | print "multigraph mysql_aggregate_%s.%s" % (table, key.replace(".", "_"))
|
||
| 139 | print "graph_title Agregate - %s, value %s" % (table, key.replace(".", "_"))
|
||
| 140 | print "graph_vlabel count(*)" |
||
| 141 | print "graph_category mysql" |
||
| 142 | print "" |
||
| 143 | print "%s_count.label %s" % (key.replace(".", "_"), key)
|
||
| 144 | print "" |
||
| 145 | |||
| 146 | else: |
||
| 147 | try: |
||
| 148 | cursor.execute(aggregateQuery) |
||
| 149 | results = cursor.fetchall() |
||
| 150 | |||
| 151 | for result in results: |
||
| 152 | values[result[0]] = result[1] |
||
| 153 | print "multigraph mysql_aggregate_%s" % table |
||
| 154 | |||
| 155 | for key in values.keys(): |
||
| 156 | print "%s_count.value %s" % (key.replace(".", "_"), values[key])
|
||
| 157 | |||
| 158 | for key in values.keys(): |
||
| 159 | print "" |
||
| 160 | print "multigraph mysql_aggregate_%s.%s" % (table, key.replace(".", "_"))
|
||
| 161 | print "%s_count.value %s" % (key.replace(".", "_"), values[key])
|
||
| 162 | |||
| 163 | except MySQLdb.Error, e: |
||
| 164 | print "Error %d: %s" % (e.args[0], e.args[1]) |
||
| 165 | |||
| 166 | if conn: |
||
| 167 | conn.close() |
