释放双眼,带上耳机,听听看~!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560 1#!/usr/bin/env python
2#-*- coding: UTF-8 -*-
3from __future__ import print_function
4from mysql import connector
5import logging,argparse,sys
6import sys
7
8#create user monitoruser@'127.0.0.1' identified by '123456';
9#grant replication client on *.* to monitoruser@'127.0.0.1';
10#grant super on *.* to monitoruser@'127.0.0.1';
11
12class MonitorItem(object):
13 """
14 所有监控项的基类
15 """
16 def __init__(self,user='monitoruser',password='123456',host='127.0.0.1',port=3306):
17 """初始化属性与到数据库端的连接"""
18 self.user=user
19 self.password=password
20 self.host=host
21 self.port=port
22 self.cnx=None
23 self.cursor=None
24 try:
25 config={'user':self.user,'password':self.password,'host':self.host,'port':self.port}
26 self.cnx=connector.connect(**config)
27 self.cursor=self.cnx.cursor(prepared=True)
28 except connector.Error as err:
29 """如果连接失败就赋空值"""
30 self.cnx=None
31 self.cursor=None
32 sys.stderr.write(err.msg+'\n')
33
34 def __str__(self):
35 attrs={}
36 attrs['user']=self.user
37 attrs['password']=self.password
38 attrs['host']=self.host
39 attrs['port']=self.port
40 return "instance of {0} {1}".format(self.__class__,attrs)
41
42 def __del__(self):
43 """在python 进行垃圾回收时关闭连接"""
44 if self.cnx != None:
45 self.cnx.close()
46
47 def get_result(self):
48 """返回监控项的状态,由子类实现相应的功能"""
49 pass
50
51 def print_result(self):
52 """打印监控项的状态"""
53 print(self.get_result())
54
55 def action(self):
56 """监控项达到阀值时可以触发的操作"""
57 print("末定义任何有意义的操作")
58
59########计算磁盘使用率##############
60class MysqlDiskUsed(MonitorItem):
61 def get_result(self):
62 try:
63 sql_cmd = "select TABLE_SCHEMA, concat(truncate(sum(data_length)/1024/1024,2)) as data_size,concat(truncate(sum(index_length)/1024/1024,2)) as index_size from information_schema.tables group by TABLE_SCHEMA order by data_length desc;" #单位MB
64 self.cursor.execute(sql_cmd)
65 row = self.cursor.fetchall()
66 #self.cursor.close()
67 #self.cnx.close()
68 disk_size = 0
69 for i in row:
70 for j in i[1:]:
71 disk_size += float(j.decode('utf8'))
72 disk_used_percent = disk_size / 50 * 100
73 return int(disk_used_percent)
74 except Exception as err:
75 sys.stderr.write(err.__str__()+'\n')
76 return -1
77#####################################
78
79################计算内存使用率##############
80class MysqlMemUsed(MonitorItem):
81 """计算内存使用率"""
82 def get_result(self):
83 try:
84 sql_cmd = "select (@@key_buffer_size + @@query_cache_size + @@tmp_table_size +@@innodb_buffer_pool_size +@@innodb_additional_mem_pool_size +@@innodb_log_buffer_size +@@max_connections * (@@read_buffer_size +@@read_rnd_buffer_size +@@sort_buffer_size + @@join_buffer_size +@@binlog_cache_size +@@thread_stack)) /1024/1024/1024 AS MAX_MEMORY_GB;"
85 self.cursor.execute(sql_cmd)
86 row = self.cursor.fetchone()
87 mem_used_GB = 0 #代为GB
88 mem_used_GB = float(row[0].decode('utf8'))
89 mem_used_percent = mem_used_GB / 16 * 100
90 return "%.2f" % mem_used_percent
91 except Exception as err:
92 sys.stderr.write(err.__str__()+'\n')
93 return -1
94
95
96#以下类用于检测MySQL数据库的正常与否
97class IsAlive(MonitorItem):
98 """监控MySQL数据库是否正常运行、{正常:on line,宕机:off line}"""
99 def get_result(self):
100 if self.cnx != None:
101 return "on line"
102 else:
103 return "off line"
104
105#以下类用于检测MySQL数据库的基本信息
106class MysqlVariable(MonitorItem):
107 """派生自MonitorItem类,用于所有variable 监控项的基类"""
108 variable_name=None
109 def get_result(self):
110 try:
111 if self.cursor != None:
112 stmt=r"""show global variables like '{0}';""".format(self.variable_name)
113 self.cursor.execute(stmt)
114 return self.cursor.fetchone()[1].decode('utf8')
115 except Exception as err:
116 sys.stderr.write(err.__str__()+'\n')
117 return -1
118
119class MysqlPort(MonitorItem):
120 """监控MySQL数据库监听是否正常、{正常:端口号,异常:-1}"""
121 def get_result(self):
122 if self.cnx != None:
123 return self.port
124 else:
125 return -1
126
127class MysqlBasedir(MysqlVariable):
128 """监控MySQL安装目录所在位置,{正常:安装目录位置,异常:-1}"""
129 variable_name="basedir"
130
131class MysqlDatadir(MysqlVariable):
132 """监控MySQL数据目录所在位置,{正常:数据目录位置,异常:-1}"""
133 variable_name="datadir"
134
135class MysqlVersion(MysqlVariable):
136 """监控MySQL版本号,{正常:版本号,异常:-1}"""
137 variable_name="version"
138
139class MysqlServerId(MysqlVariable):
140 """监控MySQL的server_id"""
141 variable_name="server_id"
142
143class MysqlLogBin(MysqlVariable):
144 """binlog 是否有开启"""
145 variable_name="log_bin"
146
147class MysqlLogError(MysqlVariable):
148 """errorlog文件名"""
149 variable_name="log_error"
150
151class MysqlPerformanceSchema(MysqlVariable):
152 """performance_schema是否有开启"""
153 variable_name="performance_schema"
154
155class MysqlInnodbBufferPoolSize(MysqlVariable):
156 """监控MySQL innodb_buffer_pool的大小,{正常:缓冲池大小(byte),异常:-1}"""
157 variable_name="innodb_buffer_pool_size"
158
159class MysqlMaxConnections(MysqlVariable):
160 """最大连接数"""
161 variable_name="max_connections"
162
163#派生自MonitorItem类,用于所有status 监控项的基类
164class MysqlStatu(MonitorItem):
165 """派生自MonitorItem类,用于所有statu 监控项的基类"""
166 statu_name=None
167 def get_result(self):
168 try:
169 if self.cursor != None:
170 stmt=r"""show global status like '{0}';""".format(self.statu_name)
171 self.cursor.execute(stmt)
172 return self.cursor.fetchone()[1].decode('utf8')
173 except Exception as err:
174 sys.stderr.write(err.__str__()+'\n')
175 return -1
176
177
178class MysqlCurrentClient(MysqlStatu):
179 """当前的客户端连接数"""
180 statu_name="Threads_connected"
181
182class MysqlTableOpenCacheHitRate(MysqlStatu):
183 """表缓存命中率"""
184 def get_result(self):
185 try:
186 if self.cursor != None:
187 stmt=r"""show global status like 'table_open_cache_hits';"""
188 self.cursor.execute(stmt)
189 hit=float((self.cursor.fetchone()[1].decode('utf8')))
190 stmt=r"""show global status like 'table_open_cache_misses';"""
191 self.cursor.execute(stmt)
192 miss=float(self.cursor.fetchone()[1].decode('utf8'))
193 return hit/(hit+miss)
194 except Exception as err:
195 sys.stderr.write(err.__str__())
196 return -1
197
198
199class MysqlTableOpenCacheOverflows(MysqlStatu):
200 """表缓存溢出次数,如果大于0,可以增大table_open_cache和table_open_cache_instances."""
201 statu_name="Table_open_cache_overflows"
202
203class MysqlTableLocksWaited(MysqlStatu):
204 """因不能立刻获得表锁而等待的次数"""
205 statu_name="table_locks_waited"
206
207class MysqlSlowqueries(MysqlStatu):
208 """执行时间超过long_query_time的查询次数,不管慢查询日志有没有打开"""
209 statu_name="slow_queries"
210
211class MysqlSortScan(MysqlStatu):
212 """全表扫描之后又排序(排序键不是主键)的次数"""
213 statu_name="sort_scan"
214
215class MysqlSortRows(MysqlStatu):
216 """与sortscan差不多,前者指的是sortscan的次数,srotrows指的是sort操作影响的行数"""
217 statu_name="sort_rows"
218
219class MysqlSortRange(MysqlStatu):
220 """根据索引进行范围扫描之后再进行排序(排序键不能是主键)的次数"""
221 statu_name="sort_range"
222
223class MysqlSortMergePasses(MysqlStatu):
224 """排序时归并的次数,如果这个值比较大(要求高一点大于0)那么可以考虑增大sort_buffer_size的大小"""
225 statu_name="sort_merge_passes"
226
227class MysqlSelectRangeCheck(MysqlStatu):
228 """如果这个值不是0那么就要好好的检查表上的索引了"""
229 statu_name="select_range_check"
230
231class MysqlQuestions(MysqlStatu):
232 """erver端执行的语句数量,但是每执行一个语句它又只增加一,这点让我特别被动"""
233 statu_name="Questions"
234
235class MysqlQcacheFreeMemory(MysqlStatu):
236 """query cache 的可用内存大小"""
237 statu_name="qcache_free_memory"
238
239class MysqlPreparedStmtCount(MysqlStatu):
240 """由于本监控程序就是通过prepare语句完成的,所以这个监控项的值最少会是1不是0"""
241 statu_name="prepared_stmt_count"
242
243class MysqlOpenedTables(MysqlStatu):
244 """mysql数据库打开过的表,如果这个值过大,应该适当的增大table_open_cache的值"""
245 statu_name="opened_tables"
246
247class MysqlOpenTables(MysqlStatu):
248 """当前mysql数据库打开的表数量"""
249 statu_name="open_tables"
250
251class MysqlServerLevelOpenFiles(MysqlStatu):
252 """mysql数据库的server层当前正打开的文件数据"""
253 statu_name="open_files"
254
255class MysqlInnodbAvailableUndoLogs(MysqlStatu):
256 """innodb当前可用的undo段的数据"""
257 statu_name="innodb_available_undo_logs"
258
259class MysqlInnodbNumOpenFiles(MysqlStatu):
260 """innodb当前打开的文件数量"""
261 statu_name="innodb_num_open_files"
262
263class MysqlInnodbRowsUpdated(MysqlStatu):
264 """innodb层面执行的update所影响的行数"""
265 statu_name="innodb_rows_updated"
266
267class MysqlInnodbRowsRead(MysqlStatu):
268 """innodb 层面受读操作所影响的行数"""
269 statu_name="innodb_rows_read"
270
271class MysqlInnodbRowsInserted(MysqlStatu):
272 """innodb 层面受insert操作所影响的行数"""
273 statu_name="innodb_rows_inserted"
274
275class MysqlInnodbRowsDeleted(MysqlStatu):
276 """innodb 层面受delete操作所影响的行数"""
277 statu_name="innodb_rows_deleted"
278
279class MysqlInnodbRowLockWaits(MysqlStatu):
280 """innodb 行锁等待的次数"""
281 statu_name="innodb_row_lock_waits"
282
283class MysqlInnodbRowLockTimeMax(MysqlStatu):
284 """innodb层面行锁等待的最大毫秒数"""
285 statu_name="innodb_row_lock_time_max"
286
287class MysqlInnodbRowLockTimeAvg(MysqlStatu):
288 """innodb层面行锁等待的平均毫秒数"""
289 statu_name="Innodb_row_lock_time_avg"
290
291class MysqlInnodbRowLockTime(MysqlStatu):
292 """innodb层面行锁等待的总毫秒数"""
293 statu_name="Innodb_row_lock_time"
294
295class MysqlInnodbPagesWritten(MysqlStatu):
296 """innodb层面写入磁盘的页面数"""
297 statu_name="Innodb_pages_written"
298
299class MysqlInnodbPagesRead(MysqlStatu):
300 """从innodb buffer pool 中读取的页数"""
301 statu_name="Innodb_pages_read"
302
303class MysqlInnodbOsLogWritten(MysqlStatu):
304 """innodb redo 写入字节数"""
305 statu_name="Innodb_os_log_written"
306
307class MysqlInnodbOsLogPendingWrites(MysqlStatu):
308 """innodb redo log 被挂起的写操作次数"""
309 statu_name="Innodb_os_log_pending_writes"
310
311class MysqlInnodbOsLogPendingFsyncs(MysqlStatu):
312 """innodb redo log 被挂起的fsync操作次数"""
313 statu_name="Innodb_os_log_pending_fsyncs"
314
315class MysqlInnodbOsLogFsyncs(MysqlStatu):
316 """innodb redo log fsync的次数"""
317 statu_name="Innodb_os_log_fsyncs"
318
319class MysqlInnodbLogWrites(MysqlStatu):
320 """innodb redo log 物理写的次数"""
321 statu_name="innodb_log_writes"
322
323class MysqlInnodbLogWriteRequests(MysqlStatu):
324 """innodb redo log 逻辑写的次数"""
325 statu_name="Innodb_log_write_requests"
326
327class MysqlInnodbLogWaits(MysqlStatu):
328 """innodb 写redo 之前必须等待的次数"""
329 statu_name="Innodb_log_waits"
330
331class MysqlInnodbDblwrWrites(MysqlStatu):
332 """innodb double write 的次数"""
333 statu_name="Innodb_dblwr_writes"
334
335class MysqlInnodbDblwrPagesWritten(MysqlStatu):
336 """innodb double write 的页面数量"""
337 statu_name="Innodb_dblwr_pages_written"
338
339class MysqlInnodbDoubleWriteLoader(MysqlStatu):
340 """innodb double write 压力1~64、数值越大压力越大"""
341 def get_result(self):
342 try:
343 if self.cursor != None:
344 stmt=r"""show global status like 'innodb_dblwr_pages_written';"""
345 self.cursor.execute(stmt)
346 pages=float((self.cursor.fetchone()[1].decode('utf8')))
347 stmt=r"""show global status like 'innodb_dblwr_writes';"""
348 self.cursor.execute(stmt)
349 requests=float(self.cursor.fetchone()[1].decode('utf8'))
350 if requests == 0:
351 return 0
352 return pages/requests
353 except Exception as err:
354 sys.stderr.write(err.__str__())
355 return -1
356
357class MysqlInnodbBufferPoolHitRate(MysqlStatu):
358 """innodb buffer pool 命中率"""
359 def get_result(self):
360 try:
361 if self.cursor != None:
362 stmt=r"""show global status like 'innodb_buffer_pool_read_requests';"""
363 self.cursor.execute(stmt)
364 hit_read=float((self.cursor.fetchone()[1].decode('utf8')))
365 stmt=r"""show global status like 'innodb_buffer_pool_reads';"""
366 self.cursor.execute(stmt)
367 miss_read=float(self.cursor.fetchone()[1].decode('utf8'))
368 total_read=(miss_read+hit_read)
369 if total_read == 0:
370 return 0
371 return hit_read/total_read
372 except Exception as err:
373 sys.stderr.write(err.__str__())
374 return -1
375
376class MysqlInnodbBufferPoolFreePagePercent(MysqlStatu):
377 """innodb buffer pool free page 百分比"""
378 def get_result(self):
379 try:
380 if self.cursor != None:
381 stmt=r"""show global status like 'innodb_buffer_pool_pages_total';"""
382 self.cursor.execute(stmt)
383 total_page=float((self.cursor.fetchone()[1].decode('utf8')))
384 stmt=r"""show global status like 'innodb_buffer_pool_pages_free';"""
385 self.cursor.execute(stmt)
386 free_page=float(self.cursor.fetchone()[1].decode('utf8'))
387 return free_page/total_page
388 except Exception as err:
389 sys.stderr.write(err.__str__())
390 return -1
391
392class MysqlInnodbBufferPoolDirtyPercent(MysqlStatu):
393 """innodb buffer pool dirty page 百分比"""
394 def get_result(self):
395 try:
396 if self.cursor != None:
397 stmt=r"""show global status like 'innodb_buffer_pool_pages_total';"""
398 self.cursor.execute(stmt)
399 total_page=float((self.cursor.fetchone()[1].decode('utf8')))
400 stmt=r"""show global status like 'innodb_buffer_pool_pages_dirty';"""
401 self.cursor.execute(stmt)
402 dirty_page=float(self.cursor.fetchone()[1].decode('utf8'))
403 return dirty_page/total_page
404 except Exception as err:
405 sys.stderr.write(err.__str__())
406 return -1
407
408class MysqlCreated_tmp_disk_tables(MysqlStatu):
409 """mysql运行时所创建的磁盘临时表的数量,如果这个数值比较大,可以适当的增大 tmp_table_size | max_heap_table_size"""
410 statu_name="Created_tmp_disk_tables"
411
412class MysqlComSelect(MysqlStatu):
413 """select 语句执行的次数"""
414 statu_name="com_select"
415
416class MysqlComInsert(MysqlStatu):
417 """insert 语句执行的次数"""
418 statu_name="com_insert"
419
420class MysqlComDelete(MysqlStatu):
421 """delete 语句执行的次数"""
422 statu_name="com_delete"
423
424class MysqlComUpdate(MysqlStatu):
425 """update 语句执行的次数"""
426 statu_name="com_update"
427
428class MysqlBinlogCacheDiskUse(MysqlStatu):
429 """事务引擎因binlog缓存不足而用到临时文件的次数,如果这个值过大,可以通过增大binlog_cache_size来解决"""
430 statu_name="Binlog_cache_disk_use"
431
432class MysqlBinlogStmtCacheDiskUse(MysqlStatu):
433 """非事务引擎因binlog缓存不足而用到临时文件的次数,如果这个值过大,可以通过增大binlog_stmt_cache_size来解决"""
434 statu_name="Binlog_stmt_cache_disk_use"
435
436class MysqlReplication(MonitorItem):
437 """所有监控mysql replication的基类"""
438 def __init__(self,user='monitoruser',password='123456',host='127.0.0.1',port=3306):
439 MonitorItem.__init__(self,user,password,host,port)
440 try:
441 if self.cursor != None:
442 stmt="show slave status;"
443 self.cursor.execute(stmt)
444 self.replication_info=self.cursor.fetchone()
445 except Exception as err:
446 pass
447
448class MysqlReplicationIsRunning(MysqlReplication):
449 """mysql replication 是否正常运行"""
450 def get_result(self):
451 if self.replication_info == None:
452 return "replication is not running"
453 else:
454 slave_io_running=self.replication_info[10].decode('utf8')
455 slave_sql_running=self.replication_info[11].decode('utf8')
456 if slave_io_running == 'Yes' and slave_sql_running == 'Yes':
457 return "running"
458 return "replication is not running"
459
460class MysqlReplicationBehindMaster(MysqlReplication):
461 """监控seconde behind master """
462 def get_result(self):
463 if self.replication_info != None:
464 return self.replication_info[32]
465 else:
466 return -1
467
468
469
470
471#监控项字典
472items={
473 #实例配置信息收集项
474 'port' :MysqlPort,
475 'baseDir' :MysqlBasedir,
476 'dataDir' :MysqlDatadir,
477 'version' :MysqlVersion,
478 'serverId' :MysqlServerId,
479 'isBinlogEnable' :MysqlLogBin,
480 'isErrorlogEnable' :MysqlLogError,
481 'isPerformanceScheamEnable' :MysqlPerformanceSchema,
482 'innodbBufferPoolSize' :MysqlInnodbBufferPoolSize,
483 'maxConnections' :MysqlMaxConnections,
484
485
486 #实例运行时信息收集项
487 'isOnLine' :IsAlive,
488 'currentConnections' :MysqlCurrentClient,
489 'tableCacheHitRate' :MysqlTableOpenCacheHitRate,
490 'tableOpenCacheOverflows' :MysqlTableOpenCacheOverflows,
491 'tableLocksWaited' :MysqlTableLocksWaited,
492 'slowqueries' :MysqlSlowqueries,
493 'sortScan' :MysqlSortScan,
494 'sortRows' :MysqlSortRows,
495 'sortRange' :MysqlSortRange,
496 'sortMergePasses' :MysqlSortMergePasses,
497 'selectRangeCheck' :MysqlSelectRangeCheck,
498 'questions' :MysqlQuestions,
499 'qcacheFreeMemory' :MysqlQcacheFreeMemory,
500 'preparedStmtCount' :MysqlPreparedStmtCount,
501 'openedTables' :MysqlOpenedTables,
502 'openTables' :MysqlOpenTables,
503 'serverLevelOpenFiles' :MysqlServerLevelOpenFiles,
504 'created_tmp_disk_tables' :MysqlCreated_tmp_disk_tables,
505 'comSelect' :MysqlComSelect,
506 'comInsert' :MysqlComInsert,
507 'comDelete' :MysqlComDelete,
508 'comUpdate' :MysqlComUpdate,
509 'binlogCacheDiskUse' :MysqlBinlogCacheDiskUse,
510 'binlogStmtCacheDiskUse' :MysqlBinlogStmtCacheDiskUse,
511 'MysqlDiskUsed' :MysqlDiskUsed,
512 'MysqlMemUsed':MysqlMemUsed,
513
514 #innodb运行时信息收集项
515 'innodbAvailableUndoLogs' :MysqlInnodbAvailableUndoLogs,
516 'innodbOpenFiles' :MysqlInnodbNumOpenFiles,
517 'innodbRowsUpdated' :MysqlInnodbRowsUpdated,
518 'innodbRowsRead' :MysqlInnodbRowsRead,
519 'innodbRowsInserted' :MysqlInnodbRowsInserted,
520 'innodbRowsDeleted' :MysqlInnodbRowsDeleted,
521 'innodbRowLockWaits' :MysqlInnodbRowLockWaits,
522 'innodbRowLockTimeMax' :MysqlInnodbRowLockTimeMax,
523 'innodbRowLockTimeAvg' :MysqlInnodbRowLockTimeAvg,
524 'innodbRowLockTime' :MysqlInnodbRowLockTime,
525 'innodbPagesWritten' :MysqlInnodbPagesWritten,
526 'innodbPagesRead' :MysqlInnodbPagesRead,
527 'innodbOsLogWritten' :MysqlInnodbOsLogWritten,
528 'innodbOsLogPendingWrites' :MysqlInnodbOsLogPendingWrites,
529 'innodbOsLogPendingFsyncs' :MysqlInnodbOsLogPendingFsyncs,
530 'innodbOsLogFsyncs' :MysqlInnodbOsLogFsyncs,
531 'innodbLogWrites' :MysqlInnodbLogWrites,
532 'innodbLogWriteRequests' :MysqlInnodbLogWriteRequests,
533 'innodbLogWaits' :MysqlInnodbLogWaits,
534 'innodbDblwrWrites' :MysqlInnodbDblwrWrites,
535 'innodbDblwrPagesWritten' :MysqlInnodbDblwrPagesWritten,
536 'innodbDoubleWriteLoader' :MysqlInnodbDoubleWriteLoader,
537 'innodbBufferPoolHitRate' :MysqlInnodbBufferPoolHitRate,
538 'innodbBufferPoolFreePagePercent' :MysqlInnodbBufferPoolFreePagePercent,
539 'innodbBufferPoolDirtyPercent' :MysqlInnodbBufferPoolDirtyPercent,
540
541 #对mysql replication 的监控
542 'replicationIsRunning' :MysqlReplicationIsRunning,
543 'replicationBehindMaster' :MysqlReplicationBehindMaster,
544}
545
546#
547item_key_names=[name for name in items.keys()]
548
549
550if __name__=="__main__":
551 parser=argparse.ArgumentParser()
552 parser.add_argument('--user',default='root',help='user name for connect to mysql')
553 parser.add_argument('--password',default='xxxxxx',help='user password for connect to mysql')
554 parser.add_argument('--host',default='172.31.x.x',help='mysql host ip')
555 parser.add_argument('--port',default=1231,type=int,help='mysql port')
556 parser.add_argument('monitor_item_name',choices=item_key_names)
557 args=parser.parse_args()
558 m=items[args.monitor_item_name](host=args.host,port=args.port,user=args.user,password=args.password)
559 m.print_result()
560