9
9
GAUGE = "gauge"
10
10
RATE = "rate"
11
11
12
- QUERIES_COMMON = [
13
- ('mysql.net.connections' , "SHOW STATUS LIKE 'Connections'" , RATE ),
14
- ('mysql.net.max_connections' , "SHOW STATUS LIKE 'Max_used_connections'" , GAUGE ),
15
- ('mysql.performance.open_files' , "SHOW STATUS LIKE 'Open_files'" , GAUGE ),
16
- ('mysql.performance.table_locks_waited' , "SHOW STATUS LIKE 'Table_locks_waited'" , GAUGE ),
17
- ('mysql.performance.threads_connected' , "SHOW STATUS LIKE 'Threads_connected'" , GAUGE ),
18
- ('mysql.innodb.data_reads' , "SHOW STATUS LIKE 'Innodb_data_reads'" , RATE ),
19
- ('mysql.innodb.data_writes' , "SHOW STATUS LIKE 'Innodb_data_writes'" , RATE ),
20
- ('mysql.innodb.os_log_fsyncs' , "SHOW STATUS LIKE 'Innodb_os_log_fsyncs'" , RATE ),
21
- ('mysql.innodb.buffer_pool_size' , "SHOW STATUS LIKE 'Innodb_data_reads'" , RATE ),
22
- ]
23
-
24
- QUERIES_GREATER_502 = [
25
- ('mysql.performance.created_tmp_disk_tables' , "SHOW GLOBAL STATUS LIKE 'Created_tmp_disk_tables'" , GAUGE ),
26
- ('mysql.performance.slow_queries' , "SHOW GLOBAL STATUS LIKE 'Slow_queries'" , RATE ),
27
- ('mysql.performance.questions' , "SHOW GLOBAL STATUS LIKE 'Questions'" , RATE ),
28
- ('mysql.performance.queries' , "SHOW GLOBAL STATUS LIKE 'Queries'" , RATE ),
29
- ]
30
-
31
- QUERIES_OLDER_502 = [
32
- ('mysql.performance.created_tmp_disk_tables' , "SHOW STATUS LIKE 'Created_tmp_disk_tables'" , GAUGE ),
33
- ('mysql.performance.slow_queries' , "SHOW STATUS LIKE 'Slow_queries'" , RATE ),
34
- ('mysql.performance.questions' , "SHOW STATUS LIKE 'Questions'" , RATE ),
35
- ('mysql.performance.queries' , "SHOW STATUS LIKE 'Queries'" , RATE ),
36
- ]
37
-
12
+ STATUS_VARS = {
13
+ 'Connections' : ('mysql.net.connections' , RATE ),
14
+ 'Max_used_connections' : ('mysql.net.max_connections' , GAUGE ),
15
+ 'Open_files' : ('mysql.performance.open_files' , GAUGE ),
16
+ 'Table_locks_waited' : ('mysql.performance.table_locks_waited' , GAUGE ),
17
+ 'Threads_connected' : ('mysql.performance.threads_connected' , GAUGE ),
18
+ 'Innodb_data_reads' : ('mysql.innodb.data_reads' , RATE ),
19
+ 'Innodb_data_writes' : ('mysql.innodb.data_writes' , RATE ),
20
+ 'Innodb_os_log_fsyncs' : ('mysql.innodb.os_log_fsyncs' , RATE ),
21
+ 'Innodb_data_reads' : ('mysql.innodb.buffer_pool_size' , RATE ),
22
+ 'Created_tmp_disk_tables' : ('mysql.performance.created_tmp_disk_tables' , GAUGE ),
23
+ 'Slow_queries' : ('mysql.performance.slow_queries' , RATE ),
24
+ 'Questions' : ('mysql.performance.questions' , RATE ),
25
+ 'Queries' : ('mysql.performance.queries' , RATE ),
26
+ }
38
27
39
28
class MySql (AgentCheck ):
40
29
def __init__ (self , name , init_config , agentConfig ):
@@ -93,25 +82,19 @@ def _connect(self, host, port, mysql_sock, user, password, defaults_file):
93
82
return db
94
83
95
84
def _collect_metrics (self , host , db , tags , options ):
96
- if self ._version_greater_502 (db , host ):
97
- queries = QUERIES_GREATER_502 + QUERIES_COMMON
98
- else :
99
- queries = QUERIES_OLDER_502 + QUERIES_COMMON
100
-
101
- for metric_name , query , metric_type in queries :
102
- value = self ._collect_scalar (query , db )
103
- if value is not None :
104
- if metric_type == RATE :
105
- self .rate (metric_name , value , tags = tags )
106
- elif metric_type == GAUGE :
107
- self .gauge (metric_name , value , tags = tags )
85
+ cursor = db .cursor ()
86
+ cursor .execute ("SHOW /*!50002 GLOBAL */ STATUS;" )
87
+ results = dict (cursor .fetchall ())
88
+ self ._rate_or_gauge_statuses (STATUS_VARS , results , tags )
89
+ cursor .close ()
90
+ del cursor
108
91
109
92
# Compute InnoDB buffer metrics
110
- page_size = self ._collect_scalar ("SHOW STATUS LIKE 'Innodb_page_size'" , db )
111
93
# Be sure InnoDB is enabled
112
- if page_size :
113
- innodb_buffer_pool_pages_total = self ._collect_scalar ("SHOW STATUS LIKE 'Innodb_buffer_pool_pages_total'" , db )
114
- innodb_buffer_pool_pages_free = self ._collect_scalar ("SHOW STATUS LIKE 'Innodb_buffer_pool_pages_free'" , db )
94
+ if 'Innodb_page_size' in results :
95
+ page_size = self ._collect_scalar ('Innodb_page_size' , results )
96
+ innodb_buffer_pool_pages_total = self ._collect_scalar ('Innodb_buffer_pool_pages_total' , results )
97
+ innodb_buffer_pool_pages_free = self ._collect_scalar ('Innodb_buffer_pool_pages_free' , results )
115
98
innodb_buffer_pool_pages_total = innodb_buffer_pool_pages_total * page_size
116
99
innodb_buffer_pool_pages_free = innodb_buffer_pool_pages_free * page_size
117
100
innodb_buffer_pool_pages_used = innodb_buffer_pool_pages_total - innodb_buffer_pool_pages_free
@@ -120,12 +103,22 @@ def _collect_metrics(self, host, db, tags, options):
120
103
self .gauge ("mysql.innodb.buffer_pool_used" , innodb_buffer_pool_pages_used , tags = tags )
121
104
self .gauge ("mysql.innodb.buffer_pool_total" , innodb_buffer_pool_pages_total , tags = tags )
122
105
123
- if 'galera_cluster' in options . keys () and options ['galera_cluster' ]:
124
- value = self ._collect_scalar ("SHOW STATUS LIKE 'wsrep_cluster_size'" , db )
106
+ if 'galera_cluster' in options and options ['galera_cluster' ]:
107
+ value = self ._collect_scalar ('wsrep_cluster_size' , results )
125
108
self .gauge ('mysql.galera.wsrep_cluster_size' , value , tags = tags )
126
109
127
- if 'replication' in options . keys () and options ['replication' ]:
110
+ if 'replication' in options and options ['replication' ]:
128
111
self ._collect_dict (GAUGE , {"Seconds_behind_master" : "mysql.replication.seconds_behind_master" }, "SHOW SLAVE STATUS" , db , tags = tags )
112
+
113
+ def _rate_or_gauge_statuses (self , statuses , dbResults , tags ):
114
+ for status , metric in statuses .iteritems ():
115
+ metric_name , metric_type = metric
116
+ value = self ._collect_scalar (status , dbResults )
117
+ if value is not None :
118
+ if metric_type == RATE :
119
+ self .rate (metric_name , value , tags = tags )
120
+ elif metric_type == GAUGE :
121
+ self .gauge (metric_name , value , tags = tags )
129
122
130
123
def _version_greater_502 (self , db , host ):
131
124
# show global status was introduced in 5.0.2
@@ -170,21 +163,13 @@ def _get_version(self, db, host):
170
163
self .mysql_version [host ] = version
171
164
return version
172
165
173
- def _collect_scalar (self , query , db ):
174
- self .log .debug ("Collecting data with %s" % (query ))
175
- try :
176
- cursor = db .cursor ()
177
- cursor .execute (query )
178
- result = cursor .fetchone ()
179
- cursor .close ()
180
- del cursor
181
- if result is None :
182
- self .log .debug ("%s returned None" % query )
183
- return None
184
- self .log .debug ("Collecting done, value %s" % result [1 ])
185
- return float (result [1 ])
186
- except Exception :
187
- self .log .exception ("Error while running %s" % query )
166
+ def _collect_scalar (self , key , dict ):
167
+ self .log .debug ("Collecting data with %s" % key )
168
+ if key not in dict :
169
+ self .log .debug ("%s returned None" % key )
170
+ return None
171
+ self .log .debug ("Collecting done, value %s" % dict [key ])
172
+ return float (dict [key ])
188
173
189
174
def _collect_dict (self , metric_type , field_metric_map , query , db , tags ):
190
175
"""
0 commit comments