summaryrefslogtreecommitdiffabout
path: root/src/mysqlstat.c
blob: 9d9975e779b74a19864a31c8218f34e1bfc2f670 (plain)
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
/* This file is part of Mysqlstat
 * Copyright (C) 2016 Sergey Poznyakoff
 *
 * Mysqlstat is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * Mysqlstat is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Mysqlstat.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <mysqlstat.h>
#include <mysql/mysql.h>
#undef NDEBUG
#include <assert.h>
#include "mysqlstat_mib.h"

static char *config_file = CONFDIR "/mysqlstat.cnf";

struct mysqlstat_connection {
	MYSQL mysql;
};

static void *
xmalloc(size_t s)
{
	void *p = malloc(s);
	assert(p != NULL);
	return p;
}

static void *
xcalloc(size_t nmemb, size_t size)
{
	void *p = calloc(nmemb, size);
	assert(p != NULL);
	return p;
}

mysqlstat_connection_t 
mysqlstat_connect(void)
{
	static struct mysqlstat_connection *conn;
	my_bool t = 1;

	if (!conn) {
		conn = xcalloc(1, sizeof(*conn));
		mysql_init(&conn->mysql);
		if (access(config_file, F_OK) == 0)
			mysql_options(&conn->mysql, MYSQL_READ_DEFAULT_FILE,
				      config_file);
		mysql_options(&conn->mysql,
			      MYSQL_READ_DEFAULT_GROUP, "mysqlstat");
		mysql_options(&conn->mysql, MYSQL_OPT_RECONNECT, &t);
		if (!mysql_real_connect(&conn->mysql,
					NULL, NULL, NULL, NULL, 0,
					NULL, 0)) {
			snmp_log(LOG_ERR, "can't connect to MySQL: %s\n",
				 mysql_error(&conn->mysql));
			free(conn);
			conn = NULL;
		}
	}
	return conn;
}

unsigned mysqlstat_cache_timeout = CACHE_TIMEOUT;

#define __cat2__(a,b) a ## b
#define ASSIGN_STRING(e,m,s)				                      \
	do {						                      \
	        if (s) {		                                      \
		        (e)->__cat2__(m,_len) = strlen(s);                    \
			(e)->m = xmalloc((e)->__cat2__(m,_len));	      \
			memcpy((e)->m, s, (e)->__cat2__(m,_len));             \
	        } else {		                                      \
		        (e)->__cat2__(m,_len) = 0;			      \
			(e)->m = NULL;                                        \
		}						              \
	} while (0)

#define ASSIGN_ROW(e,m,r)  ASSIGN_STRING(e,m,(r)[__cat2__(FI_,m)])

#define STREQ(pat,str) (strcmp(#pat, str) == 0)

struct process_list {
	time_t ts;
	MYSQL_RES *res;
	uint32_t total;
	uint32_t active;
	uint32_t slaves;
	uint32_t itr;
};

enum {
	FI_processID,
	FI_processUser,
	FI_processHost,
	FI_processDatabase,
	FI_processCommand,
	FI_processTime,
	FI_processState,
	FI_processInfo
};

static int
get_process_list(struct process_list **p)
{
	static struct process_list plist;
	time_t now;

	now = time(NULL);
	if (!plist.res || now - plist.ts >= mysqlstat_cache_timeout) {
		mysqlstat_connection_t conn;
		unsigned i;
		
		if (plist.res) {
			mysql_free_result(plist.res);
			plist.res = NULL;
			plist.total = plist.active = plist.slaves = 0;
		}
		DEBUGMSGTL(("mysqlstat:sql", "Getting process list\n"));

		conn = mysqlstat_connect();
		if (!conn)
			return SNMP_ERR_GENERR;
	
		if (mysql_query(&conn->mysql, "SHOW PROCESSLIST")) {
			snmp_log(LOG_ERR, "can't get slave process list: %s\n",
				 mysql_error(&conn->mysql));
			return SNMP_ERR_NOSUCHNAME;
		}
		plist.res = mysql_store_result(&conn->mysql);
		plist.ts = now;

                plist.total = mysql_num_rows(plist.res);
		for (i = 0; i < plist.total; i++) {
                        MYSQL_ROW row;
		
		        mysql_data_seek(plist.res, i);
			row = mysql_fetch_row(plist.res);
			if (!row[FI_processState]
			    || STREQ(Sleep, row[FI_processState]))
				continue;
			if (row[FI_processCommand]
			    && strcmp(row[FI_processCommand], "Binlog Dump") == 0)
				plist.slaves++;
			else
				plist.active++;
		}
	}
	*p = &plist;
	return 0;
}

MYSQL_ROW
process_next(struct process_list *p)
{
	if (p->itr >= p->total)
		return NULL;
	mysql_data_seek(p->res, p->itr);
	++p->itr;
	return mysql_fetch_row(p->res);
}

MYSQL_ROW
process_first(struct process_list **state)
{
	struct process_list *p;
	if (get_process_list(&p))
		return NULL;
	p->itr = 0;
	*state = p;
	return process_next(p);
}

static long
val_Slave_IO_Running(char const *val)
{
	if (strcmp(val, "Yes") == 0)
		return 1;
	else if (strcmp(val, "No") == 0)
		return 2;
	else if (strcmp(val, "Connected") == 0)
		return 3;
	return 4;
}

static long
val_bool(char const *val)
{
	if (strcmp(val, "Yes") == 0)
		return 1;
	return 2;
}

static void
store_slave_status_row(struct replSlaveStatusTable_entry *ent,
		       char const *name, char const *value)
{
	if (STREQ(Slave_IO_State, name)) {
		ASSIGN_STRING(ent, replSlaveIOState, value);
	} else if (STREQ(Master_Host, name)) {
		ASSIGN_STRING(ent, replMasterHost, value);
	} else if (STREQ(Master_User, name)) {
		ASSIGN_STRING(ent, replMasterUser, value);
	} else if (STREQ(Master_Port, name)) {
		ent->replMasterPort = strtoul(value, NULL, 10);
	} else if (STREQ(Connect_Retry, name)) {
		ent->replConnectRetry = strtoul(value, NULL, 10);
	} else if (STREQ(Master_Log_File, name)) {
		ASSIGN_STRING(ent, replMasterLogFile, value);
	} else if (STREQ(Read_Master_Log_Pos, name)) {
		ent->replReadMasterLogPos = strtoul(value, NULL, 10);
	} else if (STREQ(Relay_Log_File, name)) {
		ASSIGN_STRING(ent, replRelayLogFile, value);
	} else if (STREQ(Relay_Log_Pos, name)) {
		ent->replRelayLogPos = strtoul(value, NULL, 10);
	} else if (STREQ(Relay_Master_Log_File, name)) {
		ASSIGN_STRING(ent, replMasterLogFile, value);
	} else if (STREQ(Slave_IO_Running, name)) {
		ent->replSlaveIORunning = val_Slave_IO_Running(value);
	} else if (STREQ(Slave_SQL_Running, name)) {
		ent->replSlaveSQLRunning = val_bool(value);
	} else if (STREQ(Replicate_Do_DB, name)) {
		ASSIGN_STRING(ent, replReplicateDoDB, value);
	} else if (STREQ(Replicate_Ignore_DB, name)) {
		ASSIGN_STRING(ent, replReplicateIgnoreDB, value);
	} else if (STREQ(Replicate_Do_Table, name)) {
		ASSIGN_STRING(ent, replReplicateDoTable, value);
	} else if (STREQ(Replicate_Ignore_Table, name)) {
		ASSIGN_STRING(ent, replReplicateIgnoreTable, value);
	} else if (STREQ(Replicate_Wild_Do_Table, name)) {
		ASSIGN_STRING(ent, replReplicateWildDoTable, value);
	} else if (STREQ(Replicate_Wild_Ignore_Table, name)) {
		ASSIGN_STRING(ent, replReplicateWildIgnoreTable, value);
	} else if (STREQ(Last_IO_Errno, name)) {
		ent->replLastIOErrno = strtoul(value, NULL, 10);
	} else if (STREQ(Last_IO_Error, name)) {
		ASSIGN_STRING(ent, replLastIOError, value);
	} else if (STREQ(Last_SQL_Errno, name)) {
		ent->replLastSQLErrno = strtoul(value, NULL, 10);
	} else if (STREQ(Last_SQL_Error, name)) {
		ASSIGN_STRING(ent, replLastSQLError, value);
	} else if (STREQ(Skip_Counter, name)) {
		ent->replSkipCounter = strtoul(value, NULL, 10);
	} else if (STREQ(Exec_Master_Log_Pos, name)) {
		ent->replExecMasterLogPos = strtoul(value, NULL, 10);
	} else if (STREQ(Relay_Log_Space, name)) {
		ent->replRelayLogSpace = strtoul(value, NULL, 10);
	} else if (STREQ(Until_Condition, name)) {
		ASSIGN_STRING(ent, replUntilCondition, value);
	} else if (STREQ(Until_Log_File, name)) {
		ASSIGN_STRING(ent, replUntilLogFile, value);
	} else if (STREQ(Until_Log_Pos, name)) {
		ASSIGN_STRING(ent, replUntilLogPos, value);
	} else if (STREQ(Master_SSL_Allowed, name)) {
		ASSIGN_STRING(ent, replMasterSSLAllowed, value);
	} else if (STREQ(Master_SSL_CA_File, name)) {
		ASSIGN_STRING(ent, replMasterSSLCAFile, value);
	} else if (STREQ(Master_SSL_CA_Path, name)) {
		ASSIGN_STRING(ent, replMasterSSLCAPath, value);
	} else if (STREQ(Master_SSL_Cert, name)) {
		ASSIGN_STRING(ent, replMasterSSLCert, value);
	} else if (STREQ(Master_SSL_Cipher, name)) {
		ASSIGN_STRING(ent, replMasterSSLCipher, value);
	} else if (STREQ(Master_SSL_Key, name)) {
		ASSIGN_STRING(ent, replMasterSSLKey, value);
	} else if (STREQ(Seconds_Behind_Master, name)) {
		ent->replSecondsBehindMaster = strtoul(value, NULL, 10) * 100;
	} else if (STREQ(Master_SSL_Verify_Server_Cert, name)) {
		ent->replMasterSSLVerifyServerCert = val_bool(value);
	}
}

int
replSlaveStatusTable_load(netsnmp_cache *cache, void *vmagic)
{
	mysqlstat_connection_t conn;
	MYSQL_RES *res;
	MYSQL_ROW row;
	MYSQL_FIELD *fields;
	unsigned int num_fields, i;
	netsnmp_tdata *table = (netsnmp_tdata *) vmagic;
	struct replSlaveStatusTable_entry *ent;
	netsnmp_tdata_row *data_row;
	
	conn = mysqlstat_connect();
	if (!conn)
		return SNMP_ERR_NOSUCHNAME;
	
	DEBUGMSGTL(("mysqlstat:sql", "Getting slave status\n"));
	if (mysql_query(&conn->mysql, "SHOW SLAVE STATUS")) {
		snmp_log(LOG_ERR, "can't get slave status: %s\n",
			 mysql_error(&conn->mysql));
		return SNMP_ERR_NOSUCHNAME;
	}

	res = mysql_store_result(&conn->mysql);
	if (mysql_num_rows(res) < 1) {
		/* snmp_log(LOG_INFO, "empty slave status\n"); */
		return 0;
	}

	num_fields = mysql_num_fields(res);
	fields = mysql_fetch_fields(res);
	row = mysql_fetch_row(res);

	ent = SNMP_MALLOC_TYPEDEF(struct replSlaveStatusTable_entry);
	if (!ent) {
		return SNMP_ERR_GENERR;
	}

	data_row = netsnmp_tdata_create_row();
	if (!data_row) {
		SNMP_FREE(ent);
		return SNMP_ERR_GENERR;
	}
	data_row->data = ent;
	ent->replSlaveIndex = 0;
	netsnmp_tdata_row_add_index(data_row, ASN_INTEGER,
				    &ent->replSlaveIndex,
				    sizeof(ent->replSlaveIndex));

	netsnmp_tdata_add_row(table, data_row);
	
	for (i = 0; i < num_fields; i++)
		store_slave_status_row(ent, fields[i].name, row[i]);

	mysql_free_result(res);
	return 0;
}

