~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/memcached_stats/stats_table.cc

  • Committer: Brian Aker
  • Date: 2010-04-05 23:46:43 UTC
  • Revision ID: brian@gaz-20100405234643-0he3xnj902rc70r8
Fixing tests to work with PBXT.

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 "config.h"
 
31
 
 
32
#include "stats_table.h"
 
33
#include "sysvar_holder.h"
 
34
 
 
35
#include "drizzled/error.h"
 
36
 
 
37
using namespace std;
 
38
using namespace drizzled;
 
39
 
 
40
#if !defined(HAVE_MEMCACHED_SERVER_FN)
 
41
typedef memcached_server_function memcached_server_fn;
 
42
#endif
 
43
 
 
44
extern "C"
 
45
memcached_return  server_function(const memcached_st *ptr,
 
46
                                  memcached_server_st *server,
 
47
                                  void *context);
 
48
 
 
49
 
 
50
struct server_function_context
 
51
{
 
52
  StatsTableTool::Generator* generator; 
 
53
  server_function_context(StatsTableTool::Generator *generator_arg)
 
54
    : generator(generator_arg)
 
55
  {}
 
56
};
 
57
 
 
58
 
 
59
extern "C"
 
60
memcached_return  server_function(const memcached_st *memc,
 
61
                                  memcached_server_st *server,
 
62
                                  void *context)
 
63
{
 
64
  server_function_context *ctx= static_cast<server_function_context *>(context);
 
65
 
 
66
  char *server_name= memcached_server_name(memc, *server);
 
67
  in_port_t server_port= memcached_server_port(memc, *server);
 
68
 
 
69
  memcached_stat_st stats;
 
70
  memcached_return ret= memcached_stat_servername(&stats, NULL,
 
71
                                                  server_name, server_port);
 
72
 
 
73
  if (ret != MEMCACHED_SUCCESS)
 
74
  {
 
75
    my_printf_error(ER_UNKNOWN_ERROR, _("Unable get stats from memcached server %s.  Got error from memcached_stat_servername()."), MYF(0), server_name);
 
76
    return ret;
 
77
  }
 
78
 
 
79
  char **list= memcached_stat_get_keys((memcached_st *)memc, &stats, &ret);
 
80
  char **ptr= NULL;
 
81
 
 
82
  ctx->generator->push(server_name);
 
83
  ctx->generator->push(static_cast<uint64_t>(server_port));
 
84
 
 
85
  for (ptr= list; *ptr; ptr++)
 
86
  {
 
87
    char *value= memcached_stat_get_value((memcached_st *)memc, &stats, *ptr, &ret);
 
88
    ctx->generator->push(value);
 
89
    free(value);
 
90
  }
 
91
  free(list);
 
92
 
 
93
  return MEMCACHED_SUCCESS;
 
94
}
 
95
 
 
96
 
 
97
StatsTableTool::StatsTableTool() :
 
98
  plugin::TableFunction("DATA_DICTIONARY", "MEMCACHED_STATS")
 
