Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / common / Graylog.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include "Graylog.h"
5 #include "common/Formatter.h"
6 #include "common/LogEntry.h"
7 #include "log/Entry.h"
8 #include "log/SubsystemMap.h"
9
10 namespace ceph {
11 namespace logging {
12
13 Graylog::Graylog(const SubsystemMap * const s, std::string logger)
14     : m_subs(s),
15       m_log_dst_valid(false),
16       m_hostname(""),
17       m_fsid(""),
18       m_logger(std::move(logger)),
19       m_ostream_compressed(std::stringstream::in |
20                            std::stringstream::out |
21                            std::stringstream::binary)
22 {
23   m_formatter = std::unique_ptr<Formatter>(Formatter::create("json"));
24   m_formatter_section = std::unique_ptr<Formatter>(Formatter::create("json"));
25 }
26
27 Graylog::Graylog(std::string logger)
28     : m_subs(NULL),
29       m_log_dst_valid(false),
30       m_hostname(""),
31       m_fsid(""),
32       m_logger(std::move(logger)),
33       m_ostream_compressed(std::stringstream::in |
34                            std::stringstream::out |
35                            std::stringstream::binary)
36 {
37   m_formatter = std::unique_ptr<Formatter>(Formatter::create("json"));
38   m_formatter_section = std::unique_ptr<Formatter>(Formatter::create("json"));
39 }
40
41 Graylog::~Graylog()
42 {
43 }
44
45 void Graylog::set_destination(const std::string& host, int port)
46 {
47   try {
48     boost::asio::ip::udp::resolver resolver(m_io_service);
49     boost::asio::ip::udp::resolver::query query(host, std::to_string(port));
50     m_endpoint = *resolver.resolve(query);
51     m_log_dst_valid = true;
52   } catch (boost::system::system_error const& e) {
53     cerr << "Error resolving graylog destination: " << e.what() << std::endl;
54     m_log_dst_valid = false;
55   }
56 }
57
58 void Graylog::set_hostname(const std::string& host)
59 {
60   m_hostname = host;
61 }
62
63 void Graylog::set_fsid(const uuid_d& fsid)
64 {
65   std::vector<char> buf(40);
66   fsid.print(&buf[0]);
67   m_fsid = std::string(&buf[0]);
68 }
69
70 void Graylog::log_entry(Entry const * const e)
71 {
72   if (m_log_dst_valid) {
73     std::string s = e->get_str();
74
75     m_formatter->open_object_section("");
76     m_formatter->dump_string("version", "1.1");
77     m_formatter->dump_string("host", m_hostname);
78     m_formatter->dump_string("short_message", s);
79     m_formatter->dump_string("_app", "ceph");
80     m_formatter->dump_float("timestamp", e->m_stamp.sec() + (e->m_stamp.usec() / 1000000.0));
81     m_formatter->dump_unsigned("_thread", (uint64_t)e->m_thread);
82     m_formatter->dump_int("_level", e->m_prio);
83     if (m_subs != NULL)
84     m_formatter->dump_string("_subsys_name", m_subs->get_name(e->m_subsys));
85     m_formatter->dump_int("_subsys_id", e->m_subsys);
86     m_formatter->dump_string("_fsid", m_fsid);
87     m_formatter->dump_string("_logger", m_logger);
88     m_formatter->close_section();
89
90     m_ostream_compressed.clear();
91     m_ostream_compressed.str("");
92
93     m_ostream.reset();
94
95     m_ostream.push(m_compressor);
96     m_ostream.push(m_ostream_compressed);
97
98     m_formatter->flush(m_ostream);
99     m_ostream << std::endl;
100
101     m_ostream.reset();
102
103     try {
104       boost::asio::ip::udp::socket socket(m_io_service);
105       socket.open(m_endpoint.protocol());
106       socket.send_to(boost::asio::buffer(m_ostream_compressed.str()), m_endpoint);
107     } catch (boost::system::system_error const& e) {
108       cerr << "Error sending graylog message: " << e.what() << std::endl;
109     }
110   }
111 }
112
113 void Graylog::log_log_entry(LogEntry const * const e)
114 {
115   if (m_log_dst_valid) {
116     m_formatter->open_object_section("");
117     m_formatter->dump_string("version", "1.1");
118     m_formatter->dump_string("host", m_hostname);
119     m_formatter->dump_string("short_message", e->msg);
120     m_formatter->dump_float("timestamp", e->stamp.sec() + (e->stamp.usec() / 1000000.0));
121     m_formatter->dump_string("_app", "ceph");
122
123     m_formatter_section->open_object_section("");
124     e->who.addr.dump(m_formatter_section.get());
125     e->who.name.dump(m_formatter_section.get());
126     m_formatter_section->close_section();
127
128     m_ostream_section.clear();
129     m_ostream_section.str("");
130     m_formatter_section->flush(m_ostream_section);
131     m_formatter->dump_string("_who", m_ostream_section.str());
132
133     m_formatter->dump_int("_seq", e->seq);
134     m_formatter->dump_string("_prio", clog_type_to_string(e->prio));
135     m_formatter->dump_string("_channel", e->channel);
136     m_formatter->dump_string("_fsid", m_fsid);
137     m_formatter->dump_string("_logger", m_logger);
138     m_formatter->close_section();
139
140     m_ostream_compressed.clear();
141     m_ostream_compressed.str("");
142
143     m_ostream.reset();
144
145     m_ostream.push(m_compressor);
146     m_ostream.push(m_ostream_compressed);
147
148     m_formatter->flush(m_ostream);
149     m_ostream << std::endl;
150
151     m_ostream.reset();
152
153     try {
154       boost::asio::ip::udp::socket socket(m_io_service);
155       socket.open(m_endpoint.protocol());
156       socket.send_to(boost::asio::buffer(m_ostream_compressed.str()), m_endpoint);
157     } catch (boost::system::system_error const& e) {
158       cerr << "Error sending graylog message: " << e.what() << std::endl;
159     }
160   }
161 }
162
163 } // ceph::logging::
164 } // ceph::