-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmaxdiskusage.cc
273 lines (246 loc) · 11.1 KB
/
maxdiskusage.cc
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
#include <mysql/plugin.h>
#include <mysql/plugin_audit.h>
#include <mysql/service_my_plugin_log.h>
#include <mysql/service_security_context.h>
#include <stdio.h>
#include <sys/statvfs.h>
#if MYSQL_VERSION_ID >= 80000
#ifndef FALSE
#define FALSE (0)
#endif
#ifndef TRUE
#define TRUE (1)
#endif
#include "sql/sql_error.h"
#else
#include "sql_error.h"
#endif
#define MAX_WARN_MSG 1024
static uint64_t maxdiskusage_minfree_mb;
static uint64_t maxdiskusage_pct;
static uint64_t maxdiskusage_block_pct;
static uint64_t maxdiskusage_warn_skip_count;
static uint64_t warn_skipped = 0;
static char *maxdiskusage_monitor_fs = NULL;
static char *maxdiskusage_action = NULL;
static char *maxdiskusage_note = NULL;
static MYSQL_PLUGIN plugin = NULL;
static bool is_super(MYSQL_THD thd) {
MYSQL_SECURITY_CONTEXT ctx;
my_svc_bool is_super = FALSE;
/* setting panic mode requires super */
return (thd != NULL && !thd_get_security_context(thd, &ctx) &&
!security_context_get_option(ctx, "privilege_super", &is_super) &&
is_super);
}
static int maxdiskusage_notify(MYSQL_THD thd, mysql_event_class_t event_class,
const void *event) {
struct statvfs vfs;
char msg[MAX_WARN_MSG];
/* Always allow super */
if (is_super(thd))
return FALSE;
if (event_class == MYSQL_AUDIT_TABLE_ACCESS_CLASS) {
const struct mysql_event_table_access *table_access =
(const struct mysql_event_table_access *)event;
uint64_t freespace_mb;
uint64_t used_pct;
/* Always allow DELETE, TRUNCATE, SELECT */
switch (table_access->sql_command_id) {
case SQLCOM_DELETE:
case SQLCOM_DELETE_MULTI:
case SQLCOM_TRUNCATE:
case SQLCOM_SELECT:
return FALSE;
default:;
}
/* TODO: replace / with @@datadir */
if (statvfs(maxdiskusage_monitor_fs, &vfs) != 0)
return TRUE;
freespace_mb = (vfs.f_bsize * vfs.f_bavail) / 1024 / 1024;
used_pct =
(uint64_t)(100 - (100 * ((double)vfs.f_bavail / (double)vfs.f_blocks)));
if ((maxdiskusage_pct < 100) && ((used_pct >= maxdiskusage_pct) ||
used_pct >= maxdiskusage_block_pct)) {
if (strncmp(maxdiskusage_action, "WARN", 10) == 0 ||
(strncmp(maxdiskusage_action, "WARN_AND_BLOCK", 10) == 0 &&
used_pct < maxdiskusage_block_pct)) {
if (warn_skipped >= maxdiskusage_warn_skip_count) {
warn_skipped = 0;
snprintf(msg, MAX_WARN_MSG,
"Writing to a server which has not a lot of free space "
"(Percentage)%s",
maxdiskusage_note);
/* 1642 == ER_SIGNAL_WARN */
push_warning(thd, Sql_condition::SL_WARNING, 1642, msg);
} else {
warn_skipped++;
}
} else if (strncmp(maxdiskusage_action, "BLOCK", 10) == 0) {
my_plugin_log_message(
&plugin, MY_ERROR_LEVEL,
"BLOCKING QUERY: Using %lu%%, which is more that %lu%%: %s",
used_pct, maxdiskusage_pct, table_access->query.str);
return TRUE;
} else if (strncmp(maxdiskusage_action, "WARN_AND_BLOCK", 10) == 0 &&
used_pct >= maxdiskusage_block_pct) {
my_plugin_log_message(
&plugin, MY_ERROR_LEVEL,
"BLOCKING QUERY: Using %lu%%, which is more that %lu%%: %s",
used_pct, maxdiskusage_block_pct, table_access->query.str);
return TRUE;
} else {
my_plugin_log_message(&plugin, MY_ERROR_LEVEL, "Invalid action set: %s",
maxdiskusage_action);
}
}
if ((maxdiskusage_minfree_mb > 0) &&
(freespace_mb < maxdiskusage_minfree_mb)) {
if (strncmp(maxdiskusage_action, "WARN", 6) == 0) {
if (warn_skipped >= maxdiskusage_warn_skip_count) {
warn_skipped = 0;
snprintf(msg, MAX_WARN_MSG,
"Writing to a server which has not a lot of free space "
"(Free Bytes)%s",
maxdiskusage_note);
/* 1642 == ER_SIGNAL_WARN */
push_warning(thd, Sql_condition::SL_WARNING, 1642, msg);
} else {
warn_skipped++;
}
} else if (strncmp(maxdiskusage_action, "BLOCK", 6) == 0) {
my_plugin_log_message(&plugin, MY_ERROR_LEVEL,
"BLOCKING QUERY: Free filesystem space on %s "
"(%lu MB) is less than %lu MB: %s",
maxdiskusage_monitor_fs, freespace_mb,
maxdiskusage_minfree_mb, table_access->query.str);
return TRUE;
} else {
my_plugin_log_message(&plugin, MY_ERROR_LEVEL, "Invalid action set: %s",
maxdiskusage_action);
}
}
}
return FALSE;
}
static struct st_mysql_audit maxdiskusage_descriptor = {
MYSQL_AUDIT_INTERFACE_VERSION, /* interface version */
NULL, /* release_thd function */
maxdiskusage_notify, /* notify function */
{
0, /* general */
0, /* connection */
0, /* parse */
0, /* authorization */
MYSQL_AUDIT_TABLE_ACCESS_ALL, /* table access */
0, /* global variables */
0, /* server startup */
0, /* server shutdown */
0, /* command */
0, /* query */
0 /* stored program */
}};
/* plumbing */
static MYSQL_SYSVAR_ULONG(warn_skip_count, /* name */
maxdiskusage_warn_skip_count, /* value */
PLUGIN_VAR_OPCMDARG, /* flags */
"Skip x events to limit warning rate", /* comment */
NULL, /* check() */
NULL, /* update() */
1000, /* default */
0, /* minimum */
UINT64_MAX, /* maximum */
0 /* blocksize */
);
static MYSQL_SYSVAR_ULONG(pct, /* name */
maxdiskusage_pct, /* value */
PLUGIN_VAR_OPCMDARG, /* flags */
"Maximum percentage in use", /* comment */
NULL, /* check() */
NULL, /* update() */
100, /* default */
0, /* minimum */
100, /* maximum */
0 /* blocksize */
);
static MYSQL_SYSVAR_ULONG(block_pct, /* name */
maxdiskusage_block_pct, /* value */
PLUGIN_VAR_OPCMDARG, /* flags */
"Maximum percentage for blocking", /* comment */
NULL, /* check() */
NULL, /* update() */
100, /* default */
0, /* minimum */
100, /* maximum */
0 /* blocksize */
);
static MYSQL_SYSVAR_ULONG(minfree, /* name */
maxdiskusage_minfree_mb, /* value */
PLUGIN_VAR_OPCMDARG, /* flags */
"Minimum free disk space", /* comment */
NULL, /* check() */
NULL, /* update() */
0, /* default */
0, /* minimum */
UINT64_MAX, /* maximum */
0 /* blocksize */
);
static MYSQL_SYSVAR_STR(monitor_fs, /* name */
maxdiskusage_monitor_fs, /* value */
PLUGIN_VAR_OPCMDARG | PLUGIN_VAR_MEMALLOC, /* flags */
"Filesystem to test for disk usage", /* comment */
NULL, /* check() */
NULL, /* update() */
"/var/lib/mysql" /* default */
);
static MYSQL_SYSVAR_STR(action, /* name */
maxdiskusage_action, /* value */
PLUGIN_VAR_OPCMDARG | PLUGIN_VAR_MEMALLOC, /* flags */
"Action to take: BLOCK or WARN", /* comment */
NULL, /* check() */
NULL, /* update() */
"WARN" /* default */
);
static MYSQL_SYSVAR_STR(note, /* name */
maxdiskusage_note, /* value */
PLUGIN_VAR_OPCMDARG | PLUGIN_VAR_MEMALLOC, /* flags */
"Note to add to the warning message", /* comment */
NULL, /* check() */
NULL, /* update() */
"" /* default */
);
#if MYSQL_VERSION_ID >= 80000
SYS_VAR *system_variables[] = {
#else
static struct st_mysql_sys_var *system_variables[] = {
#endif
MYSQL_SYSVAR(warn_skip_count),
MYSQL_SYSVAR(pct),
MYSQL_SYSVAR(block_pct),
MYSQL_SYSVAR(minfree),
MYSQL_SYSVAR(monitor_fs),
MYSQL_SYSVAR(action),
MYSQL_SYSVAR(note),
NULL};
static int maxdiskusage_init(MYSQL_PLUGIN p) {
plugin = p;
return 0;
}
/** Plugin declaration */
mysql_declare_plugin(maxdiskusage) {
MYSQL_AUDIT_PLUGIN, /* type */
&maxdiskusage_descriptor, /* descriptor */
"maxdiskusage", /* name */
"Daniël van Eeden", /* author */
"Better handle high diskusage", /* description */
PLUGIN_LICENSE_GPL, maxdiskusage_init, /* init function (when loaded) */
#if MYSQL_VERSION_ID >= 80000
NULL,
#endif
NULL, /* deinit function (when unloaded) */
0x0100, /* version */
NULL, /* status variables */
system_variables, /* system variables */
NULL, 0,
}
mysql_declare_plugin_end;