artdaq_mfextensions  v1_03_05
SMTP_mfPlugin.cc
1 #include "cetlib/PluginTypeDeducer.h"
2 #include "fhiclcpp/ParameterSet.h"
3 #include "fhiclcpp/types/ConfigurationTable.h"
4 #include "fhiclcpp/types/Sequence.h"
5 #include "fhiclcpp/types/TableFragment.h"
6 
7 #include "messagefacility/MessageService/ELdestination.h"
8 #include "messagefacility/Utilities/ELseverityLevel.h"
9 #if MESSAGEFACILITY_HEX_VERSION < 0x20201 // v2_02_01 is s67
10 #include "messagefacility/MessageService/MessageDrop.h"
11 #else
12 #include "messagefacility/MessageLogger/MessageLogger.h"
13 #endif
14 #include "messagefacility/Utilities/exception.h"
15 
16 // C/C++ includes
17 #include <arpa/inet.h>
18 #include <ifaddrs.h>
19 #include <netdb.h>
20 #include <netinet/in.h>
21 #include <algorithm>
22 #include <atomic>
23 #include <boost/thread.hpp>
24 #include <memory>
25 #include <mutex>
26 #include <random>
27 
28 #include <QtCore/QString>
29 #include "cetlib/compiler_macros.h"
31 
32 namespace mfplugins {
33 using mf::ELseverityLevel;
34 using mf::ErrorObj;
35 using mf::service::ELdestination;
36 
40 class ELSMTP : public ELdestination {
41  public:
45  struct Config {
47  using strings_t = fhicl::Sequence<std::string>::default_type;
49  fhicl::TableFragment<ELdestination::Config> elDestConfig;
51  fhicl::Atom<std::string> host = fhicl::Atom<std::string>{fhicl::Name{"host"}, fhicl::Comment{"SMTP Server hostname"}, "smtp.fnal.gov"};
53  fhicl::Atom<int> port = fhicl::Atom<int>{fhicl::Name{"port"}, fhicl::Comment{"SMTP Server port"}, 25};
55  fhicl::Sequence<std::string> to = fhicl::Sequence<std::string>{
56  fhicl::Name{"to_addresses"},
57  fhicl::Comment{"The list of email addresses that SMTP mfPlugin should sent to"},
58  strings_t{}};
60  fhicl::Atom<std::string> from =
61  fhicl::Atom<std::string>{fhicl::Name{"from_address"}, fhicl::Comment{"Source email address"}};
63  fhicl::Atom<std::string> subject = fhicl::Atom<std::string>{
64  fhicl::Name{"subject"}, fhicl::Comment{"Subject of the email message"},
65  "MessageFacility SMTP Message Digest"};
67  fhicl::Atom<std::string> messageHeader = fhicl::Atom<std::string>{
68  fhicl::Name{"message_header"},
69  fhicl::Comment{"String to preface messages with in email body"}, ""};
71  fhicl::Atom<bool> useSmtps =
72  fhicl::Atom<bool>{fhicl::Name{"use_smtps"}, fhicl::Comment{"Use SMTPS protocol"}, false};
74  fhicl::Atom<std::string> user =
75  fhicl::Atom<std::string>{fhicl::Name{"smtp_username"}, fhicl::Comment{"Username for SMTP server"}, ""};
77  fhicl::Atom<std::string> pw =
78  fhicl::Atom<std::string>{fhicl::Name{"smtp_password"}, fhicl::Comment{"Password for SMTP server"}, ""};
80  fhicl::Atom<bool> verifyCert =
81  fhicl::Atom<bool>{fhicl::Name{"verify_host_ssl_certificate"},
82  fhicl::Comment{"Whether to run full SSL verify on SMTP server in SMTPS mode"}, true};
84  fhicl::Atom<size_t> sendInterval = fhicl::Atom<size_t>{fhicl::Name{"email_send_interval_seconds"},
85  fhicl::Comment{"Only send email every N seconds"}, 15};
86  };
88  using Parameters = fhicl::WrappedTable<Config>;
89 
90  public:
95  ELSMTP(Parameters const& pset);
96 
97  ~ELSMTP() {
98  abort_sleep_ = true;
99  while (sending_thread_active_) usleep(1000);
100  }
101 
107  virtual void routePayload(const std::ostringstream& o, const ErrorObj& msg) override;
108 
109  private:
110  void send_message_();
111  std::string generateMessageId_() const;
112  std::string dateTimeNow_();
113  std::string to_html(std::string msgString, const ErrorObj& msg);
114 
115  std::string smtp_host_;
116  int port_;
117  std::vector<std::string> to_;
118  std::string from_;
119  std::string subject_;
120  std::string message_prefix_;
121 
122  // Message information
123  long pid_;
124  std::string hostname_;
125  std::string hostaddr_;
126  std::string app_;
127 
128  bool use_ssl_;
129  std::string username_;
130  std::string password_;
131  bool ssl_verify_host_cert_;
132 
133  std::atomic<bool> sending_thread_active_;
134  std::atomic<bool> abort_sleep_;
135  size_t send_interval_s_;
136  mutable std::mutex message_mutex_;
137  std::ostringstream message_contents_;
138 };
139 
140 // END DECLARATION
141 //======================================================================
142 // BEGIN IMPLEMENTATION
143 
144 //======================================================================
145 // ELSMTP c'tor
146 //======================================================================
148  : ELdestination(pset().elDestConfig()),
149  smtp_host_(pset().host()),
150  port_(pset().port()),
151  to_(pset().to()),
152  from_(pset().from()),
153  subject_(pset().subject()),
154  message_prefix_(pset().messageHeader()),
155  pid_(static_cast<long>(getpid())),
156  use_ssl_(pset().useSmtps()),
157  username_(pset().user()),
158  password_(pset().pw()),
159  ssl_verify_host_cert_(pset().verifyCert()),
160  sending_thread_active_(false),
161  abort_sleep_(false),
162  send_interval_s_(pset().sendInterval()) {
163  // hostname
164  char hostname_c[1024];
165  hostname_ = (gethostname(hostname_c, 1023) == 0) ? hostname_c : "Unkonwn Host";
166 
167  // host ip address
168  hostent* host = nullptr;
169  host = gethostbyname(hostname_c);
170 
171  if (host != nullptr) {
172  // ip address from hostname if the entry exists in /etc/hosts
173  char* ip = inet_ntoa(*(struct in_addr*)host->h_addr);
174  hostaddr_ = ip;
175  } else {
176  // enumerate all network interfaces
177  struct ifaddrs* ifAddrStruct = nullptr;
178  struct ifaddrs* ifa = nullptr;
179  void* tmpAddrPtr = nullptr;
180 
181  if (getifaddrs(&ifAddrStruct)) {
182  // failed to get addr struct
183  hostaddr_ = "127.0.0.1";
184  } else {
185  // iterate through all interfaces
186  for (ifa = ifAddrStruct; ifa != nullptr; ifa = ifa->ifa_next) {
187  if (ifa->ifa_addr->sa_family == AF_INET) {
188  // a valid IPv4 addres
189  tmpAddrPtr = &((struct sockaddr_in*)ifa->ifa_addr)->sin_addr;
190  char addressBuffer[INET_ADDRSTRLEN];
191  inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
192  hostaddr_ = addressBuffer;
193  }
194 
195  else if (ifa->ifa_addr->sa_family == AF_INET6) {
196  // a valid IPv6 address
197  tmpAddrPtr = &((struct sockaddr_in6*)ifa->ifa_addr)->sin6_addr;
198  char addressBuffer[INET6_ADDRSTRLEN];
199  inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
200  hostaddr_ = addressBuffer;
201  }
202 
203  // find first non-local address
204  if (!hostaddr_.empty() && hostaddr_.compare("127.0.0.1") && hostaddr_.compare("::1")) break;
205  }
206 
207  if (hostaddr_.empty()) // failed to find anything
208  hostaddr_ = "127.0.0.1";
209  }
210  }
211 
212  // get process name from '/proc/pid/exe'
213  std::string exe;
214  std::ostringstream pid_ostr;
215  pid_ostr << "/proc/" << pid_ << "/exe";
216  exe = realpath(pid_ostr.str().c_str(), NULL);
217 
218  size_t end = exe.find('\0');
219  size_t start = exe.find_last_of('/', end);
220 
221  app_ = exe.substr(start + 1, end - start - 1);
222 }
223 
224 std::string ELSMTP::to_html(std::string msgString, const ErrorObj& msg) {
225  auto sevid = msg.xid().severity().getLevel();
226 
227  QString text_ = QString("<font color=");
228 
229  switch (sevid) {
230  case mf::ELseverityLevel::ELsev_success:
231  case mf::ELseverityLevel::ELsev_zeroSeverity:
232  case mf::ELseverityLevel::ELsev_unspecified:
233  text_ += QString("#505050>");
234  break;
235 
236  case mf::ELseverityLevel::ELsev_info:
237  text_ += QString("#008000>");
238  break;
239 
240  case mf::ELseverityLevel::ELsev_warning:
241  text_ += QString("#E08000>");
242  break;
243 
244  case mf::ELseverityLevel::ELsev_error:
245  case mf::ELseverityLevel::ELsev_severe:
246  case mf::ELseverityLevel::ELsev_highestSeverity:
247  text_ += QString("#FF0000>");
248  break;
249 
250  default:
251  break;
252  }
253 
254  // std::cout << "qt_mf_msg.cc:" << msg.message() << std::endl;
255  text_ += QString("<pre>") + QString(msgString.c_str()).toHtmlEscaped() // + "<br>"
256  + QString("</pre>");
257 
258  text_ += QString("</font>");
259  return text_.toStdString();
260 }
261 
262 //======================================================================
263 // Message router ( overriddes ELdestination::routePayload )
264 //======================================================================
265 void ELSMTP::routePayload(const std::ostringstream& oss, const ErrorObj& msg) {
266  std::lock_guard<std::mutex> lk(message_mutex_);
267  message_contents_ << to_html(oss.str(), msg);
268 
269  if (!sending_thread_active_) {
270  sending_thread_active_ = true;
271  boost::thread t([=] { send_message_(); });
272  t.detach();
273  }
274 }
275 
276 void ELSMTP::send_message_() {
277  size_t slept = 0;
278  while (!abort_sleep_ && slept < send_interval_s_ * 1000000) {
279  usleep(10000);
280  slept += 10000;
281  }
282 
283  std::string payload;
284  {
285  std::lock_guard<std::mutex> lk(message_mutex_);
286  payload = message_contents_.str();
287  message_contents_.str("");
288  }
289  std::string destination = (use_ssl_ ? "smtps://" : "smtp://") + smtp_host_ + ":" + std::to_string(port_);
290 
291  std::vector<const char*> to;
292  to.reserve(to_.size());
293  std::string toString;
294  for (size_t i = 0; i < to_.size(); ++i) {
295  to.push_back(to_[i].c_str());
296  toString += to_[i];
297  if (i < to_.size() - 1) {
298  toString += ", ";
299  }
300  }
301 
302  std::ostringstream headers_builder;
303 
304  headers_builder << "Date: " << dateTimeNow_() << "\r\n";
305  headers_builder << "To: " << toString << "\r\n";
306  headers_builder << "From: " << from_ << "\r\n";
307  headers_builder << "Message-ID: <" + generateMessageId_() + "@" + from_.substr(from_.find('@') + 1) + ">\r\n";
308  headers_builder << "Subject: " << subject_ << " @ " << dateTimeNow_() << " from PID " << getpid() << "\r\n";
309  headers_builder << "Content-Type: text/html; charset=\"UTF-8\"\r\n";
310  headers_builder << "\r\n";
311 
312  std::string headers = headers_builder.str();
313  std::ostringstream message_builder;
314  message_builder << headers << "<html><body><p>" << message_prefix_ << "</p>" << payload << "</body></html>";
315  std::string payloadWithHeaders = message_builder.str();
316 
317  if (use_ssl_) {
318  send_message_ssl(destination.c_str(), &to[0], to_.size(), from_.c_str(), payloadWithHeaders.c_str(),
319  payloadWithHeaders.size(), username_.c_str(), password_.c_str(), !ssl_verify_host_cert_);
320  } else {
321  send_message(destination.c_str(), &to[0], to_.size(), from_.c_str(), payloadWithHeaders.c_str(),
322  payloadWithHeaders.size());
323  }
324  sending_thread_active_ = false;
325 }
326 
327 // https://codereview.stackexchange.com/questions/140409/sending-email-using-libcurl-follow-up/140562
328 std::string ELSMTP::generateMessageId_() const {
329  const size_t MESSAGE_ID_LEN = 37;
330  tm t;
331  time_t tt;
332  time(&tt);
333  gmtime_r(&tt, &t);
334 
335  std::string ret;
336  ret.resize(MESSAGE_ID_LEN);
337  size_t datelen = std::strftime(&ret[0], MESSAGE_ID_LEN, "%Y%m%d%H%M%S", &t);
338  static const std::string alphaNum{
339  "0123456789"
340  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
341  "abcdefghijklmnopqrstuvwxyz"};
342  std::mt19937 gen;
343  std::uniform_int_distribution<> dis(0, alphaNum.length() - 1);
344  std::generate_n(ret.begin() + datelen, MESSAGE_ID_LEN - datelen, [&]() { return alphaNum[dis(gen)]; });
345  return ret;
346 }
347 
348 std::string ELSMTP::dateTimeNow_() {
349  const int RFC5322_TIME_LEN = 32;
350 
351  std::string ret;
352  ret.resize(RFC5322_TIME_LEN);
353 
354  time_t tt;
355  tm tv, *t = &tv;
356  tt = time(&tt);
357  localtime_r(&tt, t);
358 
359  strftime(&ret[0], RFC5322_TIME_LEN, "%a, %d %b %Y %H:%M:%S %z", t);
360 
361  return ret;
362 }
363 } // end namespace mfplugins
364 
365 //======================================================================
366 //
367 // makePlugin function
368 //
369 //======================================================================
370 
371 #ifndef EXTERN_C_FUNC_DECLARE_START
372 #define EXTERN_C_FUNC_DECLARE_START extern "C" {
373 #endif
374 
375 EXTERN_C_FUNC_DECLARE_START
376 auto makePlugin(const std::string&, const fhicl::ParameterSet& pset) {
377  return std::make_unique<mfplugins::ELSMTP>(pset);
378 }
379 }
380 
381 DEFINE_BASIC_PLUGINTYPE_FUNC(mf::service::ELdestination)
fhicl::Atom< std::string > from
&quot;from_address&quot; (REQUIRED): Source email address
fhicl::Atom< std::string > user
&quot;smtp_username&quot; (Default: &quot;&quot;): Username for SMTP server
fhicl::Sequence< std::string > to
&quot;to_addresses&quot; (Default: {}): The list of email addresses that SMTP mfPlugin should sent to ...
fhicl::Atom< size_t > sendInterval
&quot;email_send_interval_seconds&quot; (Default: 15): Only send email every N seconds
fhicl::WrappedTable< Config > Parameters
Used for ParameterSet validation.
fhicl::Atom< std::string > host
&quot;host&quot; (Default: &quot;smtp.fnal.gov&quot;): SMTP Server hostname
void send_message_ssl(const char *dest, const char *to[], size_t to_size, const char *from, const char *payload, size_t payload_size, const char *username, const char *pw, int disableVerify)
Sends a message to the given SMTP server, using SSL encryption.
fhicl::Sequence< std::string >::default_type strings_t
Array of strings.
SMTP Message Facility destination plugin (Using libcurl)
fhicl::Atom< std::string > messageHeader
&quot;message_header&quot; (Default: &quot;&quot;): String to preface messages with in email body
void send_message(const char *dest, const char *to[], size_t to_size, const char *from, const char *payload, size_t payload_size)
Sends a message to the given SMTP server.
fhicl::Atom< std::string > subject
&quot;subject&quot; (Default: &quot;MessageFacility SMTP Message Digest&quot;): Subject of the email message ...
virtual void routePayload(const std::ostringstream &o, const ErrorObj &msg) override
Serialize a MessageFacility message to the output.
fhicl::TableFragment< ELdestination::Config > elDestConfig
ELDestination common config parameters.
fhicl::Atom< bool > verifyCert
&quot;verify_host_ssl_certificate&quot; (Default: true): Whether to run full SSL verify on SMTP server in SMTPS...
ELSMTP(Parameters const &pset)
ELSMTP Constructor
fhicl::Atom< std::string > pw
&quot;smtp_password&quot; (Default: &quot;&quot;): Password for SMTP server
fhicl::Atom< bool > useSmtps
&quot;use_smtps&quot; (Default: false): Use SMTPS protocol
Configuration parameters for ELSMTP.
fhicl::Atom< int > port
&quot;port&quot; (Default: 25): SMTP Server port