Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / mysql / mysql_aggregate_ @ 527cb89f

Historique | Voir | Annoter | Télécharger (6,5 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 42a11deb iborodikhin
# table: Mysql table name (no default, raises exception)
18
# field: field name, used in GROUP BY statement (default - empty, no group by)
19 c561076a iborodikhin
# where: optional where condition (without "where", default - empty)
20 ce558089 Alex Dehnert
# only: optional; "max" or "min" to indicate that only the largest or smallest value should be graphed
21 c561076a iborodikhin
#
22
# This plugin shows graphs of Mysql COUNT(*) results.
23
#
24
# ## Requirements
25
# This plugin requires pythons MySQLdb module which can be installed via easy_install.
26
#
27
# ## Installation
28
# Copy file to directory /usr/share/munin/pligins/ and create symbolic links for each table you wish to monitor.
29
# For example, if you wish to monitor how many users Mysql has per host create this symlink:
30
#
31
#     ln -s /usr/share/munin/plugins/mysql_aggregate_ /etc/munin/plugins/mysql_aggregate_user
32
#
33
# And specify some options in munin-node.conf:
34
#
35
#     [mysql_aggregate_*]
36
#     env.host 10.216.0.141
37
#     env.port 3306
38
#     env.user root
39
#     env.password vErYsEcReT
40
#     env.database mysql
41
#     env.table user
42
#     env.field Host
43 86847af6 iborodikhin
#     env.label Mysql users
44
#     env.vlabel users
45 c561076a iborodikhin
#
46
#%# capabilities=autoconf
47
#%# family=contrib
48
49
import os, sys, MySQLdb, MySQLdb.cursors
50
51
progName = sys.argv[0]
52
53
# Parse environment variables
54
# Mysql host
55
if "host" in os.environ and os.environ["host"] != None:
56
    server = os.environ["host"]
57
else:
58
    server =  "localhost"
59
60
# Mysql port
61
if "port" in os.environ and os.environ["port"] != None:
62
    try:
63
        port = int(os.environ["port"])
64
    except ValueError:
65
        port = 3306
66
else:
67
    port = 3306
68
69
# Mysql username
70
if "user" in os.environ and os.environ["user"] != None:
71
    login = os.environ["user"]
72
else:
73
    login = ""
74
75
# Mysql password
76
if "password" in os.environ and os.environ["password"] != None:
77
    passw = os.environ["password"]
78
else:
79
    passw = ""
80
81
# Mysql database
82
if "database" in os.environ and os.environ["database"] != None:
83
    db = os.environ["database"]
84
else:
85
    db = ""
86
87
# Mysql table name
88
if "table" in os.environ and os.environ["table"] != None:
89
    table = os.environ["table"]
90
else:
91
    raise Exception("You should provide 'env.table' in configuration file")
92
# Mysql group by field
93
if "field" in os.environ and os.environ["field"] != None:
94 42a11deb iborodikhin
    groupBy = "GROUP BY %s" % os.environ["field"]
95
    field = "%s, " % os.environ["field"]
96 c561076a iborodikhin
else:
97 42a11deb iborodikhin
    groupBy = ""
98
    field = ""
99 c561076a iborodikhin
100 ce558089 Alex Dehnert
if "only" in os.environ and os.environ["only"] != None:
101
    if not field:
102
        raise Exception("You should provide 'env.field' in configuration file")
103
    only = os.environ["only"]
104
    if only == "max":
105
        dir = "DESC"
106
    elif only == "min":
107
        dir = "ASC"
108
    else:
109
        raise Exception("env.only should be 'max' or 'min'; found %s") % (only, )
110
    order_by = "ORDER BY COUNT(*) %s LIMIT 1" % (dir, )
111
else:
112
    order_by = ""
113
114 c561076a iborodikhin
# Mysql where condition
115
if "where" in os.environ and os.environ["where"] != None:
116
    where = "WHERE %s" % os.environ["where"]
117
else:
118
    where = ""
119
120
# Mysql connection handler
121
conn = None
122
123 ce558089 Alex Dehnert
# Query to get field values (used only when graphing several values)
124 42a11deb iborodikhin
valuesQuery = "SELECT DISTINCT %s 1 FROM %s %s" % (field, table, where)
125 c561076a iborodikhin
# Query to get graph data
126 ce558089 Alex Dehnert
aggregateQuery = "SELECT %sCOUNT(*) FROM %s %s %s %s" % (field, table, where, groupBy, order_by)
127 c561076a iborodikhin
128
# Connect to mysql
129
try:
130
    conn = MySQLdb.connect(host=server, user=login, passwd=passw, db=db)
131
    cursor = conn.cursor()
132
except MySQLdb.Error, e:
133
    print "Error %d: %s" % (e.args[0], e.args[1])
134
    sys.exit(1)
135
136 ce558089 Alex Dehnert
single_value = field == "" or order_by != ""
137
138 c561076a iborodikhin
# init values tuple
139 ce558089 Alex Dehnert
if not single_value:
140 42a11deb iborodikhin
    values = {}
141
    cursor.execute(valuesQuery)
142
    results = cursor.fetchall()
143
    for result in results:
144
        values[result[0]] = 0
145 c561076a iborodikhin
146
if len(sys.argv) == 2 and sys.argv[1] == "autoconf":
147
    print "yes"
148
elif len(sys.argv) == 2 and sys.argv[1] == "config":
149 86847af6 iborodikhin
150
    if "label" in os.environ:
151
        label = os.environ["label"]
152
    else:
153
        label = "Aggregate - %s" % table
154
155
    if "vlabel" in os.environ:
156
        vlabel = os.environ["vlabel"]
157
    else:
158
        vlabel = "count(*)"
159
160 ce558089 Alex Dehnert
    if single_value:
161 42a11deb iborodikhin
        print "graph mysql_aggregate_%s" % table
162 86847af6 iborodikhin
        print "graph_title %s" % label
163
        print "graph_vlabel %s" % vlabel
164 c561076a iborodikhin
        print "graph_category mysql"
165
        print ""
166 ce558089 Alex Dehnert
        if "only" in os.environ:
167
            print "values_count.label %s" % (os.environ["only"], )
168
        else:
169
            print "values_count.label count"
170 42a11deb iborodikhin
    else:
171 c561076a iborodikhin
        print "multigraph mysql_aggregate_%s" % table
172 86847af6 iborodikhin
        print "graph_title %s" % label
173
        print "graph_vlabel %s" % vlabel
174 42a11deb iborodikhin
        print "graph_category mysql"
175
        print ""
176 c561076a iborodikhin
177
        for key in values.keys():
178 42a11deb iborodikhin
            print "%s_count.label %s" % (key.replace(".", "_"), key.replace(".", "_"))
179 c561076a iborodikhin
180
        for key in values.keys():
181
            print ""
182
            print "multigraph mysql_aggregate_%s.%s" % (table, key.replace(".", "_"))
183 86847af6 iborodikhin
            print "graph_title %s, value %s" % (label, key.replace(".", "_"))
184
            print "graph_vlabel %s" % vlabel
185 42a11deb iborodikhin
            print "graph_category mysql"
186
            print ""
187
            print "%s_count.label %s" % (key.replace(".", "_"), key)
188
            print ""
189
190
else:
191
    try:
192
        cursor.execute(aggregateQuery)
193
194 ce558089 Alex Dehnert
        if single_value:
195 42a11deb iborodikhin
            result = cursor.fetchone()
196
            count = 0
197 ce558089 Alex Dehnert
            if field:
198
                ind = 1
199
            else:
200
                ind = 0
201
            if result[ind]:
202
                count = count + result[ind]
203 42a11deb iborodikhin
            print "values_count.value %s" % count
204
        else:
205
            results = cursor.fetchall()
206
207
            for result in results:
208
                values[result[0]] = result[1]
209
            print "multigraph mysql_aggregate_%s" % table
210
211
            for key in values.keys():
212
                print "%s_count.value %s" % (key.replace(".", "_"), values[key])
213
214
            for key in values.keys():
215
                print ""
216
                print "multigraph mysql_aggregate_%s.%s" % (table, key.replace(".", "_"))
217
                print "%s_count.value %s" % (key.replace(".", "_"), values[key])
218 c561076a iborodikhin
219
    except MySQLdb.Error, e:
220
        print "Error %d: %s" % (e.args[0], e.args[1])
221
222
if conn:
223
    conn.close()