~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/memcached_stats/analysis_table.cc

Cleanup around SAFEMALLOC

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 "analysis_table.h"
33
 
#include "sysvar_holder.h"
34
 
 
35
 
#include "drizzled/error.h"
36
 
 
37
 
#include <libmemcached/memcached.h>
38
 
 
39
 
using namespace std;
40
 
using namespace drizzled;
41
 
 
42
 
AnalysisTableTool::AnalysisTableTool() :
43
 
  plugin::TableFunction("DATA_DICTIONARY", "MEMCACHED_ANALYSIS")
44
 
{
45
 
  add_field("SERVERS_ANALYZED", plugin::TableFunction::NUMBER);
46
 
  add_field("AVERAGE_ITEM_SIZE", plugin::TableFunction::NUMBER);
47
 
  add_field("NODE_WITH_MOST_MEM_CONSUMPTION");
48
 
  add_field("USED_BYTES", plugin::TableFunction::NUMBER);
49
 
  add_field("NODE_WITH_LEAST_FREE_SPACE");
50
 
  add_field("FREE_BYTES", plugin::TableFunction::NUMBER);
51
 
  add_field("NODE_WITH_LONGEST_UPTIME");
52
 
  add_field("LONGEST_UPTIME", plugin::TableFunction::NUMBER);
53
 
  add_field("POOL_WIDE_HIT_RATIO", plugin::TableFunction::NUMBER); 
54
 
}
55
 
 
56
 
AnalysisTableTool::Generator::Generator(Field **arg) :
57
 
  plugin::TableFunction::Generator(arg)
58
 
{
59
 
  is_done= false;
60
 
}
61
 
 
62
 
bool AnalysisTableTool::Generator::populate()
63
 
{
64
 
  if (is_done)
65
 
  {
66
 
    return false;
67
 
  }
68
 
  is_done= true;
69
 
 
70
 
  SysvarHolder &sysvar_holder= SysvarHolder::singleton();
71
 
  const string servers_string= sysvar_holder.getServersString();
72
 
 
73
 
  if (servers_string.empty()) 
74
 
  {       
75
 
    my_printf_error(ER_UNKNOWN_ERROR, _("No value in MEMCACHED_STATS_SERVERS variable."), MYF(0));
76
 
    return false;
77
 
  }    
78
 
 
79
 
  memcached_return rc;
80
 
  memcached_st *serv= memcached_create(NULL);
81
 
  memcached_server_st *tmp_serv=
82
 
    memcached_servers_parse(servers_string.c_str());
83
 
  memcached_server_push(serv, tmp_serv);
84
 
  memcached_server_list_free(tmp_serv);
85
 
  memcached_stat_st *stats= memcached_stat(serv, NULL, &rc);
86
 
  memcached_server_st *servers= memcached_server_list(serv);
87
 
 
88
 
  uint32_t server_count= memcached_server_count(serv);
89
 
 
90
 
  if (server_count > 1)
91
 
  {
92
 
    memcached_analysis_st *report= memcached_analyze(serv, stats, &rc);
93
 
 
94
 
    push(static_cast<uint64_t>(server_count));
95
 
    push(static_cast<uint64_t>(report->average_item_size));
96
 
    push(memcached_server_name(serv, servers[report->most_consumed_server]));
97
 
    push(report->most_used_bytes);
98
 
    push(memcached_server_name(serv, servers[report->least_free_server]));
99
 
    push(report->least_remaining_bytes);
100
 
    push(memcached_server_name(serv, servers[report->oldest_server]));
101
 
    push(static_cast<uint64_t>(report->longest_uptime));
102
 
    push(static_cast<int64_t>(report->pool_hit_ratio));
103
 
    free(report);
104
 
  } 
105
 
 
106
 
  memcached_stat_free(serv, stats);
107
 
  memcached_free(serv);
108
 
 
109
 
  return true;
110
 
}