3 # vim: ts=4 sw=4 smarttab expandtab
16 logging.basicConfig(level=logging.INFO)
17 log = logging.getLogger('ceph-crash')
19 auth_names = ['client.crash.%s' % socket.gethostname(),
25 parser = argparse.ArgumentParser()
27 '-p', '--path', default='/var/lib/ceph/crash',
28 help='base path to monitor for crash dumps')
30 '-d', '--delay', default=10.0, type=float,
31 help='minutes to delay between scans (0 to exit after one)',
35 help='ceph name to authenticate as '
36 '(default: try client.crash, client.admin)')
39 help='log level output (default: INFO), support INFO or DEBUG')
41 return parser.parse_args()
47 pr = subprocess.Popen(
48 args=['timeout', '30', 'ceph',
50 'crash', 'post', '-i', '-'],
51 stdin=subprocess.PIPE,
52 stderr=subprocess.PIPE,
54 f = open(os.path.join(path, 'meta'), 'rb')
55 (_, stderr) = pr.communicate(input=f.read())
56 stderr = stderr.decode()
59 if rc != 0 or stderr != "":
60 log.warning('post %s as %s failed: %s' % (path, n, stderr))
66 def scrape_path(path):
67 for p in os.listdir(path):
68 crashpath = os.path.join(path, p)
69 if not os.access(crashpath, os.R_OK):
70 log.warning('unable to read crash path %s' % (crashpath))
72 metapath = os.path.join(crashpath, 'meta')
73 donepath = os.path.join(crashpath, 'done')
74 if os.path.isfile(metapath):
75 if not os.path.isfile(donepath):
76 # hang out just for a bit; either we interrupted the dump
77 # or the daemon crashed before finishing it
79 if not os.path.isfile(donepath):
81 # ok, we can process this one
82 rc = post_crash(crashpath)
84 os.rename(crashpath, os.path.join(path, 'posted/', p))
86 "posted %s and renamed %s -> %s " %
87 (metapath, p, os.path.join('posted/', p))
91 def handler(signum, frame):
92 print('*** Interrupted with signal %d ***' % signum)
99 ceph_uid = pwd.getpwnam("ceph").pw_uid
100 ceph_gid = grp.getgrnam("ceph").gr_gid
104 except Exception as e:
105 log.error(f"Unable to drop privileges: {e}")
112 # run as unprivileged ceph user
115 # exit code 0 on SIGINT, SIGTERM
116 signal.signal(signal.SIGINT, handler)
117 signal.signal(signal.SIGTERM, handler)
120 if args.log_level == 'DEBUG':
121 log.setLevel(logging.DEBUG)
123 postdir = os.path.join(args.path, 'posted')
125 auth_names = [args.name]
127 while not os.path.isdir(postdir):
128 log.error("directory %s does not exist; please create" % postdir)
131 log.info("pinging cluster to exercise our key")
132 pr = subprocess.Popen(args=['timeout', '30', 'ceph', '-s'])
135 log.info("monitoring path %s, delay %ds" % (args.path, args.delay * 60.0))
138 scrape_path(args.path)
139 except Exception as e:
140 log.error(f"Error scraping {args.path}: {e}")
143 time.sleep(args.delay * 60)
146 if __name__ == "__main__":