~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/memcached_stats/stats_table.cc

  • Committer: Brian Aker
  • Date: 2009-10-07 04:06:47 UTC
  • mfrom: (1144.5.15 memc-is-tables)
  • Revision ID: brian@gaz-20091007040647-1vl4f42gs9ggfu8w
Merging Padraig

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
 * Copyright (c) 2009, Padraig O'Sullivan
 
3
 * All rights reserved.
 
4
 *
 
5
 * Redistribution and use in source and binary forms, with or without
 
6
 * modification, are permitted provided that the following conditions are met:
 
7
 *
 
8
 *   * Redistributions of source code must retain the above copyright notice,
 
9
 *     this list of conditions and the following disclaimer.
 
10
 *   * Redistributions in binary form must reproduce the above copyright notice,
 
11
 *     this list of conditions and the following disclaimer in the documentation
 
12
 *     and/or other materials provided with the distribution.
 
13
 *   * Neither the name of Padraig O'Sullivan nor the names of its contributors
 
14
 *     may be used to endorse or promote products derived from this software
 
15
 *     without specific prior written permission.
 
16
 *
 
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
18
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
20
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 
21
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 
22
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 
23
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 
24
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 
25
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
26
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 
27
 * THE POSSIBILITY OF SUCH DAMAGE.
 
28
 */
 
29
 
 
30
#include "drizzled/server_includes.h"
 
31
#include "drizzled/session.h"
 
32
#include "drizzled/show.h"
 
33
 
 
34
#include "stats_table.h"
 
35
#include "sysvar_holder.h"
 
36
 
 
37
#include <libmemcached/memcached.h>
 
38
 
 
39
#include <string>
 
40
#include <vector>
 
41
 
 
42
using namespace std;
 
43
using namespace drizzled;
 
44
 
 
45
int MemcachedStatsISMethods::fillTable(Session *session,
 
46
                                       TableList *tables,
 
47
                                       COND *)
 
48
{
 
49
  const CHARSET_INFO * const scs= system_charset_info;
 
50
  Table *table= tables->table;
 
51
  SysvarHolder &sysvar_holder= SysvarHolder::singleton();
 
52
  const string servers_string= sysvar_holder.getServersString();
 
53
 
 
54
  table->restoreRecordAsDefault();
 
55
 
 
56
  memcached_return rc;
 
57
  memcached_st *serv= memcached_create(NULL);
 
58
  memcached_server_st *tmp_serv=
 
59
    memcached_servers_parse(servers_string.c_str());
 
60
  memcached_server_push(serv, tmp_serv);
 
61
  memcached_server_list_free(tmp_serv);
 
62
  memcached_stat_st *stats= memcached_stat(serv, NULL, &rc);
 
63
  memcached_server_st *servers= memcached_server_list(serv);
 
64
 
 
65
  for (uint32_t i= 0; i < memcached_server_count(serv); i++)
 
66
  {
 
67
    char **list= memcached_stat_get_keys(serv, &stats[i], &rc);
 
68
    char **ptr= NULL;
 
69
 
 
70
    table->field[0]->store(memcached_server_name(serv, servers[i]),
 
71
                           64,
 
72
                           scs);
 
73
    table->field[1]->store(memcached_server_port(serv, servers[i]));
 
74
    uint32_t col= 2;
 
75
    for (ptr= list; *ptr; ptr++)
 
76
    {
 
77
      char *value= memcached_stat_get_value(serv, &stats[i], *ptr, &rc);
 
78
      table->field[col]->store(value,
 
79
                               64,
 
80
                               scs);
 
81
      col++;
 
82
      free(value);
 
83
    }
 
84
    free(list);
 
85
    /* store the actual record now */
 
86
    if (schema_table_store_record(session, table))
 
87
    {
 
88
      return 1;
 
89
    }
 
90
  }
 
91
 
 
92
  memcached_stat_free(serv, stats);
 
93
  memcached_free(serv);
 
94
 
 
95
  return 0;
 
96
}
 
97
 
 
98
bool createMemcachedStatsColumns(vector<const plugin::ColumnInfo *> &cols)
 
