1
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2
// vim: ts=8 sw=2 smarttab
4
* Ceph - scalable distributed file system
6
* Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
8
* This is free software; you can redistribute it and/or
9
* modify it under the terms of the GNU Lesser General Public
10
* License version 2.1, as published by the Free Software
11
* Foundation. See file COPYING.
21
#include "messages/MMonCommand.h"
22
#include "messages/MMonCommandAck.h"
23
#include "mon/MonClient.h"
24
#include "mon/MonMap.h"
25
#include "msg/SimpleMessenger.h"
26
#include "tools/ceph.h"
28
#include "common/Cond.h"
29
#include "common/Mutex.h"
30
#include "common/Timer.h"
31
#include "common/common_init.h"
38
#include <sys/types.h>
47
CEPH_TOOL_MODE_CLI_INPUT = 0,
48
CEPH_TOOL_MODE_OBSERVER = 1,
49
CEPH_TOOL_MODE_ONE_SHOT_OBSERVER = 2,
50
CEPH_TOOL_MODE_GUI = 3
53
static enum CephToolMode ceph_tool_mode(CEPH_TOOL_MODE_CLI_INPUT);
55
struct ceph_tool_data g;
58
static SimpleMessenger *messenger = 0;
59
static Tokenizer *tok;
61
static const char *outfile = 0;
66
vector<string> pending_cmd;
67
bufferlist pending_bl;
72
entity_inst_t reply_from;
73
Context *resend_event = 0;
78
#include "mon/PGMap.h"
79
#include "osd/OSDMap.h"
80
#include "mds/MDSMap.h"
81
#include "include/LogEntry.h"
82
#include "include/ClassLibrary.h"
84
#include "mon/mon_types.h"
86
#include "messages/MMonObserve.h"
87
#include "messages/MMonObserveNotify.h"
90
static set<int> registered, seen;
92
version_t map_ver[PAXOS_NUM];
94
static void handle_observe(MMonObserve *observe)
96
dout(1) << observe->get_source() << " -> " << get_paxos_name(observe->machine_id)
97
<< " registered" << dendl;
99
registered.insert(observe->machine_id);
104
static void handle_notify(MMonObserveNotify *notify)
106
utime_t now = g_clock.now();
108
dout(1) << notify->get_source() << " -> " << get_paxos_name(notify->machine_id)
109
<< " v" << notify->ver
110
<< (notify->is_latest ? " (latest)" : "")
113
if (ceph_fsid_compare(¬ify->fsid, &g.mc.monmap.fsid)) {
114
dout(0) << notify->get_source_inst() << " notify fsid " << notify->fsid << " != " << g.mc.monmap.fsid << dendl;
119
if (map_ver[notify->machine_id] >= notify->ver)
122
switch (notify->machine_id) {
125
bufferlist::iterator p = notify->bl.begin();
126
if (notify->is_latest) {
129
PGMap::Incremental inc;
131
g.pgmap.apply_incremental(inc);
133
*g.log << now << " pg " << g.pgmap << std::endl;
134
g.updates |= PG_MON_UPDATE;
139
g.mdsmap.decode(notify->bl);
140
*g.log << now << " mds " << g.mdsmap << std::endl;
141
g.updates |= MDS_MON_UPDATE;
146
if (notify->is_latest) {
147
g.osdmap.decode(notify->bl);
149
OSDMap::Incremental inc(notify->bl);
150
g.osdmap.apply_incremental(inc);
152
*g.log << now << " osd " << g.osdmap << std::endl;
154
g.updates |= OSD_MON_UPDATE;
159
bufferlist::iterator p = notify->bl.begin();
160
if (notify->is_latest) {
162
::decode(summary, p);
163
// show last log message
164
if (!summary.tail.empty())
165
*g.log << now << " log " << summary.tail.back() << std::endl;
172
*g.log << now << " log " << le << std::endl;
180
bufferlist::iterator p = notify->bl.begin();
181
if (notify->is_latest) {
184
// show the first class info
185
map<string, ClassVersionMap>::iterator mapiter = list.library_map.begin();
186
if (mapiter != list.library_map.end()) {
187
ClassVersionMap& map = mapiter->second;
188
tClassVersionMap::iterator iter = map.begin();
190
if (iter != map.end())
191
*g.log << now << " class " << iter->second << std::endl;
197
ClassLibraryIncremental inc;
200
inc.decode_info(info);
201
*g.log << now << " class " << info << std::endl;
210
bufferlist::iterator p = notify->bl.begin();
211
if (notify->is_latest) {
214
*g.log << now << " auth " << std::endl;
217
AuthMonitor::Incremental inc;
219
*g.log << now << " auth " << inc.name.to_str() << std::endl;
223
/* ignoring auth incremental.. don't want to decode it */
229
g.mc.monmap.decode(notify->bl);
230
*g.log << now << " mon " << g.mc.monmap << std::endl;
235
*g.log << now << " ignoring unknown machine id " << notify->machine_id << std::endl;
238
map_ver[notify->machine_id] = notify->ver;
240
// have we seen them all?
241
seen.insert(notify->machine_id);
242
switch (ceph_tool_mode) {
243
case CEPH_TOOL_MODE_ONE_SHOT_OBSERVER:
244
if (seen.size() == PAXOS_NUM) {
245
messenger->shutdown();
248
case CEPH_TOOL_MODE_GUI:
259
static void send_observe_requests();
261
class C_ObserverRefresh : public Context {
264
C_ObserverRefresh(bool n) : newmon(n) {}
266
send_observe_requests();
270
static void send_observe_requests()
272
dout(1) << "send_observe_requests " << dendl;
275
for (int i=0; i<PAXOS_NUM; i++) {
276
MMonObserve *m = new MMonObserve(g.mc.monmap.fsid, i, map_ver[i]);
277
dout(1) << "mon" << " <- observe " << get_paxos_name(i) << dendl;
278
g.mc.send_mon_message(m);
283
float seconds = g_conf.paxos_observer_timeout/2;
284
dout(1) << " refresh after " << seconds << " with same mon" << dendl;
285
g.timer.add_event_after(seconds, new C_ObserverRefresh(false));
288
static void handle_ack(MMonCommandAck *ack)
292
reply_from = ack->get_source_inst();
295
reply_bl = ack->get_data();
298
g.timer.cancel_event(resend_event);
305
static void send_command()
307
version_t last_seen_version = 0;
308
MMonCommand *m = new MMonCommand(g.mc.monmap.fsid, last_seen_version);
309
m->cmd = pending_cmd;
310
m->set_data(pending_bl);
312
*g.log << g_clock.now() << " mon" << " <- " << pending_cmd << std::endl;
313
g.mc.send_mon_message(m);
316
class Admin : public Dispatcher {
317
bool ms_dispatch(Message *m) {
318
switch (m->get_type()) {
319
case MSG_MON_COMMAND_ACK:
320
handle_ack((MMonCommandAck*)m);
322
case MSG_MON_OBSERVE_NOTIFY:
323
handle_notify((MMonObserveNotify *)m);
325
case MSG_MON_OBSERVE:
326
handle_observe((MMonObserve *)m);
328
case CEPH_MSG_MON_MAP:
337
void ms_handle_connect(Connection *con) {
338
if (con->get_peer_type() == CEPH_ENTITY_TYPE_MON) {
340
if (ceph_tool_mode != CEPH_TOOL_MODE_CLI_INPUT) {
341
send_observe_requests();
343
if (pending_cmd.size())
348
bool ms_handle_reset(Connection *con) { return false; }
349
void ms_handle_remote_reset(Connection *con) {}
353
int do_command(vector<string>& cmd, bufferlist& bl, string& rs, bufferlist& rbl)
355
Mutex::Locker l(g.lock);
364
cmd_cond.Wait(g.lock);
368
*g.log << g_clock.now() << " "
369
<< reply_from.name << " -> '"
370
<< reply_rs << "' (" << reply_rc << ")"
378
cerr << "usage: ceph [options] [commands]" << std::endl;
379
cerr << "If no commands are specified, enter interactive mode.\n";
380
cerr << "Commands:" << std::endl;
381
cerr << " stop -- cleanly shut down file system" << std::endl
382
<< " (osd|pg|mds) stat -- get monitor subsystem status" << std::endl
383
<< " ..." << std::endl;
384
cerr << "Options:" << std::endl;
385
cerr << " -i infile\n";
386
cerr << " -o outfile\n";
387
cerr << " specify input or output file (for certain commands)\n";
388
cerr << " -s or --status\n";
389
cerr << " print current system status\n";
390
cerr << " -w or --watch\n";
391
cerr << " watch system status changes in real time (push)\n";
392
cerr << " -g or --gui\n";
393
cerr << " watch system status changes graphically\n";
394
generic_client_usage();
397
static const char *cli_prompt(EditLine *e)
405
EditLine *el = el_init("ceph", stdin, stdout, stderr);
406
el_set(el, EL_PROMPT, &cli_prompt);
407
el_set(el, EL_EDITOR, "emacs");
409
History *myhistory = history_init();
410
if (myhistory == 0) {
411
fprintf(stderr, "history could not be initialized\n");
417
/* Set the size of the history */
418
history(myhistory, &ev, H_SETSIZE, 800);
420
/* This sets up the call back functions for history functionality */
421
el_set(el, EL_HIST, history, myhistory);
425
const char *line = el_gets(el, &chars_read);
427
//*g.log << "typed '" << line << "'" << std::endl;
429
if (chars_read == 0) {
430
*g.log << "quit" << std::endl;
434
history(myhistory, &ev, H_ENTER, line);
436
if (run_command(line))
440
history_end(myhistory);
446
int run_command(const char *line)
448
if (strcmp(line, "quit\n") == 0)
453
tok_str(tok, line, &argc, &argv);
457
const char *infile = 0;
458
const char *outfile = 0;
459
for (int i=0; i<argc; i++) {
460
if (strcmp(argv[i], ">") == 0 && i < argc-1) {
464
if (argv[i][0] == '>') {
465
outfile = argv[i] + 1;
466
while (*outfile == ' ') outfile++;
469
if (strcmp(argv[i], "<") == 0 && i < argc-1) {
473
if (argv[i][0] == '<') {
474
infile = argv[i] + 1;
475
while (*infile == ' ') infile++;
478
cmd.push_back(argv[i]);
484
if (cmd.size() == 1 && cmd[0] == "print") {
485
*g.log << "----" << std::endl;
486
write(1, in.c_str(), in.length());
487
*g.log << "---- (" << in.length() << " bytes)" << std::endl;
491
//out << "cmd is " << cmd << std::endl;
495
if (out.read_file(infile) == 0) {
496
*g.log << "read " << out.length() << " from " << infile << std::endl;
499
*g.log << "couldn't read from " << infile << ": " << strerror_r(errno, buf, sizeof(buf)) << std::endl;
506
do_command(cmd, out, rs, in);
508
if (in.length() == 0)
512
if (strcmp(outfile, "-") == 0) {
513
*g.log << "----" << std::endl;
514
write(1, in.c_str(), in.length());
515
*g.log << "---- (" << in.length() << " bytes)" << std::endl;
518
in.write_file(outfile);
519
*g.log << "wrote " << in.length() << " to "
520
<< outfile << std::endl;
524
*g.log << "got " << in.length() << " byte payload; 'print' "
525
<< "to dump to terminal, or add '>-' to command." << std::endl;
530
int main(int argc, const char **argv)
533
DEFINE_CONF_VARS(usage);
534
vector<const char*> args;
535
argv_to_vec(argc, argv, args);
538
ceph_set_default_id("admin");
540
common_set_defaults(false);
541
common_init(args, "ceph", true);
543
vec_to_argv(args, argc, argv);
547
// default to 'admin' user
548
if (!g_conf.id || !g_conf.id[0])
549
g_conf.id = strdup("admin");
553
vector<const char*> nargs;
556
if (CONF_ARG_EQ("out_file", 'o')) {
557
CONF_SAFE_SET_ARG_VAL(&outfile, OPT_STR);
558
} else if (CONF_ARG_EQ("in_file", 'i')) {
559
CONF_SAFE_SET_ARG_VAL(&fname, OPT_STR);
560
int fd = ::open(fname, O_RDONLY);
562
if (::fstat(fd, &st) == 0) {
563
indata.push_back(buffer::create(st.st_size));
565
::read(fd, indata.c_str(), st.st_size);
567
cout << "read " << st.st_size << " bytes from " << args[i] << std::endl;
569
} else if (CONF_ARG_EQ("status", 's')) {
570
ceph_tool_mode = CEPH_TOOL_MODE_ONE_SHOT_OBSERVER;
571
} else if (CONF_ARG_EQ("watch", 'w')) {
572
ceph_tool_mode = CEPH_TOOL_MODE_OBSERVER;
573
} else if (CONF_ARG_EQ("help", 'h')) {
575
} else if (CONF_ARG_EQ("gui", 'g')) {
576
ceph_tool_mode = CEPH_TOOL_MODE_GUI;
577
} else if (args[i][0] == '-' && nargs.empty()) {
578
cerr << "unrecognized option " << args[i] << std::endl;
581
nargs.push_back(args[i]);
587
for (unsigned i=0; i<nargs.size(); i++) {
590
vcmd.push_back(string(nargs[i]));
594
if (g.mc.build_initial_monmap() < 0)
597
// initialize tokenizer
598
tok = tok_init(NULL);
601
messenger = new SimpleMessenger();
602
messenger->register_entity(entity_name_t::CLIENT());
603
messenger->add_dispatcher_head(&dispatcher);
611
g.mc.set_messenger(messenger);
616
if (g.mc.authenticate() < 0) {
617
cerr << "unable to authenticate as " << *g_conf.entity_name << std::endl;
620
if (g.mc.get_monmap() < 0) {
621
cerr << "unable to get monmap" << std::endl;
625
switch (ceph_tool_mode)
627
case CEPH_TOOL_MODE_OBSERVER:
628
case CEPH_TOOL_MODE_ONE_SHOT_OBSERVER:
630
send_observe_requests();
634
case CEPH_TOOL_MODE_CLI_INPUT: {
638
messenger->shutdown();
643
ret = do_command(vcmd, indata, rs, odata);
644
int len = odata.length();
647
if (strcmp(outfile, "-") == 0) {
648
::write(1, odata.c_str(), len);
650
odata.write_file(outfile);
652
cout << g_clock.now() << " wrote " << len << " byte payload to " << outfile << std::endl;
654
cout << g_clock.now() << " got " << len << " byte payload, discarding (specify -o <outfile)" << std::endl;
657
messenger->shutdown();
661
case CEPH_TOOL_MODE_GUI: {
666
// TODO: make sure that we capture the log this generates in the GUI
668
send_observe_requests();
671
run_gui(argc, (char **)argv);
673
cerr << "I'm sorry. This tool was not compiled with support for "
674
<< "GTK2." << std::endl;
677
messenger->shutdown();
689
// wait for messenger to finish
691
messenger->destroy();