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