1 #include "cetlib/PluginTypeDeducer.h"
2 #include "fhiclcpp/ParameterSet.h"
4 #include "messagefacility/MessageService/ELdestination.h"
5 #include "messagefacility/Utilities/ELseverityLevel.h"
6 #include "messagefacility/MessageLogger/MessageLogger.h"
7 #include "cetlib/compiler_macros.h"
8 #include "messagefacility/Utilities/exception.h"
11 #include <arpa/inet.h>
14 #include <netinet/in.h>
22 #define TRACE_NAME "UDP_mfPlugin"
26 #include <boost/algorithm/string.hpp>
30 using mf::service::ELdestination;
36 class ELUDP :
public ELdestination
49 fhicl::Name{
"error_turnoff_threshold"},
50 fhicl::Comment{
"Number of errors before turning off destination (default: 0, don't turn off)"}, 0};
52 fhicl::Atom<int>
error_report = fhicl::Atom<int>{fhicl::Name{
"error_report_backoff_factor"},
53 fhicl::Comment{
"Print an error message every N errors"}, 100};
55 fhicl::Atom<std::string>
host =
56 fhicl::Atom<std::string>{fhicl::Name{
"host"}, fhicl::Comment{
"Address to send messages to"},
"227.128.12.27"};
58 fhicl::Atom<int>
port = fhicl::Atom<int>{fhicl::Name{
"port"}, fhicl::Comment{
"Port to send messages to"}, 5140};
61 fhicl::Name{
"multicast_enabled"}, fhicl::Comment{
"Whether messages should be sent via multicast"},
false};
65 fhicl::Name{
"multicast_interface_ip"},
66 fhicl::Comment{
"Use this hostname for multicast output(to assign to the proper NIC)"},
"0.0.0.0"};
69 fhicl::Atom<std::string>{fhicl::Name{
"filename_delimit"},
70 fhicl::Comment{
"Grab path after this. \"/srcs/\" /x/srcs/y/z.cc => y/z.cc. NOTE: only works if full filename is given to this plugin (based on which mf::<method> is used)."},
"/"};
87 void fillPrefix(std::ostringstream& o,
const ErrorObj& msg)
override;
94 void fillUsrMsg(std::ostringstream& o,
const ErrorObj& msg)
override;
99 void fillSuffix(std::ostringstream& ,
const ErrorObj& )
override {}
106 void routePayload(
const std::ostringstream& o,
const ErrorObj& e)
override;
112 int error_report_backoff_factor_;
116 bool multicast_enabled_;
117 std::string multicast_out_addr_;
120 struct sockaddr_in message_addr_;
123 int consecutive_success_count_;
125 int next_error_report_;
129 std::string hostname_;
130 std::string hostaddr_;
132 std::string filename_delimit_;
144 : ELdestination(pset().elDestConfig()), error_report_backoff_factor_(pset().error_report()), error_max_(pset().error_max())
145 , host_(pset().host()), port_(pset().port()), multicast_enabled_(pset().multicast_enabled()), multicast_out_addr_(pset().output_address())
146 , message_socket_(-1), consecutive_success_count_(0), error_count_(0), next_error_report_(1), seqNum_(0), pid_(static_cast<int64_t>(getpid()))
147 , filename_delimit_(pset().filename_delimit())
150 char hostname_c[1024];
151 hostname_ = (gethostname(hostname_c, 1023) == 0) ? hostname_c :
"Unkonwn Host";
154 hostent* host =
nullptr;
155 host = gethostbyname(hostname_c);
160 char* ip = inet_ntoa(*reinterpret_cast<struct in_addr*>(host->h_addr));
166 struct ifaddrs* ifAddrStruct =
nullptr;
167 struct ifaddrs* ifa =
nullptr;
168 void* tmpAddrPtr =
nullptr;
170 if (getifaddrs(&ifAddrStruct) != 0)
173 hostaddr_ =
"127.0.0.1";
178 for (ifa = ifAddrStruct; ifa !=
nullptr; ifa = ifa->ifa_next)
180 if (ifa->ifa_addr->sa_family == AF_INET)
183 tmpAddrPtr = &(
reinterpret_cast<struct sockaddr_in*
>(ifa->ifa_addr)->sin_addr);
184 char addressBuffer[INET_ADDRSTRLEN];
185 inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
186 hostaddr_ = addressBuffer;
189 else if (ifa->ifa_addr->sa_family == AF_INET6)
192 tmpAddrPtr = &(
reinterpret_cast<struct sockaddr_in6*
>(ifa->ifa_addr)->sin6_addr);
193 char addressBuffer[INET6_ADDRSTRLEN];
194 inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
195 hostaddr_ = addressBuffer;
199 if (!hostaddr_.empty() && (hostaddr_ !=
"127.0.0.1") && (hostaddr_ !=
"::1"))
205 if (hostaddr_.empty())
207 hostaddr_ =
"127.0.0.1";
215 std::ostringstream pid_ostr;
216 pid_ostr <<
"/proc/" << pid_ <<
"/exe";
217 exe = realpath(pid_ostr.str().c_str(), NULL);
219 size_t end = exe.find(
'\0');
220 size_t start = exe.find_last_of(
'/', end);
222 app_ = exe.substr(start + 1, end - start - 1);
225 std::stringstream ss;
226 ss <<
"//proc//" << pid_ <<
"//cmdline";
227 std::ifstream procfile{ss.str().c_str()};
229 std::string procinfo;
231 if (procfile.is_open())
233 procfile >> procinfo;
237 size_t end = procinfo.find(
'\0');
238 size_t start = procinfo.find_last_of(
'/', end);
240 app_ = procinfo.substr(start + 1, end - start - 1);
244 void ELUDP::reconnect_()
246 static std::mutex mutex;
247 std::lock_guard<std::mutex> lk(mutex);
248 if (message_socket_ == -1)
250 message_socket_ = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
251 if (message_socket_ < 0)
253 TLOG(TLVL_ERROR) <<
"I failed to create the socket for sending Data messages! err=" << strerror(errno);
256 int sts =
ResolveHost(host_.c_str(), port_, message_addr_);
259 TLOG(TLVL_ERROR) <<
"Unable to resolve Data message address, err=" << strerror(errno);
263 if (multicast_out_addr_ ==
"0.0.0.0")
265 multicast_out_addr_.reserve(HOST_NAME_MAX);
266 sts = gethostname(&multicast_out_addr_[0], HOST_NAME_MAX);
269 TLOG(TLVL_ERROR) <<
"Could not get current hostname, err=" << strerror(errno);
274 if (multicast_out_addr_ !=
"localhost")
281 TLOG(TLVL_ERROR) <<
"Unable to resolve multicast interface address, err=" << strerror(errno);
285 if (setsockopt(message_socket_, IPPROTO_IP, IP_MULTICAST_IF, &addr,
sizeof(addr)) == -1)
287 TLOG(TLVL_ERROR) <<
"Cannot set outgoing interface, err=" << strerror(errno);
292 if (setsockopt(message_socket_, SOL_SOCKET, SO_REUSEADDR, &yes,
sizeof(yes)) < 0)
294 TLOG(TLVL_ERROR) <<
"Unable to enable port reuse on message socket, err=" << strerror(errno);
297 if (setsockopt(message_socket_, IPPROTO_IP, IP_MULTICAST_LOOP, &yes,
sizeof(yes)) < 0)
299 TLOG(TLVL_ERROR) <<
"Unable to enable multicast loopback on message socket, err=" << strerror(errno);
302 if (setsockopt(message_socket_, SOL_SOCKET, SO_BROADCAST, &yes,
sizeof(yes)) == -1)
304 TLOG(TLVL_ERROR) <<
"Cannot set message socket to broadcast, err=" << strerror(errno);
315 const auto& xid = msg.xid();
318 auto module = xid.module();
320 std::replace(
id.begin(),
id.end(),
'|',
'!');
321 std::replace(app.begin(), app.end(),
'|',
'!');
322 std::replace(module.begin(), module.end(),
'|',
'!');
324 oss << format_.timestamp(msg.timestamp()) <<
"|";
325 oss << std::to_string(++seqNum_) <<
"|";
326 oss << hostname_ <<
"|";
327 oss << hostaddr_ <<
"|";
328 oss << xid.severity().getName() <<
"|";
332 oss << mf::GetIteration() <<
"|";
334 oss << module <<
"|";
335 if (filename_delimit_.empty())
337 oss << msg.filename();
339 else if (filename_delimit_.size() == 1)
341 oss << (strrchr(&msg.filename()[0], filename_delimit_[0]) !=
nullptr
342 ? strrchr(&msg.filename()[0], filename_delimit_[0]) + 1
347 const char* cp = strstr(&msg.filename()[0], &filename_delimit_[0]);
351 cp += filename_delimit_.size() - 1;
352 while (*cp && *cp !=
'/') ++cp;
357 oss << msg.filename();
359 oss <<
"|" << std::to_string(msg.lineNumber()) <<
"|";
367 std::ostringstream tmposs;
369 for (
auto const& val : msg.items())
375 const std::string& usrMsg = tmposs.str().compare(0, 1,
"\n") == 0 ? tmposs.str().erase(0, 1) : tmposs.str();
385 if (message_socket_ == -1)
389 if (error_count_ < error_max_ || error_max_ == 0)
391 char str[INET_ADDRSTRLEN];
392 inet_ntop(AF_INET, &(message_addr_.sin_addr), str, INET_ADDRSTRLEN);
394 auto string =
"UDPMFMESSAGE" + std::to_string(pid_) +
"|" + oss.str();
395 auto sts = sendto(message_socket_,
string.c_str(),
string.size(), 0, reinterpret_cast<struct sockaddr*>(&message_addr_),
396 sizeof(message_addr_));
400 consecutive_success_count_ = 0;
402 if (error_count_ == next_error_report_)
404 TLOG(TLVL_ERROR) <<
"Error sending message " << seqNum_ <<
" to " << host_ <<
", errno=" << errno <<
" ("
405 << strerror(errno) <<
")";
406 next_error_report_ *= error_report_backoff_factor_;
411 ++consecutive_success_count_;
412 if (consecutive_success_count_ >= 5)
415 next_error_report_ = 1;
428 #ifndef EXTERN_C_FUNC_DECLARE_START
429 #define EXTERN_C_FUNC_DECLARE_START extern "C" {
432 EXTERN_C_FUNC_DECLARE_START
433 auto makePlugin(
const std::string& ,
const fhicl::ParameterSet& pset)
435 return std::make_unique<mfplugins::ELUDP>(pset);
439 DEFINE_BASIC_PLUGINTYPE_FUNC(mf::service::ELdestination)
ELUDP(Parameters const &pset)
ELUDP Constructor
int ResolveHost(char const *host_in, in_addr &addr)
Convert a string hostname to a in_addr suitable for socket communication.
Message Facility UDP Streamer Destination Formats messages into a delimited string and sends via UDP ...
void fillPrefix(std::ostringstream &o, const ErrorObj &msg) override
Fill the "Prefix" portion of the message.
int GetInterfaceForNetwork(char const *host_in, in_addr &addr)
Convert an IP address to the network address of the interface sharing the subnet mask.
fhicl::Atom< int > error_report
"error_report_backoff_factor" (Default: 100): Print an error message every N errors ...
fhicl::Atom< int > port
"port" (Default: 5140): Port to send messages to
fhicl::Atom< std::string > host
"host" (Default: "227.128.12.27"): Address to send messages to
fhicl::Atom< std::string > output_address
Configuration Parameters for ELUDP.
void routePayload(const std::ostringstream &o, const ErrorObj &e) override
Serialize a MessageFacility message to the output.
fhicl::WrappedTable< Config > Parameters
Used for ParameterSet validation.
fhicl::Atom< int > error_max
fhicl::TableFragment< ELdestination::Config > elDestConfig
ELDestination common config parameters.
void fillUsrMsg(std::ostringstream &o, const ErrorObj &msg) override
Fill the "User Message" portion of the message.
fhicl::Atom< std::string > filename_delimit
filename_delimit (Default: "/"): Grab path after this. "/srcs/" /x/srcs/y/z.cc => y/z...
void fillSuffix(std::ostringstream &, const ErrorObj &) override
Fill the "Suffix" portion of the message (Unused)
fhicl::Atom< bool > multicast_enabled
"multicast_enabled" (Default: false): Whether messages should be sent via multicast ...