void
replSlaveStatusTable_entry_free(void *data)
{
	struct replSlaveStatusTable_entry *ent = data;
	free(ent->replSlaveIOState);
	free(ent->replMasterLogFile);
	free(ent->replRelayLogFile);
	free(ent->replRelayMasterLogFile);
	free(ent->replLastSQLError);
	free(ent->replLastIOError);
	free(ent->replMasterHost);
	free(ent->replMasterUser);
	free(ent->replReplicateDoDB);
	free(ent->replReplicateIgnoreDB);
	free(ent->replReplicateDoTable);
	free(ent->replReplicateIgnoreTable);
	free(ent->replReplicateWildDoTable);
	free(ent->replReplicateWildIgnoreTable);
	free(ent->replUntilCondition);
	free(ent->replUntilLogFile);
	free(ent->replUntilLogPos);
	free(ent->replMasterSSLAllowed);
	free(ent->replMasterSSLCAFile);
	free(ent->replMasterSSLCAPath);
	free(ent->replMasterSSLCert);
	free(ent->replMasterSSLCipher);
	free(ent->replMasterSSLKey);
}

static void
process_list_add(netsnmp_tdata *table_data, long idx, MYSQL_ROW mysql_row)
{
	struct processListTable_entry *ent;
	netsnmp_tdata_row *data_row;

	ent = SNMP_MALLOC_TYPEDEF(struct processListTable_entry);
	if (!ent)
		return;

	data_row = netsnmp_tdata_create_row();
	if (!data_row) {
		SNMP_FREE(ent);
		return;
	}
	data_row->data = ent;

	ent->processIndex = idx;
	if (mysql_row[FI_processID])
		ent->processID = strtol(mysql_row[FI_processID], NULL, 10);

	ASSIGN_ROW(ent, processUser, mysql_row);
	
	ent->processHost_len = strcspn(mysql_row[FI_processHost], ":");
	ent->processHost = xmalloc(ent->processHost_len);
	memcpy(ent->processHost, mysql_row[FI_processHost], ent->processHost_len);
	
	ASSIGN_ROW(ent, processDatabase, mysql_row);
	ASSIGN_ROW(ent, processCommand, mysql_row);
	if (mysql_row[FI_processTime])
		ent->processTime = strtol(mysql_row[FI_processTime], NULL, 10) * 100;
	ASSIGN_ROW(ent, processState, mysql_row);
	ASSIGN_ROW(ent, processInfo, mysql_row);

	netsnmp_tdata_row_add_index(data_row, ASN_INTEGER,
				    &ent->processIndex,
				    sizeof(ent->processIndex));

	netsnmp_tdata_add_row(table_data, data_row);
}

int
processListTable_load(netsnmp_cache *cache, void *vmagic)
{
	struct process_list *p;
	netsnmp_tdata *table = (netsnmp_tdata *) vmagic;
	MYSQL_ROW row;
	uint32_t i;
	
	for (row = process_first(&p), i = 0; row; row = process_next(p), i++) {
		process_list_add(table, i, row);
	}
	return 0;
}

void
processListTable_entry_free(void *data)
{
	struct processListTable_entry *ent = data;
	free(ent->processHost);
	free(ent->processUser);
	free(ent->processCommand);
	free(ent->processState);
	free(ent->processInfo);
}

uint32_t
process_total_count(void)
{
	struct process_list *p;
	if (get_process_list(&p))
		return 0;
	return p->total;
}

uint32_t
process_active_count(void)
{
	struct process_list *p;
	if (get_process_list(&p))
		return 0;
	return p->active;
}

uint32_t
process_slave_count(void)
{
	struct process_list *p;
	if (get_process_list(&p))
		return 0;
	return p->slaves;
}







	

Return to:

Send suggestions and report system problems to the System administrator.