2
0
Fork 0
mirror of https://github.com/munin-monitoring/contrib.git synced 2018-11-08 00:59:34 +01:00

Added simple graph mode for queries without GROUP BY statement

This commit is contained in:
iborodikhin 2012-09-12 10:17:19 +06:00
parent c561076a32
commit 42a11debca

View file

@ -14,8 +14,8 @@
# user: username to access Mysql server (default - empty) # user: username to access Mysql server (default - empty)
# password: password of Mysql user (default - empty) # password: password of Mysql user (default - empty)
# database: Mysql database name (default - empty) # database: Mysql database name (default - empty)
# table: Mysql table name (no defaul, raises exception) # table: Mysql table name (no default, raises exception)
# field: field name, used in GROUP BY statement (no default, raises exception) # field: field name, used in GROUP BY statement (default - empty, no group by)
# where: optional where condition (without "where", default - empty) # where: optional where condition (without "where", default - empty)
# #
# This plugin shows graphs of Mysql COUNT(*) results. # This plugin shows graphs of Mysql COUNT(*) results.
@ -88,9 +88,11 @@ else:
raise Exception("You should provide 'env.table' in configuration file") raise Exception("You should provide 'env.table' in configuration file")
# Mysql group by field # Mysql group by field
if "field" in os.environ and os.environ["field"] != None: if "field" in os.environ and os.environ["field"] != None:
field = os.environ["field"] groupBy = "GROUP BY %s" % os.environ["field"]
field = "%s, " % os.environ["field"]
else: else:
raise Exception("You should provide 'env.field' in configuration file") groupBy = ""
field = ""
# Mysql where condition # Mysql where condition
if "where" in os.environ and os.environ["where"] != None: if "where" in os.environ and os.environ["where"] != None:
@ -102,9 +104,9 @@ else:
conn = None conn = None
# Query to get field values # Query to get field values
valuesQuery = "SELECT DISTINCT %s FROM %s %s" % (field, table, where) valuesQuery = "SELECT DISTINCT %s 1 FROM %s %s" % (field, table, where)
# Query to get graph data # Query to get graph data
aggregateQuery = "SELECT %s, COUNT(*) FROM %s %s GROUP BY %s" % (field, table, where, field) aggregateQuery = "SELECT %sCOUNT(*) FROM %s %s %s" % (field, table, where, groupBy)
# Connect to mysql # Connect to mysql
try: try:
@ -115,6 +117,7 @@ except MySQLdb.Error, e:
sys.exit(1) sys.exit(1)
# init values tuple # init values tuple
if field != "":
values = {} values = {}
cursor.execute(valuesQuery) cursor.execute(valuesQuery)
results = cursor.fetchall() results = cursor.fetchall()
@ -124,6 +127,14 @@ for result in results:
if len(sys.argv) == 2 and sys.argv[1] == "autoconf": if len(sys.argv) == 2 and sys.argv[1] == "autoconf":
print "yes" print "yes"
elif len(sys.argv) == 2 and sys.argv[1] == "config": elif len(sys.argv) == 2 and sys.argv[1] == "config":
if field == "":
print "graph mysql_aggregate_%s" % table
print "graph_title Aggregate - %s" % table
print "graph_vlabel count(*)"
print "graph_category mysql"
print ""
print "values_count.label count"
else:
print "multigraph mysql_aggregate_%s" % table print "multigraph mysql_aggregate_%s" % table
print "graph_title Aggregate - %s" % table print "graph_title Aggregate - %s" % table
print "graph_vlabel count(*)" print "graph_vlabel count(*)"
@ -146,6 +157,14 @@ elif len(sys.argv) == 2 and sys.argv[1] == "config":
else: else:
try: try:
cursor.execute(aggregateQuery) cursor.execute(aggregateQuery)
if field == "":
result = cursor.fetchone()
count = 0
if result[0]:
count = count + result[0]
print "values_count.value %s" % count
else:
results = cursor.fetchall() results = cursor.fetchall()
for result in results: for result in results: