19 #include <ndb_global.h>
21 #include "CpcClient.hpp"
23 #include <portlib/NdbEnv.h>
24 #include <util/NdbOut.hpp>
26 #define DEFAULT_PORT 1234
27 #define ENV_HOSTS "NDB_CPCC_HOSTS"
32 } g_settings = { 0 , DEFAULT_PORT };
67 if(m_field ==
"name") v = p.m_name;
69 if(m_field ==
"type") v = p.m_type;
70 if(m_field ==
"status") v = p.m_status;
71 if(m_field ==
"owner") v = p.m_owner;
72 if(m_field ==
"group") v = p.m_group;
73 if(m_field ==
"path") v = p.m_path;
74 if(m_field ==
"args") v = p.m_args;
75 if(m_field ==
"env") v = p.m_env;
76 if(m_field ==
"cwd") v = p.m_cwd;
78 if(m_field ==
"stdin") v = p.m_stdin;
79 if(m_field ==
"stdout") v = p.m_stdout;
80 if(m_field ==
"stderr") v = p.m_stderr;
91 : m_cond(condition), m_apply(rule) {
96 if(m_cond.evaluate(c, p))
97 return m_apply.evaluate(c, p);
125 return p.m_id == (int)
id && c == host;
143 for(
size_t i = 0;
i<m_cond.size();
i++){
144 if(m_cond[
i]->evaluate(c, p)){
150 return m_rule->evaluate(c, p);
155 m_cond.push_back(expr);
162 tmp.
split(split,
":");
164 short port = g_settings.m_port;
166 port = atoi(split[1].c_str());
175 for(
size_t i = 0;
i<split.size();
i++){
176 add_host(hosts, split[
i]);
181 main(
int argc,
const char** argv){
184 const char *cmd=0, *
name=0, *
group=0, *owner=0;
185 int list = 0, start = 0, stop = 0, rm = 0;
187 {
"cmd",
'c', arg_string, &cmd,
"command",
"command to run (default ls)" }
188 ,{
"name",
'n', arg_string, &
name,
189 "apply command for all processes with name",
"" }
190 ,{
"group",
'g', arg_string, &
group,
191 "apply command for all processes in group",
"" }
192 ,{
"owner",
'g', arg_string, &owner,
193 "apply command for all processes with owner",
"" }
194 ,{
"long",
'l', arg_flag, &g_settings.m_longl,
"long",
"long listing"}
195 ,{
"usage",
'?', arg_flag, &help,
"Print help",
"" }
196 ,{
"ls", 0, arg_flag, &list,
"-c list",
"list process(es)" }
197 ,{
"start", 0, arg_flag, &start,
"-c start",
"start process(es)" }
198 ,{
"stop", 0, arg_flag, &stop,
"-c stop",
"stop process(es)" }
199 ,{
"rm", 0, arg_flag, &rm,
"-c rm",
"undefine process(es)" }
201 const int num_args = 10;
204 char desc[] =
"[host:[port]]\n";
206 if(getarg(args, num_args, argc, argv, &optind) || help) {
207 arg_printusage(args, num_args, argv[0], desc);
211 if(list + start + stop + rm > 1){
212 ndbout_c(
"Can only specify one command");
213 arg_printusage(args, num_args, argv[0], desc);
217 if(list) cmd =
"list";
218 if(start) cmd =
"start";
219 if(stop) cmd =
"stop";
221 if(!cmd) cmd =
"list";
225 for(i = optind; i<argc; i++){
226 add_host(g_hosts, argv[i]);
231 for(i = optind; i<argc; i++){
234 tmp.
split(split,
":");
236 if(split.size() > 2){
237 Uint32
id = atoi(split[2].c_str());
238 orE->push_back(
new ProcEQ(g_hosts[i-optind],
id));
242 if(g_hosts.size() == 0){
244 if(NdbEnv_GetEnv(ENV_HOSTS, buf,
sizeof(buf))){
249 if(g_hosts.size() == 0){
250 g_hosts.push_back(
new SimpleCpcClient(
"localhost", g_settings.m_port));
255 m_expr =
new Match(* tmp, * m_expr);
260 m_expr =
new Match(* tmp, * m_expr);
265 m_expr =
new Match(* tmp, * m_expr);
269 for_each(g_hosts, * m_expr);
276 for(
size_t i = 0; i<list.size(); i++){
277 if(list[i]->connect() != 0){
278 ndbout_c(
"Failed to connect to %s:%d",
279 list[i]->getHost(), list[i]->getPort());
280 delete list[
i]; list[
i] = 0;
288 for(
size_t i = 0; i<list.size(); i++){
293 if(list[i]->list_processes(procs, p) != 0){
294 ndbout <<
"Failed to list processes on "
295 << list[
i]->getHost() <<
":" << list[
i]->getPort() << endl;
297 for(
size_t j = 0; j<procs.size(); j++)
298 expr.evaluate(list[i], procs[j]);
309 if(strcasecmp(cmd,
"start") == 0)
310 res = c->start_process(
id, p);
311 else if(strcasecmp(cmd,
"stop") == 0)
312 res = c->stop_process(
id, p);
313 else if(strcasecmp(cmd,
"rm") == 0)
314 res = c->undefine_process(
id, p);
315 else if(strcasecmp(cmd,
"list") == 0){
318 ndbout_c(
"--- %s:%d", c->getHost(), c->getPort());
324 const char * status = pp.m_status.
c_str();
325 if(strcmp(status,
"stopped") == 0) s =
'-';
326 if(strcmp(status,
"starting") == 0) s =
's';
327 if(strcmp(status,
"running") == 0) s =
'r';
328 if(strcmp(status,
"stopping") == 0) s =
'k';
332 ndbout_c(
"%c%c\t%d\t%s\t%s\t%s(%s)",
337 ndbout_c(
"%c%c %s:%d:%d %s %s %s(%s)",
338 s, pp.m_type.
c_str()[0], c->getHost(), c->getPort(),
347 p.get(
"errormessage", msg);
348 ndbout_c(
"Failed to %s %d on %s:%d - %s",
350 c->getHost(), c->getPort(), msg.
c_str());