Drizzled Public API Documentation

logging_gearman.cc

00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008, 2009 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00020 #include <config.h>
00021 
00022 #include <boost/scoped_array.hpp>
00023 
00024 #include <drizzled/plugin/logging.h>
00025 #include <drizzled/gettext.h>
00026 #include <drizzled/session.h>
00027 #include <drizzled/sql_parse.h>
00028 #include <drizzled/errmsg_print.h>
00029 #include <boost/date_time.hpp>
00030 #include <boost/program_options.hpp>
00031 #include <drizzled/module/option_map.h>
00032 #include <libgearman/gearman.h>
00033 #include <limits.h>
00034 #include <sys/types.h>
00035 #include <sys/stat.h>
00036 #include <fcntl.h>
00037 #include <cstdio>
00038 #include <cerrno>
00039 #include <memory>
00040 
00041 
00042 namespace drizzle_plugin
00043 {
00044 
00045 namespace po= boost::program_options;
00046 
00047 /* TODO make this dynamic as needed */
00048 static const int MAX_MSG_LEN= 32*1024;
00049 
00050 /* quote a string to be safe to include in a CSV line
00051    that means backslash quoting all commas, doublequotes, backslashes,
00052    and all the ASCII unprintable characters
00053    as long as we pass the high-bit bytes unchanged
00054    this is safe to do to a UTF8 string
00055    we dont allow overrunning the targetbuffer
00056    to avoid having a very long query overwrite memory
00057 
00058    TODO consider remapping the unprintables instead to "Printable
00059    Representation", the Unicode characters from the area U+2400 to
00060    U+2421 reserved for representing control characters when it is
00061    necessary to print or display them rather than have them perform
00062    their intended function.
00063 
00064 */
00065 static unsigned char *quotify (const unsigned char *src, size_t srclen,
00066                                unsigned char *dst, size_t dstlen)
00067 {
00068   static const char hexit[]= { '0', '1', '2', '3', '4', '5', '6', '7',
00069                                '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
00070   size_t dst_ndx;  /* ndx down the dst */
00071   size_t src_ndx;  /* ndx down the src */
00072 
00073   assert(dst);
00074   assert(dstlen > 0);
00075 
00076   for (dst_ndx= 0,src_ndx= 0; src_ndx < srclen; src_ndx++)
00077     {
00078 
00079       /* Worst case, need 5 dst bytes for the next src byte.
00080          backslash x hexit hexit null
00081          so if not enough room, just terminate the string and return
00082       */
00083       if ((dstlen - dst_ndx) < 5)
00084         {
00085           dst[dst_ndx]= (unsigned char)0x00;
00086           return dst;
00087         }
00088 
00089       if (src[src_ndx] > 0x7f)
00090         {
00091           // pass thru high bit characters, they are non-ASCII UTF8 Unicode
00092           dst[dst_ndx++]= src[src_ndx];
00093         }
00094       else if (src[src_ndx] == 0x00)  // null
00095         {
00096           dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) '0';
00097         }
00098       else if (src[src_ndx] == 0x07)  // bell
00099         {
00100           dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'a';
00101         }
00102       else if (src[src_ndx] == 0x08)  // backspace
00103         {
00104           dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'b';
00105         }
00106       else if (src[src_ndx] == 0x09)  // horiz tab
00107         {
00108           dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 't';
00109         }
00110       else if (src[src_ndx] == 0x0a)  // line feed
00111         {
00112           dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'n';
00113         }
00114       else if (src[src_ndx] == 0x0b)  // vert tab
00115         {
00116           dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'v';
00117         }
00118       else if (src[src_ndx] == 0x0c)  // formfeed
00119         {
00120           dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'f';
00121         }
00122       else if (src[src_ndx] == 0x0d)  // carrage return
00123         {
00124           dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'r';
00125         }
00126       else if (src[src_ndx] == 0x1b)  // escape
00127         {
00128           dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'e';
00129         }
00130       else if (src[src_ndx] == 0x22)  // quotation mark
00131         {
00132           dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= 0x22;
00133         }
00134       else if (src[src_ndx] == 0x2C)  // comma
00135         {
00136           dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= 0x2C;
00137         }
00138       else if (src[src_ndx] == 0x5C)  // backslash
00139         {
00140           dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= 0x5C;
00141         }
00142       else if ((src[src_ndx] < 0x20) || (src[src_ndx] == 0x7F))  // other unprintable ASCII
00143         {
00144           dst[dst_ndx++]= 0x5C;
00145           dst[dst_ndx++]= (unsigned char) 'x';
00146           dst[dst_ndx++]= hexit[(src[src_ndx] >> 4) & 0x0f];
00147           dst[dst_ndx++]= hexit[src[src_ndx] & 0x0f];
00148         }
00149       else  // everything else
00150         {
00151           dst[dst_ndx++]= src[src_ndx];
00152         }
00153       dst[dst_ndx]= '\0';
00154     }
00155   return dst;
00156 }
00157 
00158 class LoggingGearman :
00159   public drizzled::plugin::Logging
00160 {
00161 
00162   const std::string _host;
00163   const std::string _function;
00164 
00165   int _gearman_client_ok;
00166   gearman_client_st _gearman_client;
00167 
00168   LoggingGearman();
00169   LoggingGearman(const LoggingGearman&);
00170 
00171 public:
00172 
00173   LoggingGearman(const std::string &host,
00174                  const std::string &function) :
00175     drizzled::plugin::Logging("LoggingGearman"),
00176     _host(host),
00177     _function(function),
00178     _gearman_client_ok(0),
00179     _gearman_client()
00180   {
00181     gearman_return_t ret;
00182 
00183 
00184     if (gearman_client_create(&_gearman_client) == NULL)
00185     {
00186       drizzled::sql_perror(_("fail gearman_client_create()"));
00187       return;
00188     }
00189 
00190     /* TODO, be able to override the port */
00191     /* TODO, be able send to multiple servers */
00192     ret= gearman_client_add_server(&_gearman_client,
00193                                    host.c_str(), 0);
00194     if (ret != GEARMAN_SUCCESS)
00195     {
00196       drizzled::errmsg_printf(drizzled::error::ERROR, _("fail gearman_client_add_server(): %s"),
00197                               gearman_client_error(&_gearman_client));
00198       return;
00199     }
00200 
00201     _gearman_client_ok= 1;
00202 
00203   }
00204 
00205   ~LoggingGearman()
00206   {
00207     if (_gearman_client_ok)
00208     {
00209       gearman_client_free(&_gearman_client);
00210     }
00211   }
00212 
00213   virtual bool post(drizzled::Session *session)
00214   {
00215     boost::scoped_array<char> msgbuf(new char[MAX_MSG_LEN]);
00216     int msgbuf_len= 0;
00217   
00218     assert(session != NULL);
00219 
00220     /* in theory, we should return "true", meaning that the plugin isn't happy,
00221        but that crashes the server, so for now, we just lie a little bit
00222     */
00223 
00224     if (not _gearman_client_ok)
00225         return false;
00226   
00227     /* 
00228       TODO, the session object should have a "utime command completed"
00229       inside itself, so be more accurate, and so this doesnt have to
00230       keep calling current_utime, which can be slow.
00231     */
00232     uint64_t t_mark= session->getCurrentTimestamp(false);
00233   
00234 
00235     // buffer to quotify the query
00236     unsigned char qs[255];
00237   
00238     // to avoid trying to printf %s something that is potentially NULL
00239     drizzled::util::string::const_shared_ptr dbs(session->schema());
00240   
00241     msgbuf_len=
00242       snprintf(msgbuf.get(), MAX_MSG_LEN,
00243                "%"PRIu64",%"PRIu64",%"PRIu64",\"%.*s\",\"%s\",\"%.*s\","
00244                "%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64","
00245                "%"PRIu32",%"PRIu32",%"PRIu32",\"%s\"",
00246                t_mark,
00247                session->thread_id,
00248                session->getQueryId(),
00249                // dont need to quote the db name, always CSV safe
00250                (int)dbs->size(), dbs->c_str(),
00251                // do need to quote the query
00252                quotify((const unsigned char *)session->getQueryString()->c_str(), session->getQueryString()->length(), qs, sizeof(qs)),
00253                // getCommandName is defined in drizzled/sql_parse.h dont
00254                // need to quote the command name, always CSV safe
00255                (int)drizzled::getCommandName(session->command).size(),
00256                drizzled::getCommandName(session->command).c_str(),
00257                // counters are at end, to make it easier to add more
00258                (t_mark - session->getConnectMicroseconds()),
00259                (session->getElapsedTime()),
00260                (t_mark - session->utime_after_lock),
00261                session->sent_row_count,
00262                session->examined_row_count,
00263                session->tmp_table,
00264                session->total_warn_count,
00265                session->getServerId(),
00266                drizzled::getServerHostname().c_str()
00267                );
00268   
00269     char job_handle[GEARMAN_JOB_HANDLE_SIZE];
00270   
00271     (void) gearman_client_do_background(&_gearman_client,
00272                                         _function.c_str(),
00273                                         NULL,
00274                                         (void *) msgbuf.get(),
00275                                         (size_t) msgbuf_len,
00276                                         job_handle);
00277   
00278     return false;
00279   }
00280 };
00281 
00282 static LoggingGearman *handler= NULL;
00283 
00284 static int logging_gearman_plugin_init(drizzled::module::Context &context)
00285 {
00286   const drizzled::module::option_map &vm= context.getOptions();
00287 
00288   handler= new LoggingGearman(vm["host"].as<std::string>(),
00289                               vm["function"].as<std::string>());
00290   context.add(handler);
00291   context.registerVariable(new drizzled::sys_var_const_string_val("host", vm["host"].as<std::string>()));
00292   context.registerVariable(new drizzled::sys_var_const_string_val("function", vm["function"].as<std::string>()));
00293 
00294   return 0;
00295 }
00296 
00297 static void init_options(drizzled::module::option_context &context)
00298 {
00299   context("host",
00300           po::value<std::string>()->default_value("localhost"),
00301           _("Hostname for logging to a Gearman server"));
00302   context("function",
00303           po::value<std::string>()->default_value("drizzlelog"),
00304           _("Gearman Function to send logging to"));
00305 }
00306 
00307 } /* namespace drizzle_plugin */
00308 
00309 DRIZZLE_DECLARE_PLUGIN
00310 {
00311   DRIZZLE_VERSION_ID,
00312     "logging-gearman",
00313     "0.1",
00314     "Mark Atwood <mark@fallenpegasus.com>",
00315     N_("Log queries to a Gearman server"),
00316     drizzled::PLUGIN_LICENSE_GPL,
00317     drizzle_plugin::logging_gearman_plugin_init,
00318     NULL,
00319     drizzle_plugin::init_options
00320 }
00321 DRIZZLE_DECLARE_PLUGIN_END;