~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to plugin/logging_syslog/logging_syslog.cc

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-03-18 12:12:31 UTC
  • Revision ID: james.westby@ubuntu.com-20100318121231-k6g1xe6cshbwa0f8
Tags: upstream-2010.03.1347
ImportĀ upstreamĀ versionĀ 2010.03.1347

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2009 Sun Microsystems
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; version 2 of the License.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 */
 
19
 
 
20
#include "config.h"
 
21
#include <drizzled/plugin/logging.h>
 
22
#include <drizzled/gettext.h>
 
23
#include <drizzled/session.h>
 
24
 
 
25
#ifdef __sun
 
26
# include <syslog.h>
 
27
# include <plugin/logging_syslog/names.h>
 
28
#else
 
29
# define SYSLOG_NAMES 1
 
30
# include <syslog.h>
 
31
#endif
 
32
 
 
33
#include <stdarg.h>
 
34
#include <limits.h>
 
35
#include <sys/time.h>
 
36
#include <sys/types.h>
 
37
#include <sys/stat.h>
 
38
#include <fcntl.h>
 
39
 
 
40
 
 
41
using namespace drizzled;
 
42
 
 
43
static bool sysvar_logging_syslog_enable= false;
 
44
static char* sysvar_logging_syslog_ident= NULL;
 
45
static char* sysvar_logging_syslog_facility= NULL;
 
46
static char* sysvar_logging_syslog_priority= NULL;
 
47
static ulong sysvar_logging_syslog_threshold_slow= 0;
 
48
static ulong sysvar_logging_syslog_threshold_big_resultset= 0;
 
49
static ulong sysvar_logging_syslog_threshold_big_examined= 0;
 
50
 
 
51
/* stolen from mysys/my_getsystime
 
52
   until the Session has a good utime "now" we can use
 
53
   will have to use this instead */
 
54
 
 
55
static uint64_t get_microtime()
 
56
{
 
57
#if defined(HAVE_GETHRTIME)
 
58
  return gethrtime()/1000;
 
59
#else
 
60
  uint64_t newtime;
 
61
  struct timeval t;
 
62
  /* loop is because gettimeofday may fail on some systems */
 
63
  while (gettimeofday(&t, NULL) != 0) {}
 
64
  newtime= (uint64_t)t.tv_sec * 1000000 + t.tv_usec;
 
65
  return newtime;
 
66
#endif
 
67
}
 
68
 
 
69
class Logging_syslog: public drizzled::plugin::Logging
 
