|
| 1 | +/* Copyright (C) 2006 MySQL AB |
| 2 | +
|
| 3 | + This program is free software; you can redistribute it and/or modify |
| 4 | + it under the terms of the GNU General Public License as published by |
| 5 | + the Free Software Foundation; version 2 of the License. |
| 6 | +
|
| 7 | + This program is distributed in the hope that it will be useful, |
| 8 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | + GNU General Public License for more details. |
| 11 | +
|
| 12 | + You should have received a copy of the GNU General Public License |
| 13 | + along with this program; if not, write to the Free Software |
| 14 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 15 | + |
| 16 | +#include <mysql_priv.h> |
| 17 | +#include <stdlib.h> |
| 18 | +#include <ctype.h> |
| 19 | +#include <mysql_version.h> |
| 20 | +#include <mysql/plugin.h> |
| 21 | +#include <my_global.h> |
| 22 | +#include <my_dir.h> |
| 23 | + |
| 24 | +/* |
| 25 | +#if !defined(__attribute__) && (defined(__cplusplus) || !defined(__GNUC__) || __GNUC__ == 2 && __GNUC_MINOR__ < 8) |
| 26 | +#define __attribute__(A) |
| 27 | +#endif |
| 28 | +*/ |
| 29 | + |
| 30 | +#define HEART_STRING_BUFFER 100 |
| 31 | + |
| 32 | +struct mysql_heartbeat_context |
| 33 | +{ |
| 34 | + pthread_t heartbeat_thread; |
| 35 | + File heartbeat_file; |
| 36 | +}; |
| 37 | + |
| 38 | +pthread_handler_t mysql_heartbeat(void *p) |
| 39 | +{ |
| 40 | + DBUG_ENTER("mysql_heartbeat"); |
| 41 | + struct mysql_heartbeat_context *con= (struct mysql_heartbeat_context *)p; |
| 42 | + char buffer[HEART_STRING_BUFFER]; |
| 43 | + unsigned int x= 0; |
| 44 | + time_t result; |
| 45 | + struct tm tm_tmp; |
| 46 | + |
| 47 | + while(1) |
| 48 | + { |
| 49 | + sleep(5); |
| 50 | + |
| 51 | + result= time(NULL); |
| 52 | + localtime_r(&result, &tm_tmp); |
| 53 | + my_snprintf(buffer, sizeof(buffer), |
| 54 | + "Heartbeat at %02d%02d%02d %2d:%02d:%02d\n", |
| 55 | + tm_tmp.tm_year % 100, |
| 56 | + tm_tmp.tm_mon+1, |
| 57 | + tm_tmp.tm_mday, |
| 58 | + tm_tmp.tm_hour, |
| 59 | + tm_tmp.tm_min, |
| 60 | + tm_tmp.tm_sec); |
| 61 | + my_write(con->heartbeat_file, buffer, strlen(buffer), MYF(0)); |
| 62 | + x++; |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + DBUG_RETURN(0); |
| 67 | +} |
| 68 | + |
| 69 | +/* |
| 70 | + Initialize the daemon example at server start or plugin installation. |
| 71 | +
|
| 72 | + SYNOPSIS |
| 73 | + daemon_example_plugin_init() |
| 74 | +
|
| 75 | + DESCRIPTION |
| 76 | + Starts up heartbeatbeat thread |
| 77 | +
|
| 78 | + RETURN VALUE |
| 79 | + 0 success |
| 80 | + 1 failure (cannot happen) |
| 81 | +*/ |
| 82 | + |
| 83 | +static int daemon_example_plugin_init(void *p) |
| 84 | +{ |
| 85 | + DBUG_ENTER("daemon_example_plugin_init"); |
| 86 | + struct mysql_heartbeat_context *con; |
| 87 | + pthread_attr_t attr; /* Thread attributes */ |
| 88 | + char heartbeat_filename[FN_REFLEN]; |
| 89 | + char buffer[HEART_STRING_BUFFER]; |
| 90 | + time_t result= time(NULL); |
| 91 | + struct tm tm_tmp; |
| 92 | + |
| 93 | + struct st_plugin_int *plugin= (struct st_plugin_int *)p; |
| 94 | + |
| 95 | + con= (struct mysql_heartbeat_context *)my_malloc(sizeof(struct mysql_heartbeat_context), MYF(0)); |
| 96 | + |
| 97 | + fn_format(heartbeat_filename, "mysql-heartbeat", "", ".log", MY_REPLACE_EXT | MY_UNPACK_FILENAME); |
| 98 | + unlink(heartbeat_filename); |
| 99 | + con->heartbeat_file= my_open(heartbeat_filename, O_CREAT|O_RDWR, MYF(0)); |
| 100 | + |
| 101 | + /* |
| 102 | + No threads exist at this point in time, so this is thread safe. |
| 103 | + */ |
| 104 | + localtime_r(&result, &tm_tmp); |
| 105 | + my_snprintf(buffer, sizeof(buffer), |
| 106 | + "Starting up at %02d%02d%02d %2d:%02d:%02d\n", |
| 107 | + tm_tmp.tm_year % 100, |
| 108 | + tm_tmp.tm_mon+1, |
| 109 | + tm_tmp.tm_mday, |
| 110 | + tm_tmp.tm_hour, |
| 111 | + tm_tmp.tm_min, |
| 112 | + tm_tmp.tm_sec); |
| 113 | + my_write(con->heartbeat_file, buffer, strlen(buffer), MYF(0)); |
| 114 | + |
| 115 | + pthread_attr_init(&attr); |
| 116 | + pthread_attr_setdetachstate(&attr, |
| 117 | + PTHREAD_CREATE_JOINABLE); |
| 118 | + |
| 119 | + |
| 120 | + /* now create the thread */ |
| 121 | + if (pthread_create(&con->heartbeat_thread, &attr, mysql_heartbeat, (void *)con) != 0) |
| 122 | + { |
| 123 | + fprintf(stderr,"Could not create heartbeat thread!\n"); |
| 124 | + exit(0); |
| 125 | + } |
| 126 | + |
| 127 | + plugin->data= (void *)con; |
| 128 | + |
| 129 | + DBUG_RETURN(0); |
| 130 | +} |
| 131 | + |
| 132 | + |
| 133 | +/* |
| 134 | + Terminate the daemon example at server shutdown or plugin deinstallation. |
| 135 | +
|
| 136 | + SYNOPSIS |
| 137 | + daemon_example_plugin_deinit() |
| 138 | + Does nothing. |
| 139 | +
|
| 140 | + RETURN VALUE |
| 141 | + 0 success |
| 142 | + 1 failure (cannot happen) |
| 143 | +
|
| 144 | +*/ |
| 145 | +static int daemon_example_plugin_deinit(void *p) |
| 146 | +{ |
| 147 | + DBUG_ENTER("daemon_example_plugin_deinit"); |
| 148 | + char buffer[HEART_STRING_BUFFER]; |
| 149 | + struct st_plugin_int *plugin= (struct st_plugin_int *)p; |
| 150 | + struct mysql_heartbeat_context *con= (struct mysql_heartbeat_context *)plugin->data; |
| 151 | + time_t result= time(NULL); |
| 152 | + struct tm tm_tmp; |
| 153 | + |
| 154 | + pthread_cancel(con->heartbeat_thread); |
| 155 | + |
| 156 | + localtime_r(&result, &tm_tmp); |
| 157 | + my_snprintf(buffer, sizeof(buffer), |
| 158 | + "Shutting down at %02d%02d%02d %2d:%02d:%02d\n", |
| 159 | + tm_tmp.tm_year % 100, |
| 160 | + tm_tmp.tm_mon+1, |
| 161 | + tm_tmp.tm_mday, |
| 162 | + tm_tmp.tm_hour, |
| 163 | + tm_tmp.tm_min, |
| 164 | + tm_tmp.tm_sec); |
| 165 | + my_write(con->heartbeat_file, buffer, strlen(buffer), MYF(0)); |
| 166 | + my_close(con->heartbeat_file, MYF(0)); |
| 167 | + |
| 168 | + |
| 169 | + my_free((char *)con, MYF(0)); |
| 170 | + |
| 171 | + DBUG_RETURN(0); |
| 172 | +} |
| 173 | + |
| 174 | +struct st_mysql_daemon daemon_example_plugin= |
| 175 | +{ MYSQL_DAEMON_INTERFACE_VERSION }; |
| 176 | + |
| 177 | +/* |
| 178 | + Plugin library descriptor |
| 179 | +*/ |
| 180 | + |
| 181 | +mysql_declare_plugin(daemon_example) |
| 182 | +{ |
| 183 | + MYSQL_DAEMON_PLUGIN, |
| 184 | + &daemon_example_plugin, |
| 185 | + "daemon_example", |
| 186 | + "Brian Aker", |
| 187 | + "Daemon example, creates a heartbeat beat file in mysql-heartbeat.log", |
| 188 | + PLUGIN_LICENSE_GPL, |
| 189 | + daemon_example_plugin_init, /* Plugin Init */ |
| 190 | + daemon_example_plugin_deinit, /* Plugin Deinit */ |
| 191 | + 0x0100 /* 1.0 */, |
| 192 | + NULL, /* status variables */ |
| 193 | + NULL, /* system variables */ |
| 194 | + NULL /* config options */ |
| 195 | +} |
| 196 | +mysql_declare_plugin_end; |
0 commit comments