]>
git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/ceph_daemon.py
2 # vim: ts=4 sw=4 smarttab expandtab
5 Copyright (C) 2015 Red Hat
7 This is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public
9 License version 2, as published by the Free Software
10 Foundation. See file COPYING.
19 from collections
.abc
import OrderedDict
21 from collections
import OrderedDict
22 from fcntl
import ioctl
23 from fnmatch
import fnmatch
24 from prettytable
import PrettyTable
, HEADER
25 from signal
import signal
, SIGWINCH
26 from termios
import TIOCGWINSZ
27 from typing
import Optional
29 from ceph_argparse
import parse_json_funcsigs
, validate_command
32 LONG_RUNNING_AVG
= 0x4
33 READ_CHUNK_SIZE
= 4096
36 def admin_socket(asok_path
: str,
38 format
: Optional
[str] = '') -> bytes
:
40 Send a daemon (--admin-daemon) command 'cmd'. asok_path is the
41 path to the admin socket; cmd is a list of strings; format may be
42 set to one of the formatted forms to get output in that form
43 (daemon commands don't support 'plain' output).
46 def do_sockio(path
, cmd_bytes
):
47 """ helper: do all the actual low-level stream I/O """
48 sock
= socket
.socket(socket
.AF_UNIX
, socket
.SOCK_STREAM
)
51 sock
.sendall(cmd_bytes
+ b
'\0')
52 len_str
= sock
.recv(4)
54 raise RuntimeError("no data returned from admin socket")
55 l
, = struct
.unpack(">I", len_str
)
60 # recv() receives signed int, i.e max 2GB
61 # workaround by capping READ_CHUNK_SIZE per call.
62 want
= min(l
- got
, READ_CHUNK_SIZE
)
67 except Exception as sock_e
:
68 raise RuntimeError('exception: ' + str(sock_e
))
72 cmd_json
= do_sockio(asok_path
,
73 b
'{"prefix": "get_command_descriptions"}')
74 except Exception as e
:
75 raise RuntimeError('exception getting command descriptions: ' + str(e
))
77 if cmd
== 'get_command_descriptions':
80 sigdict
= parse_json_funcsigs(cmd_json
.decode('utf-8'), 'cli')
81 valid_dict
= validate_command(sigdict
, cmd
)
83 raise RuntimeError('invalid command')
86 valid_dict
['format'] = format
89 ret
= do_sockio(asok_path
, json
.dumps(valid_dict
).encode('utf-8'))
90 except Exception as e
:
91 raise RuntimeError('exception: ' + str(e
))
96 class Termsize(object):
97 DEFAULT_SIZE
= (25, 80)
99 self
.rows
, self
.cols
= self
._gettermsize
()
102 def _gettermsize(self
):
104 fd
= sys
.stdin
.fileno()
105 sz
= struct
.pack('hhhh', 0, 0, 0, 0)
106 rows
, cols
= struct
.unpack('hhhh', ioctl(fd
, TIOCGWINSZ
, sz
))[:2]
109 return self
.DEFAULT_SIZE
112 rows
, cols
= self
._gettermsize
()
114 self
.changed
= (self
.rows
, self
.cols
) != (rows
, cols
)
115 self
.rows
, self
.cols
= rows
, cols
117 def reset_changed(self
):
121 return '%s(%dx%d, changed %s)' % (self
.__class
__,
122 self
.rows
, self
.cols
, self
.changed
)
125 return 'Termsize(%d,%d,%s)' % (self
.__class
__,
126 self
.rows
, self
.cols
, self
.changed
)
129 class DaemonWatcher(object):
131 Given a Ceph daemon's admin socket path, poll its performance counters
132 and output a series of output lines showing the momentary values of
133 counters of interest (those with the 'nick' property in Ceph's schema)
146 RESET_SEQ
= "\033[0m"
147 COLOR_SEQ
= "\033[1;%dm"
148 COLOR_DARK_SEQ
= "\033[0;%dm"
150 UNDERLINE_SEQ
= "\033[4m"
152 def __init__(self
, asok
, statpats
=None, min_prio
=0):
153 self
.asok_path
= asok
154 self
._colored
= False
158 self
._statpats
= statpats
159 self
._stats
_that
_fit
= dict()
160 self
._min
_prio
= min_prio
161 self
.termsize
= Termsize()
163 def supports_color(self
, ostr
):
165 Returns True if the running system's terminal supports color, and False
168 unsupported_platform
= (sys
.platform
in ('win32', 'Pocket PC'))
169 # isatty is not always implemented, #6223.
170 is_a_tty
= hasattr(ostr
, 'isatty') and ostr
.isatty()
171 if unsupported_platform
or not is_a_tty
:
175 def colorize(self
, msg
, color
, dark
=False):
177 Decorate `msg` with escape sequences to give the requested color
179 return (self
.COLOR_DARK_SEQ
if dark
else self
.COLOR_SEQ
) % (30 + color
) \
180 + msg
+ self
.RESET_SEQ
184 Decorate `msg` with escape sequences to make it appear bold
186 return self
.BOLD_SEQ
+ msg
+ self
.RESET_SEQ
188 def format_dimless(self
, n
, width
):
190 Format a number without units, so as to fit into `width` characters, substituting
191 an appropriate unit suffix.
193 units
= [' ', 'k', 'M', 'G', 'T', 'P', 'E', 'Z']
195 while len("%s" % (int(n
) // (1000**unit
))) > width
- 1:
196 if unit
>= len(units
) - 1:
201 truncated_float
= ("%f" % (n
/ (1000.0 ** unit
)))[0:width
- 1]
202 if truncated_float
[-1] == '.':
203 truncated_float
= " " + truncated_float
[0:-1]
205 truncated_float
= "%{wid}d".format(wid
=width
-1) % n
206 formatted
= "%s%s" % (truncated_float
, units
[unit
])
210 color
= self
.BLACK
, False
212 color
= self
.YELLOW
, False
213 return self
.bold(self
.colorize(formatted
[0:-1], color
[0], color
[1])) \
214 + self
.bold(self
.colorize(formatted
[-1], self
.YELLOW
, False))
218 def col_width(self
, nick
):
220 Given the short name `nick` for a column, how many characters
221 of width should the column be allocated? Does not include spacing
224 return max(len(nick
), 4)
226 def get_stats_that_fit(self
):
228 Get a possibly-truncated list of stats to display based on
229 current terminal width. Allow breaking mid-section.
231 current_fit
= OrderedDict()
232 if self
.termsize
.changed
or not self
._stats
_that
_fit
:
234 for section_name
, names
in self
._stats
.items():
235 for name
, stat_data
in names
.items():
236 width
+= self
.col_width(stat_data
) + 1
237 if width
> self
.termsize
.cols
:
239 if section_name
not in current_fit
:
240 current_fit
[section_name
] = OrderedDict()
241 current_fit
[section_name
][name
] = stat_data
242 if width
> self
.termsize
.cols
:
245 self
.termsize
.reset_changed()
246 changed
= current_fit
and (current_fit
!= self
._stats
_that
_fit
)
248 self
._stats
_that
_fit
= current_fit
249 return self
._stats
_that
_fit
, changed
251 def _print_headers(self
, ostr
):
253 Print a header row to `ostr`
256 stats
, _
= self
.get_stats_that_fit()
257 for section_name
, names
in stats
.items():
259 sum([self
.col_width(x
) + 1 for x
in names
.values()]) - 1
260 pad
= max(section_width
- len(section_name
), 0)
261 pad_prefix
= pad
// 2
262 header
+= (pad_prefix
* '-')
263 header
+= (section_name
[0:section_width
])
264 header
+= ((pad
- pad_prefix
) * '-')
267 ostr
.write(self
.colorize(header
, self
.BLUE
, True))
270 for section_name
, names
in stats
.items():
271 for stat_name
, stat_nick
in names
.items():
272 sub_header
+= self
.UNDERLINE_SEQ \
274 stat_nick
.ljust(self
.col_width(stat_nick
)),
277 sub_header
= sub_header
[0:-1] + self
.colorize('|', self
.BLUE
)
279 ostr
.write(sub_header
)
281 def _print_vals(self
, ostr
, dump
, last_dump
):
283 Print a single row of values to `ostr`, based on deltas between `dump` and
287 fit
, changed
= self
.get_stats_that_fit()
289 self
._print
_headers
(ostr
)
290 for section_name
, names
in fit
.items():
291 for stat_name
, stat_nick
in names
.items():
292 stat_type
= self
._schema
[section_name
][stat_name
]['type']
293 if bool(stat_type
& COUNTER
):
294 n
= max(dump
[section_name
][stat_name
] -
295 last_dump
[section_name
][stat_name
], 0)
296 elif bool(stat_type
& LONG_RUNNING_AVG
):
297 entries
= dump
[section_name
][stat_name
]['avgcount'] - \
298 last_dump
[section_name
][stat_name
]['avgcount']
300 n
= (dump
[section_name
][stat_name
]['sum'] -
301 last_dump
[section_name
][stat_name
]['sum']) \
303 n
*= 1000.0 # Present in milliseconds
307 n
= dump
[section_name
][stat_name
]
309 val_row
+= self
.format_dimless(n
, self
.col_width(stat_nick
))
311 val_row
= val_row
[0:-1]
312 val_row
+= self
.colorize("|", self
.BLUE
)
313 val_row
= val_row
[0:-len(self
.colorize("|", self
.BLUE
))]
314 ostr
.write("{0}\n".format(val_row
))
316 def _should_include(self
, sect
, name
, prio
):
318 boolean: should we output this stat?
320 1) If self._statpats exists and the name filename-glob-matches
321 anything in the list, and prio is high enough, or
322 2) If self._statpats doesn't exist and prio is high enough
327 sectname
= '.'.join((sect
, name
))
329 p
for p
in self
._statpats
330 if fnmatch(name
, p
) or fnmatch(sectname
, p
)
334 if self
._min
_prio
is not None and prio
is not None:
335 return (prio
>= self
._min
_prio
)
339 def _load_schema(self
):
341 Populate our instance-local copy of the daemon's performance counter
342 schema, and work out which stats we will display.
344 self
._schema
= json
.loads(
345 admin_socket(self
.asok_path
, ["perf", "schema"]).decode('utf-8'),
346 object_pairs_hook
=OrderedDict
)
348 # Build list of which stats we will display
349 self
._stats
= OrderedDict()
350 for section_name
, section_stats
in self
._schema
.items():
351 for name
, schema_data
in section_stats
.items():
352 prio
= schema_data
.get('priority', 0)
353 if self
._should
_include
(section_name
, name
, prio
):
354 if section_name
not in self
._stats
:
355 self
._stats
[section_name
] = OrderedDict()
356 self
._stats
[section_name
][name
] = schema_data
['nick']
357 if not len(self
._stats
):
358 raise RuntimeError("no stats selected by filters")
360 def _handle_sigwinch(self
, signo
, frame
):
361 self
.termsize
.update()
363 def run(self
, interval
, count
=None, ostr
=sys
.stdout
):
365 Print output at regular intervals until interrupted.
367 :param ostr: Stream to which to send output
371 self
._colored
= self
.supports_color(ostr
)
373 self
._print
_headers
(ostr
)
375 last_dump
= json
.loads(admin_socket(self
.asok_path
, ["perf", "dump"]).decode('utf-8'))
376 rows_since_header
= 0
379 signal(SIGWINCH
, self
._handle
_sigwinch
)
381 dump
= json
.loads(admin_socket(self
.asok_path
, ["perf", "dump"]).decode('utf-8'))
382 if rows_since_header
>= self
.termsize
.rows
- 2:
383 self
._print
_headers
(ostr
)
384 rows_since_header
= 0
385 self
._print
_vals
(ostr
, dump
, last_dump
)
386 if count
is not None:
390 rows_since_header
+= 1
393 # time.sleep() is interrupted by SIGWINCH; avoid that
394 end
= time
.time() + interval
395 while time
.time() < end
:
396 time
.sleep(end
- time
.time())
398 except KeyboardInterrupt:
401 def list(self
, ostr
=sys
.stdout
):
403 Show all selected stats with section, full name, nick, and prio
405 table
= PrettyTable(('section', 'name', 'nick', 'prio'))
406 table
.align
['section'] = 'l'
407 table
.align
['name'] = 'l'
408 table
.align
['nick'] = 'l'
409 table
.align
['prio'] = 'r'
411 for section_name
, section_stats
in self
._stats
.items():
412 for name
, nick
in section_stats
.items():
413 prio
= self
._schema
[section_name
][name
].get('priority') or 0
414 table
.add_row((section_name
, name
, nick
, prio
))
415 ostr
.write(table
.get_string(hrules
=HEADER
) + '\n')