70
{
 
71
 
 
72
  int syslog_facility;
 
73
  int syslog_priority;
 
74
 
 
75
public:
 
76
 
 
77
  Logging_syslog()
 
78
    : drizzled::plugin::Logging("Logging_syslog"),
 
79
      syslog_facility(-1), syslog_priority(-1)
 
80
  {
 
81
 
 
82
    for (int ndx= 0; facilitynames[ndx].c_name; ndx++)
 
83
    {
 
84
      if (strcasecmp(facilitynames[ndx].c_name, sysvar_logging_syslog_facility) == 0)
 
85
      {
 
86
        syslog_facility= facilitynames[ndx].c_val;
 
87
        break;
 
88
      }
 
89
    }
 
90
    if (syslog_facility == -1)
 
91
    {
 
92
      errmsg_printf(ERRMSG_LVL_WARN,
 
93
                    _("syslog facility \"%s\" not known, using \"local0\""),
 
94
                    sysvar_logging_syslog_facility);
 
95
      syslog_facility= LOG_LOCAL0;
 
96
    }
 
97
 
 
98
    for (int ndx= 0; prioritynames[ndx].c_name; ndx++)
 
99
    {
 
100
      if (strcasecmp(prioritynames[ndx].c_name, sysvar_logging_syslog_priority) == 0)
 
101
      {
 
102
        syslog_priority= prioritynames[ndx].c_val;
 
103
        break;
 
104
      }
 
105
    }
 
106
    if (syslog_priority == -1)
 
107
    {
 
108
      errmsg_printf(ERRMSG_LVL_WARN,
 
109
                    _("syslog priority \"%s\" not known, using \"info\""),
 
110
                    sysvar_logging_syslog_priority);
 
111
      syslog_priority= LOG_INFO;
 
112
    }
 
113
 
 
114
    openlog(sysvar_logging_syslog_ident,
 
115
            LOG_PID, syslog_facility);
 
116
  }
 
117
 
 
118
  ~Logging_syslog()
 
119
  {
 
120
    closelog();
 
121
  }
 
122
 
 
123
  virtual bool post (Session *session)
 
124
  {
 
125
    assert(session != NULL);
 
126
  
 
127
    // return if not enabled or query was too fast or resultset was too small
 
128
    if (sysvar_logging_syslog_enable == false)
 
129
      return false;
 
130
    if (session->sent_row_count < sysvar_logging_syslog_threshold_big_resultset)
 
131
      return false;
 
132
    if (session->examined_row_count < sysvar_logging_syslog_threshold_big_examined)
 
133
      return false;
 
134
  
 
135
    /* TODO, the session object should have a "utime command completed"
 
136
       inside itself, so be more accurate, and so this doesnt have to
 
137
       keep calling current_utime, which can be slow */
 
138
  
 
139
    uint64_t t_mark= get_microtime();
 
140
  
 
141
    if ((t_mark - session->start_utime) < sysvar_logging_syslog_threshold_slow)
 
142
      return false;
 
143
  
 
144
    /* to avoid trying to printf %s something that is potentially NULL */
 
145
  
 
146
    const char *dbs= session->db.empty() ? "" : session->db.c_str();
 
147
  
 
148
    const char *qys= (! session->getQueryString().empty()) ? session->getQueryString().c_str() : "";
 
149
    int qyl= 0;
 
150
    if (qys)
 
151
      qyl= session->getQueryLength();
 
152
    
 
153
    syslog(syslog_priority,
 
154
           "thread_id=%ld query_id=%ld"
 
155
           " db=\"%.*s\""
 
156
           " query=\"%.*s\""
 
157
           " command=\"%.*s\""
 
158
           " t_connect=%lld t_start=%lld t_lock=%lld"
 
159
           " rows_sent=%ld rows_examined=%ld"
 
160
           " tmp_table=%ld total_warn_count=%ld\n",
 
161
           (unsigned long) session->thread_id,
 
162
           (unsigned long) session->getQueryId(),
 
163
           (int)session->db.length(), dbs,
 
164
           qyl, qys,
 
165
           (int) command_name[session->command].length,
 
166
           command_name[session->command].str,
 
167
           (unsigned long long) (t_mark - session->getConnectMicroseconds()),
 
168
           (unsigned long long) (t_mark - session->start_utime),
 
169
           (unsigned long long) (t_mark - session->utime_after_lock),
 
170
           (unsigned long) session->sent_row_count,
 
171
           (unsigned long) session->examined_row_count,
 
172
           (unsigned long) session->tmp_table,
 
173
           (unsigned long) session->total_warn_count);
 
174
  
 
175
    return false;
 
176
  }
 
177
};
 
178
 
 
179
static Logging_syslog *handler= NULL;
 
180
 
 
181
static int logging_syslog_plugin_init(drizzled::plugin::Registry &registry)
 
182
{
 
183
  handler= new Logging_syslog();
 
184
  registry.add(handler);
 
185
 
 
186
  return 0;
 
187
}
 
188
 
 
189
static int logging_syslog_plugin_deinit(drizzled::plugin::Registry &registry)
 
190
{
 
191
  registry.remove(handler);
 
192
  delete handler;
 
193
 
 
194
  return 0;
 
195
}
 
196
 
 
197
static DRIZZLE_SYSVAR_BOOL(
 
198
  enable,
 
199
  sysvar_logging_syslog_enable,
 
200
  PLUGIN_VAR_NOCMDARG,
 
201
  N_("Enable logging to syslog"),
 
202
  NULL, /* check func */
 
203
  NULL, /* update func */
 
204
  false /* default */);
 
