gearadmin.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011 Data Differential, http://datadifferential.com/
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are
  10. * met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following disclaimer
  17. * in the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * The names of its contributors may not be used to endorse or
  21. * promote products derived from this software without specific prior
  22. * written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. */
  37. #include "gear_config.h"
  38. #include <cstdio>
  39. #include <cstdlib>
  40. #include <cstring>
  41. #include <iostream>
  42. #include <vector>
  43. #include <netdb.h>
  44. #include <sys/socket.h>
  45. #ifdef HAVE_ERRNO_H
  46. #include <errno.h>
  47. #endif
  48. #ifdef HAVE_FCNTL_H
  49. #include <fcntl.h>
  50. #endif
  51. #ifdef HAVE_PWD_H
  52. #include <pwd.h>
  53. #endif
  54. #ifdef HAVE_SIGNAL_H
  55. #include <signal.h>
  56. #endif
  57. #ifdef HAVE_STDIO_H
  58. #include <stdio.h>
  59. #endif
  60. #ifdef HAVE_STDLIB_H
  61. #include <stdlib.h>
  62. #endif
  63. #ifdef HAVE_STRING_H
  64. #include <string.h>
  65. #endif
  66. #ifdef HAVE_SYS_RESOURCE_H
  67. #include <sys/resource.h>
  68. #endif
  69. #ifdef HAVE_SYS_STAT_H
  70. #include <sys/stat.h>
  71. #endif
  72. #ifdef HAVE_SYS_TYPES_H
  73. #include <sys/types.h>
  74. #endif
  75. #ifdef HAVE_UNISTD_H
  76. #include <unistd.h>
  77. #endif
  78. #include <libgearman-1.0/protocol.h>
  79. #include <boost/program_options.hpp>
  80. #include <util/instance.hpp>
  81. #include <util/string.hpp>
  82. using namespace datadifferential;
  83. /*
  84. This is a very quickly build application, I am just tired of telneting to the port.
  85. */
  86. class Finish : public util::Instance::Finish
  87. {
  88. public:
  89. bool call(bool success, const std::string &response)
  90. {
  91. if (success)
  92. {
  93. if (response.empty())
  94. {
  95. std::cout << "OK" << std::endl;
  96. }
  97. else
  98. {
  99. std::cout << response;
  100. }
  101. }
  102. else if (not response.empty())
  103. {
  104. std::cerr << "Error: " << response;
  105. }
  106. else
  107. {
  108. #if 0
  109. std::cerr << "Error" << std::endl;
  110. #endif
  111. }
  112. return true;
  113. }
  114. };
  115. int main(int args, char *argv[])
  116. {
  117. boost::program_options::options_description desc("Options");
  118. std::string host;
  119. std::string port;
  120. desc.add_options()
  121. ("help", "Options related to the program.")
  122. ("host,h", boost::program_options::value<std::string>(&host)->default_value("localhost"),"Connect to the host")
  123. ("port,p", boost::program_options::value<std::string>(&port)->default_value(GEARMAN_DEFAULT_TCP_PORT_STRING), "Port number or service to use for connection")
  124. ("server-version", "Fetch the version number for the server.")
  125. ("server-verbose", "Fetch the verbose setting for the server.")
  126. ("create-function", boost::program_options::value<std::string>(), "Create the function from the server.")
  127. ("drop-function", boost::program_options::value<std::string>(), "Drop the function from the server.")
  128. ("getpid", "Get Process ID for the server.")
  129. ("status", "Status for the server.")
  130. ("workers", "Workers for the server.")
  131. ("shutdown", "Shutdown server.")
  132. ;
  133. boost::program_options::variables_map vm;
  134. try
  135. {
  136. boost::program_options::store(boost::program_options::parse_command_line(args, argv, desc), vm);
  137. boost::program_options::notify(vm);
  138. }
  139. catch(std::exception &e)
  140. {
  141. std::cout << argv[0] << " : " << e.what() << std::endl;
  142. std::cout << std::endl << desc << std::endl;
  143. return EXIT_FAILURE;
  144. }
  145. util::Instance instance(host, port);
  146. instance.set_finish(new Finish);
  147. if (vm.count("help"))
  148. {
  149. std::cout << desc << std::endl;
  150. return EXIT_SUCCESS;
  151. }
  152. if (vm.count("shutdown"))
  153. {
  154. instance.push(new util::Operation(util_literal_param("shutdown\r\n")));
  155. }
  156. if (vm.count("status"))
  157. {
  158. instance.push(new util::Operation(util_literal_param("status\r\n")));
  159. }
  160. if (vm.count("workers"))
  161. {
  162. instance.push(new util::Operation(util_literal_param("workers\r\n")));
  163. }
  164. if (vm.count("server-version"))
  165. {
  166. instance.push(new util::Operation(util_literal_param("version\r\n")));
  167. }
  168. if (vm.count("server-verbose"))
  169. {
  170. instance.push(new util::Operation(util_literal_param("verbose\r\n")));
  171. }
  172. if (vm.count("drop-function"))
  173. {
  174. std::string execute(util_literal_param("drop function "));
  175. execute.append(vm["drop-function"].as<std::string>());
  176. execute.append("\r\n");
  177. instance.push(new util::Operation(execute.c_str(), execute.size()));
  178. }
  179. if (vm.count("create-function"))
  180. {
  181. std::string execute(util_literal_param("create function "));
  182. execute.append(vm["create-function"].as<std::string>());
  183. execute.append("\r\n");
  184. instance.push(new util::Operation(execute.c_str(), execute.size()));
  185. }
  186. if (vm.count("getpid"))
  187. {
  188. instance.push(new util::Operation(util_literal_param("getpid\r\n")));
  189. }
  190. instance.run();
  191. return EXIT_SUCCESS;
  192. }