~drizzle-trunk/drizzle/development

1300.2.2 by Joe Daly
port memcached stats plugin to use data_dictionary
1
/*
1144.5.4 by Padraig O'Sullivan
Updated the licence to be BSD since I don't see why this would need to be GPL.
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.
1144.5.7 by Padraig O'Sullivan
Updated the BSD header to actually have my name instead of capttofu's!
13
 *   * Neither the name of Padraig O'Sullivan nor the names of its contributors
1144.5.4 by Padraig O'Sullivan
Updated the licence to be BSD since I don't see why this would need to be GPL.
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.
1144.5.1 by Padraig O'Sullivan
Created plugin which simply adds I_S tables for querying memcached stats.
28
 */
29
1273.13.94 by Brian Aker
Lint fixes.
30
#include "config.h"
31
1144.5.1 by Padraig O'Sullivan
Created plugin which simply adds I_S tables for querying memcached stats.
32
#include "stats_table.h"
1144.5.3 by Padraig O'Sullivan
Added support for system variables to set the memcached servers whose
33
#include "sysvar_holder.h"
1144.5.1 by Padraig O'Sullivan
Created plugin which simply adds I_S tables for querying memcached stats.
34
1300.2.2 by Joe Daly
port memcached stats plugin to use data_dictionary
35
#include "drizzled/error.h"
1452.2.5 by Monty Taylor
Fixed the memcached_stats plugin to work with the latest libmemcached, now
36
#include <libmemcached/server.h>
1144.5.1 by Padraig O'Sullivan
Created plugin which simply adds I_S tables for querying memcached stats.
37
38
using namespace std;
1144.5.12 by Padraig O'Sullivan
Updating the memcached I_S tables plugin to conform to the new work that
39
using namespace drizzled;
1144.5.1 by Padraig O'Sullivan
Created plugin which simply adds I_S tables for querying memcached stats.
40
1259.4.4 by Monty Taylor
Fixed the memcached stats test to launch a memcached, and also to deal with the
41
#if !defined(HAVE_MEMCACHED_SERVER_FN)
42
typedef memcached_server_function memcached_server_fn;
43
#endif
44
45
extern "C"
1273.13.57 by Brian Aker
Fix for recursive stat use.
46
memcached_return  server_function(const memcached_st *ptr,
1452.2.5 by Monty Taylor
Fixed the memcached_stats plugin to work with the latest libmemcached, now
47
                                  memcached_server_st *server,
1259.4.4 by Monty Taylor
Fixed the memcached stats test to launch a memcached, and also to deal with the
48
                                  void *context);
49
1300.2.2 by Joe Daly
port memcached stats plugin to use data_dictionary
50
1259.4.4 by Monty Taylor
Fixed the memcached stats test to launch a memcached, and also to deal with the
51
struct server_function_context
52
{
1300.2.2 by Joe Daly
port memcached stats plugin to use data_dictionary
53
  StatsTableTool::Generator* generator; 
54
  server_function_context(StatsTableTool::Generator *generator_arg)
55
    : generator(generator_arg)
1259.4.4 by Monty Taylor
Fixed the memcached stats test to launch a memcached, and also to deal with the
56
  {}
57
};
58
1300.2.2 by Joe Daly
port memcached stats plugin to use data_dictionary
59
1259.4.4 by Monty Taylor
Fixed the memcached stats test to launch a memcached, and also to deal with the
60
extern "C"
1300.2.2 by Joe Daly
port memcached stats plugin to use data_dictionary
61
memcached_return  server_function(const memcached_st *memc,
1452.2.5 by Monty Taylor
Fixed the memcached_stats plugin to work with the latest libmemcached, now
62
                                  memcached_server_st *server,
1259.4.4 by Monty Taylor
Fixed the memcached stats test to launch a memcached, and also to deal with the
63
                                  void *context)
