-
Notifications
You must be signed in to change notification settings - Fork 8
/
third.c~
104 lines (86 loc) · 2.25 KB
/
third.c~
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
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <plugin.h>
#include <mysql_version.h>
#include <my_global.h>
#include <my_sys.h>
#include <pthread.h>
#define MONITORING_BUFFER 1024
//sql/mysqld.cc
extern ulong thread_id;
extern uint thread_count;
extern ulong max_connections;
static pthread_t monitoring_thread;
static int monitoring_file; //fd
//func
pthread_handler_t monitoring(void *p)
{
char buffer[MONITORING_BUFFER];
char time_str[20];
while(1) {
sleep(5);
get_date(time_str, GETDATE_DATE_TIME, 0);
snprintf(buffer, sizeof(buffer), "%s: %u of %lu clients connected, "
"%lu connections made\n",
time_str, thread_count,
max_connections, thread_id);
write(monitoring_file, buffer, strlen(buffer));
}
}
static int monitor_plugin_init(void *p)
{
pthread_attr_t attr;
char monitoring_filename[FN_REFLEN];
char buffer[MONITORING_BUFFER];
char time_str[20];
fn_format(monitoring_filename, "monitor", "", ".log",
MY_REPLACE_EXT | MY_UNPACK_FILENAME);
unlink(monitoring_filename);
monitoring_file = open(monitoring_filename, O_CREAT | O_RDWR, 0644);
if (monitoring_file < 0) {
fprintf(stderr, "Plugin 'monitor cant create %s", monitoring_filename);
return 1;
}
get_date(time_str, GETDATE_DATE_TIME, 0);
snprintf(buffer, sizeof(buffer), "Monitor started at %s\n", time_str);
write(monitoring_file, buffer, strlen(buffer));
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
if (pthread_create(&monitoring_thread, &attr, monitoring, NULL) != 0) {
fprintf(stderr, "create thread failure");
return 1;
}
return 0;
}
static int monitor_plugin_deinit(void *p)
{
char time_str[20];
char buffer[MONITORING_BUFFER];
pthread_cancel(monitoring_thread);
pthread_join(monitoring_thread, NULL);
get_date(time_str, GETDATE_DATE_TIME, 0);
snprintf(buffer, sizeof(buffer), "monitor %s\n", time_str);
write(monitoring_file, buffer, strlen(buffer));
close(monitoring_file);
}
struct st_mysql_daemon monitor_plugin =
{
MYSQL_DAEMON_INTERFACE_VERSION
};
mysql_declare_plugin(monitoring)
{
MYSQL_DAEMON_PLUGIN,
&monitor_plugin,
"monitoring",
"hoterran",
"saaaa",
PLUGIN_LICENSE_GPL,
monitor_plugin_init,
monitor_plugin_deinit,
0x0100,
NULL,
NULL,
NULL,
}
mysql_declare_plugin_end;