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>
|
|
|
|
|
2008-12-10 21:22:28 +00:00
|
|
|
extern "C" {
|
|
|
|
#include <histedit.h>
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-12-15 19:15:55 +00:00
|
|
|
Mutex lock("ceph.cc lock");
|
2008-12-05 23:35:22 +00:00
|
|
|
Cond cond;
|
2007-10-17 23:34:54 +00:00
|
|
|
Messenger *messenger = 0;
|
2008-12-15 19:45:10 +00:00
|
|
|
MonMap monmap;
|
|
|
|
SafeTimer timer(lock);
|
2007-10-17 23:34:54 +00:00
|
|
|
|
2008-02-28 00:43:22 +00:00
|
|
|
const char *outfile = 0;
|
2008-11-07 20:44:26 +00:00
|
|
|
|
2008-12-15 19:45:10 +00:00
|
|
|
|
2008-11-07 20:44:26 +00:00
|
|
|
|
2008-12-05 23:35:22 +00:00
|
|
|
// sync command
|
|
|
|
vector<string> pending_cmd;
|
|
|
|
bufferlist pending_bl;
|
|
|
|
bool reply;
|
|
|
|
string reply_rs;
|
|
|
|
int reply_rc;
|
|
|
|
bufferlist reply_bl;
|
|
|
|
entity_inst_t reply_from;
|
2008-12-05 23:45:08 +00:00
|
|
|
Context *resend_event = 0;
|
2008-12-05 23:35:22 +00:00
|
|
|
|
|
|
|
|
2008-12-15 19:45:10 +00:00
|
|
|
|
|
|
|
// observe (push)
|
|
|
|
#include "mon/ClientMap.h"
|
|
|
|
#include "mon/PGMap.h"
|
|
|
|
#include "mon/ClientMap.h"
|
|
|
|
#include "osd/OSDMap.h"
|
|
|
|
#include "mds/MDSMap.h"
|
|
|
|
#include "include/LogEntry.h"
|
|
|
|
|
|
|
|
#include "mon/mon_types.h"
|
|
|
|
|
|
|
|
#include "messages/MMonObserve.h"
|
|
|
|
#include "messages/MMonObserveNotify.h"
|
|
|
|
|
|
|
|
int observe = 0;
|
|
|
|
static PGMap pgmap;
|
|
|
|
static MDSMap mdsmap;
|
|
|
|
static OSDMap osdmap;
|
|
|
|
static ClientMap clientmap;
|
|
|
|
|
2008-12-17 23:32:14 +00:00
|
|
|
static set<int> registered;
|
2008-12-15 19:45:10 +00:00
|
|
|
|
|
|
|
version_t map_ver[PAXOS_NUM];
|
|
|
|
|
2008-12-17 23:32:14 +00:00
|
|
|
void handle_observe(MMonObserve *observe)
|
2008-12-15 19:45:10 +00:00
|
|
|
{
|
2008-12-17 23:32:14 +00:00
|
|
|
dout(1) << observe->get_source() << " -> " << get_paxos_name(observe->machine_id)
|
|
|
|
<< " registered" << dendl;
|
2008-12-15 19:45:10 +00:00
|
|
|
lock.Lock();
|
2008-12-17 23:32:14 +00:00
|
|
|
registered.insert(observe->machine_id);
|
2008-12-15 19:45:10 +00:00
|
|
|
lock.Unlock();
|
2008-12-17 23:32:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void handle_notify(MMonObserveNotify *notify)
|
|
|
|
{
|
|
|
|
dout(1) << notify->get_source() << " -> " << get_paxos_name(notify->machine_id)
|
|
|
|
<< " v" << notify->ver
|
|
|
|
<< (notify->is_latest ? " (latest)" : "")
|
|
|
|
<< dendl;
|
2008-12-15 19:45:10 +00:00
|
|
|
|
|
|
|
if (map_ver[notify->machine_id] >= notify->ver)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (notify->machine_id) {
|
|
|
|
case PAXOS_PGMAP:
|
|
|
|
{
|
|
|
|
bufferlist::iterator p = notify->bl.begin();
|
|
|
|
if (notify->is_latest) {
|
|
|
|
pgmap.decode(p);
|
|
|
|
} else {
|
|
|
|
PGMap::Incremental inc;
|
|
|
|
inc.decode(p);
|
|
|
|
pgmap.apply_incremental(inc);
|
|
|
|
}
|
|
|
|
dout(0) << " pg " << pgmap << dendl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case PAXOS_MDSMAP:
|
|
|
|
mdsmap.decode(notify->bl);
|
|
|
|
dout(0) << " mds " << mdsmap << dendl;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PAXOS_OSDMAP:
|
|
|
|
{
|
|
|
|
if (notify->is_latest) {
|
|
|
|
osdmap.decode(notify->bl);
|
|
|
|
} else {
|
|
|
|
OSDMap::Incremental inc(notify->bl);
|
|
|
|
osdmap.apply_incremental(inc);
|
|
|
|
}
|
|
|
|
dout(0) << " osd " << osdmap << dendl;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PAXOS_CLIENTMAP:
|
|
|
|
{
|
|
|
|
bufferlist::iterator p = notify->bl.begin();
|
|
|
|
if (notify->is_latest) {
|
|
|
|
clientmap.decode(p);
|
|
|
|
} else {
|
|
|
|
ClientMap::Incremental inc;
|
|
|
|
inc.decode(p);
|
|
|
|
clientmap.apply_incremental(inc);
|
|
|
|
}
|
|
|
|
dout(0) << "client " << clientmap << dendl;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PAXOS_LOG:
|
|
|
|
{
|
|
|
|
LogEntry le;
|
|
|
|
bufferlist::iterator p = notify->bl.begin();
|
|
|
|
while (!p.end()) {
|
|
|
|
le.decode(p);
|
|
|
|
dout(0) << " log " << le << dendl;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
map_ver[notify->machine_id] = notify->ver;
|
|
|
|
}
|
|
|
|
|
2008-12-17 23:32:14 +00:00
|
|
|
static void send_observe_requests(bool);
|
|
|
|
static bool is_timeout = false;
|
2008-12-15 19:45:10 +00:00
|
|
|
|
|
|
|
class C_ObserverRefresh : public Context {
|
|
|
|
public:
|
2008-12-17 23:32:14 +00:00
|
|
|
bool newmon;
|
|
|
|
C_ObserverRefresh(bool n) : newmon(n) {}
|
2008-12-15 19:45:10 +00:00
|
|
|
void finish(int r) {
|
2008-12-17 23:32:14 +00:00
|
|
|
is_timeout = false;
|
|
|
|
send_observe_requests(newmon);
|
2008-12-15 19:45:10 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-12-17 23:32:14 +00:00
|
|
|
static void send_observe_requests(bool newmon)
|
2008-12-15 19:45:10 +00:00
|
|
|
{
|
2008-12-17 23:32:14 +00:00
|
|
|
dout(1) << "send_observe_requests " << newmon << dendl;
|
2008-12-15 19:45:10 +00:00
|
|
|
|
2008-12-17 23:32:14 +00:00
|
|
|
if (is_timeout)
|
|
|
|
return;
|
2008-12-15 19:45:10 +00:00
|
|
|
|
2008-12-17 23:32:14 +00:00
|
|
|
int mon = monmap.pick_mon(newmon);
|
|
|
|
bool sent = false;
|
2008-12-15 19:45:10 +00:00
|
|
|
for (int i=0; i<PAXOS_NUM; i++) {
|
2008-12-17 23:32:14 +00:00
|
|
|
if (registered.count(i))
|
|
|
|
continue;
|
2008-12-15 19:45:10 +00:00
|
|
|
MMonObserve *m = new MMonObserve(monmap.fsid, i, map_ver[i]);
|
2008-12-17 23:32:14 +00:00
|
|
|
dout(1) << "mon" << mon << " <- observe " << get_paxos_name(i) << dendl;
|
2008-12-15 19:45:10 +00:00
|
|
|
messenger->send_message(m, monmap.get_inst(mon));
|
2008-12-17 23:32:14 +00:00
|
|
|
sent = true;
|
2008-12-15 19:45:10 +00:00
|
|
|
}
|
|
|
|
|
2008-12-17 23:32:14 +00:00
|
|
|
float seconds = g_conf.paxos_observer_timeout/2;
|
|
|
|
float retry_seconds = 5.0;
|
|
|
|
if (!sent) {
|
|
|
|
// success. clear for renewal.
|
|
|
|
registered.clear();
|
|
|
|
dout(1) << " refresh after " << seconds << " with same mon" << dendl;
|
|
|
|
timer.add_event_after(seconds, new C_ObserverRefresh(false));
|
|
|
|
} else {
|
|
|
|
is_timeout = true;
|
|
|
|
dout(1) << " refresh after " << retry_seconds << " with new mon" << dendl;
|
|
|
|
timer.add_event_after(retry_seconds, new C_ObserverRefresh(true));
|
|
|
|
}
|
2008-12-15 19:45:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// watch (poll)
|
|
|
|
int watch = 0;
|
2008-11-13 18:48:25 +00:00
|
|
|
enum { OSD, MON, MDS, CLIENT, LAST };
|
2008-11-07 20:44:26 +00:00
|
|
|
int which = 0;
|
|
|
|
int same = 0;
|
2008-11-13 18:48:25 +00:00
|
|
|
const char *prefix[4] = { "mds", "osd", "pg", "client" };
|
2008-11-17 22:21:52 +00:00
|
|
|
map<string,string> status;
|
2008-11-07 20:44:26 +00:00
|
|
|
|
2008-11-25 20:14:26 +00:00
|
|
|
int lines = 0;
|
2008-11-17 17:01:03 +00:00
|
|
|
|
|
|
|
// refresh every second
|
2008-11-17 20:57:11 +00:00
|
|
|
void get_status(bool newmon=false);
|
2008-11-17 17:01:03 +00:00
|
|
|
|
|
|
|
struct C_Refresh : public Context {
|
|
|
|
void finish(int r) {
|
2008-11-17 20:57:11 +00:00
|
|
|
get_status(true);
|
2008-11-17 17:01:03 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Context *event = 0;
|
|
|
|
|
2008-11-17 20:57:11 +00:00
|
|
|
void get_status(bool newmon)
|
2008-11-07 20:44:26 +00:00
|
|
|
{
|
2008-11-17 20:57:11 +00:00
|
|
|
int mon = monmap.pick_mon(newmon);
|
2008-11-07 20:44:26 +00:00
|
|
|
|
|
|
|
vector<string> vcmd(2);
|
|
|
|
vcmd[0] = prefix[which];
|
|
|
|
vcmd[1] = "stat";
|
|
|
|
|
|
|
|
MMonCommand *m = new MMonCommand(monmap.fsid);
|
|
|
|
m->cmd.swap(vcmd);
|
|
|
|
messenger->send_message(m, monmap.get_inst(mon));
|
2008-11-17 17:01:03 +00:00
|
|
|
|
|
|
|
event = new C_Refresh;
|
2008-11-19 21:12:57 +00:00
|
|
|
timer.add_event_after(.2, event);
|
2008-11-07 20:44:26 +00:00
|
|
|
}
|
2008-02-28 00:43:22 +00:00
|
|
|
|
|
|
|
void handle_ack(MMonCommandAck *ack)
|
|
|
|
{
|
2008-11-07 20:44:26 +00:00
|
|
|
if (watch) {
|
2008-11-17 17:01:03 +00:00
|
|
|
lock.Lock();
|
2008-11-19 21:12:57 +00:00
|
|
|
|
|
|
|
which++;
|
|
|
|
which = which % LAST;
|
|
|
|
|
2008-11-17 22:21:52 +00:00
|
|
|
string w = ack->cmd[0];
|
|
|
|
if (ack->rs != status[w]) {
|
|
|
|
status[w] = ack->rs;
|
|
|
|
generic_dout(0) << w << " " << status[w] << dendl;
|
2008-11-25 20:14:26 +00:00
|
|
|
lines++;
|
|
|
|
|
|
|
|
if (lines > 20) {
|
|
|
|
generic_dout(0) << dendl;
|
|
|
|
for (map<string,string>::iterator p = status.begin(); p != status.end(); p++)
|
|
|
|
generic_dout(0) << p->first << " " << p->second << dendl;
|
|
|
|
generic_dout(0) << dendl;
|
|
|
|
lines = 0;
|
|
|
|
}
|
2008-11-07 20:44:26 +00:00
|
|
|
|
2008-11-19 21:12:57 +00:00
|
|
|
if (event)
|
|
|
|
timer.cancel_event(event);
|
|
|
|
get_status();
|
|
|
|
}
|
2008-11-17 17:01:03 +00:00
|
|
|
|
|
|
|
lock.Unlock();
|
2008-11-07 20:44:26 +00:00
|
|
|
} else {
|
2008-12-05 23:35:22 +00:00
|
|
|
lock.Lock();
|
|
|
|
reply = true;
|
|
|
|
reply_from = ack->get_source_inst();
|
|
|
|
reply_rs = ack->rs;
|
|
|
|
reply_rc = ack->r;
|
|
|
|
reply_bl = ack->get_data();
|
|
|
|
cond.Signal();
|
2008-12-05 23:45:08 +00:00
|
|
|
if (resend_event) {
|
|
|
|
timer.cancel_event(resend_event);
|
|
|
|
resend_event = 0;
|
|
|
|
}
|
2008-12-05 23:35:22 +00:00
|
|
|
lock.Unlock();
|
2008-02-28 00:43:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-17 23:34:54 +00:00
|
|
|
class Admin : public Dispatcher {
|
2008-12-04 21:31:00 +00:00
|
|
|
bool dispatch_impl(Message *m) {
|
2007-10-17 23:34:54 +00:00
|
|
|
switch (m->get_type()) {
|
|
|
|
case MSG_MON_COMMAND_ACK:
|
2008-02-28 00:43:22 +00:00
|
|
|
handle_ack((MMonCommandAck*)m);
|
2008-12-04 21:31:00 +00:00
|
|
|
break;
|
2008-12-15 19:45:10 +00:00
|
|
|
case MSG_MON_OBSERVE_NOTIFY:
|
|
|
|
handle_notify((MMonObserveNotify *)m);
|
|
|
|
break;
|
2008-12-17 23:32:14 +00:00
|
|
|
case MSG_MON_OBSERVE:
|
|
|
|
handle_observe((MMonObserve *)m);
|
|
|
|
break;
|
2008-12-04 21:31:00 +00:00
|
|
|
default:
|
|
|
|
return false;
|
2007-10-17 23:34:54 +00:00
|
|
|
}
|
2008-12-04 21:31:00 +00:00
|
|
|
return true;
|
2007-10-17 23:34:54 +00:00
|
|
|
}
|
2008-12-17 23:32:14 +00:00
|
|
|
|
|
|
|
void ms_handle_reset(const entity_addr_t& peer, entity_name_t last) {
|
|
|
|
if (observe) {
|
|
|
|
lock.Lock();
|
|
|
|
send_observe_requests(true);
|
|
|
|
lock.Unlock();
|
|
|
|
}
|
|
|
|
}
|
2007-10-17 23:34:54 +00:00
|
|
|
} dispatcher;
|
|
|
|
|
2008-12-05 23:45:08 +00:00
|
|
|
void send_command();
|
2008-02-28 00:43:22 +00:00
|
|
|
|
2008-12-05 23:45:08 +00:00
|
|
|
struct C_Resend : public Context {
|
|
|
|
void finish(int) {
|
|
|
|
monmap.pick_mon(true); // pick a new mon
|
|
|
|
if (!reply)
|
|
|
|
send_command();
|
|
|
|
}
|
|
|
|
};
|
2008-12-05 23:35:22 +00:00
|
|
|
void send_command()
|
|
|
|
{
|
|
|
|
MMonCommand *m = new MMonCommand(monmap.fsid);
|
|
|
|
m->cmd = pending_cmd;
|
|
|
|
m->get_data() = pending_bl;
|
|
|
|
|
|
|
|
int mon = monmap.pick_mon();
|
|
|
|
generic_dout(0) << "mon" << mon << " <- " << pending_cmd << dendl;
|
|
|
|
messenger->send_message(m, monmap.get_inst(mon));
|
2008-12-05 23:45:08 +00:00
|
|
|
|
|
|
|
resend_event = new C_Resend;
|
2008-12-24 20:45:07 +00:00
|
|
|
timer.add_event_after(15.0, resend_event);
|
2008-12-05 23:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int do_command(vector<string>& cmd, bufferlist& bl, string& rs, bufferlist& rbl)
|
|
|
|
{
|
|
|
|
Mutex::Locker l(lock);
|
|
|
|
|
|
|
|
pending_cmd = cmd;
|
|
|
|
pending_bl = bl;
|
|
|
|
reply = false;
|
|
|
|
|
|
|
|
send_command();
|
|
|
|
|
|
|
|
while (!reply)
|
|
|
|
cond.Wait(lock);
|
|
|
|
|
|
|
|
rs = rs;
|
|
|
|
rbl = reply_bl;
|
|
|
|
generic_dout(0) << reply_from.name << " -> '"
|
|
|
|
<< reply_rs << "' (" << reply_rc << ")"
|
|
|
|
<< dendl;
|
|
|
|
|
|
|
|
return reply_rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-05 23:45:08 +00:00
|
|
|
|
2008-03-19 14:09:07 +00:00
|
|
|
void usage()
|
|
|
|
{
|
2008-12-15 19:15:55 +00:00
|
|
|
cerr << "usage: ceph [options] monhost] command" << std::endl;
|
2008-03-19 14:09:07 +00:00
|
|
|
cerr << "Options:" << std::endl;
|
|
|
|
cerr << " -m monhost -- specify monitor hostname or ip" << std::endl;
|
|
|
|
cerr << " -i infile -- specify input file" << std::endl;
|
|
|
|
cerr << " -o outfile -- specify output file" << std::endl;
|
2008-12-15 19:45:10 +00:00
|
|
|
cerr << " -w or --watch -- watch mds, osd, pg status (push)" << std::endl;
|
|
|
|
cerr << " -p or --poll -- watch mds, osd, pg status (poll)" << std::endl;
|
2008-03-19 14:09:07 +00:00
|
|
|
cerr << "Commands:" << std::endl;
|
|
|
|
cerr << " stop -- cleanly shut down file system" << std::endl
|
|
|
|
<< " (osd|pg|mds) stat -- get monitor subsystem status" << std::endl
|
|
|
|
<< " ..." << std::endl;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2008-12-05 23:35:22 +00:00
|
|
|
|
|
|
|
const char *cli_prompt(EditLine *e) {
|
2008-12-15 19:45:10 +00:00
|
|
|
return "ceph> ";
|
2008-12-05 23:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int do_cli()
|
|
|
|
{
|
|
|
|
/* emacs style */
|
2008-12-15 19:15:55 +00:00
|
|
|
EditLine *el = el_init("ceph", stdin, stdout, stderr);
|
2008-12-05 23:35:22 +00:00
|
|
|
el_set(el, EL_PROMPT, &cli_prompt);
|
|
|
|
el_set(el, EL_EDITOR, "emacs");
|
|
|
|
|
|
|
|
History *myhistory = history_init();
|
|
|
|
if (myhistory == 0) {
|
|
|
|
fprintf(stderr, "history could not be initialized\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
HistEvent ev;
|
|
|
|
|
|
|
|
/* Set the size of the history */
|
|
|
|
history(myhistory, &ev, H_SETSIZE, 800);
|
|
|
|
|
|
|
|
/* This sets up the call back functions for history functionality */
|
|
|
|
el_set(el, EL_HIST, history, myhistory);
|
|
|
|
|
|
|
|
Tokenizer *tok = tok_init(NULL);
|
|
|
|
|
2008-12-15 20:00:17 +00:00
|
|
|
bufferlist in;
|
2008-12-05 23:35:22 +00:00
|
|
|
while (1) {
|
|
|
|
int count; // # chars read
|
|
|
|
const char *line = el_gets(el, &count);
|
|
|
|
|
|
|
|
if (!count) {
|
|
|
|
cout << "quit" << std::endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
//cout << "typed '" << line << "'" << std::endl;
|
|
|
|
|
|
|
|
if (strcmp(line, "quit\n") == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
history(myhistory, &ev, H_ENTER, line);
|
|
|
|
|
|
|
|
int argc;
|
|
|
|
const char **argv;
|
|
|
|
tok_str(tok, line, &argc, &argv);
|
|
|
|
tok_reset(tok);
|
|
|
|
|
|
|
|
vector<string> cmd;
|
2008-12-15 20:00:17 +00:00
|
|
|
const char *infile = 0;
|
|
|
|
const char *outfile = 0;
|
|
|
|
for (int i=0; i<argc; i++) {
|
|
|
|
if (strcmp(argv[i], ">") == 0 && i < argc-1) {
|
|
|
|
outfile = argv[++i];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (argv[i][0] == '>') {
|
|
|
|
outfile = argv[i] + 1;
|
|
|
|
while (*outfile == ' ') outfile++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strcmp(argv[i], "<") == 0 && i < argc-1) {
|
|
|
|
infile = argv[++i];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (argv[i][0] == '<') {
|
|
|
|
infile = argv[i] + 1;
|
|
|
|
while (*infile == ' ') infile++;
|
|
|
|
continue;
|
|
|
|
}
|
2008-12-05 23:35:22 +00:00
|
|
|
cmd.push_back(argv[i]);
|
2008-12-15 20:00:17 +00:00
|
|
|
}
|
2008-12-05 23:35:22 +00:00
|
|
|
if (cmd.empty())
|
|
|
|
continue;
|
|
|
|
|
2008-12-15 20:00:17 +00:00
|
|
|
if (cmd.size() == 1 && cmd[0] == "print") {
|
2008-12-18 00:48:38 +00:00
|
|
|
cout << "----" << std::endl;
|
|
|
|
write(1, in.c_str(), in.length());
|
|
|
|
cout << "---- (" << in.length() << " bytes)" << std::endl;
|
2008-12-15 20:00:17 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-12-05 23:35:22 +00:00
|
|
|
//cout << "cmd is " << cmd << std::endl;
|
|
|
|
|
2008-12-15 20:00:17 +00:00
|
|
|
bufferlist out;
|
|
|
|
if (infile) {
|
|
|
|
if (out.read_file(infile) == 0) {
|
|
|
|
cout << "read " << out.length() << " from " << infile << std::endl;
|
|
|
|
} else {
|
|
|
|
cerr << "couldn't read from " << infile << ": " << strerror(errno) << std::endl;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
in.clear();
|
2008-12-05 23:35:22 +00:00
|
|
|
string rs;
|
2008-12-15 20:00:17 +00:00
|
|
|
do_command(cmd, out, rs, in);
|
|
|
|
|
|
|
|
if (in.length()) {
|
|
|
|
if (outfile) {
|
|
|
|
if (strcmp(outfile, "-") == 0) {
|
2008-12-18 00:48:38 +00:00
|
|
|
cout << "----" << std::endl;
|
|
|
|
write(1, in.c_str(), in.length());
|
|
|
|
cout << "---- (" << in.length() << " bytes)" << std::endl;
|
2008-12-15 20:00:17 +00:00
|
|
|
} else {
|
|
|
|
in.write_file(outfile);
|
|
|
|
cout << "wrote " << in.length() << " to " << outfile << std::endl;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cout << "got " << in.length() << " byte payload; 'print' to dump to terminal, or add '>-' to command." << std::endl;
|
|
|
|
}
|
|
|
|
}
|
2008-12-05 23:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
history_end(myhistory);
|
|
|
|
el_end(el);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
2008-11-05 22:54:13 +00:00
|
|
|
env_to_vec(args);
|
2007-10-17 23:34:54 +00:00
|
|
|
parse_config_options(args);
|
|
|
|
|
|
|
|
vec_to_argv(args, argc, argv);
|
|
|
|
|
2008-11-13 21:33:05 +00:00
|
|
|
srand(getpid());
|
|
|
|
|
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;
|
|
|
|
}
|
2008-11-07 20:44:26 +00:00
|
|
|
} else if (strcmp(args[i], "-w") == 0 ||
|
|
|
|
strcmp(args[i], "--watch") == 0) {
|
2008-12-15 19:45:10 +00:00
|
|
|
observe = 1;
|
|
|
|
} else if (strcmp(args[i], "-p") == 0 ||
|
|
|
|
strcmp(args[i], "--poll") == 0) {
|
2008-11-07 20:44:26 +00:00
|
|
|
watch = 1;
|
2008-02-28 00:52:13 +00:00
|
|
|
} else
|
2008-02-28 00:43:22 +00:00
|
|
|
nargs.push_back(args[i]);
|
|
|
|
}
|
|
|
|
|
2008-03-19 14:09:07 +00:00
|
|
|
// build command
|
|
|
|
vector<string> vcmd;
|
|
|
|
string cmd;
|
2008-11-07 20:44:26 +00:00
|
|
|
if (!watch) {
|
|
|
|
for (unsigned i=0; i<nargs.size(); i++) {
|
|
|
|
if (i) cmd += " ";
|
|
|
|
cmd += nargs[i];
|
|
|
|
vcmd.push_back(string(nargs[i]));
|
|
|
|
}
|
2008-03-19 14:09:07 +00:00
|
|
|
}
|
|
|
|
|
2008-03-10 15:54:57 +00:00
|
|
|
// get monmap
|
|
|
|
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!
|
2007-10-17 23:34:54 +00:00
|
|
|
messenger = rank.register_entity(entity_name_t::ADMIN());
|
|
|
|
messenger->set_dispatcher(&dispatcher);
|
2008-11-13 18:40:11 +00:00
|
|
|
|
|
|
|
rank.start();
|
2008-11-17 17:01:03 +00:00
|
|
|
rank.set_policy(entity_name_t::TYPE_MON, Rank::Policy::lossy_fail_after(1.0));
|
|
|
|
|
2008-11-07 20:44:26 +00:00
|
|
|
if (watch) {
|
2008-11-17 17:01:03 +00:00
|
|
|
lock.Lock();
|
|
|
|
get_status();
|
|
|
|
lock.Unlock();
|
2008-12-15 19:45:10 +00:00
|
|
|
}
|
|
|
|
if (observe) {
|
|
|
|
lock.Lock();
|
2008-12-17 23:32:14 +00:00
|
|
|
send_observe_requests(true);
|
2008-12-15 19:45:10 +00:00
|
|
|
lock.Unlock();
|
|
|
|
}
|
|
|
|
if (!watch && !observe) {
|
2008-12-05 23:35:22 +00:00
|
|
|
if (vcmd.size()) {
|
|
|
|
|
|
|
|
string rs;
|
|
|
|
bufferlist odata;
|
|
|
|
do_command(vcmd, indata, rs, odata);
|
|
|
|
|
|
|
|
int len = odata.length();
|
|
|
|
if (len) {
|
|
|
|
if (outfile) {
|
|
|
|
if (strcmp(outfile, "-") == 0) {
|
|
|
|
::write(1, odata.c_str(), len);
|
|
|
|
} else {
|
|
|
|
odata.write_file(outfile);
|
|
|
|
}
|
|
|
|
generic_dout(0) << "wrote " << len << " byte payload to " << outfile << dendl;
|
|
|
|
} else {
|
|
|
|
generic_dout(0) << "got " << len << " byte payload, discarding (specify -o <outfile)" << dendl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// interactive mode
|
|
|
|
do_cli();
|
|
|
|
}
|
2008-11-07 20:44:26 +00:00
|
|
|
|
2008-12-05 23:35:22 +00:00
|
|
|
messenger->shutdown();
|
2008-11-07 20:44:26 +00:00
|
|
|
}
|
2007-10-17 23:34:54 +00:00
|
|
|
|
2008-12-05 23:35:22 +00:00
|
|
|
|
2007-10-17 23:34:54 +00:00
|
|
|
// wait for messenger to finish
|
|
|
|
rank.wait();
|
2008-11-20 18:36:19 +00:00
|
|
|
messenger->destroy();
|
2007-10-17 23:34:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|