99
{
 
100
  /*
 
101
   * Create each column for the memcached stats table.
 
102
   */
 
103
  const plugin::ColumnInfo *name_col= new(std::nothrow) plugin::ColumnInfo("NAME",
 
104
                                                           32,
 
105
                                                           DRIZZLE_TYPE_VARCHAR,
 
106
                                                           0,
 
107
                                                           0,
 
108
                                                           "Name",
 
109
                                                           SKIP_OPEN_TABLE);
 
110
  if (! name_col)
 
111
  {
 
112
    return true;
 
113
  }
 
114
 
 
115
  const plugin::ColumnInfo *port= new(std::nothrow) plugin::ColumnInfo("PORT_NUMBER",
 
116
                                                       4,
 
117
                                                       DRIZZLE_TYPE_LONGLONG,
 
118
                                                       0,
 
119
                                                       0, 
 
120
                                                       "Port Number",
 
121
                                                       SKIP_OPEN_TABLE);
 
122
  if (! port)
 
123
  {
 
124
    return true;
 
125
  }
 
126
 
 
127
  const plugin::ColumnInfo *pid= new(std::nothrow) plugin::ColumnInfo("PROCESS_ID",
 
128
                                                      4,
 
129
                                                      DRIZZLE_TYPE_LONGLONG,
 
130
                                                      0,
 
131
                                                      0, 
 
132
                                                      "Process ID",
 
133
                                                      SKIP_OPEN_TABLE);
 
134
  if (! pid)
 
135
  {
 
136
    return true;
 
137
  }
 
138
 
 
139
  const plugin::ColumnInfo *uptime= new(std::nothrow) plugin::ColumnInfo("UPTIME",
 
140
                                                         4,
 
141
                                                         DRIZZLE_TYPE_LONGLONG,
 
142
                                                         0,
 
143
                                                         0, 
 
144
                                                         "Uptime",
 
145
                                                         SKIP_OPEN_TABLE);
 
146
  if (! uptime)
 
147
  {
 
148
    return true;
 
149
  }
 
150
 
 
151
  const plugin::ColumnInfo *time= new(std::nothrow) plugin::ColumnInfo("TIME",
 
152
                                                       4,
 
153
                                                       DRIZZLE_TYPE_LONGLONG,
 
154
                                                       0,
 
155
                                                       0, 
 
156
                                                       "Time",
 
157
                                                       SKIP_OPEN_TABLE);
 
158
  if (! time)
 
159
  {
 
160
    return true;
 
161
  }
 
162
 
 
163
  const plugin::ColumnInfo *version= new(std::nothrow) plugin::ColumnInfo("VERSION",
 
164
                                                          8,
 
165
                                                          DRIZZLE_TYPE_VARCHAR,
 
166
                                                          0,
 
167
                                                          0,
 
168
                                                          "Version",
 
169
                                                          SKIP_OPEN_TABLE);
 
170
  if (! version)
 
171
  {
 
172
    return true;
 
173
  }
 
174
 
 
175
  const plugin::ColumnInfo *ptr_size= new(std::nothrow) plugin::ColumnInfo("POINTER_SIZE",
 
176
                                                           4,
 
177
                                                           DRIZZLE_TYPE_LONGLONG,
 
178
                                                           0,
 
179
                                                           0, 
 
180
                                                           "Pointer Size",
 
181
                                                           SKIP_OPEN_TABLE);
 
182
  if (! ptr_size)
 
183
  {
 
184
    return true;
 
185
  }
 
186
 
 
187
  const plugin::ColumnInfo *r_user= new(std::nothrow) plugin::ColumnInfo("RUSAGE_USER",
 
188
                                                         4,
 
189
                                                         DRIZZLE_TYPE_LONGLONG,
 
190
                                                         0,
 
191
                                                         0, 
 
192
                                                         "rusage user",
 
193
                                                         SKIP_OPEN_TABLE);
 
194
  if (! r_user)
 
195
  {
 
196
    return true;
 
197
  }
 
198
 
 
199
  const plugin::ColumnInfo *r_sys= new(std::nothrow) plugin::ColumnInfo("RUSAGE_SYSTEM",
 
200
                                                        4,
 
201
                                                        DRIZZLE_TYPE_LONGLONG,
 
202
                                                        0,
 
203
                                                        0, 
 
204
                                                        "rusage system",
 
205
                                                        SKIP_OPEN_TABLE);
 
206
  if (! r_sys)
 
207
  {
 
208
    return true;
 
209
  }
 
210
  const plugin::ColumnInfo *curr_items= new(std::nothrow) plugin::ColumnInfo("CURRENT_ITEMS",
 
211
                                                             4,
 
212
                                                             DRIZZLE_TYPE_LONGLONG,
 
213
                                                             0,
 
214
                                                             0, 
 
215
                                                             "Current Items",
 
216
                                                             SKIP_OPEN_TABLE);
 
217
  if (! curr_items)
 
218
  {
 
219
    return true;
 
220
  }
 
221
 
 
222
  const plugin::ColumnInfo *total_items= new(std::nothrow) plugin::ColumnInfo("TOTAL_ITEMS",
 
223
                                                              4,
 
224
                                                              DRIZZLE_TYPE_LONGLONG,
 
225
                                                              0,
 
226
                                                              0,
 
227
                                                              "Total Items",
 
228
                                                              SKIP_OPEN_TABLE);
 
229
  if (! total_items)
 
230
  {
 
231
    return true;
 
232
  }
 
233
 
 
234
  const plugin::ColumnInfo *bytes= new(std::nothrow) plugin::ColumnInfo("BYTES",
 
235
                                                        4,
 
236
                                                        DRIZZLE_TYPE_LONGLONG,
 
237
                                                        0,
 
238
                                                        0,
 
239
                                                        "Bytes",
 
240
                                                        SKIP_OPEN_TABLE);
 
241
  if (! bytes)
 
242
  {
 
243
    return true;
 
244
  }
 
245
 
 
246
  const plugin::ColumnInfo *curr_cons= new(std::nothrow) plugin::ColumnInfo("CURRENT_CONNECTIONS",
 
247
                                                            4,
 
248
                                                            DRIZZLE_TYPE_LONGLONG,
 
249
                                                            0,
 
250
                                                            0,
 
251
                                                            "Current Connections",
 
252
                                                            SKIP_OPEN_TABLE);
 
253
  if (! curr_cons)
 
254
  {
 
255
    return true;
 
256
  }
 
257
 
 
258
  const plugin::ColumnInfo *total_cons= new(std::nothrow) plugin::ColumnInfo("TOTAL_CONNECTIONS",
 
259
                                                             4,
 
260
                                                             DRIZZLE_TYPE_LONGLONG,
 
261
                                                             0,
 
262
                                                             0,
 
263
                                                             "Total Connections",
 
264
                                                             SKIP_OPEN_TABLE);
 
265
  if (! total_cons)
 
266
  {
 
267
    return true;
 
268
  }
 
269
 
 
270
  const plugin::ColumnInfo *con_structs= new(std::nothrow) plugin::ColumnInfo("CONNECTION_STRUCTURES",
 
271
                                                              4,
 
272
                                                              DRIZZLE_TYPE_LONGLONG,
 
273
                                                              0,
 
274
                                                              0,
 
275
                                                              "Connection Structures",
 
276
                                                              SKIP_OPEN_TABLE);
 
277
  if (! con_structs)
 
278
  {
 
279
    return true;
 
280
  }
 
281
 
 
282
  const plugin::ColumnInfo *cmd_gets= new(std::nothrow) plugin::ColumnInfo("GETS",
 
283
                                                           4,
 
284
                                                           DRIZZLE_TYPE_LONGLONG,
 
285
                                                           0,
 
286
                                                           0,
 
287
                                                           "Gets",
 
288
                                                           SKIP_OPEN_TABLE);
 
289
  if (! cmd_gets)
 
290
  {
 
291
    return true;
 
292
  }
 
293
 
 
294
  const plugin::ColumnInfo *cmd_sets= new(std::nothrow) plugin::ColumnInfo("SETS",
 
295
                                                           4,
 
296
                                                           DRIZZLE_TYPE_LONGLONG,
 
297
                                                           0,
 
298
                                                           0,
 
299
                                                           "Sets",
 
300
                                                           SKIP_OPEN_TABLE);
 
301
  if (! cmd_sets)
 
302
  {
 
303
    return true;
 
304
  }
 
305
 
 
306
  const plugin::ColumnInfo *hits= new(std::nothrow) plugin::ColumnInfo("HITS",
 
307
                                                       4,
 
308
                                                       DRIZZLE_TYPE_LONGLONG,
 
309
                                                       0,
 
310
                                                       0,
 
311
                                                       "Hits",
 
312
                                                       SKIP_OPEN_TABLE);
 
313
  if (! hits)
 
314
  {
 
315
    return true;
 
316
  }
 
317
 
 
318
  const plugin::ColumnInfo *misses= new(std::nothrow) plugin::ColumnInfo("MISSES",
 
319
                                                         4,
 
320
                                                         DRIZZLE_TYPE_LONGLONG,
 
321
                                                         0,
 
322
                                                         0,
 
323
                                                         "Misses",
 
324
                                                         SKIP_OPEN_TABLE);
 
325
  if (! misses)
 
326
  {
 
327
    return true;
 
328
  }
 
329
 
 
330
  const plugin::ColumnInfo *evicts= new(std::nothrow) plugin::ColumnInfo("EVICTIONS",
 
331
                                                         4,
 
332
                                                         DRIZZLE_TYPE_LONGLONG,
 
333
                                                         0,
 
334
                                                         0,
 
335
                                                         "Evictions",
 
336
                                                         SKIP_OPEN_TABLE);
 
337
  if (! evicts)
 
338
  {
 
339
    return true;
 
340
  }
 
341
 
 
342
  const plugin::ColumnInfo *bytes_read= new(std::nothrow) plugin::ColumnInfo("BYTES_READ",
 
343
                                                             4,
 
344
                                                             DRIZZLE_TYPE_LONGLONG,
 
345
                                                             0,
 
346
                                                             0,
 
347
                                                             "bytes read",
 
348
                                                             SKIP_OPEN_TABLE);
 
349
  if (! bytes_read)
 
350
  {
 
351
    return true;
 
352
  }
 
353
 
 
354
  const plugin::ColumnInfo *bytes_written= new(std::nothrow) plugin::ColumnInfo("BYTES_WRITTEN",
 
355
                                                                4,
 
356
                                                                DRIZZLE_TYPE_LONGLONG,
 
357
                                                                0,
 
358
                                                                0,
 
359
                                                                "bytes written",
 
360
                                                                SKIP_OPEN_TABLE);
 
361
  if (! bytes_written)
 
362
  {
 
363
    return true;
 
364
  }
 
365
 
 
366
  const plugin::ColumnInfo *lim_max_bytes= new(std::nothrow) plugin::ColumnInfo("LIMIT_MAXBYTES",
 
367
                                                                4,
 
368
                                                                DRIZZLE_TYPE_LONGLONG,
 
369
                                                                0,
 
370
                                                                0,
 
371
                                                                "limit maxbytes",
 
372
                                                                SKIP_OPEN_TABLE);
 
373
  if (! lim_max_bytes)
 
374
  {
 
375
    return true;
 
376
  }
 
377
 
 
378
  const plugin::ColumnInfo *threads= new(std::nothrow) plugin::ColumnInfo("THREADS",
 
379
                                                          4,
 
380
                                                          DRIZZLE_TYPE_LONGLONG,
 
381
                                                          0,
 
382
                                                          0,
 
383
                                                          "Threads",
 
384
                                                          SKIP_OPEN_TABLE);
 
385
  if (! threads)
 
386
  {
 
387
    return true;
 
388
  }
 
389
 
 
390
  cols.push_back(name_col);
 
391
  cols.push_back(port);
 
392
  cols.push_back(pid);
 
393
  cols.push_back(uptime);
 
394
  cols.push_back(time);
 
395
  cols.push_back(version);
 
396
  cols.push_back(ptr_size);
 
397
  cols.push_back(r_user);
 
398
  cols.push_back(r_sys);
 
399
  cols.push_back(curr_items);
 
400
  cols.push_back(total_items);
 
401
  cols.push_back(bytes);
 
402
  cols.push_back(curr_cons);
 
403
  cols.push_back(total_cons);
 
404
  cols.push_back(con_structs);
 
405
  cols.push_back(cmd_gets);
 
406
  cols.push_back(cmd_sets);
 
407
  cols.push_back(hits);
 
408
  cols.push_back(misses);
 
409
  cols.push_back(evicts);
 
410
  cols.push_back(bytes_read);
 
411
  cols.push_back(bytes_written);
 
412
  cols.push_back(lim_max_bytes);
 
413
  cols.push_back(threads);
 
414
 
 
415
  return false;
 
416
}
 
417
 
 
418
class DeleteMemcachedCols
 
419
{
 
420
public:
 
421
  template<typename T>
 
422
  inline void operator()(const T *ptr) const
 
423
  {
 
424
    delete ptr;
 
425
  }
 
426
};
 
427
 
 
428
void clearMemcachedColumns(vector<const plugin::ColumnInfo *> &cols)
 
429
{
 
430
  for_each(cols.begin(), cols.end(), DeleteMemcachedCols());
 
431
  cols.clear();
 
432
}