205
 
 
206
static DRIZZLE_SYSVAR_STR(
 
207
  ident,
 
208
  sysvar_logging_syslog_ident,
 
209
  PLUGIN_VAR_READONLY,
 
210
  N_("Syslog Ident"),
 
211
  NULL, /* check func */
 
212
  NULL, /* update func*/
 
213
  "drizzled" /* default */);
 
214
 
 
215
static DRIZZLE_SYSVAR_STR(
 
216
  facility,
 
217
  sysvar_logging_syslog_facility,
 
218
  PLUGIN_VAR_READONLY,
 
219
  N_("Syslog Facility"),
 
220
  NULL, /* check func */
 
221
  NULL, /* update func*/
 
222
  "local0" /* default */);  // local0 is what PostGreSQL uses by default
 
223
 
 
224
static DRIZZLE_SYSVAR_STR(
 
225
  priority,
 
226
  sysvar_logging_syslog_priority,
 
227
  PLUGIN_VAR_READONLY,
 
228
  N_("Syslog Priority"),
 
229
  NULL, /* check func */
 
230
  NULL, /* update func*/
 
231
  "info" /* default */);
 
232
 
 
233
static DRIZZLE_SYSVAR_ULONG(
 
234
  threshold_slow,
 
235
  sysvar_logging_syslog_threshold_slow,
 
236
  PLUGIN_VAR_OPCMDARG,
 
237
  N_("Threshold for logging slow queries, in microseconds"),
 
238
  NULL, /* check func */
 
239
  NULL, /* update func */
 
240
  0, /* default */
 
241
  0, /* min */
 
242
  ULONG_MAX, /* max */
 
243
  0 /* blksiz */);
 
244
 
 
245
static DRIZZLE_SYSVAR_ULONG(
 
246
  threshold_big_resultset,
 
247
  sysvar_logging_syslog_threshold_big_resultset,
 
248
  PLUGIN_VAR_OPCMDARG,
 
249
  N_("Threshold for logging big queries, for rows returned"),
 
250
  NULL, /* check func */
 
251
  NULL, /* update func */
 
252
  0, /* default */
 
253
  0, /* min */
 
254
  ULONG_MAX, /* max */
 
255
  0 /* blksiz */);
 
256
 
 
257
static DRIZZLE_SYSVAR_ULONG(
 
258
  threshold_big_examined,
 
259
  sysvar_logging_syslog_threshold_big_examined,
 
260
  PLUGIN_VAR_OPCMDARG,
 
261
  N_("Threshold for logging big queries, for rows examined"),
 
262
  NULL, /* check func */
 
263
  NULL, /* update func */
 
264
  0, /* default */
 
265
  0, /* min */
 
266
  ULONG_MAX, /* max */
 
267
  0 /* blksiz */);
 
268
 
 
269
static drizzle_sys_var* logging_syslog_system_variables[]= {
 
270
  DRIZZLE_SYSVAR(enable),
 
271
  DRIZZLE_SYSVAR(ident),
 
272
  DRIZZLE_SYSVAR(facility),
 
273
  DRIZZLE_SYSVAR(priority),
 
274
  DRIZZLE_SYSVAR(threshold_slow),
 
275
  DRIZZLE_SYSVAR(threshold_big_resultset),
 
276
  DRIZZLE_SYSVAR(threshold_big_examined),
 
277
  NULL
 
278
};
 
279
 
 
280
DRIZZLE_DECLARE_PLUGIN
 
281
{
 
282
  DRIZZLE_VERSION_ID,
 
283
  "logging_syslog",
 
284
  "0.2",
 
285
  "Mark Atwood <mark@fallenpegasus.com>",
 
286
  N_("Log to syslog"),
 
287
  PLUGIN_LICENSE_GPL,
 
288
  logging_syslog_plugin_init,
 
289
  logging_syslog_plugin_deinit,
 
290
  logging_syslog_system_variables,
 
291
  NULL
 
292
}
 
293
DRIZZLE_DECLARE_PLUGIN_END;