~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/memcached_stats/stats_table.cc

  • Committer: Padraig O'Sullivan
  • Date: 2009-09-29 19:34:58 UTC
  • mto: This revision was merged to the branch mainline in revision 1167.
  • Revision ID: osullivan.padraig@gmail.com-20090929193458-zs97say0t1t85b4h
Created plugin which simply adds I_S tables for querying memcached stats.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 2009 Sun Microsystems
 
3
 *
 
4
 *  This program is free software; you can redistribute it and/or modify
 
5
 *  it under the terms of the GNU General Public License as published by
 
6
 *  the Free Software Foundation; either version 2 of the License, or
 
7
 *  (at your option) any later version.
 
8
 *
 
9
 *  This program is distributed in the hope that it will be useful,
 
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 *  GNU General Public License for more details.
 
13
 *
 
14
 *  You should have received a copy of the GNU General Public License
 
15
 *  along with this program; if not, write to the Free Software
 
16
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
17
 */
 
18
 
 
19
#include "drizzled/server_includes.h"
 
20
#include "drizzled/session.h"
 
21
#include "drizzled/show.h"
 
22
 
 
23
#include "stats_table.h"
 
24
 
 
25
#include <libmemcached/memcached.h>
 
26
 
 
27
#include <string>
 
28
#include <vector>
 
29
 
 
30
using namespace std;
 
31
 
 
32
int MemcachedStatsISMethods::fillTable(Session *session,
 
33
                                       TableList *tables,
 
34
                                       COND *)
 
35
{
 
36
  const CHARSET_INFO * const scs= system_charset_info;
 
37
  Table *table= tables->table;
 
38
 
 
39
  table->restoreRecordAsDefault();
 
40
 
 
41
  memcached_return rc;
 
42
  memcached_st *serv= memcached_create(NULL);
 
43
  memcached_server_st *tmp_serv=
 
44
    memcached_servers_parse(servers_string.c_str());
 
45
  memcached_server_push(serv, tmp_serv);
 
46
  memcached_server_list_free(tmp_serv);
 
47
  memcached_stat_st *stats= memcached_stat(serv, NULL, &rc);
 
48
  memcached_server_st *servers= memcached_server_list(serv);
 
49
 
 
50
  for (uint32_t i= 0; i < memcached_server_count(serv); i++)
 
51
  {
 
52
    char **list= memcached_stat_get_keys(serv, &stats[i], &rc);
 
53
    char **ptr= NULL;
 
54
 
 
55
    table->field[0]->store(memcached_server_name(serv, servers[i]),
 
56
                           64,
 
57
                           scs);
 
58
    table->field[1]->store(memcached_server_port(serv, servers[i]));
 
59
    uint32_t col= 2;
 
60
    for (ptr= list; *ptr; ptr++)
 
61
    {
 
62
      char *value= memcached_stat_get_value(serv, &stats[i], *ptr, &rc);
 
63
      table->field[col]->store(value,
 
64
                               64,
 
65
                               scs);
 
66
      col++;
 
67
      free(value);
 
68
    }
 
69
    free(list);
 
70
  }
 
71
 
 
72
  memcached_stat_free(serv, stats);
 
73
  memcached_free(serv);
 
74
 
 
75
  /* store the actual record now */
 
76
  if (schema_table_store_record(session, table))
 
77
  {
 
78
    return 1;
 
79
  }
 
80
 
 
81
  return 0;
 
82
}
 
83
 
 
84
bool createMemcachedStatsColumns(vector<const ColumnInfo *> &cols)
 
