gearadmin.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011-2013 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. ("cancel-job", boost::program_options::value<std::string>(), "Remove a given job from the server's queue")
  128. ("drop-function", boost::program_options::value<std::string>(), "Drop the function from the server.")
  129. ("show-unique-jobs", "Show unique IDs of jobs on the server.")
  130. ("show-jobs", "Show all jobs on the server.")
  131. ("getpid", "Get Process ID for the server.")
  132. ("status", "Status for the server.")
  133. ("priority-status", "Queued jobs status by priority.")
  134. ("workers", "Workers for the server.")
  135. ("ssl,S", "Enable SSL connections.")
  136. ;
  137. boost::program_options::variables_map vm;
  138. try
  139. {
  140. boost::program_options::store(boost::program_options::parse_command_line(args, argv, desc), vm);
  141. boost::program_options::notify(vm);
  142. }
  143. catch(std::exception &e)
  144. {
  145. std::cout << argv[0] << " : " << e.what() << std::endl;
  146. std::cout << std::endl << desc << std::endl;
  147. return EXIT_FAILURE;
  148. }
  149. util::Instance instance(host, port);
  150. instance.set_finish(new Finish);
  151. if (vm.count("ssl"))
  152. {
  153. instance.use_ssl(true);
  154. }
  155. if (vm.count("help"))
  156. {
  157. std::cout << desc << std::endl;
  158. return EXIT_SUCCESS;
  159. }
  160. if (vm.count("server-version") == 0 and
  161. vm.count("server-verbose") == 0 and
  162. vm.count("create-function") == 0 and
  163. vm.count("drop-function") == 0 and
  164. vm.count("cancel-job") == 0 and
  165. vm.count("show-unique-jobs") == 0 and
  166. vm.count("show-jobs") == 0 and
  167. vm.count("getpid") == 0 and
  168. vm.count("status") == 0 and
  169. vm.count("priority-status") == 0 and
  170. vm.count("workers") == 0)
  171. {
  172. std::cout << "No option execution operation given." << std::endl << std::endl;
  173. std::cout << desc << std::endl;
  174. return EXIT_FAILURE;
  175. }
  176. if (vm.count("status"))
  177. {
  178. instance.push(new util::Operation(util_literal_param("status\r\n")));
  179. }
  180. if (vm.count("priority-status"))
  181. {
  182. instance.push(new util::Operation(util_literal_param("prioritystatus\r\n")));
  183. }
  184. if (vm.count("workers"))
  185. {
  186. instance.push(new util::Operation(util_literal_param("workers\r\n")));
  187. }
  188. if (vm.count("server-version"))
  189. {
  190. instance.push(new util::Operation(util_literal_param("version\r\n")));
  191. }
  192. if (vm.count("server-verbose"))
  193. {
  194. instance.push(new util::Operation(util_literal_param("verbose\r\n")));
  195. }
  196. if (vm.count("cancel-job"))
  197. {
  198. std::string execute(util_literal_param("cancel job "));
  199. execute.append(vm["cancel-job"].as<std::string>());
  200. execute.append("\r\n");
  201. instance.push(new util::Operation(execute.c_str(), execute.size()));
  202. }
  203. if (vm.count("show-unique-jobs"))
  204. {
  205. instance.push(new util::Operation(util_literal_param("show unique jobs\r\n")));
  206. }
  207. if (vm.count("show-jobs"))
  208. {
  209. instance.push(new util::Operation(util_literal_param("show jobs\r\n")));
  210. }
  211. if (vm.count("drop-function"))
  212. {
  213. std::string execute(util_literal_param("drop function "));
  214. execute.append(vm["drop-function"].as<std::string>());
  215. execute.append("\r\n");
  216. instance.push(new util::Operation(execute.c_str(), execute.size()));
  217. }
  218. if (vm.count("create-function"))
  219. {
  220. std::string execute(util_literal_param("create function "));
  221. execute.append(vm["create-function"].as<std::string>());
  222. execute.append("\r\n");
  223. instance.push(new util::Operation(execute.c_str(), execute.size()));
  224. }
  225. if (vm.count("getpid"))
  226. {
  227. instance.push(new util::Operation(util_literal_param("getpid\r\n")));
  228. }
  229. if (not instance.run())
  230. {
  231. /* will produce a read error since nothing is read */
  232. std::cerr << "Error: " << instance.last_error() << std::endl;
  233. return EXIT_FAILURE;
  234. }
  235. return EXIT_SUCCESS;
  236. }