2007-10-17 23:34:54 +00:00
|
|
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
|
|
|
|
// vim: ts=8 sw=2 smarttab
|
|
|
|
/*
|
|
|
|
* Ceph - scalable distributed file system
|
|
|
|
*
|
|
|
|
* Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
|
|
|
|
*
|
|
|
|
* This is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License version 2.1, as published by the Free Software
|
|
|
|
* Foundation. See file COPYING.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "mon/MonMap.h"
|
2008-03-10 15:54:57 +00:00
|
|
|
#include "mon/MonClient.h"
|
2007-10-17 23:34:54 +00:00
|
|
|
#include "msg/SimpleMessenger.h"
|
|
|
|
#include "messages/MMonCommand.h"
|
|
|
|
#include "messages/MMonCommandAck.h"
|
|
|
|
|
|
|
|
#include "common/Timer.h"
|
|
|
|
|
|
|
|
#ifndef DARWIN
|
|
|
|
#include <envz.h>
|
|
|
|
#endif // DARWIN
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
|
|
|
|
Messenger *messenger = 0;
|
|
|
|
|
2008-02-28 00:43:22 +00:00
|
|
|
const char *outfile = 0;
|
|
|
|
|
|
|
|
void handle_ack(MMonCommandAck *ack)
|
|
|
|
{
|
|
|
|
generic_dout(0) << ack->get_source() << " -> '"
|
|
|
|
<< ack->rs << "' (" << ack->r << ")"
|
|
|
|
<< dendl;
|
|
|
|
int len = ack->get_data().length();
|
|
|
|
if (len) {
|
|
|
|
if (outfile) {
|
|
|
|
int fd = ::open(outfile, O_WRONLY|O_TRUNC|O_CREAT);
|
|
|
|
::write(fd, ack->get_data().c_str(), len);
|
|
|
|
::fchmod(fd, 0777);
|
|
|
|
::close(fd);
|
|
|
|
generic_dout(0) << "wrote " << len << " byte payload to " << outfile << dendl;
|
|
|
|
} else {
|
|
|
|
generic_dout(0) << "got " << len << " byte payload, discarding (specify -o <outfile)" << dendl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
messenger->shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-17 23:34:54 +00:00
|
|
|
class Admin : public Dispatcher {
|
|
|
|
void dispatch(Message *m) {
|
|
|
|
switch (m->get_type()) {
|
|
|
|
case MSG_MON_COMMAND_ACK:
|
2008-02-28 00:43:22 +00:00
|
|
|
handle_ack((MMonCommandAck*)m);
|
2007-10-17 23:34:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} dispatcher;
|
|
|
|
|
2008-02-28 00:43:22 +00:00
|
|
|
|
2008-01-01 22:04:31 +00:00
|
|
|
int main(int argc, const char **argv, const char *envp[]) {
|
2007-10-17 23:34:54 +00:00
|
|
|
|
2008-01-01 22:04:31 +00:00
|
|
|
vector<const char*> args;
|
2007-10-17 23:34:54 +00:00
|
|
|
argv_to_vec(argc, argv, args);
|
|
|
|
parse_config_options(args);
|
|
|
|
|
|
|
|
vec_to_argv(args, argc, argv);
|
|
|
|
|
2008-02-28 00:52:13 +00:00
|
|
|
bufferlist indata;
|
2008-02-28 00:43:22 +00:00
|
|
|
vector<const char*> nargs;
|
|
|
|
for (unsigned i=0; i<args.size(); i++) {
|
|
|
|
if (strcmp(args[i],"-o") == 0)
|
|
|
|
outfile = args[++i];
|
2008-02-28 00:52:13 +00:00
|
|
|
else if (strcmp(args[i], "-i") == 0) {
|
|
|
|
int fd = ::open(args[++i], O_RDONLY);
|
|
|
|
struct stat st;
|
|
|
|
if (::fstat(fd, &st) == 0) {
|
|
|
|
indata.push_back(buffer::create(st.st_size));
|
|
|
|
indata.zero();
|
|
|
|
::read(fd, indata.c_str(), st.st_size);
|
|
|
|
::close(fd);
|
|
|
|
cout << "read " << st.st_size << " bytes from " << args[i] << std::endl;
|
|
|
|
}
|
|
|
|
} else
|
2008-02-28 00:43:22 +00:00
|
|
|
nargs.push_back(args[i]);
|
|
|
|
}
|
|
|
|
|
2008-03-10 15:54:57 +00:00
|
|
|
// get monmap
|
2007-10-17 23:34:54 +00:00
|
|
|
MonMap monmap;
|
2008-03-10 15:54:57 +00:00
|
|
|
MonClient mc;
|
|
|
|
if (mc.get_monmap(&monmap) < 0)
|
|
|
|
return -1;
|
2007-10-17 23:34:54 +00:00
|
|
|
|
|
|
|
// start up network
|
2008-01-26 17:33:13 +00:00
|
|
|
rank.bind();
|
|
|
|
g_conf.daemonize = false; // not us!
|
|
|
|
rank.start();
|
2007-10-17 23:34:54 +00:00
|
|
|
messenger = rank.register_entity(entity_name_t::ADMIN());
|
|
|
|
messenger->set_dispatcher(&dispatcher);
|
|
|
|
|
|
|
|
// build command
|
|
|
|
MMonCommand *m = new MMonCommand(messenger->get_myinst());
|
2008-02-28 00:52:13 +00:00
|
|
|
m->set_data(indata);
|
2007-10-17 23:34:54 +00:00
|
|
|
string cmd;
|
2008-02-28 00:43:22 +00:00
|
|
|
for (unsigned i=0; i<nargs.size(); i++) {
|
2007-10-17 23:34:54 +00:00
|
|
|
if (i) cmd += " ";
|
2008-02-28 00:43:22 +00:00
|
|
|
cmd += nargs[i];
|
|
|
|
m->cmd.push_back(string(nargs[i]));
|
2007-10-17 23:34:54 +00:00
|
|
|
}
|
|
|
|
int mon = monmap.pick_mon();
|
|
|
|
|
|
|
|
generic_dout(0) << "mon" << mon << " <- '" << cmd << "'" << dendl;
|
|
|
|
|
|
|
|
// send it
|
|
|
|
messenger->send_message(m, monmap.get_inst(mon));
|
|
|
|
|
|
|
|
// wait for messenger to finish
|
|
|
|
rank.wait();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|