16 #include <my_global.h>
20 #include <mysql_version.h>
21 #include <mysql/plugin.h>
23 #include "my_pthread.h"
26 #include "sql_plugin.h"
31 #if !defined(__attribute__) && !defined(__GNUC__)
32 #define __attribute__(A)
36 #define HEART_STRING_BUFFER 100
40 pthread_t heartbeat_thread;
44 pthread_handler_t mysql_heartbeat(
void *p)
46 DBUG_ENTER(
"mysql_heartbeat");
48 char buffer[HEART_STRING_BUFFER];
57 localtime_r(&result, &tm_tmp);
58 my_snprintf(buffer,
sizeof(buffer),
59 "Heartbeat at %02d%02d%02d %2d:%02d:%02d\n",
66 my_write(con->heartbeat_file, (uchar*) buffer, strlen(buffer), MYF(0));
86 static int daemon_example_plugin_init(
void *p)
89 DBUG_ENTER(
"daemon_example_plugin_init");
92 char heartbeat_filename[FN_REFLEN];
93 char buffer[HEART_STRING_BUFFER];
94 time_t result= time(NULL);
102 fn_format(heartbeat_filename,
"mysql-heartbeat",
"",
".log",
103 MY_REPLACE_EXT | MY_UNPACK_FILENAME);
104 unlink(heartbeat_filename);
105 con->heartbeat_file= my_open(heartbeat_filename, O_CREAT|O_RDWR, MYF(0));
110 localtime_r(&result, &tm_tmp);
111 my_snprintf(buffer,
sizeof(buffer),
112 "Starting up at %02d%02d%02d %2d:%02d:%02d\n",
113 tm_tmp.tm_year % 100,
119 my_write(con->heartbeat_file, (uchar*) buffer, strlen(buffer), MYF(0));
121 pthread_attr_init(&attr);
122 pthread_attr_setdetachstate(&attr,
123 PTHREAD_CREATE_JOINABLE);
127 if (pthread_create(&con->heartbeat_thread, &attr, mysql_heartbeat,
130 fprintf(stderr,
"Could not create heartbeat thread!\n");
133 plugin->data= (
void *)con;
152 static int daemon_example_plugin_deinit(
void *p)
154 DBUG_ENTER(
"daemon_example_plugin_deinit");
155 char buffer[HEART_STRING_BUFFER];
159 time_t result= time(NULL);
163 pthread_cancel(con->heartbeat_thread);
165 localtime_r(&result, &tm_tmp);
166 my_snprintf(buffer,
sizeof(buffer),
167 "Shutting down at %02d%02d%02d %2d:%02d:%02d\n",
168 tm_tmp.tm_year % 100,
174 my_write(con->heartbeat_file, (uchar*) buffer, strlen(buffer), MYF(0));
180 pthread_join(con->heartbeat_thread, &dummy_retval);
182 my_close(con->heartbeat_file, MYF(0));
191 { MYSQL_DAEMON_INTERFACE_VERSION };
197 mysql_declare_plugin(daemon_example)
200 &daemon_example_plugin,
203 "Daemon example, creates a heartbeat beat file in mysql-heartbeat.log",
205 daemon_example_plugin_init,
206 daemon_example_plugin_deinit,
213 mysql_declare_plugin_end;