]> git.proxmox.com Git - ceph.git/blob - ceph/src/mon/LogMonitor.cc
update sources to v12.2.3
[ceph.git] / ceph / src / mon / LogMonitor.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
7 *
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.
12 *
13 */
14
15 #include <boost/algorithm/string/predicate.hpp>
16
17 #include <sstream>
18 #include <syslog.h>
19
20 #include "LogMonitor.h"
21 #include "Monitor.h"
22 #include "MonitorDBStore.h"
23
24 #include "messages/MMonCommand.h"
25 #include "messages/MLog.h"
26 #include "messages/MLogAck.h"
27 #include "common/Graylog.h"
28 #include "common/errno.h"
29 #include "common/strtol.h"
30 #include "include/assert.h"
31 #include "include/str_list.h"
32 #include "include/str_map.h"
33 #include "include/compat.h"
34
35 #define dout_subsys ceph_subsys_mon
36 #undef dout_prefix
37 #define dout_prefix _prefix(_dout, mon, get_last_committed())
38 static ostream& _prefix(std::ostream *_dout, Monitor *mon, version_t v) {
39 return *_dout << "mon." << mon->name << "@" << mon->rank
40 << "(" << mon->get_state_name()
41 << ").log v" << v << " ";
42 }
43
44 ostream& operator<<(ostream &out, const LogMonitor &pm)
45 {
46 return out << "log";
47 }
48
49 /*
50 Tick function to update the map based on performance every N seconds
51 */
52
53 void LogMonitor::tick()
54 {
55 if (!is_active()) return;
56
57 dout(10) << *this << dendl;
58
59 }
60
61 void LogMonitor::create_initial()
62 {
63 dout(10) << "create_initial -- creating initial map" << dendl;
64 LogEntry e;
65 memset(&e.who, 0, sizeof(e.who));
66 e.name = g_conf->name;
67 e.stamp = ceph_clock_now();
68 e.prio = CLOG_INFO;
69 std::stringstream ss;
70 ss << "mkfs " << mon->monmap->get_fsid();
71 e.msg = ss.str();
72 e.seq = 0;
73 pending_log.insert(pair<utime_t,LogEntry>(e.stamp, e));
74 }
75
76 void LogMonitor::update_from_paxos(bool *need_bootstrap)
77 {
78 dout(10) << __func__ << dendl;
79 version_t version = get_last_committed();
80 dout(10) << __func__ << " version " << version
81 << " summary v " << summary.version << dendl;
82 if (version == summary.version)
83 return;
84 assert(version >= summary.version);
85
86 map<string,bufferlist> channel_blog;
87
88 version_t latest_full = get_version_latest_full();
89 dout(10) << __func__ << " latest full " << latest_full << dendl;
90 if ((latest_full > 0) && (latest_full > summary.version)) {
91 bufferlist latest_bl;
92 get_version_full(latest_full, latest_bl);
93 assert(latest_bl.length() != 0);
94 dout(7) << __func__ << " loading summary e" << latest_full << dendl;
95 bufferlist::iterator p = latest_bl.begin();
96 ::decode(summary, p);
97 dout(7) << __func__ << " loaded summary e" << summary.version << dendl;
98 }
99
100 // walk through incrementals
101 while (version > summary.version) {
102 bufferlist bl;
103 int err = get_version(summary.version+1, bl);
104 assert(err == 0);
105 assert(bl.length());
106
107 bufferlist::iterator p = bl.begin();
108 __u8 v;
109 ::decode(v, p);
110 while (!p.end()) {
111 LogEntry le;
112 le.decode(p);
113 dout(7) << "update_from_paxos applying incremental log " << summary.version+1 << " " << le << dendl;
114
115 string channel = le.channel;
116 if (channel.empty()) // keep retrocompatibility
117 channel = CLOG_CHANNEL_CLUSTER;
118
119 if (g_conf->get_val<bool>("mon_cluster_log_to_stderr")) {
120 cerr << channel << " " << le << std::endl;
121 }
122
123 if (channels.do_log_to_syslog(channel)) {
124 string level = channels.get_level(channel);
125 string facility = channels.get_facility(channel);
126 if (level.empty() || facility.empty()) {
127 derr << __func__ << " unable to log to syslog -- level or facility"
128 << " not defined (level: " << level << ", facility: "
129 << facility << ")" << dendl;
130 continue;
131 }
132 le.log_to_syslog(channels.get_level(channel),
133 channels.get_facility(channel));
134 }
135
136 if (channels.do_log_to_graylog(channel)) {
137 ceph::logging::Graylog::Ref graylog = channels.get_graylog(channel);
138 if (graylog) {
139 graylog->log_log_entry(&le);
140 }
141 dout(7) << "graylog: " << channel << " " << graylog
142 << " host:" << channels.log_to_graylog_host << dendl;
143 }
144
145 string log_file = channels.get_log_file(channel);
146 dout(20) << __func__ << " logging for channel '" << channel
147 << "' to file '" << log_file << "'" << dendl;
148
149 if (!log_file.empty()) {
150 string log_file_level = channels.get_log_file_level(channel);
151 if (log_file_level.empty()) {
152 dout(1) << __func__ << " warning: log file level not defined for"
153 << " channel '" << channel << "' yet a log file is --"
154 << " will assume lowest level possible" << dendl;
155 }
156
157 int min = string_to_syslog_level(log_file_level);
158 int l = clog_type_to_syslog_level(le.prio);
159 if (l <= min) {
160 stringstream ss;
161 ss << le << "\n";
162 // init entry if DNE
163 bufferlist &blog = channel_blog[channel];
164 blog.append(ss.str());
165 }
166 }
167
168 summary.add(le);
169 }
170
171 summary.version++;
172 summary.prune(g_conf->mon_log_max_summary);
173 }
174
175 dout(15) << __func__ << " logging for "
176 << channel_blog.size() << " channels" << dendl;
177 for(map<string,bufferlist>::iterator p = channel_blog.begin();
178 p != channel_blog.end(); ++p) {
179 if (!p->second.length()) {
180 dout(15) << __func__ << " channel '" << p->first
181 << "': nothing to log" << dendl;
182 continue;
183 }
184
185 dout(15) << __func__ << " channel '" << p->first
186 << "' logging " << p->second.length() << " bytes" << dendl;
187 string log_file = channels.get_log_file(p->first);
188
189 int fd = ::open(log_file.c_str(), O_WRONLY|O_APPEND|O_CREAT, 0600);
190 if (fd < 0) {
191 int err = -errno;
192 dout(1) << "unable to write to '" << log_file << "' for channel '"
193 << p->first << "': " << cpp_strerror(err) << dendl;
194 } else {
195 int err = p->second.write_fd(fd);
196 if (err < 0) {
197 dout(1) << "error writing to '" << log_file << "' for channel '"
198 << p->first << ": " << cpp_strerror(err) << dendl;
199 }
200 VOID_TEMP_FAILURE_RETRY(::close(fd));
201 }
202 }
203
204 check_subs();
205 }
206
207 void LogMonitor::create_pending()
208 {
209 pending_log.clear();
210 pending_summary = summary;
211 dout(10) << "create_pending v " << (get_last_committed() + 1) << dendl;
212 }
213
214 void LogMonitor::encode_pending(MonitorDBStore::TransactionRef t)
215 {
216 version_t version = get_last_committed() + 1;
217 bufferlist bl;
218 dout(10) << __func__ << " v" << version << dendl;
219 __u8 v = 1;
220 ::encode(v, bl);
221 multimap<utime_t,LogEntry>::iterator p;
222 for (p = pending_log.begin(); p != pending_log.end(); ++p)
223 p->second.encode(bl, mon->get_quorum_con_features());
224
225 put_version(t, version, bl);
226 put_last_committed(t, version);
227 }
228
229 void LogMonitor::encode_full(MonitorDBStore::TransactionRef t)
230 {
231 dout(10) << __func__ << " log v " << summary.version << dendl;
232 assert(get_last_committed() == summary.version);
233
234 bufferlist summary_bl;
235 ::encode(summary, summary_bl, mon->get_quorum_con_features());
236
237 put_version_full(t, summary.version, summary_bl);
238 put_version_latest_full(t, summary.version);
239 }
240
241 version_t LogMonitor::get_trim_to()
242 {
243 if (!mon->is_leader())
244 return 0;
245
246 unsigned max = g_conf->mon_max_log_epochs;
247 version_t version = get_last_committed();
248 if (version > max)
249 return version - max;
250 return 0;
251 }
252
253 bool LogMonitor::preprocess_query(MonOpRequestRef op)
254 {
255 op->mark_logmon_event("preprocess_query");
256 PaxosServiceMessage *m = static_cast<PaxosServiceMessage*>(op->get_req());
257 dout(10) << "preprocess_query " << *m << " from " << m->get_orig_source_inst() << dendl;
258 switch (m->get_type()) {
259 case MSG_MON_COMMAND:
260 return preprocess_command(op);
261
262 case MSG_LOG:
263 return preprocess_log(op);
264
265 default:
266 ceph_abort();
267 return true;
268 }
269 }
270
271 bool LogMonitor::prepare_update(MonOpRequestRef op)
272 {
273 op->mark_logmon_event("prepare_update");
274 PaxosServiceMessage *m = static_cast<PaxosServiceMessage*>(op->get_req());
275 dout(10) << "prepare_update " << *m << " from " << m->get_orig_source_inst() << dendl;
276 switch (m->get_type()) {
277 case MSG_MON_COMMAND:
278 return prepare_command(op);
279 case MSG_LOG:
280 return prepare_log(op);
281 default:
282 ceph_abort();
283 return false;
284 }
285 }
286
287 bool LogMonitor::preprocess_log(MonOpRequestRef op)
288 {
289 op->mark_logmon_event("preprocess_log");
290 MLog *m = static_cast<MLog*>(op->get_req());
291 dout(10) << "preprocess_log " << *m << " from " << m->get_orig_source() << dendl;
292 int num_new = 0;
293
294 MonSession *session = m->get_session();
295 if (!session)
296 goto done;
297 if (!session->is_capable("log", MON_CAP_W)) {
298 dout(0) << "preprocess_log got MLog from entity with insufficient privileges "
299 << session->caps << dendl;
300 goto done;
301 }
302
303 for (deque<LogEntry>::iterator p = m->entries.begin();
304 p != m->entries.end();
305 ++p) {
306 if (!pending_summary.contains(p->key()))
307 num_new++;
308 }
309 if (!num_new) {
310 dout(10) << " nothing new" << dendl;
311 goto done;
312 }
313
314 return false;
315
316 done:
317 return true;
318 }
319
320 struct LogMonitor::C_Log : public C_MonOp {
321 LogMonitor *logmon;
322 C_Log(LogMonitor *p, MonOpRequestRef o) :
323 C_MonOp(o), logmon(p) {}
324 void _finish(int r) override {
325 if (r == -ECANCELED) {
326 return;
327 }
328 logmon->_updated_log(op);
329 }
330 };
331
332 bool LogMonitor::prepare_log(MonOpRequestRef op)
333 {
334 op->mark_logmon_event("prepare_log");
335 MLog *m = static_cast<MLog*>(op->get_req());
336 dout(10) << "prepare_log " << *m << " from " << m->get_orig_source() << dendl;
337
338 if (m->fsid != mon->monmap->fsid) {
339 dout(0) << "handle_log on fsid " << m->fsid << " != " << mon->monmap->fsid
340 << dendl;
341 return false;
342 }
343
344 for (deque<LogEntry>::iterator p = m->entries.begin();
345 p != m->entries.end();
346 ++p) {
347 dout(10) << " logging " << *p << dendl;
348 if (!pending_summary.contains(p->key())) {
349 pending_summary.add(*p);
350 pending_log.insert(pair<utime_t,LogEntry>(p->stamp, *p));
351 }
352 }
353 pending_summary.prune(g_conf->mon_log_max_summary);
354 wait_for_finished_proposal(op, new C_Log(this, op));
355 return true;
356 }
357
358 void LogMonitor::_updated_log(MonOpRequestRef op)
359 {
360 MLog *m = static_cast<MLog*>(op->get_req());
361 dout(7) << "_updated_log for " << m->get_orig_source_inst() << dendl;
362 mon->send_reply(op, new MLogAck(m->fsid, m->entries.rbegin()->seq));
363 }
364
365 bool LogMonitor::should_propose(double& delay)
366 {
367 // commit now if we have a lot of pending events
368 if (g_conf->mon_max_log_entries_per_event > 0 &&
369 pending_log.size() >= (unsigned)g_conf->mon_max_log_entries_per_event)
370 return true;
371
372 // otherwise fall back to generic policy
373 return PaxosService::should_propose(delay);
374 }
375
376
377 bool LogMonitor::preprocess_command(MonOpRequestRef op)
378 {
379 op->mark_logmon_event("preprocess_command");
380 MMonCommand *m = static_cast<MMonCommand*>(op->get_req());
381 int r = -EINVAL;
382 bufferlist rdata;
383 stringstream ss;
384
385 map<string, cmd_vartype> cmdmap;
386 if (!cmdmap_from_json(m->cmd, &cmdmap, ss)) {
387 string rs = ss.str();
388 mon->reply_command(op, -EINVAL, rs, get_last_committed());
389 return true;
390 }
391 MonSession *session = m->get_session();
392 if (!session) {
393 mon->reply_command(op, -EACCES, "access denied", get_last_committed());
394 return true;
395 }
396
397 string prefix;
398 cmd_getval(g_ceph_context, cmdmap, "prefix", prefix);
399
400 string format;
401 cmd_getval(g_ceph_context, cmdmap, "format", format, string("plain"));
402 boost::scoped_ptr<Formatter> f(Formatter::create(format));
403
404 if (prefix == "log last") {
405 int64_t num = 20;
406 cmd_getval(g_ceph_context, cmdmap, "num", num);
407 if (f) {
408 f->open_array_section("tail");
409 }
410
411 std::string level_str;
412 clog_type level;
413 if (cmd_getval(g_ceph_context, cmdmap, "level", level_str)) {
414 level = LogEntry::str_to_level(level_str);
415 if (level == CLOG_UNKNOWN) {
416 ss << "Invalid severity '" << level_str << "'";
417 mon->reply_command(op, -EINVAL, ss.str(), get_last_committed());
418 return true;
419 }
420 } else {
421 level = CLOG_INFO;
422 }
423
424 std::string channel;
425 if (!cmd_getval(g_ceph_context, cmdmap, "channel", channel)) {
426 channel = CLOG_CHANNEL_DEFAULT;
427 }
428
429 // We'll apply this twice, once while counting out lines
430 // and once while outputting them.
431 auto match = [level, channel](const LogEntry &entry) {
432 return entry.prio >= level && (entry.channel == channel || channel == "*");
433 };
434
435 auto rp = summary.tail.rbegin();
436 for (; num > 0 && rp != summary.tail.rend(); ++rp) {
437 if (match(*rp)) {
438 num--;
439 }
440 }
441 if (rp == summary.tail.rend()) {
442 --rp;
443 }
444 ostringstream ss;
445 for (; rp != summary.tail.rbegin(); --rp) {
446 if (!match(*rp)) {
447 continue;
448 }
449
450 if (f) {
451 f->dump_object("entry", *rp);
452 } else {
453 ss << *rp << "\n";
454 }
455 }
456 if (f) {
457 f->close_section();
458 f->flush(rdata);
459 } else {
460 rdata.append(ss.str());
461 }
462 r = 0;
463 } else {
464 return false;
465 }
466
467 string rs;
468 getline(ss, rs);
469 mon->reply_command(op, r, rs, rdata, get_last_committed());
470 return true;
471 }
472
473
474 bool LogMonitor::prepare_command(MonOpRequestRef op)
475 {
476 op->mark_logmon_event("prepare_command");
477 MMonCommand *m = static_cast<MMonCommand*>(op->get_req());
478 stringstream ss;
479 string rs;
480 int err = -EINVAL;
481
482 map<string, cmd_vartype> cmdmap;
483 if (!cmdmap_from_json(m->cmd, &cmdmap, ss)) {
484 // ss has reason for failure
485 string rs = ss.str();
486 mon->reply_command(op, -EINVAL, rs, get_last_committed());
487 return true;
488 }
489
490 string prefix;
491 cmd_getval(g_ceph_context, cmdmap, "prefix", prefix);
492
493 MonSession *session = m->get_session();
494 if (!session) {
495 mon->reply_command(op, -EACCES, "access denied", get_last_committed());
496 return true;
497 }
498
499 if (prefix == "log") {
500 vector<string> logtext;
501 cmd_getval(g_ceph_context, cmdmap, "logtext", logtext);
502 LogEntry le;
503 le.who = m->get_orig_source_inst();
504 le.name = session->entity_name;
505 le.stamp = m->get_recv_stamp();
506 le.seq = 0;
507 le.prio = CLOG_INFO;
508 le.channel = CLOG_CHANNEL_DEFAULT;
509 le.msg = str_join(logtext, " ");
510 pending_summary.add(le);
511 pending_summary.prune(g_conf->mon_log_max_summary);
512 pending_log.insert(pair<utime_t,LogEntry>(le.stamp, le));
513 wait_for_finished_proposal(op, new Monitor::C_Command(
514 mon, op, 0, string(), get_last_committed() + 1));
515 return true;
516 }
517
518 getline(ss, rs);
519 mon->reply_command(op, err, rs, get_last_committed());
520 return false;
521 }
522
523
524 int LogMonitor::sub_name_to_id(const string& n)
525 {
526 if (n.substr(0, 4) == "log-" && n.size() > 4) {
527 return LogEntry::str_to_level(n.substr(4));
528 } else {
529 return CLOG_UNKNOWN;
530 }
531 }
532
533 void LogMonitor::check_subs()
534 {
535 dout(10) << __func__ << dendl;
536 for (map<string, xlist<Subscription*>*>::iterator i = mon->session_map.subs.begin();
537 i != mon->session_map.subs.end();
538 ++i) {
539 for (xlist<Subscription*>::iterator j = i->second->begin(); !j.end(); ++j) {
540 if (sub_name_to_id((*j)->type) >= 0)
541 check_sub(*j);
542 }
543 }
544 }
545
546 void LogMonitor::check_sub(Subscription *s)
547 {
548 dout(10) << __func__ << " client wants " << s->type << " ver " << s->next << dendl;
549
550 int sub_level = sub_name_to_id(s->type);
551 assert(sub_level >= 0);
552
553 version_t summary_version = summary.version;
554 if (s->next > summary_version) {
555 dout(10) << __func__ << " client " << s->session->inst
556 << " requested version (" << s->next << ") is greater than ours ("
557 << summary_version << "), which means we already sent him"
558 << " everything we have." << dendl;
559 return;
560 }
561
562 MLog *mlog = new MLog(mon->monmap->fsid);
563
564 if (s->next == 0) {
565 /* First timer, heh? */
566 bool ret = _create_sub_summary(mlog, sub_level);
567 if (!ret) {
568 dout(1) << __func__ << " ret = " << ret << dendl;
569 mlog->put();
570 return;
571 }
572 } else {
573 /* let us send you an incremental log... */
574 _create_sub_incremental(mlog, sub_level, s->next);
575 }
576
577 dout(1) << __func__ << " sending message to " << s->session->inst
578 << " with " << mlog->entries.size() << " entries"
579 << " (version " << mlog->version << ")" << dendl;
580
581 if (!mlog->entries.empty()) {
582 s->session->con->send_message(mlog);
583 } else {
584 mlog->put();
585 }
586 if (s->onetime)
587 mon->session_map.remove_sub(s);
588 else
589 s->next = summary_version+1;
590 }
591
592 /**
593 * Create a log message containing only the last message in the summary.
594 *
595 * @param mlog Log message we'll send to the client.
596 * @param level Maximum log level the client is interested in.
597 * @return 'true' if we consider we successfully populated @mlog;
598 * 'false' otherwise.
599 */
600 bool LogMonitor::_create_sub_summary(MLog *mlog, int level)
601 {
602 dout(10) << __func__ << dendl;
603
604 assert(mlog != NULL);
605
606 if (!summary.tail.size())
607 return false;
608
609 list<LogEntry>::reverse_iterator it = summary.tail.rbegin();
610 for (; it != summary.tail.rend(); ++it) {
611 LogEntry e = *it;
612 if (e.prio < level)
613 continue;
614
615 mlog->entries.push_back(e);
616 mlog->version = summary.version;
617 break;
618 }
619
620 return true;
621 }
622
623 /**
624 * Create an incremental log message from version \p sv to \p summary.version
625 *
626 * @param mlog Log message we'll send to the client with the messages received
627 * since version \p sv, inclusive.
628 * @param level The max log level of the messages the client is interested in.
629 * @param sv The version the client is looking for.
630 */
631 void LogMonitor::_create_sub_incremental(MLog *mlog, int level, version_t sv)
632 {
633 dout(10) << __func__ << " level " << level << " ver " << sv
634 << " cur summary ver " << summary.version << dendl;
635
636 if (sv < get_first_committed()) {
637 dout(10) << __func__ << " skipped from " << sv
638 << " to first_committed " << get_first_committed() << dendl;
639 LogEntry le;
640 le.stamp = ceph_clock_now();
641 le.prio = CLOG_WARN;
642 ostringstream ss;
643 ss << "skipped log messages from " << sv << " to " << get_first_committed();
644 le.msg = ss.str();
645 mlog->entries.push_back(le);
646 sv = get_first_committed();
647 }
648
649 version_t summary_ver = summary.version;
650 while (sv <= summary_ver) {
651 bufferlist bl;
652 int err = get_version(sv, bl);
653 assert(err == 0);
654 assert(bl.length());
655 bufferlist::iterator p = bl.begin();
656 __u8 v;
657 ::decode(v,p);
658 while (!p.end()) {
659 LogEntry le;
660 le.decode(p);
661
662 if (le.prio < level) {
663 dout(20) << __func__ << " requested " << level
664 << " entry " << le.prio << dendl;
665 continue;
666 }
667
668 mlog->entries.push_back(le);
669 }
670 mlog->version = sv++;
671 }
672
673 dout(10) << __func__ << " incremental message ready ("
674 << mlog->entries.size() << " entries)" << dendl;
675 }
676
677 void LogMonitor::update_log_channels()
678 {
679 ostringstream oss;
680
681 channels.clear();
682
683 int r = get_conf_str_map_helper(g_conf->mon_cluster_log_to_syslog,
684 oss, &channels.log_to_syslog,
685 CLOG_CONFIG_DEFAULT_KEY);
686 if (r < 0) {
687 derr << __func__ << " error parsing 'mon_cluster_log_to_syslog'" << dendl;
688 return;
689 }
690
691 r = get_conf_str_map_helper(g_conf->mon_cluster_log_to_syslog_level,
692 oss, &channels.syslog_level,
693 CLOG_CONFIG_DEFAULT_KEY);
694 if (r < 0) {
695 derr << __func__ << " error parsing 'mon_cluster_log_to_syslog_level'"
696 << dendl;
697 return;
698 }
699
700 r = get_conf_str_map_helper(g_conf->mon_cluster_log_to_syslog_facility,
701 oss, &channels.syslog_facility,
702 CLOG_CONFIG_DEFAULT_KEY);
703 if (r < 0) {
704 derr << __func__ << " error parsing 'mon_cluster_log_to_syslog_facility'"
705 << dendl;
706 return;
707 }
708
709 r = get_conf_str_map_helper(g_conf->mon_cluster_log_file, oss,
710 &channels.log_file,
711 CLOG_CONFIG_DEFAULT_KEY);
712 if (r < 0) {
713 derr << __func__ << " error parsing 'mon_cluster_log_file'" << dendl;
714 return;
715 }
716
717 r = get_conf_str_map_helper(g_conf->mon_cluster_log_file_level, oss,
718 &channels.log_file_level,
719 CLOG_CONFIG_DEFAULT_KEY);
720 if (r < 0) {
721 derr << __func__ << " error parsing 'mon_cluster_log_file_level'"
722 << dendl;
723 return;
724 }
725
726 r = get_conf_str_map_helper(g_conf->mon_cluster_log_to_graylog, oss,
727 &channels.log_to_graylog,
728 CLOG_CONFIG_DEFAULT_KEY);
729 if (r < 0) {
730 derr << __func__ << " error parsing 'mon_cluster_log_to_graylog'"
731 << dendl;
732 return;
733 }
734
735 r = get_conf_str_map_helper(g_conf->mon_cluster_log_to_graylog_host, oss,
736 &channels.log_to_graylog_host,
737 CLOG_CONFIG_DEFAULT_KEY);
738 if (r < 0) {
739 derr << __func__ << " error parsing 'mon_cluster_log_to_graylog_host'"
740 << dendl;
741 return;
742 }
743
744 r = get_conf_str_map_helper(g_conf->mon_cluster_log_to_graylog_port, oss,
745 &channels.log_to_graylog_port,
746 CLOG_CONFIG_DEFAULT_KEY);
747 if (r < 0) {
748 derr << __func__ << " error parsing 'mon_cluster_log_to_graylog_port'"
749 << dendl;
750 return;
751 }
752
753 channels.expand_channel_meta();
754 }
755
756 void LogMonitor::log_channel_info::expand_channel_meta(map<string,string> &m)
757 {
758 generic_dout(20) << __func__ << " expand map: " << m << dendl;
759 for (map<string,string>::iterator p = m.begin(); p != m.end(); ++p) {
760 m[p->first] = expand_channel_meta(p->second, p->first);
761 }
762 generic_dout(20) << __func__ << " expanded map: " << m << dendl;
763 }
764
765 string LogMonitor::log_channel_info::expand_channel_meta(
766 const string &input,
767 const string &change_to)
768 {
769 size_t pos = string::npos;
770 string s(input);
771 while ((pos = s.find(LOG_META_CHANNEL)) != string::npos) {
772 string tmp = s.substr(0, pos) + change_to;
773 if (pos+LOG_META_CHANNEL.length() < s.length())
774 tmp += s.substr(pos+LOG_META_CHANNEL.length());
775 s = tmp;
776 }
777 generic_dout(20) << __func__ << " from '" << input
778 << "' to '" << s << "'" << dendl;
779
780 return s;
781 }
782
783 bool LogMonitor::log_channel_info::do_log_to_syslog(const string &channel) {
784 string v = get_str_map_key(log_to_syslog, channel,
785 &CLOG_CONFIG_DEFAULT_KEY);
786 // We expect booleans, but they are in k/v pairs, kept
787 // as strings, in 'log_to_syslog'. We must ensure
788 // compatibility with existing boolean handling, and so
789 // we are here using a modified version of how
790 // md_config_t::set_val_raw() handles booleans. We will
791 // accept both 'true' and 'false', but will also check for
792 // '1' and '0'. The main distiction between this and the
793 // original code is that we will assume everything not '1',
794 // '0', 'true' or 'false' to be 'false'.
795 bool ret = false;
796
797 if (boost::iequals(v, "false")) {
798 ret = false;
799 } else if (boost::iequals(v, "true")) {
800 ret = true;
801 } else {
802 std::string err;
803 int b = strict_strtol(v.c_str(), 10, &err);
804 ret = (err.empty() && b == 1);
805 }
806
807 return ret;
808 }
809
810 ceph::logging::Graylog::Ref LogMonitor::log_channel_info::get_graylog(
811 const string &channel)
812 {
813 generic_dout(25) << __func__ << " for channel '"
814 << channel << "'" << dendl;
815
816 if (graylogs.count(channel) == 0) {
817 auto graylog(std::make_shared<ceph::logging::Graylog>("mon"));
818
819 graylog->set_fsid(g_conf->get_val<uuid_d>("fsid"));
820 graylog->set_hostname(g_conf->host);
821 graylog->set_destination(get_str_map_key(log_to_graylog_host, channel,
822 &CLOG_CONFIG_DEFAULT_KEY),
823 atoi(get_str_map_key(log_to_graylog_port, channel,
824 &CLOG_CONFIG_DEFAULT_KEY).c_str()));
825
826 graylogs[channel] = graylog;
827 generic_dout(20) << __func__ << " for channel '"
828 << channel << "' to graylog host '"
829 << log_to_graylog_host[channel] << ":"
830 << log_to_graylog_port[channel]
831 << "'" << dendl;
832 }
833 return graylogs[channel];
834 }
835
836 void LogMonitor::handle_conf_change(const struct md_config_t *conf,
837 const std::set<std::string> &changed)
838 {
839 if (changed.count("mon_cluster_log_to_syslog") ||
840 changed.count("mon_cluster_log_to_syslog_level") ||
841 changed.count("mon_cluster_log_to_syslog_facility") ||
842 changed.count("mon_cluster_log_file") ||
843 changed.count("mon_cluster_log_file_level") ||
844 changed.count("mon_cluster_log_to_graylog") ||
845 changed.count("mon_cluster_log_to_graylog_host") ||
846 changed.count("mon_cluster_log_to_graylog_port")) {
847 update_log_channels();
848 }
849 }