99
{
 
100
  add_field("NAME");
 
101
  add_field("PORT_NUMBER", plugin::TableFunction::NUMBER);
 
102
  add_field("PROCESS_ID", plugin::TableFunction::NUMBER);
 
103
  add_field("UPTIME", plugin::TableFunction::NUMBER);
 
104
  add_field("TIME", plugin::TableFunction::NUMBER);
 
105
  add_field("VERSION");
 
106
  add_field("POINTER_SIZE", plugin::TableFunction::NUMBER);
 
107
  add_field("RUSAGE_USER", plugin::TableFunction::NUMBER);
 
108
  add_field("RUSAGE_SYSTEM", plugin::TableFunction::NUMBER);
 
109
  add_field("CURRENT_ITEMS", plugin::TableFunction::NUMBER);
 
110
  add_field("TOTAL_ITEMS", plugin::TableFunction::NUMBER);
 
111
  add_field("BYTES",  plugin::TableFunction::NUMBER);
 
112
  add_field("CURRENT_CONNECTIONS", plugin::TableFunction::NUMBER);
 
113
  add_field("TOTAL_CONNECTIONS", plugin::TableFunction::NUMBER);
 
114
  add_field("CONNECTION_STRUCTURES", plugin::TableFunction::NUMBER);
 
115
  add_field("GETS", plugin::TableFunction::NUMBER);
 
116
  add_field("SETS", plugin::TableFunction::NUMBER);
 
117
  add_field("HITS", plugin::TableFunction::NUMBER);
 
118
  add_field("MISSES", plugin::TableFunction::NUMBER); 
 
119
  add_field("EVICTIONS", plugin::TableFunction::NUMBER);
 
120
  add_field("BYTES_READ", plugin::TableFunction::NUMBER);
 
121
  add_field("BYTES_WRITTEN", plugin::TableFunction::NUMBER);
 
122
  add_field("LIMIT_MAXBYTES", plugin::TableFunction::NUMBER);
 
123
  add_field("THREADS", plugin::TableFunction::NUMBER);
 
124
}
 
125
 
 
126
 
 
127
StatsTableTool::Generator::Generator(Field **arg) :
 
128
  plugin::TableFunction::Generator(arg)
 
129
{
 
130
  /* This will be set to the real number if we initialize properly below */
 
131
  number_of_hosts= 0;
 
132
  
 
133
  host_number= 0;
 
134
 
 
135
  /* set to NULL if we are not able to init we dont want to call delete on this */
 
136
  memc= NULL;
 
137
 
 
138
  SysvarHolder &sysvar_holder= SysvarHolder::singleton();
 
139
  const string servers_string= sysvar_holder.getServersString();
 
140
 
 
141
  if (servers_string.empty())
 
142
  {
 
143
    my_printf_error(ER_UNKNOWN_ERROR, _("No value in MEMCACHED_STATS_SERVERS variable."), MYF(0));
 
144
    return; 
 
145
  }
 
146
 
 
147
  memc= memcached_create(NULL);
 
148
  if (memc == NULL)
 
149
  {
 
150
    my_printf_error(ER_UNKNOWN_ERROR, _("Unable to create memcached struct.  Got error from memcached_create()."), MYF(0));
 
151
    return;
 
152
  }
 
153
 
 
154
  memcached_server_st *tmp_serv=
 
155
    memcached_servers_parse(servers_string.c_str());
 
156
  if (tmp_serv == NULL)
 
157
  {
 
158
    my_printf_error(ER_UNKNOWN_ERROR, _("Unable to create memcached server list.  Got error from memcached_servers_parse(%s)."), MYF(0), servers_string.c_str());
 
159
    return;
 
160
  }
 
161
 
 
162
  memcached_server_push(memc, tmp_serv);
 
163
  memcached_server_list_free(tmp_serv);
 
164
 
 
165
  number_of_hosts= memc->number_of_hosts;  
 
166
}
 
167
 
 
168
 
 
169
StatsTableTool::Generator::~Generator()
 
170
{
 
171
  if (memc != NULL)
 
172
  {
 
173
    memcached_free(memc);
 
174
  }
 
175
}
 
176
 
 
177
 
 
178
bool StatsTableTool::Generator::populate()
 
179
{
 
180
  if (host_number == number_of_hosts)
 
181
  {
 
182
    return false;
 
183
  }
 
184
 
 
185
  server_function_context context(this);
 
186
 
 
187
  memcached_server_fn callbacks[1];
 
188
  callbacks[0]= server_function;
 
189
 
 
190
  unsigned int iferror; 
 
191
  iferror= (*callbacks[0])(memc, &memc->servers[host_number], (void *)&context); 
 
192
 
 
193
  if (iferror)
 
194
  {
 
195
    return false;
 
196
  }
 
197
 
 
198
  host_number++;
 
199
 
 
200
  return true;
 
201
}