85
{
 
86
  /*
 
87
   * Create each column for the memcached stats table.
 
88
   */
 
89
  const ColumnInfo *name_col= new(std::nothrow) ColumnInfo("NAME",
 
90
                                                           32,
 
91
                                                           DRIZZLE_TYPE_VARCHAR,
 
92
                                                           0,
 
93
                                                           0,
 
94
                                                           "Name",
 
95
                                                           SKIP_OPEN_TABLE);
 
96
  if (! name_col)
 
97
  {
 
98
    return true;
 
99
  }
 
100
 
 
101
  const ColumnInfo *port= new(std::nothrow) ColumnInfo("PORT_NUMBER",
 
102
                                                       4,
 
103
                                                       DRIZZLE_TYPE_LONGLONG,
 
104
                                                       0,
 
105
                                                       0, 
 
106
                                                       "Port Number",
 
107
                                                       SKIP_OPEN_TABLE);
 
108
  if (! port)
 
109
  {
 
110
    return true;
 
111
  }
 
112
 
 
113
  const ColumnInfo *pid= new(std::nothrow) ColumnInfo("PROCESS_ID",
 
114
                                                      4,
 
115
                                                      DRIZZLE_TYPE_LONGLONG,
 
116
                                                      0,
 
117
                                                      0, 
 
118
                                                      "Process ID",
 
119
                                                      SKIP_OPEN_TABLE);
 
120
  if (! pid)
 
121
  {
 
122
    return true;
 
123
  }
 
124
 
 
125
  const ColumnInfo *uptime= new(std::nothrow) ColumnInfo("UPTIME",
 
126
                                                         4,
 
127
                                                         DRIZZLE_TYPE_LONGLONG,
 
128
                                                         0,
 
129
                                                         0, 
 
130
                                                         "Uptime",
 
131
                                                         SKIP_OPEN_TABLE);
 
132
  if (! uptime)
 
133
  {
 
134
    return true;
 
135
  }
 
136
 
 
137
  const ColumnInfo *time= new(std::nothrow) ColumnInfo("TIME",
 
138
                                                       4,
 
139
                                                       DRIZZLE_TYPE_LONGLONG,
 
140
                                                       0,
 
141
                                                       0, 
 
142
                                                       "Time",
 
143
                                                       SKIP_OPEN_TABLE);
 
144
  if (! time)
 
145
  {
 
146
    return true;
 
147
  }
 
148
 
 
149
  const ColumnInfo *version= new(std::nothrow) ColumnInfo("VERSION",
 
150
                                                          8,
 
151
                                                          DRIZZLE_TYPE_VARCHAR,
 
152
                                                          0,
 
153
                                                          0,
 
154
                                                          "Version",
 
155
                                                          SKIP_OPEN_TABLE);
 
156
  if (! version)
 
157
  {
 
158
    return true;
 
159
  }
 
160
 
 
161
  const ColumnInfo *ptr_size= new(std::nothrow) ColumnInfo("POINTER_SIZE",
 
162
                                                           4,
 
163
                                                           DRIZZLE_TYPE_LONGLONG,
 
164
                                                           0,
 
165
                                                           0, 
 
166
                                                           "Pointer Size",
 
167
                                                           SKIP_OPEN_TABLE);
 
168
  if (! ptr_size)
 
169
  {
 
170
    return true;
 
171
  }
 
172
 
 
173
  const ColumnInfo *r_user= new(std::nothrow) ColumnInfo("RUSAGE_USER",
 
174
                                                         4,
 
175
                                                         DRIZZLE_TYPE_LONGLONG,
 
176
                                                         0,
 
177
                                                         0, 
 
178
                                                         "rusage user",
 
179
                                                         SKIP_OPEN_TABLE);
 
180
  if (! r_user)
 
181
  {
 
182
    return true;
 
183
  }
 
184
 
 
185
  const ColumnInfo *r_sys= new(std::nothrow) ColumnInfo("RUSAGE_SYSTEM",
 
186
                                                        4,
 
187
                                                        DRIZZLE_TYPE_LONGLONG,
 
188
                                                        0,
 
189
                                                        0, 
 
190
                                                        "rusage system",
 
191
                                                        SKIP_OPEN_TABLE);
 
192
  if (! r_sys)
 
193
  {
 
194
    return true;
 
195
  }
 
196
  const ColumnInfo *curr_items= new(std::nothrow) ColumnInfo("CURRENT_ITEMS",
 
197
                                                             4,
 
198
                                                             DRIZZLE_TYPE_LONGLONG,
 
199
                                                             0,
 
200
                                                             0, 
 
201
                                                             "Current Items",
 
202
                                                             SKIP_OPEN_TABLE);
 
203
  if (! curr_items)
 
204
  {
 
205
    return true;
 
206
  }
 
207
 
 
208
  const ColumnInfo *total_items= new(std::nothrow) ColumnInfo("TOTAL_ITEMS",
 
209
                                                              4,
 
210
                                                              DRIZZLE_TYPE_LONGLONG,
 
211
                                                              0,
 
212
                                                              0,
 
213
                                                              "Total Items",
 
214
                                                              SKIP_OPEN_TABLE);
 
215
  if (! total_items)
 
216
  {
 
217
    return true;
 
218
  }
 
219
 
 
220
  const ColumnInfo *bytes= new(std::nothrow) ColumnInfo("BYTES",
 
221
                                                        4,
 
222
                                                        DRIZZLE_TYPE_LONGLONG,
 
223
                                                        0,
 
224
                                                        0,
 
225
                                                        "Bytes",
 
226
                                                        SKIP_OPEN_TABLE);
 
227
  if (! bytes)
 
228
  {
 
229
    return true;
 
230
  }
 
231
 
 
232
  const ColumnInfo *curr_cons= new(std::nothrow) ColumnInfo("CURRENT_CONNECTIONS",
 
233
                                                            4,
 
234
                                                            DRIZZLE_TYPE_LONGLONG,
 
235
                                                            0,
 
236
                                                            0,
 
237
                                                            "Current Connections",
 
238
                                                            SKIP_OPEN_TABLE);
 
239
  if (! curr_cons)
 
240
  {
 
241
    return true;
 
242
  }
 
243
 
 
244
  const ColumnInfo *total_cons= new(std::nothrow) ColumnInfo("TOTAL_CONNECTIONS",
 
245
                                                             4,
 
246
                                                             DRIZZLE_TYPE_LONGLONG,
 
247
                                                             0,
 
248
                                                             0,
 
249
                                                             "Total Connections",
 
250
                                                             SKIP_OPEN_TABLE);
 
251
  if (! total_cons)
 
252
  {
 
253
    return true;
 
254
  }
 
255
 
 
256
  const ColumnInfo *con_structs= new(std::nothrow) ColumnInfo("CONNECTION_STRUCTURES",
 
257
                                                              4,
 
258
                                                              DRIZZLE_TYPE_LONGLONG,
 
259
                                                              0,
 
260
                                                              0,
 
261
                                                              "Connection Structures",
 
262
                                                              SKIP_OPEN_TABLE);
 
263
  if (! con_structs)
 
264
  {
 
265
    return true;
 
266
  }
 
267
 
 
268
  const ColumnInfo *cmd_gets= new(std::nothrow) ColumnInfo("GETS",
 
269
                                                           4,
 
270
                                                           DRIZZLE_TYPE_LONGLONG,
 
271
                                                           0,
 
272
                                                           0,
 
273
                                                           "Gets",
 
274
                                                           SKIP_OPEN_TABLE);
 
275
  if (! cmd_gets)
 
276
  {
 
277
    return true;
 
278
  }
 
279
 
 
280
  const ColumnInfo *cmd_sets= new(std::nothrow) ColumnInfo("SETS",
 
281
                                                           4,
 
282
                                                           DRIZZLE_TYPE_LONGLONG,
 
283
                                                           0,
 
284
                                                           0,
 
285
                                                           "Sets",
 
286
                                                           SKIP_OPEN_TABLE);
 
287
  if (! cmd_sets)
 
288
  {
 
289
    return true;
 
290
  }
 
291
 
 
292
  const ColumnInfo *hits= new(std::nothrow) ColumnInfo("HITS",
 
293
                                                       4,
 
294
                                                       DRIZZLE_TYPE_LONGLONG,
 
295
                                                       0,
 
296
                                                       0,
 
297
                                                       "Hits",
 
298
                                                       SKIP_OPEN_TABLE);
 
299
  if (! hits)
 
300
  {
 
301
    return true;
 
302
  }
 
303
 
 
304
  const ColumnInfo *misses= new(std::nothrow) ColumnInfo("MISSES",
 
305
                                                         4,
 
306
                                                         DRIZZLE_TYPE_LONGLONG,
 
307
                                                         0,
 
308
                                                         0,
 
309
                                                         "Misses",
 
310
                                                         SKIP_OPEN_TABLE);
 
311
  if (! misses)
 
312
  {
 
313
    return true;
 
314
  }
 
315
 
 
316
  const ColumnInfo *evicts= new(std::nothrow) ColumnInfo("EVICTIONS",
 
317
                                                         4,
 
318
                                                         DRIZZLE_TYPE_LONGLONG,
 
319
                                                         0,
 
320
                                                         0,
 
321
                                                         "Evictions",
 
322
                                                         SKIP_OPEN_TABLE);
 
323
  if (! evicts)
 
324
  {
 
325
    return true;
 
326
  }
 
327
 
 
328
  const ColumnInfo *bytes_read= new(std::nothrow) ColumnInfo("BYTES_READ",
 
329
                                                             4,
 
330
                                                             DRIZZLE_TYPE_LONGLONG,
 
331
                                                             0,
 
332
                                                             0,
 
333
                                                             "bytes read",
 
334
                                                             SKIP_OPEN_TABLE);
 
335
  if (! bytes_read)
 
336
  {
 
337
    return true;
 
338
  }
 
339
 
 
340
  const ColumnInfo *bytes_written= new(std::nothrow) ColumnInfo("BYTES_WRITTEN",
 
341
                                                                4,
 
342
                                                                DRIZZLE_TYPE_LONGLONG,
 
343
                                                                0,
 
344
                                                                0,
 
345
                                                                "bytes written",
 
346
                                                                SKIP_OPEN_TABLE);
 
347
  if (! bytes_written)
 
348
  {
 
349
    return true;
 
350
  }
 
351
 
 
352
  const ColumnInfo *lim_max_bytes= new(std::nothrow) ColumnInfo("LIMIT_MAXBYTES",
 
353
                                                                4,
 
354
                                                                DRIZZLE_TYPE_LONGLONG,
 
355
                                                                0,
 
356
                                                                0,
 
357
                                                                "limit maxbytes",
 
358
                                                                SKIP_OPEN_TABLE);
 
359
  if (! lim_max_bytes)
 
360
  {
 
361
    return true;
 
362
  }
 
363
 
 
364
  const ColumnInfo *threads= new(std::nothrow) ColumnInfo("THREADS",
 
365
                                                          4,
 
366
                                                          DRIZZLE_TYPE_LONGLONG,
 
367
                                                          0,
 
368
                                                          0,
 
369
                                                          "Threads",
 
370
                                                          SKIP_OPEN_TABLE);
 
371
  if (! threads)
 
372
  {
 
373
    return true;
 
374
  }
 
375
 
 
376
  cols.push_back(name_col);
 
377
  cols.push_back(port);
 
378
  cols.push_back(pid);
 
379
  cols.push_back(uptime);
 
380
  cols.push_back(time);
 
381
  cols.push_back(version);
 
382
  cols.push_back(ptr_size);
 
383
  cols.push_back(r_user);
 
384
  cols.push_back(r_sys);
 
385
  cols.push_back(curr_items);
 
386
  cols.push_back(total_items);
 
387
  cols.push_back(bytes);
 
388
  cols.push_back(curr_cons);
 
389
  cols.push_back(total_cons);
 
390
  cols.push_back(con_structs);
 
391
  cols.push_back(cmd_gets);
 
392
  cols.push_back(cmd_sets);
 
393
  cols.push_back(hits);
 
394
  cols.push_back(misses);
 
395
  cols.push_back(evicts);
 
396
  cols.push_back(bytes_read);
 
397
  cols.push_back(bytes_written);
 
398
  cols.push_back(lim_max_bytes);
 
399
  cols.push_back(threads);
 
400
 
 
401
  return false;
 
402
}
 
403
 
 
404
class DeleteMemcachedCols
 
405
{
 
406
public:
 
407
  template<typename T>
 
408
  inline void operator()(const T *ptr) const
 
409
  {
 
410
    delete ptr;
 
411
  }
 
412
};
 
413
 
 
414
void clearMemcachedColumns(vector<const ColumnInfo *> &cols)
 
415
{
 
416
  for_each(cols.begin(), cols.end(), DeleteMemcachedCols());
 
417
  cols.clear();
 
418
}