Drizzled Public API Documentation

memc_stats.cc

00001 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  * Copyright (C) 2009, Patrick "CaptTofu" Galbraith, Padraig O'Sullivan
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions are met:
00009  *
00010  *   * Redistributions of source code must retain the above copyright notice,
00011  *     this list of conditions and the following disclaimer.
00012  *   * Redistributions in binary form must reproduce the above copyright notice,
00013  *     this list of conditions and the following disclaimer in the documentation
00014  *     and/or other materials provided with the distribution.
00015  *   * Neither the name of Patrick Galbraith nor the names of its contributors
00016  *     may be used to endorse or promote products derived from this software
00017  *     without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00020  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00023  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00024  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00025  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00026  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00027  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00028  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
00029  * THE POSSIBILITY OF SUCH DAMAGE.
00030  */
00031 
00032 #include <config.h>
00033 #include <drizzled/item/func.h>
00034 #include <drizzled/function/str/strfunc.h>
00035 
00036 #include "memcached_functions.h"
00037 #include "memc_stats.h"
00038 
00039 #include <libmemcached/memcached.h>
00040 
00041 #include <string>
00042 #include <algorithm>
00043 
00044 using namespace std;
00045 using namespace drizzled;
00046 
00047 void MemcachedStats::setFailureString(const char *error)
00048 {
00049   size_t size= strlen(error);
00050   failure_buff.realloc(size);
00051   failure_buff.length(size);
00052   memcpy(failure_buff.ptr(), error, size);
00053 }
00054 
00055 String *MemcachedStats::val_str(String *str)
00056 {
00057   memcached_return rc;
00058   unsigned int count;
00059   char buff[100];
00060   memcached_stat_st *stat;
00061   memcached_server_st *servers;
00062   memcached_server_st *server_list;
00063   String *server_names;
00064 
00065 
00066   if (arg_count != 1 ||
00067       ! (server_names= args[0]->val_str(str)) ||
00068       ! memc)
00069   {
00070     setFailureString("USAGE: memc_stats('<server list>')");
00071     return &failure_buff;
00072   }
00073 
00074   servers= memcached_servers_parse(server_names->c_ptr());
00075   if (servers == NULL)
00076   {
00077     setFailureString(" ERROR: unable to parse servers string!");
00078     return &failure_buff;
00079   }
00080   memcached_server_push(memc, servers);
00081   memcached_server_list_free(servers);
00082 
00083   stat= memcached_stat(memc, NULL, &rc);
00084 
00085   if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_SOME_ERRORS)
00086   {
00087     snprintf(buff, 100, "Failure to communicate with servers (%s)\n",
00088             memcached_strerror(memc, rc));
00089 
00090     setFailureString(buff);
00091     return &failure_buff;
00092   }
00093 
00094   server_list= memcached_server_list(memc);
00095 
00096   results_buff.length(0);
00097   snprintf(buff, 100, "Listing %u Server\n\n", memcached_server_count(memc));
00098   results_buff.append(buff);
00099 
00100   for (count= 0; count < memcached_server_count(memc); count++)
00101   {
00102     char **list;
00103     char **ptr;
00104 
00105     list= memcached_stat_get_keys(memc, &stat[count], &rc);
00106 
00107     snprintf(buff, 100, "Server: %s (%u)\n",
00108             memcached_server_name(memc, server_list[count]),
00109             memcached_server_port(memc, server_list[count]));
00110 
00111 
00112     results_buff.append(buff);
00113 
00114     for (ptr= list; *ptr; ptr++)
00115     {
00116       char *value= memcached_stat_get_value(memc, &stat[count], *ptr, &rc);
00117 
00118       snprintf(buff, 100, "\t %s: %s\n", *ptr, value);
00119       free(value);
00120       results_buff.append(buff);
00121     }
00122 
00123     free(list);
00124     results_buff.append("\n");
00125   }
00126 
00127   free(stat);
00128 
00129   return &results_buff;
00130 
00131 }