64
{
65
  server_function_context *ctx= static_cast<server_function_context *>(context);
1300.2.2 by Joe Daly
port memcached stats plugin to use data_dictionary
66
1452.2.5 by Monty Taylor
Fixed the memcached_stats plugin to work with the latest libmemcached, now
67
  const char *server_name= memcached_server_name(*memc, *server);
68
  in_port_t server_port= memcached_server_port(*memc, *server);
1259.4.4 by Monty Taylor
Fixed the memcached stats test to launch a memcached, and also to deal with the
69
70
  memcached_stat_st stats;
71
  memcached_return ret= memcached_stat_servername(&stats, NULL,
72
                                                  server_name, server_port);
1300.2.2 by Joe Daly
port memcached stats plugin to use data_dictionary
73
1259.4.4 by Monty Taylor
Fixed the memcached stats test to launch a memcached, and also to deal with the
74
  if (ret != MEMCACHED_SUCCESS)
75
  {
76
    my_printf_error(ER_UNKNOWN_ERROR, _("Unable get stats from memcached server %s.  Got error from memcached_stat_servername()."), MYF(0), server_name);
77
    return ret;
78
  }
79
1300.2.2 by Joe Daly
port memcached stats plugin to use data_dictionary
80
  char **list= memcached_stat_get_keys((memcached_st *)memc, &stats, &ret);
1259.4.4 by Monty Taylor
Fixed the memcached stats test to launch a memcached, and also to deal with the
81
  char **ptr= NULL;
1300.2.2 by Joe Daly
port memcached stats plugin to use data_dictionary
82
 
83
  ctx->generator->push(server_name);
1368 by Brian Aker
Fix build issues, disabled tests (someone needs to go through them... who is
84
  ctx->generator->push(static_cast<uint64_t>(server_port));
1300.2.2 by Joe Daly
port memcached stats plugin to use data_dictionary
85
1259.4.4 by Monty Taylor
Fixed the memcached stats test to launch a memcached, and also to deal with the
86
  for (ptr= list; *ptr; ptr++)
87
  {
1300.2.2 by Joe Daly
port memcached stats plugin to use data_dictionary
88
    char *value= memcached_stat_get_value((memcached_st *)memc, &stats, *ptr, &ret);
89
    ctx->generator->push(value);
1259.4.4 by Monty Taylor
Fixed the memcached stats test to launch a memcached, and also to deal with the
90
    free(value);
91
  }
92
  free(list);
1273.13.57 by Brian Aker
Fix for recursive stat use.
93
1259.4.4 by Monty Taylor
Fixed the memcached stats test to launch a memcached, and also to deal with the
94
  return MEMCACHED_SUCCESS;
95
}
96
1300.2.2 by Joe Daly
port memcached stats plugin to use data_dictionary
97
98
StatsTableTool::StatsTableTool() :
99
  plugin::TableFunction("DATA_DICTIONARY", "MEMCACHED_STATS")
100
{
101
  add_field("NAME");
102
  add_field("PORT_NUMBER", plugin::TableFunction::NUMBER);
103
  add_field("PROCESS_ID", plugin::TableFunction::NUMBER);
104
  add_field("UPTIME", plugin::TableFunction::NUMBER);
105
  add_field("TIME", plugin::TableFunction::NUMBER);
106
  add_field("VERSION");
107
  add_field("POINTER_SIZE", plugin::TableFunction::NUMBER);
108
  add_field("RUSAGE_USER", plugin::TableFunction::NUMBER);
109
  add_field("RUSAGE_SYSTEM", plugin::TableFunction::NUMBER);
110
  add_field("CURRENT_ITEMS", plugin::TableFunction::NUMBER);
111
  add_field("TOTAL_ITEMS", plugin::TableFunction::NUMBER);
112
  add_field("BYTES",  plugin::TableFunction::NUMBER);
113
  add_field("CURRENT_CONNECTIONS", plugin::TableFunction::NUMBER);
114
  add_field("TOTAL_CONNECTIONS", plugin::TableFunction::NUMBER);
115
  add_field("CONNECTION_STRUCTURES", plugin::TableFunction::NUMBER);
116
  add_field("GETS", plugin::TableFunction::NUMBER);
117
  add_field("SETS", plugin::TableFunction::NUMBER);
118
  add_field("HITS", plugin::TableFunction::NUMBER);
119
  add_field("MISSES", plugin::TableFunction::NUMBER); 
120
  add_field("EVICTIONS", plugin::TableFunction::NUMBER);
121
  add_field("BYTES_READ", plugin::TableFunction::NUMBER);
122
  add_field("BYTES_WRITTEN", plugin::TableFunction::NUMBER);
123
  add_field("LIMIT_MAXBYTES", plugin::TableFunction::NUMBER);
124
  add_field("THREADS", plugin::TableFunction::NUMBER);
125
}
126
127
128
StatsTableTool::Generator::Generator(Field **arg) :
129
  plugin::TableFunction::Generator(arg)
130
{
131
  /* This will be set to the real number if we initialize properly below */
132
  number_of_hosts= 0;
133
  
134
  host_number= 0;
135
136
  /* set to NULL if we are not able to init we dont want to call delete on this */
137
  memc= NULL;
138
1144.5.3 by Padraig O'Sullivan
Added support for system variables to set the memcached servers whose
139
  SysvarHolder &sysvar_holder= SysvarHolder::singleton();
140
  const string servers_string= sysvar_holder.getServersString();
1144.5.1 by Padraig O'Sullivan
Created plugin which simply adds I_S tables for querying memcached stats.
141
1259.4.4 by Monty Taylor
Fixed the memcached stats test to launch a memcached, and also to deal with the
142
  if (servers_string.empty())
143
  {
144
    my_printf_error(ER_UNKNOWN_ERROR, _("No value in MEMCACHED_STATS_SERVERS variable."), MYF(0));
1300.2.2 by Joe Daly
port memcached stats plugin to use data_dictionary
145
    return; 
146
  }
1259.4.4 by Monty Taylor
Fixed the memcached stats test to launch a memcached, and also to deal with the
147
1300.2.2 by Joe Daly
port memcached stats plugin to use data_dictionary
148
  memc= memcached_create(NULL);
1259.4.4 by Monty Taylor
Fixed the memcached stats test to launch a memcached, and also to deal with the
149
  if (memc == NULL)
150
  {
151
    my_printf_error(ER_UNKNOWN_ERROR, _("Unable to create memcached struct.  Got error from memcached_create()."), MYF(0));
1300.2.2 by Joe Daly
port memcached stats plugin to use data_dictionary
152
    return;
1259.4.4 by Monty Taylor
Fixed the memcached stats test to launch a memcached, and also to deal with the
153
  }
154
1144.5.1 by Padraig O'Sullivan
Created plugin which simply adds I_S tables for querying memcached stats.
155
  memcached_server_st *tmp_serv=
156
    memcached_servers_parse(servers_string.c_str());
1259.4.4 by Monty Taylor
Fixed the memcached stats test to launch a memcached, and also to deal with the
157
  if (tmp_serv == NULL)
158
  {
159
    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());
1300.2.2 by Joe Daly
port memcached stats plugin to use data_dictionary
160
    return;
1259.4.4 by Monty Taylor
Fixed the memcached stats test to launch a memcached, and also to deal with the
161
  }
162
163
  memcached_server_push(memc, tmp_serv);
1144.5.1 by Padraig O'Sullivan
Created plugin which simply adds I_S tables for querying memcached stats.
164
  memcached_server_list_free(tmp_serv);
1259.4.4 by Monty Taylor
Fixed the memcached stats test to launch a memcached, and also to deal with the
165
1300.2.2 by Joe Daly
port memcached stats plugin to use data_dictionary
166
  number_of_hosts= memc->number_of_hosts;  
167
}
168
169
170
StatsTableTool::Generator::~Generator()
171
{
172
  if (memc != NULL)
173
  {
174
    memcached_free(memc);
175
  }
176
}
177
178
179
bool StatsTableTool::Generator::populate()
180
{
181
  if (host_number == number_of_hosts)
182
  {
183
    return false;
184
  }
185
186
  server_function_context context(this);
187
1452.2.5 by Monty Taylor
Fixed the memcached_stats plugin to work with the latest libmemcached, now
188
  memcached_server_function callbacks[1];
1259.4.4 by Monty Taylor
Fixed the memcached stats test to launch a memcached, and also to deal with the
189
  callbacks[0]= server_function;
1300.2.2 by Joe Daly
port memcached stats plugin to use data_dictionary
190
191
  unsigned int iferror; 
192
  iferror= (*callbacks[0])(memc, &memc->servers[host_number], (void *)&context); 
193
194
  if (iferror)
195
  {
196
    return false;
197
  }
198
199
  host_number++;
200
 
201
  return true;
1144.5.1 by Padraig O'Sullivan
Created plugin which simply adds I_S tables for querying memcached stats.
202
}