2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2012-09-17 13:35:55 +02:00
|
|
|
|
2016-09-04 16:53:24 +02:00
|
|
|
#ifndef _WIN32
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/sysloglogger.hpp"
|
2018-01-18 13:50:38 +01:00
|
|
|
#include "base/sysloglogger-ti.cpp"
|
2015-08-15 20:28:05 +02:00
|
|
|
#include "base/configtype.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/statsfunction.hpp"
|
2015-03-28 11:04:42 +01:00
|
|
|
|
2012-07-10 13:31:17 +02:00
|
|
|
using namespace icinga;
|
|
|
|
|
2013-06-06 11:26:30 +02:00
|
|
|
REGISTER_TYPE(SyslogLogger);
|
|
|
|
|
2015-09-21 11:44:58 +02:00
|
|
|
REGISTER_STATSFUNCTION(SyslogLogger, &SyslogLogger::StatsFunc);
|
2014-02-17 16:34:18 +01:00
|
|
|
|
2017-09-19 16:42:32 +02:00
|
|
|
INITIALIZE_ONCE(&SyslogLogger::StaticInitialize);
|
|
|
|
|
|
|
|
std::map<String, int> SyslogLogger::m_FacilityMap;
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void SyslogLogger::StaticInitialize()
|
2017-09-19 16:42:32 +02:00
|
|
|
{
|
2018-08-07 13:55:41 +02:00
|
|
|
ScriptGlobal::Set("System.FacilityAuth", "LOG_AUTH", true);
|
|
|
|
ScriptGlobal::Set("System.FacilityAuthPriv", "LOG_AUTHPRIV", true);
|
|
|
|
ScriptGlobal::Set("System.FacilityCron", "LOG_CRON", true);
|
|
|
|
ScriptGlobal::Set("System.FacilityDaemon", "LOG_DAEMON", true);
|
|
|
|
ScriptGlobal::Set("System.FacilityFtp", "LOG_FTP", true);
|
|
|
|
ScriptGlobal::Set("System.FacilityKern", "LOG_KERN", true);
|
|
|
|
ScriptGlobal::Set("System.FacilityLocal0", "LOG_LOCAL0", true);
|
|
|
|
ScriptGlobal::Set("System.FacilityLocal1", "LOG_LOCAL1", true);
|
|
|
|
ScriptGlobal::Set("System.FacilityLocal2", "LOG_LOCAL2", true);
|
|
|
|
ScriptGlobal::Set("System.FacilityLocal3", "LOG_LOCAL3", true);
|
|
|
|
ScriptGlobal::Set("System.FacilityLocal4", "LOG_LOCAL4", true);
|
|
|
|
ScriptGlobal::Set("System.FacilityLocal5", "LOG_LOCAL5", true);
|
|
|
|
ScriptGlobal::Set("System.FacilityLocal6", "LOG_LOCAL6", true);
|
|
|
|
ScriptGlobal::Set("System.FacilityLocal7", "LOG_LOCAL7", true);
|
|
|
|
ScriptGlobal::Set("System.FacilityLpr", "LOG_LPR", true);
|
|
|
|
ScriptGlobal::Set("System.FacilityMail", "LOG_MAIL", true);
|
|
|
|
ScriptGlobal::Set("System.FacilityNews", "LOG_NEWS", true);
|
|
|
|
ScriptGlobal::Set("System.FacilitySyslog", "LOG_SYSLOG", true);
|
|
|
|
ScriptGlobal::Set("System.FacilityUser", "LOG_USER", true);
|
|
|
|
ScriptGlobal::Set("System.FacilityUucp", "LOG_UUCP", true);
|
2017-09-19 16:42:32 +02:00
|
|
|
|
|
|
|
m_FacilityMap["LOG_AUTH"] = LOG_AUTH;
|
|
|
|
m_FacilityMap["LOG_AUTHPRIV"] = LOG_AUTHPRIV;
|
|
|
|
m_FacilityMap["LOG_CRON"] = LOG_CRON;
|
|
|
|
m_FacilityMap["LOG_DAEMON"] = LOG_DAEMON;
|
|
|
|
m_FacilityMap["LOG_FTP"] = LOG_FTP;
|
|
|
|
m_FacilityMap["LOG_KERN"] = LOG_KERN;
|
|
|
|
m_FacilityMap["LOG_LOCAL0"] = LOG_LOCAL0;
|
|
|
|
m_FacilityMap["LOG_LOCAL1"] = LOG_LOCAL1;
|
|
|
|
m_FacilityMap["LOG_LOCAL2"] = LOG_LOCAL2;
|
|
|
|
m_FacilityMap["LOG_LOCAL3"] = LOG_LOCAL3;
|
|
|
|
m_FacilityMap["LOG_LOCAL4"] = LOG_LOCAL4;
|
|
|
|
m_FacilityMap["LOG_LOCAL5"] = LOG_LOCAL5;
|
|
|
|
m_FacilityMap["LOG_LOCAL6"] = LOG_LOCAL6;
|
|
|
|
m_FacilityMap["LOG_LOCAL7"] = LOG_LOCAL7;
|
|
|
|
m_FacilityMap["LOG_LPR"] = LOG_LPR;
|
|
|
|
m_FacilityMap["LOG_MAIL"] = LOG_MAIL;
|
|
|
|
m_FacilityMap["LOG_NEWS"] = LOG_NEWS;
|
|
|
|
m_FacilityMap["LOG_SYSLOG"] = LOG_SYSLOG;
|
|
|
|
m_FacilityMap["LOG_USER"] = LOG_USER;
|
|
|
|
m_FacilityMap["LOG_UUCP"] = LOG_UUCP;
|
|
|
|
}
|
|
|
|
|
2015-02-13 11:28:43 +01:00
|
|
|
void SyslogLogger::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr&)
|
2014-02-17 16:34:18 +01:00
|
|
|
{
|
2018-01-11 11:17:38 +01:00
|
|
|
DictionaryData nodes;
|
2014-02-18 10:53:44 +01:00
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const SyslogLogger::Ptr& sysloglogger : ConfigType::GetObjectsByType<SyslogLogger>()) {
|
2018-01-11 11:17:38 +01:00
|
|
|
nodes.emplace_back(sysloglogger->GetName(), 1); //add more stats
|
2014-02-18 10:53:44 +01:00
|
|
|
}
|
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
status->Set("sysloglogger", new Dictionary(std::move(nodes)));
|
2014-02-17 16:34:18 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void SyslogLogger::OnConfigLoaded()
|
2017-09-19 16:42:32 +02:00
|
|
|
{
|
|
|
|
ObjectImpl<SyslogLogger>::OnConfigLoaded();
|
|
|
|
|
|
|
|
String facilityString = GetFacility();
|
|
|
|
|
|
|
|
auto it = m_FacilityMap.find(facilityString);
|
|
|
|
|
|
|
|
if (it != m_FacilityMap.end())
|
|
|
|
m_Facility = it->second;
|
|
|
|
else
|
|
|
|
m_Facility = Convert::ToLong(facilityString);
|
|
|
|
}
|
|
|
|
|
2018-01-11 07:08:09 +01:00
|
|
|
void SyslogLogger::ValidateFacility(const Lazy<String>& lvalue, const ValidationUtils& utils)
|
2017-09-19 16:42:32 +02:00
|
|
|
{
|
2018-01-11 07:08:09 +01:00
|
|
|
ObjectImpl<SyslogLogger>::ValidateFacility(lvalue, utils);
|
2017-09-19 16:42:32 +02:00
|
|
|
|
2018-01-11 07:08:09 +01:00
|
|
|
if (m_FacilityMap.find(lvalue()) == m_FacilityMap.end()) {
|
2017-09-19 16:42:32 +02:00
|
|
|
try {
|
2018-01-11 07:08:09 +01:00
|
|
|
Convert::ToLong(lvalue());
|
2017-09-19 16:42:32 +02:00
|
|
|
} catch (const std::exception&) {
|
2017-11-30 18:09:38 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ValidationError(this, { "facility" }, "Invalid facility specified."));
|
2017-09-19 16:42:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-10 13:31:17 +02:00
|
|
|
/**
|
|
|
|
* Processes a log entry and outputs it to syslog.
|
|
|
|
*
|
|
|
|
* @param entry The log entry.
|
|
|
|
*/
|
|
|
|
void SyslogLogger::ProcessLogEntry(const LogEntry& entry)
|
|
|
|
{
|
|
|
|
int severity;
|
|
|
|
switch (entry.Severity) {
|
|
|
|
case LogDebug:
|
|
|
|
severity = LOG_DEBUG;
|
|
|
|
break;
|
2014-05-22 12:12:15 +02:00
|
|
|
case LogNotice:
|
|
|
|
severity = LOG_NOTICE;
|
|
|
|
break;
|
2012-07-10 13:31:17 +02:00
|
|
|
case LogWarning:
|
|
|
|
severity = LOG_WARNING;
|
|
|
|
break;
|
|
|
|
case LogCritical:
|
|
|
|
severity = LOG_CRIT;
|
|
|
|
break;
|
2013-03-12 18:53:44 +01:00
|
|
|
case LogInformation:
|
2012-07-10 13:31:17 +02:00
|
|
|
default:
|
2013-03-12 18:53:44 +01:00
|
|
|
severity = LOG_INFO;
|
|
|
|
break;
|
2012-07-10 13:31:17 +02:00
|
|
|
}
|
|
|
|
|
2017-09-19 16:42:32 +02:00
|
|
|
syslog(severity | m_Facility, "%s", entry.Message.CStr());
|
2012-07-10 13:31:17 +02:00
|
|
|
}
|
2014-08-07 08:34:38 +02:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void SyslogLogger::Flush()
|
2014-08-07 08:34:38 +02:00
|
|
|
{
|
|
|
|
/* Nothing to do here. */
|
|
|
|
}
|
2012-07-27 16:05:02 +02:00
|
|
|
#endif /* _WIN32 */
|