Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / ceph.in
1 #!@PYTHON_EXECUTABLE@
2 # -*- mode:python -*-
3 # vim: ts=4 sw=4 smarttab expandtab
4 #
5 # Processed in Makefile to add python #! line and version variable
6 #
7 #
8
9
10 """
11 ceph.in becomes ceph, the command-line management tool for Ceph clusters.
12 This is a replacement for tools/ceph.cc and tools/common.cc.
13
14 Copyright (C) 2013 Inktank Storage, Inc.
15
16 This is free software; you can redistribute it and/or
17 modify it under the terms of the GNU General Public
18 License version 2, as published by the Free Software
19 Foundation.  See file COPYING.
20 """
21
22 from __future__ import print_function
23 import codecs
24 import os
25 import sys
26 import platform
27
28 try:
29     input = raw_input
30 except NameError:
31     pass
32
33 CEPH_GIT_VER = "@CEPH_GIT_VER@"
34 CEPH_GIT_NICE_VER = "@CEPH_GIT_NICE_VER@"
35 CEPH_RELEASE = "@CEPH_RELEASE@"
36 CEPH_RELEASE_NAME = "@CEPH_RELEASE_NAME@"
37 CEPH_RELEASE_TYPE = "@CEPH_RELEASE_TYPE@"
38
39 # Flags from src/mon/Monitor.h
40 FLAG_NOFORWARD = (1 << 0)
41 FLAG_OBSOLETE = (1 << 1)
42 FLAG_DEPRECATED = (1 << 2)
43
44 # priorities from src/common/perf_counters.h
45 PRIO_CRITICAL = 10
46 PRIO_INTERESTING = 8
47 PRIO_USEFUL = 5
48 PRIO_UNINTERESTING = 2
49 PRIO_DEBUGONLY = 0
50
51 PRIO_DEFAULT = PRIO_INTERESTING
52
53 # Make life easier on developers:
54 # If our parent dir contains CMakeCache.txt and bin/init-ceph,
55 # assume we're running from a build dir (i.e. src/build/bin/ceph)
56 # and tweak sys.path and LD_LIBRARY_PATH to use built files.
57 # Since this involves re-execing, if CEPH_DBG is set in the environment
58 # re-exec with -mpdb.  Also, if CEPH_DEV is in the env, suppress
59 # the warning message about the DEVELOPER MODE.
60
61 MYPATH = os.path.abspath(__file__)
62 MYDIR = os.path.dirname(MYPATH)
63 MYPDIR = os.path.dirname(MYDIR)
64 DEVMODEMSG = '*** DEVELOPER MODE: setting PATH, PYTHONPATH and LD_LIBRARY_PATH ***'
65
66
67 def respawn_in_path(lib_path, pybind_path, pythonlib_path):
68     execv_cmd = ['python']
69     if 'CEPH_DBG' in os.environ:
70         execv_cmd += ['-mpdb']
71
72     if platform.system() == "Darwin":
73         lib_path_var = "DYLD_LIBRARY_PATH"
74     else:
75         lib_path_var = "LD_LIBRARY_PATH"
76
77     py_binary = os.environ.get("PYTHON", "python")
78
79     if lib_path_var in os.environ:
80         if lib_path not in os.environ[lib_path_var]:
81             os.environ[lib_path_var] += ':' + lib_path
82             if "CEPH_DEV" not in os.environ:
83                 print(DEVMODEMSG, file=sys.stderr)
84             os.execvp(py_binary, execv_cmd + sys.argv)
85     else:
86         os.environ[lib_path_var] = lib_path
87         if "CEPH_DEV" not in os.environ:
88             print(DEVMODEMSG, file=sys.stderr)
89         os.execvp(py_binary, execv_cmd + sys.argv)
90     sys.path.insert(0, os.path.join(MYDIR, pybind_path))
91     sys.path.insert(0, os.path.join(MYDIR, pythonlib_path))
92
93
94 def get_pythonlib_dir():
95     """Returns the name of a distutils build directory"""
96     return "lib.{version[0]}".format(version=sys.version_info)
97
98 if os.path.exists(os.path.join(MYPDIR, "CMakeCache.txt")) \
99      and os.path.exists(os.path.join(MYPDIR, "bin/init-ceph")):
100     src_path = None
101     for l in open(os.path.join(MYPDIR, "CMakeCache.txt")):
102         if l.startswith("ceph_SOURCE_DIR:STATIC="):
103             src_path = l.split("=")[1].strip()
104
105     if src_path is None:
106         # Huh, maybe we're not really in a cmake environment?
107         pass
108     else:
109         # Developer mode, but in a cmake build dir instead of the src dir
110         lib_path = os.path.join(MYPDIR, "lib")
111         bin_path = os.path.join(MYPDIR, "bin")
112         pybind_path = os.path.join(src_path, "src", "pybind")
113         pythonlib_path = os.path.join(lib_path,
114                                       "cython_modules",
115                                       get_pythonlib_dir())
116
117         respawn_in_path(lib_path, pybind_path, pythonlib_path)
118
119         if 'PATH' in os.environ and bin_path not in os.environ['PATH']:
120             os.environ['PATH'] += ':' + bin_path
121
122 import argparse
123 import errno
124 import json
125 import rados
126 import shlex
127 import signal
128 import string
129 import subprocess
130
131 from ceph_argparse import \
132     concise_sig, descsort_key, parse_json_funcsigs, \
133     matchnum, validate_command, find_cmd_target, \
134     send_command, json_command, run_in_thread
135
136 from ceph_daemon import admin_socket, DaemonWatcher, Termsize
137
138 # just a couple of globals
139
140 verbose = False
141 cluster_handle = None
142
143 # Always use Unicode (UTF-8) for stdout
144 if sys.version_info[0] >= 3:
145     raw_stdout = sys.stdout.buffer
146     raw_stderr = sys.stderr.buffer
147 else:
148     raw_stdout = sys.__stdout__
149     raw_stderr = sys.__stderr__
150     sys.stdout = codecs.getwriter('utf-8')(raw_stdout)
151     sys.stderr = codecs.getwriter('utf-8')(raw_stderr)
152
153
154 def raw_write(buf):
155     sys.stdout.flush()
156     raw_stdout.write(buf)
157
158
159 def osdids():
160     ret, outbuf, outs = json_command(cluster_handle, prefix='osd ls')
161     if ret:
162         raise RuntimeError('Can\'t contact mon for osd list')
163     return [line.decode('utf-8') for line in outbuf.split(b'\n') if line]
164
165
166 def monids():
167     ret, outbuf, outs = json_command(cluster_handle, prefix='mon dump',
168                                      argdict={'format': 'json'})
169     if ret:
170         raise RuntimeError('Can\'t contact mon for mon list')
171     d = json.loads(outbuf.decode('utf-8'))
172     return [m['name'] for m in d['mons']]
173
174
175 def mdsids():
176     ret, outbuf, outs = json_command(cluster_handle, prefix='fs dump',
177                                      argdict={'format': 'json'})
178     if ret:
179         raise RuntimeError('Can\'t contact mon for mds list')
180     d = json.loads(outbuf.decode('utf-8'))
181     l = []
182     for info in d['standbys']:
183         l.append(info['name'])
184     for fs in d['filesystems']:
185         for info in fs['mdsmap']['info'].values():
186             l.append(info['name'])
187     return l
188
189
190 def mgrids():
191     ret, outbuf, outs = json_command(cluster_handle, prefix='mgr dump',
192                                      argdict={'format': 'json'})
193     if ret:
194         raise RuntimeError('Can\'t contact mon for mgr list')
195
196     d = json.loads(outbuf.decode('utf-8'))
197     l = []
198     l.append(d['active_name'])
199     for i in d['standbys']:
200         l.append(i['name'])
201     return l
202
203
204 def ids_by_service(service):
205     ids = {"mon": monids,
206            "osd": osdids,
207            "mds": mdsids,
208            "mgr": mgrids}
209     return ids[service]()
210
211
212 def validate_target(target):
213     """
214       this function will return true iff target is a correct
215       target, such as mon.a/osd.2/mds.a/mgr.
216
217       target: array, likes ['osd', '2']
218       return: bool, or raise RuntimeError
219     """
220
221     if len(target) == 2:
222         # for case "service.id"
223         service_name, service_id = target[0], target[1]
224         try:
225             exist_ids = ids_by_service(service_name)
226         except KeyError:
227             print('WARN: {0} is not a legal service name, should be one of mon/osd/mds/mgr'.format(service_name),
228                   file=sys.stderr)
229             return False
230
231         if service_id in exist_ids or len(exist_ids) > 0 and service_id == '*':
232             return True
233         else:
234             print('WARN: the service id you provided does not exist. service id should '
235                   'be one of {0}.'.format('/'.join(exist_ids)), file=sys.stderr)
236             return False
237
238     elif len(target) == 1 and target[0] in ['mgr', 'mon']:
239         return True
240     else:
241         print('WARN: \"{0}\" is not a legal target. it should be one of mon.<id>/osd.<int>/mds.<id>/mgr'.format('.'.join(target)), file=sys.stderr)
242         return False
243
244
245 # these args must be passed to all child programs
246 GLOBAL_ARGS = {
247     'client_id': '--id',
248     'client_name': '--name',
249     'cluster': '--cluster',
250     'cephconf': '--conf',
251 }
252
253
254 def parse_cmdargs(args=None, target=''):
255     # alias: let the line-wrapping be sane
256     AP = argparse.ArgumentParser
257
258     # format our own help
259     parser = AP(description='Ceph administration tool', add_help=False)
260
261     parser.add_argument('--completion', action='store_true',
262                         help=argparse.SUPPRESS)
263
264     parser.add_argument('-h', '--help', help='request mon help',
265                         action='store_true')
266
267     parser.add_argument('-c', '--conf', dest='cephconf',
268                         help='ceph configuration file')
269     parser.add_argument('-i', '--in-file', dest='input_file',
270                         help='input file, or "-" for stdin')
271     parser.add_argument('-o', '--out-file', dest='output_file',
272                         help='output file, or "-" for stdout')
273
274     parser.add_argument('--id', '--user', dest='client_id',
275                         help='client id for authentication')
276     parser.add_argument('--name', '-n', dest='client_name',
277                         help='client name for authentication')
278     parser.add_argument('--cluster', help='cluster name')
279
280     parser.add_argument('--admin-daemon', dest='admin_socket',
281                         help='submit admin-socket commands (\"help\" for help')
282
283     parser.add_argument('-s', '--status', action='store_true',
284                         help='show cluster status')
285
286     parser.add_argument('-w', '--watch', action='store_true',
287                         help='watch live cluster changes')
288     parser.add_argument('--watch-debug', action='store_true',
289                         help='watch debug events')
290     parser.add_argument('--watch-info', action='store_true',
291                         help='watch info events')
292     parser.add_argument('--watch-sec', action='store_true',
293                         help='watch security events')
294     parser.add_argument('--watch-warn', action='store_true',
295                         help='watch warn events')
296     parser.add_argument('--watch-error', action='store_true',
297                         help='watch error events')
298
299     parser.add_argument('--watch-channel', dest="watch_channel",
300                         help="which log channel to follow " \
301                         "when using -w/--watch.  One of ['cluster', 'audit', '*'",
302                         default='cluster')
303
304     parser.add_argument('--version', '-v', action="store_true", help="display version")
305     parser.add_argument('--verbose', action="store_true", help="make verbose")
306     parser.add_argument('--concise', dest='verbose', action="store_false",
307                         help="make less verbose")
308
309     parser.add_argument('-f', '--format', choices=['json', 'json-pretty',
310                         'xml', 'xml-pretty', 'plain'], dest='output_format')
311
312     parser.add_argument('--connect-timeout', dest='cluster_timeout',
313                         type=int,
314                         help='set a timeout for connecting to the cluster')
315
316     # returns a Namespace with the parsed args, and a list of all extras
317     parsed_args, extras = parser.parse_known_args(args)
318
319     return parser, parsed_args, extras
320
321
322 def hdr(s):
323     print('\n', s, '\n', '=' * len(s))
324
325
326 def do_basic_help(parser, args):
327     """
328     Print basic parser help
329     If the cluster is available, get and print monitor help
330     """
331     hdr('General usage:')
332     parser.print_help()
333     print_locally_handled_command_help()
334
335
336 def print_locally_handled_command_help():
337     hdr("Local commands:")
338     print("""
339 ping <mon.id>           Send simple presence/life test to a mon
340                         <mon.id> may be 'mon.*' for all mons
341 daemon {type.id|path} <cmd>
342                         Same as --admin-daemon, but auto-find admin socket
343 daemonperf {type.id | path} [stat-pats] [priority] [<interval>] [<count>]
344 daemonperf {type.id | path} list|ls [stat-pats] [priority]
345                         Get selected perf stats from daemon/admin socket
346                         Optional shell-glob comma-delim match string stat-pats
347                         Optional selection priority (can abbreviate name):
348                          critical, interesting, useful, noninteresting, debug
349                         List shows a table of all available stats
350                         Run <count> times (default forever),
351                          once per <interval> seconds (default 1)
352     """, file=sys.stdout)
353
354
355 def do_extended_help(parser, args, target, partial):
356     def help_for_sigs(sigs, partial=None):
357         sys.stdout.write(format_help(parse_json_funcsigs(sigs, 'cli'),
358                          partial=partial))
359
360     def help_for_target(target, partial=None):
361         # wait for osdmap because we know this is sent after the mgrmap
362         # and monmap (it's alphabetical).
363         cluster_handle.wait_for_latest_osdmap()
364         ret, outbuf, outs = json_command(cluster_handle, target=target,
365                                          prefix='get_command_descriptions',
366                                          timeout=10)
367         if ret:
368             print("couldn't get command descriptions for {0}: {1} ({2})".
369                   format(target, outs, ret), file=sys.stderr)
370             return ret
371         else:
372             return help_for_sigs(outbuf.decode('utf-8'), partial)
373
374     assert(cluster_handle.state == "connected")
375     return help_for_target(target, partial)
376
377 DONTSPLIT = string.ascii_letters + '{[<>]}'
378
379
380 def wrap(s, width, indent):
381     """
382     generator to transform s into a sequence of strings width or shorter,
383     for wrapping text to a specific column width.
384     Attempt to break on anything but DONTSPLIT characters.
385     indent is amount to indent 2nd-through-nth lines.
386
387     so "long string long string long string" width=11 indent=1 becomes
388     'long string', ' long string', ' long string' so that it can be printed
389     as
390     long string
391      long string
392      long string
393
394     Consumes s.
395     """
396     result = ''
397     leader = ''
398     while len(s):
399
400         if len(s) <= width:
401             # no splitting; just possibly indent
402             result = leader + s
403             s = ''
404             yield result
405
406         else:
407             splitpos = width
408             while (splitpos > 0) and (s[splitpos-1] in DONTSPLIT):
409                 splitpos -= 1
410
411             if splitpos == 0:
412                 splitpos = width
413
414             if result:
415                 # prior result means we're mid-iteration, indent
416                 result = leader
417             else:
418                 # first time, set leader and width for next
419                 leader = ' ' * indent
420                 width -= 1      # for subsequent space additions
421
422             # remove any leading spaces in this chunk of s
423             result += s[:splitpos].lstrip()
424             s = s[splitpos:]
425
426             yield result
427
428     raise StopIteration
429
430
431 def format_help(cmddict, partial=None):
432     """
433     Formats all the cmdsigs and helptexts from cmddict into a sorted-by-
434     cmdsig 2-column display, with each column wrapped and indented to
435     fit into (terminal_width / 2) characters.
436     """
437
438     fullusage = ''
439     for cmd in sorted(cmddict.values(), key=descsort_key):
440
441         if not cmd['help']:
442             continue
443         flags = cmd.get('flags', 0)
444         if flags & (FLAG_OBSOLETE | FLAG_DEPRECATED):
445             continue
446         concise = concise_sig(cmd['sig'])
447         if partial and not concise.startswith(partial):
448             continue
449         width = Termsize().cols - 1  # 1 for the line between sig and help
450         sig_width = int(width / 2)
451         # make sure width == sig_width + help_width, even (width % 2 > 0)
452         help_width = int(width / 2) + (width % 2)
453         siglines = [l for l in wrap(concise, sig_width, 1)]
454         helplines = [l for l in wrap(cmd['help'], help_width, 1)]
455
456         # make lists the same length
457         maxlen = max(len(siglines), len(helplines))
458         siglines.extend([''] * (maxlen - len(siglines)))
459         helplines.extend([''] * (maxlen - len(helplines)))
460
461         # so we can zip them for output
462         for s, h in zip(siglines, helplines):
463             fullusage += '{s:{w}s} {h}\n'.format(s=s, h=h, w=sig_width)
464
465     return fullusage
466
467
468 def ceph_conf(parsed_args, field, name):
469     args = ['ceph-conf']
470
471     if name:
472         args.extend(['--name', name])
473
474     # add any args in GLOBAL_ARGS
475     for key, val in GLOBAL_ARGS.items():
476         # ignore name in favor of argument name, if any
477         if name and key == 'client_name':
478             continue
479         if getattr(parsed_args, key):
480             args.extend([val, getattr(parsed_args, key)])
481
482     args.extend(['--show-config-value', field])
483     p = subprocess.Popen(
484         args,
485         stdout=subprocess.PIPE,
486         stderr=subprocess.PIPE)
487     outdata, errdata = p.communicate()
488     if len(errdata):
489         raise RuntimeError('unable to get conf option %s for %s: %s' % (field, name, errdata))
490     return outdata.rstrip()
491
492 PROMPT = 'ceph> '
493
494 if sys.stdin.isatty():
495     def read_input():
496         while True:
497             line = input(PROMPT).rstrip()
498             if line in ['q', 'quit', 'Q', 'exit']:
499                 return None
500             if line:
501                 return line
502 else:
503     def read_input():
504         while True:
505             line = sys.stdin.readline()
506             if not line:
507                 return None
508             line = line.rstrip()
509             if line:
510                 return line
511
512
513 def new_style_command(parsed_args, cmdargs, target, sigdict, inbuf, verbose):
514     """
515     Do new-style command dance.
516     target: daemon to receive command: mon (any) or osd.N
517     sigdict - the parsed output from the new monitor describing commands
518     inbuf - any -i input file data
519     verbose - bool
520     """
521     if verbose:
522         for cmdtag in sorted(sigdict.keys()):
523             cmd = sigdict[cmdtag]
524             sig = cmd['sig']
525             print('{0}: {1}'.format(cmdtag, concise_sig(sig)))
526
527     if True:
528         if cmdargs:
529             # Validate input args against list of sigs
530             valid_dict = validate_command(sigdict, cmdargs, verbose)
531             if valid_dict:
532                 if parsed_args.output_format:
533                     valid_dict['format'] = parsed_args.output_format
534             else:
535                 return -errno.EINVAL, '', 'invalid command'
536         else:
537             if sys.stdin.isatty():
538                 # do the command-interpreter looping
539                 # for input to do readline cmd editing
540                 import readline  # noqa
541
542             while True:
543                 interactive_input = read_input()
544                 if interactive_input is None:
545                     return 0, '', ''
546                 cmdargs = parse_cmdargs(shlex.split(interactive_input))[2]
547                 try:
548                     target = find_cmd_target(cmdargs)
549                 except Exception as e:
550                     print('error handling command target: {0}'.format(e),
551                           file=sys.stderr)
552                     continue
553                 if len(cmdargs) and cmdargs[0] == 'tell':
554                     print('Can not use \'tell\' in interactive mode.',
555                           file=sys.stderr)
556                     continue
557                 valid_dict = validate_command(sigdict, cmdargs, verbose)
558                 if valid_dict:
559                     if parsed_args.output_format:
560                         valid_dict['format'] = parsed_args.output_format
561                     if verbose:
562                         print("Submitting command: ", valid_dict, file=sys.stderr)
563                     ret, outbuf, outs = json_command(cluster_handle,
564                                                      target=target,
565                                                      argdict=valid_dict)
566                     if ret:
567                         ret = abs(ret)
568                         print('Error: {0} {1}'.format(ret, errno.errorcode.get(ret, 'Unknown')),
569                               file=sys.stderr)
570                     if outbuf:
571                         print(outbuf)
572                     if outs:
573                         print('Status:\n', outs, file=sys.stderr)
574                 else:
575                     print("Invalid command", file=sys.stderr)
576
577     if verbose:
578         print("Submitting command: ", valid_dict, file=sys.stderr)
579     return json_command(cluster_handle, target=target, argdict=valid_dict,
580                         inbuf=inbuf)
581
582
583 def complete(sigdict, args, target):
584     """
585     Command completion.  Match as much of [args] as possible,
586     and print every possible match separated by newlines.
587     Return exitcode.
588     """
589     # XXX this looks a lot like the front of validate_command().  Refactor?
590
591     complete_verbose = 'COMPVERBOSE' in os.environ
592
593     # Repulsive hack to handle tell: lop off 'tell' and target
594     # and validate the rest of the command.  'target' is already
595     # determined in our callers, so it's ok to remove it here.
596     if len(args) and args[0] == 'tell':
597         args = args[2:]
598     # look for best match, accumulate possibles in bestcmds
599     # (so we can maybe give a more-useful error message)
600
601     match_count = 0
602     comps = []
603     for cmdtag, cmd in sigdict.items():
604         sig = cmd['sig']
605         j = 0
606         # iterate over all arguments, except last one
607         for arg in args[0:-1]:
608             if j > len(sig)-1:
609                 # an out of argument definitions
610                 break
611             found_match = arg in sig[j].complete(arg)
612             if not found_match and sig[j].req:
613                 # no elements that match
614                 break
615             if not sig[j].N:
616                 j += 1
617         else:
618             # successfully matched all - except last one - arguments
619             if j < len(sig) and len(args) > 0:
620                 comps += sig[j].complete(args[-1])
621
622             match_count += 1
623             match_cmd = cmd
624
625     if match_count == 1 and len(comps) == 0:
626         # only one command matched and no hints yet => add help
627         comps = comps + [' ', '#'+match_cmd['help']]
628     print('\n'.join(sorted(set(comps))))
629     return 0
630
631
632 def ping_monitor(cluster_handle, name, timeout):
633     if 'mon.' not in name:
634         print('"ping" expects a monitor to ping; try "ping mon.<id>"', file=sys.stderr)
635         return 1
636
637     mon_id = name[len('mon.'):]
638     if mon_id == '*':
639         run_in_thread(cluster_handle.connect, timeout=timeout)
640         for m in monids():
641             s = run_in_thread(cluster_handle.ping_monitor, m)
642             if s is None:
643                 print("mon.{0}".format(m) + '\n' + "Error connecting to monitor.")
644             else:
645                 print("mon.{0}".format(m) + '\n' + s)
646     else:
647             s = run_in_thread(cluster_handle.ping_monitor, mon_id)
648             print(s)
649     return 0
650
651
652 def maybe_daemon_command(parsed_args, childargs):
653     """
654     Check if --admin-socket, daemon, or daemonperf command
655     if it is, returns (boolean handled, return code if handled == True)
656     """
657
658     daemon_perf = False
659     sockpath = None
660     if parsed_args.admin_socket:
661         sockpath = parsed_args.admin_socket
662     elif len(childargs) > 0 and childargs[0] in ["daemon", "daemonperf"]:
663         daemon_perf = (childargs[0] == "daemonperf")
664         # Treat "daemon <path>" or "daemon <name>" like --admin_daemon <path>
665         # Handle "daemonperf <path>" the same but requires no trailing args
666         require_args = 2 if daemon_perf else 3
667         if len(childargs) >= require_args:
668             if childargs[1].find('/') >= 0:
669                 sockpath = childargs[1]
670             else:
671                 # try resolve daemon name
672                 try:
673                     sockpath = ceph_conf(parsed_args, 'admin_socket',
674                                          childargs[1])
675                 except Exception as e:
676                     print('Can\'t get admin socket path: ' + str(e), file=sys.stderr)
677                     return True, errno.EINVAL
678             # for both:
679             childargs = childargs[2:]
680         else:
681             print('{0} requires at least {1} arguments'.format(childargs[0], require_args),
682                   file=sys.stderr)
683             return True, errno.EINVAL
684
685     if sockpath and daemon_perf:
686         return True, daemonperf(childargs, sockpath)
687     elif sockpath:
688         try:
689             raw_write(admin_socket(sockpath, childargs, parsed_args.output_format))
690         except Exception as e:
691             print('admin_socket: {0}'.format(e), file=sys.stderr)
692             return True, errno.EINVAL
693         return True, 0
694
695     return False, 0
696
697
698 def isnum(s):
699     try:
700         float(s)
701         return True
702     except ValueError:
703         return False
704
705
706 def daemonperf(childargs, sockpath):
707     """
708     Handle daemonperf command; returns errno or 0
709
710     daemonperf <daemon> [priority string] [statpats] [interval] [count]
711     daemonperf <daemon> list|ls [statpats]
712     """
713
714     interval = 1
715     count = None
716     statpats = None
717     priority = None
718     do_list = False
719
720     def prio_from_name(arg):
721
722         PRIOMAP = {
723             'critical': PRIO_CRITICAL,
724             'interesting': PRIO_INTERESTING,
725             'useful': PRIO_USEFUL,
726             'uninteresting': PRIO_UNINTERESTING,
727             'debugonly': PRIO_DEBUGONLY,
728         }
729
730         if arg in PRIOMAP:
731             return PRIOMAP[arg]
732         # allow abbreviation
733         for name, val in PRIOMAP.items():
734             if name.startswith(arg):
735                 return val
736         return None
737
738     # consume and analyze non-numeric args
739     while len(childargs) and not isnum(childargs[0]):
740         arg = childargs.pop(0)
741         # 'list'?
742         if arg in ['list', 'ls']:
743             do_list = True
744             continue
745         # prio?
746         prio = prio_from_name(arg)
747         if prio is not None:
748             priority = prio
749             continue
750         # statpats
751         statpats = arg.split(',')
752
753     if priority is None:
754         priority = PRIO_DEFAULT
755
756     if len(childargs) > 0:
757         try:
758             interval = float(childargs.pop(0))
759             if interval < 0:
760                 raise ValueError
761         except ValueError:
762             print('daemonperf: interval should be a positive number', file=sys.stderr)
763             return errno.EINVAL
764
765     if len(childargs) > 0:
766         arg = childargs.pop(0)
767         if (not isnum(arg)) or (int(arg) < 0):
768             print('daemonperf: count should be a positive integer', file=sys.stderr)
769             return errno.EINVAL
770         count = int(arg)
771
772     watcher = DaemonWatcher(sockpath, statpats, priority)
773     if do_list:
774         watcher.list()
775     else:
776         watcher.run(interval, count)
777
778     return 0
779
780
781 def main():
782     ceph_args = os.environ.get('CEPH_ARGS')
783     if ceph_args:
784         if "injectargs" in sys.argv:
785             i = sys.argv.index("injectargs")
786             sys.argv = sys.argv[:i] + ceph_args.split() + sys.argv[i:]
787         else:
788             sys.argv.extend([arg for arg in ceph_args.split()
789                              if '--admin-socket' not in arg])
790     parser, parsed_args, childargs = parse_cmdargs()
791
792     if parsed_args.version:
793         print('ceph version {0} ({1}) {2} ({3})'.format(
794             CEPH_GIT_NICE_VER,
795             CEPH_GIT_VER,
796             CEPH_RELEASE_NAME,
797             CEPH_RELEASE_TYPE))  # noqa
798         return 0
799
800     global verbose
801     verbose = parsed_args.verbose
802
803     if verbose:
804         print("parsed_args: {0}, childargs: {1}".format(parsed_args, childargs), file=sys.stderr)
805
806     # pass on --id, --name, --conf
807     name = 'client.admin'
808     if parsed_args.client_id:
809         name = 'client.' + parsed_args.client_id
810     if parsed_args.client_name:
811         name = parsed_args.client_name
812
813     # default '' means default conf search
814     conffile = ''
815     if parsed_args.cephconf:
816         conffile = parsed_args.cephconf
817     # For now, --admin-daemon is handled as usual.  Try it
818     # first in case we can't connect() to the cluster
819
820     format = parsed_args.output_format
821
822     done, ret = maybe_daemon_command(parsed_args, childargs)
823     if done:
824         return ret
825
826     timeout = None
827     if parsed_args.cluster_timeout:
828         timeout = parsed_args.cluster_timeout
829
830     # basic help
831     if parsed_args.help:
832         do_basic_help(parser, childargs)
833
834     # handle any 'generic' ceph arguments that we didn't parse here
835     global cluster_handle
836
837     # rados.Rados() will call rados_create2, and then read the conf file,
838     # and then set the keys from the dict.  So we must do these
839     # "pre-file defaults" first (see common_preinit in librados)
840     conf_defaults = {
841         'log_to_stderr': 'true',
842         'err_to_stderr': 'true',
843         'log_flush_on_exit': 'true',
844     }
845
846     if 'injectargs' in childargs:
847         position = childargs.index('injectargs')
848         injectargs = childargs[position:]
849         childargs = childargs[:position]
850         if verbose:
851             print('Separate childargs {0} from injectargs {1}'.format(childargs, injectargs),
852                   file=sys.stderr)
853     else:
854         injectargs = None
855
856     clustername = None
857     if parsed_args.cluster:
858         clustername = parsed_args.cluster
859
860     try:
861         cluster_handle = run_in_thread(rados.Rados,
862                                        name=name, clustername=clustername,
863                                        conf_defaults=conf_defaults,
864                                        conffile=conffile)
865         retargs = run_in_thread(cluster_handle.conf_parse_argv, childargs)
866     except rados.Error as e:
867         print('Error initializing cluster client: {0!r}'.format(e), file=sys.stderr)
868         return 1
869
870     childargs = retargs
871     if not childargs:
872         childargs = []
873
874     # -- means "stop parsing args", but we don't want to see it either
875     if '--' in childargs:
876         childargs.remove('--')
877     if injectargs and '--' in injectargs:
878         injectargs.remove('--')
879
880     # special deprecation warning for 'ceph <type> tell'
881     # someday 'mds' will be here too
882     if (len(childargs) >= 2 and
883             childargs[0] in ['mon', 'osd'] and
884             childargs[1] == 'tell'):
885         print('"{0} tell" is deprecated; try "tell {0}.<id> <command> [options...]" instead (id can be "*") '.format(childargs[0]),
886               file=sys.stderr)
887         return 1
888
889     if parsed_args.help:
890         # short default timeout for -h
891         if not timeout:
892             timeout = 5
893
894     if childargs and childargs[0] == 'ping' and not parsed_args.help:
895         if len(childargs) < 2:
896             print('"ping" requires a monitor name as argument: "ping mon.<id>"', file=sys.stderr)
897             return 1
898     if parsed_args.completion:
899         # for completion let timeout be really small
900         timeout = 3
901     try:
902         if childargs and childargs[0] == 'ping' and not parsed_args.help:
903             return ping_monitor(cluster_handle, childargs[1], timeout)
904         result = run_in_thread(cluster_handle.connect, timeout=timeout)
905         if type(result) is tuple and result[0] == -errno.EINTR:
906             print('Cluster connection interrupted or timed out', file=sys.stderr)
907             return 1
908     except KeyboardInterrupt:
909         print('Cluster connection aborted', file=sys.stderr)
910         return 1
911     except rados.PermissionDeniedError as e:
912         print(str(e), file=sys.stderr)
913         return errno.EACCES
914     except Exception as e:
915         print(str(e), file=sys.stderr)
916         return 1
917
918     if parsed_args.help:
919         hdr('Monitor commands:')
920         if verbose:
921             print('[Contacting monitor, timeout after %d seconds]' % timeout)
922
923         return do_extended_help(parser, childargs, ('mon', ''), ' '.join(childargs))
924
925     # implement "tell service.id help"
926     if len(childargs) >= 3 and childargs[0] == 'tell' and childargs[2] == 'help':
927         target = childargs[1].split('.')
928         if validate_target(target):
929             return do_extended_help(parser, childargs, target, None)
930         else:
931             print('target {0} doesn\'t exists, please pass correct target to tell command, such as mon.a/'
932                   'osd.1/mds.a/mgr'.format(childargs[1]), file=sys.stderr)
933             return 1
934     # implement -w/--watch_*
935     # This is ugly, but Namespace() isn't quite rich enough.
936     level = ''
937     for k, v in parsed_args._get_kwargs():
938         if k.startswith('watch') and v:
939             if k == 'watch':
940                 level = 'info'
941             elif k != "watch_channel":
942                 level = k.replace('watch_', '')
943     if level:
944         # an awfully simple callback
945         def watch_cb(arg, line, channel, name, who, stamp_sec, stamp_nsec, seq, level, msg):
946             # Filter on channel
947             if (channel == parsed_args.watch_channel or \
948                            parsed_args.watch_channel == "*"):
949                 print(line)
950                 sys.stdout.flush()
951
952         # first do a ceph status
953         ret, outbuf, outs = json_command(cluster_handle, prefix='status')
954         if ret:
955             print("status query failed: ", outs, file=sys.stderr)
956             return ret
957         print(outbuf)
958
959         # this instance keeps the watch connection alive, but is
960         # otherwise unused
961         run_in_thread(cluster_handle.monitor_log2, level, watch_cb, 0)
962
963         # loop forever letting watch_cb print lines
964         try:
965             signal.pause()
966         except KeyboardInterrupt:
967             # or until ^C, at least
968             return 0
969
970     # read input file, if any
971     inbuf = b''
972     if parsed_args.input_file:
973         try:
974             if parsed_args.input_file == '-':
975                 inbuf = sys.stdin.read()
976             else:
977                 with open(parsed_args.input_file, 'rb') as f:
978                     inbuf = f.read()
979         except Exception as e:
980             print('Can\'t open input file {0}: {1}'.format(parsed_args.input_file, e), file=sys.stderr)
981             return 1
982
983     # prepare output file, if any
984     if parsed_args.output_file:
985         try:
986             if parsed_args.output_file == '-':
987                 outf = sys.stdout
988             else:
989                 outf = open(parsed_args.output_file, 'wb')
990         except Exception as e:
991             print('Can\'t open output file {0}: {1}'.format(parsed_args.output_file, e), file=sys.stderr)
992             return 1
993
994     # -s behaves like a command (ceph status).
995     if parsed_args.status:
996         childargs.insert(0, 'status')
997
998     try:
999         target = find_cmd_target(childargs)
1000     except Exception as e:
1001         print('error handling command target: {0}'.format(e), file=sys.stderr)
1002         return 1
1003
1004     # Repulsive hack to handle tell: lop off 'tell' and target
1005     # and validate the rest of the command.  'target' is already
1006     # determined in our callers, so it's ok to remove it here.
1007     is_tell = False
1008     if len(childargs) and childargs[0] == 'tell':
1009         childargs = childargs[2:]
1010         is_tell = True
1011
1012     if is_tell:
1013         if injectargs:
1014             childargs = injectargs
1015         if not len(childargs):
1016             print('"{0} tell" requires additional arguments.'.format(sys.argv[0]),
1017                   'Try "{0} tell <name> <command> [options...]" instead.'.format(sys.argv[0]),
1018                   file=sys.stderr)
1019             return errno.EINVAL
1020
1021     # fetch JSON sigs from command
1022     # each line contains one command signature (a placeholder name
1023     # of the form 'cmdNNN' followed by an array of argument descriptors)
1024     # as part of the validated argument JSON object
1025
1026     if target[1] == '*':
1027         service = target[0]
1028         targets = [(service, o) for o in ids_by_service(service)]
1029     else:
1030         targets = [target]
1031
1032     final_ret = 0
1033     for target in targets:
1034         # prettify?  prefix output with target, if there was a wildcard used
1035         prefix = ''
1036         suffix = ''
1037         if not parsed_args.output_file and len(targets) > 1:
1038             prefix = '{0}.{1}: '.format(*target)
1039             suffix = '\n'
1040
1041         ret, outbuf, outs = json_command(cluster_handle, target=target,
1042                                          prefix='get_command_descriptions')
1043         if ret:
1044             where = '{0}.{1}'.format(*target)
1045             if ret > 0:
1046                 raise RuntimeError('Unexpeceted return code from {0}: {1}'.
1047                                    format(where, ret))
1048             outs = 'problem getting command descriptions from {0}'.format(where)
1049         else:
1050             sigdict = parse_json_funcsigs(outbuf.decode('utf-8'), 'cli')
1051
1052             if parsed_args.completion:
1053                 return complete(sigdict, childargs, target)
1054
1055             ret, outbuf, outs = new_style_command(parsed_args, childargs,
1056                                                   target, sigdict, inbuf,
1057                                                   verbose)
1058
1059             # debug tool: send any successful command *again* to
1060             # verify that it is idempotent.
1061             if not ret and 'CEPH_CLI_TEST_DUP_COMMAND' in os.environ:
1062                 ret, outbuf, outs = new_style_command(parsed_args, childargs,
1063                                                       target, sigdict, inbuf,
1064                                                       verbose)
1065                 if ret < 0:
1066                     ret = -ret
1067                     print(prefix +
1068                           'Second attempt of previously successful command '
1069                           'failed with {0}: {1}'.format(
1070                               errno.errorcode.get(ret, 'Unknown'), outs),
1071                           file=sys.stderr)
1072
1073         if ret < 0:
1074             ret = -ret
1075             errstr = errno.errorcode.get(ret, 'Unknown')
1076             print(u'Error {0}: {1}'.format(errstr, outs), file=sys.stderr)
1077             if len(targets) > 1:
1078                 final_ret = ret
1079             else:
1080                 return ret
1081
1082         if outs:
1083             print(prefix + outs, file=sys.stderr)
1084
1085         sys.stdout.flush()
1086
1087         if parsed_args.output_file:
1088             outf.write(outbuf)
1089         else:
1090             # hack: old code printed status line before many json outputs
1091             # (osd dump, etc.) that consumers know to ignore.  Add blank line
1092             # to satisfy consumers that skip the first line, but not annoy
1093             # consumers that don't.
1094             if parsed_args.output_format and \
1095                parsed_args.output_format.startswith('json'):
1096                 print()
1097
1098             # if we are prettifying things, normalize newlines.  sigh.
1099             if suffix:
1100                 outbuf = outbuf.rstrip()
1101             if outbuf:
1102                 try:
1103                     print(prefix, end='')
1104                     # Write directly to binary stdout
1105                     raw_write(outbuf)
1106                     print(suffix, end='')
1107                 except IOError as e:
1108                     if e.errno != errno.EPIPE:
1109                         raise e
1110
1111         sys.stdout.flush()
1112
1113     if parsed_args.output_file and parsed_args.output_file != '-':
1114         outf.close()
1115
1116     if final_ret:
1117         return final_ret
1118
1119     return 0
1120
1121 if __name__ == '__main__':
1122     retval = main()
1123     # shutdown explicitly; Rados() does not
1124     if cluster_handle:
1125         run_in_thread(cluster_handle.shutdown)
1126     sys.exit(retval)