-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathmtls-monitor
executable file
·295 lines (278 loc) · 13.4 KB
/
mtls-monitor
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
#!/usr/bin/env python3
#-*- coding: UTF-8 -*-
from mtls import base,statu,variable,mgr,replication,binlog,innodb_statu
import argparse
#---------------------------------------
#monitor.py 用于实现对 mysql 的监控
#---------------------------------------
#定义最基本的mysql监控项
basic_items={
#定义mysql绝大多数variable 主要用于信息收集 对于性能监控的意思不大 但是有助于分析问题
'ServerID':variable.ServerID,
'BaseDir':variable.BaseDir,
'DataDir':variable.DataDir,
'Port':variable.Port,
'CharacterSetServer':variable.CharacterSetServer,
'Socket':variable.Socket,
'ReadOnly':variable.ReadOnly,
'SkipNameResolve': variable.SkipNameResolve,
'LowerCaseTableNames':variable.LowerCaseTableNames,
'ThreadCacheSize':variable.ThreadCacheSize,
'TableOpenCache':variable.TableOpenCache,
'TableDefinitionCache':variable.TableDefinitionCache,
'TableOpenCacheInstances':variable.TableOpenCacheInstances,
'MaxConnections':variable.MaxConnections,
'BinlogFormat':variable.BinlogFormat,
'LogBin':variable.LogBin,
'BinlogRowsQueryLogEvents':variable.BinlogRowsQueryLogEvents,
'LogSlaveUpdates':variable.LogSlaveUpdates,
'ExpireLogsDays':variable.ExpireLogsDays,
'BinlogCacheSize':variable.BinlogCacheSize,
'SyncBinlog':variable.SyncBinlog,
'ErrorLog':variable.ErrorLog,
'GtidMode':variable.GtidMode,
'EnforceGtidConsistency':variable.EnforceGtidConsistency,
'MasterInfoRepository': variable.MasterInfoRepository,
'RelayLogInfoRepository':variable.RelayLogInfoRepository,
'SlaveParallelType':variable.SlaveParallelType,
'SlaveParallelWorkers':variable.SlaveParallelWorkers,
'InnodbDataFilePath':variable.InnodbDataFilePath,
'InnodbTempDataFilePath':variable.InnodbTempDataFilePath,
'InnodbBufferPoolFilename':variable.InnodbBufferPoolFilename,
'InnodbLogGroupHomeDir':variable.InnodbLogGroupHomeDir,
'InnodbLogFilesInGroup':variable.InnodbLogFilesInGroup,
'InnodbLogFileSize':variable.InnodbLogFileSize,
'InnodbFileformat':variable.InnodbFileformat,
'InnodbFilePerTable':variable.InnodbFilePerTable,
'InnodbOnlineAlterLogMaxSize':variable.InnodbOnlineAlterLogMaxSize,
'InnodbOpenFiles':variable.InnodbOpenFiles,
'InnodbPageSize':variable.InnodbPageSize,
'InnodbThreadConcurrency':variable.InnodbThreadConcurrency,
'InnodbReadIoThreads':variable.InnodbReadIoThreads,
'InnodbWriteIoThreads':variable.InnodbWriteIoThreads,
'InnodbPurgeThreads':variable.InnodbPurgeThreads,
'InnodbLockWaitTimeout':variable.InnodbLockWaitTimeout,
'InnodbSpinWaitDelay':variable.InnodbSpinWaitDelay,
'InnodbAutoincLockMode':variable.InnodbAutoincLockMode,
'InnodbStatsAutoRecalc':variable.InnodbStatsAutoRecalc,
'InnodbStatsPersistent':variable.InnodbStatsPersistent,
'InnodbStatsPersistentSamplePages':variable.InnodbStatsPersistentSamplePages,
'InnodbBufferPoolInstances':variable.InnodbBufferPoolInstances,
'InnodbAdaptiveHashIndex': variable.InnodbAdaptiveHashIndex,
'InnodbChangeBuffering':variable.InnodbChangeBuffering,
'InnodbChangeBufferMaxSize':variable.InnodbChangeBufferMaxSize,
'InnodbFlushNeighbors':variable.InnodbFlushNeighbors,
'InnodbFlushMethod':variable.InnodbFlushMethod,
'InnodbDoublewrite':variable.InnodbDoublewrite,
'InnodbLogBufferSize':variable.InnodbLogBufferSize,
'InnodbFlushLogAtTimeout':variable.InnodbFlushLogAtTimeout,
'InnodbFlushLogAtTrxCommit':variable.InnodbFlushLogAtTrxCommit,
'InnodbBufferPoolSize':variable.InnodbBufferPoolSize,
'Autocommit':variable.Autocommit,
'InnodbOldBlocksPct':variable.InnodbOldBlocksPct,
'InnodbOldBlocksTime':variable.InnodbOldBlocksTime,
'InnodbReadAheadThreshold':variable.InnodbReadAheadThreshold,
'InnodbRandomReadAhead':variable.InnodbRandomReadAhead,
'InnodbBufferPoolDumpPct':variable.InnodbBufferPoolDumpPct,
'InnodbBufferPoolDumpAtShutdown':variable.InnodbBufferPoolDumpAtShutdown,
'InnodbBufferPoolLoadAtStartup':variable.InnodbBufferPoolLoadAtStartup,
'QueryCacheLimit':variable.QueryCacheLimit,
'QueryCacheMinResUnit':variable.QueryCacheMinResUnit,
'QueryCacheSize':variable.QueryCacheSize,
'QueryCacheType':variable.QueryCacheType,
#Binlog写入性能
'BinlogFile':binlog.BinlogFile,
'BinlogPosition':binlog.BinlogPosition,
'BinlogDoDB':binlog.BinlogDoDB,
'BinlogIgnoreDB':binlog.BinlogIgnoreDB,
#Innodb LSN、CheckPoint、LogFlushUpTo ...
'LogSequenceNumber':innodb_statu.LogSequenceNumber,
'LogFlushedUpTo':innodb_statu.LogFlushedUpTo,
'PagesFlushedUpTo':innodb_statu.PagesFlushedUpTo,
'LastCheckpointAt':innodb_statu.LastCheckpointAt,
#定义mysql绝大多数status 主要用于性能监控
'AbortedClients':statu.AbortedClients,
'AbortedConnects':statu.AbortedConnects,
'BinlogCacheDiskUse':statu.BinlogCacheDiskUse,
'BinlogCacheUse':statu.BinlogCacheUse,
'BinlogStmtCacheDiskUse':statu.BinlogStmtCacheDiskUse,
'BinlogStmtCacheUse':statu.BinlogStmtCacheUse,
'BytesReceived':statu.BytesReceived,
'BytesSent':statu.BytesSent,
'ComBegin':statu.ComBegin,
'ComCallProcedure':statu.ComCallProcedure,
'ComChangeMaster':statu.ComChangeMaster,
'ComCommit':statu.ComCommit,
'ComDelete':statu.ComDelete,
'ComDeleteMulti':statu.ComDeleteMulti,
'ComInsert':statu.ComInsert,
'ComInsertSelect':statu.ComInsertSelect,
'ComSelect':statu.ComSelect,
'SelectFullJoin':statu.SelectFullJoin,
'SelectFullRangeJoin':statu.SelectFullRangeJoin,
'SelectRange':statu.SelectRange,
'SelectRangeCheck':statu.SelectRangeCheck,
'SelectScan':statu.SelectScan,
'ComUpdate':statu.ComUpdate,
'ComUpdateMulti':statu.ComUpdateMulti,
'Connections':statu.Connections,
#DDL操作
'CreatedTmpDiskTables':statu.CreatedTmpDiskTables,
'CreatedTmpFiles':statu.CreatedTmpFiles,
'CreatedTmpTables':statu.CreatedTmpTables,
'ComCreateTable':statu.ComCreateTable,
'ComDropTable':statu.ComDropTable,
'ComRenameTable':statu.ComRenameTable,
'ComAlterTable':statu.ComAlterTable,
## Index
'ComCreateIndex':statu.ComCreateIndex,
'ComDropIndex':statu.ComDropIndex,
## User
'ComCreateUser':statu.ComCreateUser,
'ComAlterUser':statu.ComAlterUser,
'ComDropUser':statu.ComDropUser,
## Function
'ComCreateFunction':statu.ComCreateFunction,
'ComAlterFunction':statu.ComAlterFunction,
'ComDropFunction':statu.ComDropFunction,
## DB
'ComCreateDb':statu.ComCreateDb,
'ComAlterDb':statu.ComAlterDb,
'ComDropDb':statu.ComDropDb,
## Procedure
'ComCreateProcedure':statu.ComCreateProcedure,
'ComAlterProcedure':statu.ComAlterProcedure,
'ComDropProcedure':statu.ComDropProcedure,
## View
'ComCreateView':statu.ComCreateView,
'ComDropView':statu.ComDropView,
## Role
'ComCreateRole':statu.ComCreateRole,
'ComDropRole':statu.ComDropRole,
## Trigger
'ComCreateTrigger':statu.ComCreateTrigger,
'ComDropTrigger':statu.ComDropTrigger,
'InnodbBufferPoolDumpStatus':statu.InnodbBufferPoolDumpStatus,
'InnodbBufferPoolLoadStatus':statu.InnodbBufferPoolLoadStatus,
'InnodbBufferPoolResizeStatus':statu.InnodbBufferPoolResizeStatus,
'InnodbBufferPoolBytesData':statu.InnodbBufferPoolBytesData,
'InnodbBufferPoolPagesData':statu.InnodbBufferPoolPagesData,
'InnodbBufferPoolPagesDirty':statu.InnodbBufferPoolPagesDirty,
'InnodbBufferPoolBytesDirty':statu.InnodbBufferPoolBytesDirty,
'InnodbBufferPoolPagesFlushed':statu.InnodbBufferPoolPagesFlushed,
'InnodbBufferPoolPagesFree':statu.InnodbBufferPoolPagesFree,
'InnodbBufferPoolPagesMisc':statu.InnodbBufferPoolPagesMisc,
'InnodbBufferPoolPagesTotal':statu.InnodbBufferPoolPagesTotal,
'InnodbBufferPoolReadAhead':statu.InnodbBufferPoolReadAhead,
'InnodbBufferPoolReadAheadEvicted':statu.InnodbBufferPoolReadAheadEvicted,
'InnodbBufferPoolReadRequests':statu.InnodbBufferPoolReadRequests,
'InnodbBufferPoolReads':statu.InnodbBufferPoolReads,
'InnodbBufferPoolWaitFree':statu.InnodbBufferPoolWaitFree,
'InnodbBufferPoolWriteRequests':statu.InnodbBufferPoolWriteRequests,
'InnodbDataFsyncs':statu.InnodbDataFsyncs,
'InnodbDataPendingFsyncs':statu.InnodbDataPendingFsyncs,
'InnodbDataPendingReads':statu.InnodbDataPendingReads,
'InnodbDataPendingWrites':statu.InnodbDataPendingWrites,
'InnodbDataRead':statu.InnodbDataRead,
'InnodbDataReads':statu.InnodbDataReads,
'InnodbDataWrites':statu.InnodbDataWrites,
'InnodbDataWritten':statu.InnodbDataWritten,
'InnodbDblwrPagesWritten':statu.InnodbDblwrPagesWritten,
'InnodbDblwrWrites':statu.InnodbDblwrWrites,
'InnodbLogWaits':statu.InnodbLogWaits,
'InnodbLogWriteRequests':statu.InnodbLogWriteRequests,
'InnodbLogWrites':statu.InnodbLogWrites,
'InnodbOsLogFsyncs':statu.InnodbOsLogFsyncs,
'InnodbOsLogPendingFsyncs':statu.InnodbOsLogPendingFsyncs,
'InnodbOsLogPendingWrites':statu.InnodbOsLogPendingWrites,
'InnodbOsLogWritten':statu.InnodbOsLogWritten,
'InnodbPagesCreated':statu.InnodbPagesCreated,
'InnodbPagesRead':statu.InnodbPagesRead,
'InnodbPagesWritten':statu.InnodbPagesWritten,
'InnodbRowLockCurrentWaits':statu.InnodbRowLockCurrentWaits,
'InnodbRowLockTime':statu.InnodbRowLockTime,
'InnodbRowLockTimeAvg':statu.InnodbRowLockTimeAvg,
'InnodbRowLockTimeMax':statu.InnodbRowLockTimeMax,
'InnodbRowLockWaits':statu.InnodbRowLockWaits,
'InnodbRowsDeleted':statu.InnodbRowsDeleted,
'InnodbRowsInserted':statu.InnodbRowsInserted,
'InnodbRowsRead':statu.InnodbRowsRead,
'InnodbRowsUpdated':statu.InnodbRowsUpdated,
'InnodbAvailableUndoLogs':statu.InnodbAvailableUndoLogs,
'OpenTableDefinitions':statu.OpenTableDefinitions,
'OpenTables':statu.OpenTables,
'OpenedTableDefinitions':statu.OpenedTableDefinitions,
'OpenedTables':statu.OpenedTables,
'QcacheFreeBlocks':statu.QcacheFreeBlocks,
'QcacheFreeMemory':statu.QcacheFreeMemory,
'QcacheHits':statu.QcacheHits,
'QcacheInserts':statu.QcacheInserts,
'QcacheLowmemPrunes':statu.QcacheLowmemPrunes,
'QcacheNotCached':statu.QcacheNotCached,
'QcacheQueriesInCache':statu.QcacheQueriesInCache,
'QcacheTotalBlocks':statu.QcacheTotalBlocks,
'SlowQueries':statu.SlowQueries,
'TableLocksImmediate':statu.TableLocksImmediate,
'TableLocksWaited':statu.TableLocksWaited,
'TableOpenCacheOverflows':statu.TableOpenCacheOverflows,
'ThreadsCached':statu.ThreadsCached,
'ThreadsConnected':statu.ThreadsConnected,
'ThreadsCreated':statu.ThreadsCreated,
'ThreadsRunning':statu.ThreadsRunning,
'Uptime':statu.Uptime,
#为了支持zabbix的自动发现 返回数据类似:{"data": [{"{#MYSQLPORT}": 3306}]}
'MySQLDiscovery':base.mysql_discovery,
'DiskDiscovery':base.disk_discovery,
}
#定义mysql主从复制时用到的监控项
repl_items={}
#定义mysql-group-replication时用到的监控项
mgr_items={
'MgrTotalMemberCount':mgr.MgrTotalMemberCount,
'MgrOnLineMemberCount':mgr.MgrOnLineMemberCount,
'MgrMemberState':mgr.MgrMemberState,
'MgrCountTransactionsInQueue':mgr.MgrCountTransactionsInQueue,
'MgrCountTransactionsChecked':mgr.MgrCountTransactionsChecked,
'MgrCountConflictsDetected':mgr.MgrCountConflictsDetected,
'MgrTransactionsCommittedAllMembers':mgr.MgrTransactionsCommittedAllMembers
}
replication_items = {
'RplSemiSyncMasterClients':replication.RplSemiSyncMasterClients,
'RplSemiSyncMasterStatus':replication.RplSemiSyncMasterStatus,
'RplSemiSyncMasterNoTx':replication.RplSemiSyncMasterNoTx,
'RplSemiSyncMasterYesTx':replication.RplSemiSyncMasterYesTx,
'RplSemiSyncSlaveStatus':replication.RplSemiSyncSlaveStatus,
'SlaveIORunning':replication.SlaveIORunning,
'SlaveSQLRunning':replication.SlaveSQLRunning,
'SecondsBehindMaster':replication.SecondsBehindMaster,
}
def export_zabbix_agent_config_file():
"""
monitor.py 主要是用于zabbix监控mysql、所以在这里提供一个自动生成zabbix自定义key值的文件
方便后面使用
"""
fmt="UserParameter=mysql{0}[*],/usr/local/mtls/monitor.py -u=$1 -p=$2 -s=$3 -P=$4 {0} 2>>/var/log/mtls/monitor.log"
lines=[fmt.format(line) for line in monitor_item_names if line != 'export']
for line in lines:
print(line)
monitor_items={}
monitor_items.update(basic_items)
monitor_items.update(mgr_items)
monitor_items.update(replication_items)
monitor_items.update({'export':export_zabbix_agent_config_file})
#已经定义好了的监控项名
monitor_item_names=[key for key in monitor_items.keys()]
if __name__=="__main__":
parser=argparse.ArgumentParser()
parser.add_argument('-u','--user',default='monitor',help='user name for connect to mysql')
parser.add_argument('-p','--password',default='mtls0352',help='user password for connect to mysql')
parser.add_argument('-s','--host',default='127.0.0.1',help='mysql host ip')
parser.add_argument('-P','--port',default=3306,type=int,help='mysql port')
parser.add_argument('-d','--database',default='information_schema',help='current database default information_schema')
parser.add_argument('monitor_item_name',choices=monitor_item_names)
args=parser.parse_args()
if args.monitor_item_name =='export':
export_zabbix_agent_config_file()
exit()
m=monitor_items[args.monitor_item_name](host=args.host,port=args.port,user=args.user,password=args.password,database=args.database)
print(m.value)