2013-03-19 13:04:30 +01:00
/******************************************************************************
* Icinga 2 *
2018-01-02 12:06:00 +01:00
* Copyright ( C ) 2012 - 2018 Icinga Development Team ( https : //www.icinga.com/) *
2013-03-19 13:04:30 +01:00
* *
* This program is free software ; you can redistribute it and / or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation ; either version 2 *
* of the License , or ( at your option ) any later version . *
* *
* This program is distributed in the hope that it will be useful , *
* but WITHOUT ANY WARRANTY ; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the *
* GNU General Public License for more details . *
* *
* You should have received a copy of the GNU General Public License *
* along with this program ; if not , write to the Free Software Foundation *
* Inc . , 51 Franklin St , Fifth Floor , Boston , MA 02110 - 1301 , USA . *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2014-05-25 16:23:35 +02:00
# include "compat/compatlogger.hpp"
2018-01-18 13:50:38 +01:00
# include "compat/compatlogger-ti.cpp"
2014-05-25 16:23:35 +02:00
# include "icinga/service.hpp"
# include "icinga/checkcommand.hpp"
# include "icinga/eventcommand.hpp"
# include "icinga/notification.hpp"
# include "icinga/macroprocessor.hpp"
# include "icinga/externalcommandprocessor.hpp"
# include "icinga/compatutility.hpp"
2015-08-15 20:28:05 +02:00
# include "base/configtype.hpp"
2014-05-25 16:23:35 +02:00
# include "base/objectlock.hpp"
2014-10-19 14:21:12 +02:00
# include "base/logger.hpp"
2014-05-25 16:23:35 +02:00
# include "base/exception.hpp"
# include "base/convert.hpp"
# include "base/application.hpp"
# include "base/utility.hpp"
# include "base/statsfunction.hpp"
2013-07-02 13:38:06 +02:00
# include <boost/algorithm/string.hpp>
2013-03-19 13:04:30 +01:00
using namespace icinga ;
2013-09-25 10:55:50 +02:00
REGISTER_TYPE ( CompatLogger ) ;
2013-03-19 13:04:30 +01:00
2015-09-21 11:44:58 +02:00
REGISTER_STATSFUNCTION ( CompatLogger , & CompatLogger : : StatsFunc ) ;
2014-02-17 16:34:18 +01:00
2015-02-07 22:36:17 +01:00
void CompatLogger : : 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 CompatLogger : : Ptr & compat_logger : ConfigType : : GetObjectsByType < CompatLogger > ( ) ) {
2018-01-11 11:17:38 +01:00
nodes . emplace_back ( compat_logger - > GetName ( ) , 1 ) ; // add more stats
2014-02-18 10:53:44 +01:00
}
2018-01-11 11:17:38 +01:00
status - > Set ( " compatlogger " , new Dictionary ( std : : move ( nodes ) ) ) ;
2014-02-17 16:34:18 +01:00
}
2013-03-19 13:04:30 +01:00
/**
* @ threadsafety Always .
*/
2015-08-20 17:18:48 +02:00
void CompatLogger : : Start ( bool runtimeCreated )
2013-03-19 13:04:30 +01:00
{
2015-08-20 17:18:48 +02:00
ObjectImpl < CompatLogger > : : Start ( runtimeCreated ) ;
2013-09-09 10:06:49 +02:00
2017-02-08 14:53:52 +01:00
Log ( LogInformation , " CompatLogger " )
2017-12-19 15:50:05 +01:00
< < " ' " < < GetName ( ) < < " ' started. " ;
2017-02-08 14:53:52 +01:00
2017-11-21 11:52:55 +01:00
Checkable : : OnNewCheckResult . connect ( std : : bind ( & CompatLogger : : CheckResultHandler , this , _1 , _2 ) ) ;
Checkable : : OnNotificationSentToUser . connect ( std : : bind ( & CompatLogger : : NotificationSentHandler , this , _1 , _2 , _3 , _4 , _5 , _6 , _7 , _8 ) ) ;
Downtime : : OnDowntimeTriggered . connect ( std : : bind ( & CompatLogger : : TriggerDowntimeHandler , this , _1 ) ) ;
Downtime : : OnDowntimeRemoved . connect ( std : : bind ( & CompatLogger : : RemoveDowntimeHandler , this , _1 ) ) ;
Checkable : : OnEventCommandExecuted . connect ( std : : bind ( & CompatLogger : : EventCommandHandler , this , _1 ) ) ;
2017-12-13 12:54:14 +01:00
2017-11-21 11:52:55 +01:00
Checkable : : OnFlappingChanged . connect ( std : : bind ( & CompatLogger : : FlappingChangedHandler , this , _1 ) ) ;
Checkable : : OnEnableFlappingChanged . connect ( std : : bind ( & CompatLogger : : EnableFlappingChangedHandler , this , _1 ) ) ;
2017-12-13 12:54:14 +01:00
2017-11-21 11:52:55 +01:00
ExternalCommandProcessor : : OnNewExternalCommand . connect ( std : : bind ( & CompatLogger : : ExternalCommandHandler , this , _2 , _3 ) ) ;
2013-06-28 16:08:43 +02:00
2014-11-08 21:17:16 +01:00
m_RotationTimer = new Timer ( ) ;
2017-11-21 11:52:55 +01:00
m_RotationTimer - > OnTimerExpired . connect ( std : : bind ( & CompatLogger : : RotationTimerHandler , this ) ) ;
2013-03-19 13:04:30 +01:00
m_RotationTimer - > Start ( ) ;
2013-03-20 11:11:46 +01:00
ReopenFile ( false ) ;
ScheduleNextRotation ( ) ;
2013-03-19 13:04:30 +01:00
}
2017-02-08 14:53:52 +01:00
/**
* @ threadsafety Always .
*/
void CompatLogger : : Stop ( bool runtimeRemoved )
{
Log ( LogInformation , " CompatLogger " )
2017-12-19 15:50:05 +01:00
< < " ' " < < GetName ( ) < < " ' stopped. " ;
2017-02-08 14:53:52 +01:00
ObjectImpl < CompatLogger > : : Stop ( runtimeRemoved ) ;
}
2013-03-19 13:04:30 +01:00
/**
* @ threadsafety Always .
*/
2014-04-03 15:36:13 +02:00
void CompatLogger : : CheckResultHandler ( const Checkable : : Ptr & checkable , const CheckResult : : Ptr & cr )
2013-03-19 13:04:30 +01:00
{
2014-04-03 15:36:13 +02:00
Host : : Ptr host ;
Service : : Ptr service ;
2014-04-07 21:19:07 +02:00
tie ( host , service ) = GetHostService ( checkable ) ;
2013-03-19 13:04:30 +01:00
2013-11-09 14:22:38 +01:00
Dictionary : : Ptr vars_after = cr - > GetVarsAfter ( ) ;
2013-03-19 13:04:30 +01:00
long state_after = vars_after - > Get ( " state " ) ;
long stateType_after = vars_after - > Get ( " state_type " ) ;
long attempt_after = vars_after - > Get ( " attempt " ) ;
bool reachable_after = vars_after - > Get ( " reachable " ) ;
2013-11-09 14:22:38 +01:00
Dictionary : : Ptr vars_before = cr - > GetVarsBefore ( ) ;
2013-03-19 13:04:30 +01:00
if ( vars_before ) {
long state_before = vars_before - > Get ( " state " ) ;
long stateType_before = vars_before - > Get ( " state_type " ) ;
long attempt_before = vars_before - > Get ( " attempt " ) ;
bool reachable_before = vars_before - > Get ( " reachable " ) ;
if ( state_before = = state_after & & stateType_before = = stateType_after & &
2017-12-19 15:50:05 +01:00
attempt_before = = attempt_after & & reachable_before = = reachable_after )
2013-03-19 13:04:30 +01:00
return ; /* Nothing changed, ignore this checkresult. */
}
2013-10-31 14:18:20 +01:00
String output ;
2013-12-05 14:54:16 +01:00
if ( cr )
output = CompatUtility : : GetCheckResultOutput ( cr ) ;
2013-06-26 18:23:59 +02:00
2013-03-19 13:04:30 +01:00
std : : ostringstream msgbuf ;
2014-04-07 21:19:07 +02:00
if ( service ) {
2014-04-03 15:36:13 +02:00
msgbuf < < " SERVICE ALERT: "
2017-12-19 15:50:05 +01:00
< < host - > GetName ( ) < < " ; "
< < service - > GetShortName ( ) < < " ; "
< < Service : : StateToString ( service - > GetState ( ) ) < < " ; "
< < Service : : StateTypeToString ( service - > GetStateType ( ) ) < < " ; "
< < attempt_after < < " ; "
< < output < < " "
< < " " ;
2014-04-03 15:36:13 +02:00
} else {
2014-04-08 08:54:49 +02:00
String state = Host : : StateToString ( Host : : CalculateState ( static_cast < ServiceState > ( state_after ) ) ) ;
2013-03-19 13:04:30 +01:00
msgbuf < < " HOST ALERT: "
2017-12-19 15:50:05 +01:00
< < host - > GetName ( ) < < " ; "
2017-12-21 09:24:04 +01:00
< < GetHostStateString ( host ) < < " ; "
2017-12-19 15:50:05 +01:00
< < Host : : StateTypeToString ( host - > GetStateType ( ) ) < < " ; "
< < attempt_after < < " ; "
< < output < < " "
< < " " ;
2013-03-19 13:04:30 +01:00
}
2013-06-24 08:56:13 +02:00
{
ObjectLock olock ( this ) ;
2014-04-03 15:36:13 +02:00
WriteLine ( msgbuf . str ( ) ) ;
2013-06-24 08:56:13 +02:00
Flush ( ) ;
}
2013-03-19 13:04:30 +01:00
}
2013-09-17 19:40:23 +02:00
/**
* @ threadsafety Always .
*/
2015-08-20 17:18:48 +02:00
void CompatLogger : : TriggerDowntimeHandler ( const Downtime : : Ptr & downtime )
2013-09-17 19:40:23 +02:00
{
2014-04-03 15:36:13 +02:00
Host : : Ptr host ;
Service : : Ptr service ;
2015-08-20 17:18:48 +02:00
tie ( host , service ) = GetHostService ( downtime - > GetCheckable ( ) ) ;
2013-09-17 19:40:23 +02:00
if ( ! downtime )
return ;
std : : ostringstream msgbuf ;
2014-04-07 21:19:07 +02:00
if ( service ) {
2014-04-03 15:36:13 +02:00
msgbuf < < " SERVICE DOWNTIME ALERT: "
< < host - > GetName ( ) < < " ; "
< < service - > GetShortName ( ) < < " ; "
< < " STARTED " < < " ; "
< < " Checkable has entered a period of scheduled downtime. "
< < " " ;
} else {
2013-09-17 19:40:23 +02:00
msgbuf < < " HOST DOWNTIME ALERT: "
< < host - > GetName ( ) < < " ; "
2013-09-18 10:08:28 +02:00
< < " STARTED " < < " ; "
2014-04-03 15:36:13 +02:00
< < " Checkable has entered a period of scheduled downtime. "
2013-09-17 19:40:23 +02:00
< < " " ;
}
{
ObjectLock oLock ( this ) ;
2014-04-03 15:36:13 +02:00
WriteLine ( msgbuf . str ( ) ) ;
2013-09-17 19:40:23 +02:00
Flush ( ) ;
}
}
/**
* @ threadsafety Always .
*/
2015-08-20 17:18:48 +02:00
void CompatLogger : : RemoveDowntimeHandler ( const Downtime : : Ptr & downtime )
2013-09-17 19:40:23 +02:00
{
2014-04-03 15:36:13 +02:00
Host : : Ptr host ;
Service : : Ptr service ;
2015-08-20 17:18:48 +02:00
tie ( host , service ) = GetHostService ( downtime - > GetCheckable ( ) ) ;
2013-09-17 19:40:23 +02:00
if ( ! downtime )
return ;
String downtime_output ;
String downtime_state_str ;
2013-11-09 22:08:26 +01:00
if ( downtime - > GetWasCancelled ( ) ) {
2013-09-17 19:40:23 +02:00
downtime_output = " Scheduled downtime for service has been cancelled. " ;
downtime_state_str = " CANCELLED " ;
} else {
2014-04-03 15:36:13 +02:00
downtime_output = " Checkable has exited from a period of scheduled downtime. " ;
2013-09-17 19:40:23 +02:00
downtime_state_str = " STOPPED " ;
}
std : : ostringstream msgbuf ;
2014-04-07 21:19:07 +02:00
if ( service ) {
2014-04-03 15:36:13 +02:00
msgbuf < < " SERVICE DOWNTIME ALERT: "
< < host - > GetName ( ) < < " ; "
< < service - > GetShortName ( ) < < " ; "
< < downtime_state_str < < " ; "
< < downtime_output
< < " " ;
} else {
2013-09-17 19:40:23 +02:00
msgbuf < < " HOST DOWNTIME ALERT: "
< < host - > GetName ( ) < < " ; "
< < downtime_state_str < < " ; "
< < downtime_output
< < " " ;
}
{
ObjectLock oLock ( this ) ;
2014-04-03 15:36:13 +02:00
WriteLine ( msgbuf . str ( ) ) ;
2013-09-17 19:40:23 +02:00
Flush ( ) ;
}
}
2013-06-28 13:40:01 +02:00
2013-06-28 21:31:38 +02:00
/**
* @ threadsafety Always .
*/
2014-04-03 15:36:13 +02:00
void CompatLogger : : NotificationSentHandler ( const Notification : : Ptr & notification , const Checkable : : Ptr & checkable ,
2017-12-19 15:50:05 +01:00
const User : : Ptr & user , NotificationType notification_type , CheckResult : : Ptr const & cr ,
const String & author , const String & comment_text , const String & command_name )
2013-06-28 21:31:38 +02:00
{
2014-04-03 15:36:13 +02:00
Host : : Ptr host ;
Service : : Ptr service ;
2014-04-07 21:19:07 +02:00
tie ( host , service ) = GetHostService ( checkable ) ;
2013-06-28 21:31:38 +02:00
String notification_type_str = Notification : : NotificationTypeToString ( notification_type ) ;
2014-02-04 10:54:38 +01:00
/* override problem notifications with their current state string */
2014-04-03 15:36:13 +02:00
if ( notification_type = = NotificationProblem ) {
2014-04-07 21:19:07 +02:00
if ( service )
2014-04-03 15:36:13 +02:00
notification_type_str = Service : : StateToString ( service - > GetState ( ) ) ;
else
2017-12-21 09:24:04 +01:00
notification_type_str = GetHostStateString ( host ) ;
2014-04-03 15:36:13 +02:00
}
2014-02-04 10:54:38 +01:00
2013-06-28 21:31:38 +02:00
String author_comment = " " ;
if ( notification_type = = NotificationCustom | | notification_type = = NotificationAcknowledgement ) {
2013-10-29 13:44:43 +01:00
author_comment = author + " ; " + comment_text ;
2013-06-28 21:31:38 +02:00
}
2014-08-17 17:57:20 +02:00
if ( ! cr )
return ;
2013-06-28 21:31:38 +02:00
String output ;
2013-12-05 14:54:16 +01:00
if ( cr )
output = CompatUtility : : GetCheckResultOutput ( cr ) ;
2013-06-28 21:31:38 +02:00
2014-08-17 17:57:20 +02:00
std : : ostringstream msgbuf ;
2013-06-28 21:31:38 +02:00
2014-04-07 21:19:07 +02:00
if ( service ) {
2014-04-03 15:36:13 +02:00
msgbuf < < " SERVICE NOTIFICATION: "
< < user - > GetName ( ) < < " ; "
< < host - > GetName ( ) < < " ; "
< < service - > GetShortName ( ) < < " ; "
< < notification_type_str < < " ; "
< < command_name < < " ; "
< < output < < " ; "
< < author_comment
< < " " ;
} else {
2014-08-17 17:57:20 +02:00
msgbuf < < " HOST NOTIFICATION: "
2013-08-20 11:06:04 +02:00
< < user - > GetName ( ) < < " ; "
2014-08-17 17:57:20 +02:00
< < host - > GetName ( ) < < " ; "
< < notification_type_str < < " "
2017-12-21 09:24:04 +01:00
< < " ( " < < GetHostStateString ( host ) < < " ); "
2013-10-29 13:44:43 +01:00
< < command_name < < " ; "
< < output < < " ; "
< < author_comment
2014-08-17 17:57:20 +02:00
< < " " ;
2014-04-03 15:36:13 +02:00
}
2013-06-28 21:31:38 +02:00
2014-04-03 15:36:13 +02:00
{
ObjectLock oLock ( this ) ;
WriteLine ( msgbuf . str ( ) ) ;
2014-08-17 17:57:20 +02:00
Flush ( ) ;
}
2013-06-28 21:31:38 +02:00
}
2013-07-01 14:29:07 +02:00
/**
* @ threadsafety Always .
*/
2015-08-04 14:47:44 +02:00
void CompatLogger : : FlappingChangedHandler ( const Checkable : : Ptr & checkable )
2013-07-01 14:29:07 +02:00
{
2014-04-03 15:36:13 +02:00
Host : : Ptr host ;
Service : : Ptr service ;
2014-04-07 21:19:07 +02:00
tie ( host , service ) = GetHostService ( checkable ) ;
2013-07-01 14:29:07 +02:00
String flapping_state_str ;
String flapping_output ;
2017-12-13 12:54:14 +01:00
2015-08-04 14:47:44 +02:00
if ( checkable - > IsFlapping ( ) ) {
2017-10-19 17:32:52 +02:00
flapping_output = " Checkable appears to have started flapping ( " + Convert : : ToString ( checkable - > GetFlappingCurrent ( ) ) + " % change >= " + Convert : : ToString ( checkable - > GetFlappingThresholdHigh ( ) ) + " % threshold) " ;
2015-08-04 14:47:44 +02:00
flapping_state_str = " STARTED " ;
} else {
2017-10-19 17:32:52 +02:00
flapping_output = " Checkable appears to have stopped flapping ( " + Convert : : ToString ( checkable - > GetFlappingCurrent ( ) ) + " % change < " + Convert : : ToString ( checkable - > GetFlappingThresholdLow ( ) ) + " % threshold) " ;
2015-08-04 14:47:44 +02:00
flapping_state_str = " STOPPED " ;
}
std : : ostringstream msgbuf ;
2013-07-01 14:29:07 +02:00
2015-08-04 14:47:44 +02:00
if ( service ) {
msgbuf < < " SERVICE FLAPPING ALERT: "
< < host - > GetName ( ) < < " ; "
< < service - > GetShortName ( ) < < " ; "
< < flapping_state_str < < " ; "
< < flapping_output
< < " " ;
} else {
msgbuf < < " HOST FLAPPING ALERT: "
< < host - > GetName ( ) < < " ; "
< < flapping_state_str < < " ; "
< < flapping_output
< < " " ;
}
{
ObjectLock oLock ( this ) ;
WriteLine ( msgbuf . str ( ) ) ;
Flush ( ) ;
2013-07-01 14:29:07 +02:00
}
2015-08-04 14:47:44 +02:00
}
void CompatLogger : : EnableFlappingChangedHandler ( const Checkable : : Ptr & checkable )
{
Host : : Ptr host ;
Service : : Ptr service ;
tie ( host , service ) = GetHostService ( checkable ) ;
if ( checkable - > GetEnableFlapping ( ) )
return ;
2017-12-13 12:54:14 +01:00
2015-08-04 14:47:44 +02:00
String flapping_output = " Flap detection has been disabled " ;
String flapping_state_str = " DISABLED " ;
2013-07-01 14:29:07 +02:00
2014-08-17 17:57:20 +02:00
std : : ostringstream msgbuf ;
2013-07-01 14:29:07 +02:00
2014-04-07 21:19:07 +02:00
if ( service ) {
2014-04-03 15:36:13 +02:00
msgbuf < < " SERVICE FLAPPING ALERT: "
< < host - > GetName ( ) < < " ; "
< < service - > GetShortName ( ) < < " ; "
< < flapping_state_str < < " ; "
< < flapping_output
< < " " ;
} else {
2014-08-17 17:57:20 +02:00
msgbuf < < " HOST FLAPPING ALERT: "
< < host - > GetName ( ) < < " ; "
< < flapping_state_str < < " ; "
< < flapping_output
< < " " ;
2014-04-03 15:36:13 +02:00
}
2013-07-01 14:29:07 +02:00
2014-04-03 15:36:13 +02:00
{
ObjectLock oLock ( this ) ;
WriteLine ( msgbuf . str ( ) ) ;
2014-08-17 17:57:20 +02:00
Flush ( ) ;
}
2013-07-01 14:29:07 +02:00
}
2013-06-28 21:31:38 +02:00
2013-09-30 20:34:55 +02:00
void CompatLogger : : ExternalCommandHandler ( const String & command , const std : : vector < String > & arguments )
{
2014-08-17 17:57:20 +02:00
std : : ostringstream msgbuf ;
msgbuf < < " EXTERNAL COMMAND: "
< < command < < " ; "
< < boost : : algorithm : : join ( arguments , " ; " )
< < " " ;
{
ObjectLock oLock ( this ) ;
WriteLine ( msgbuf . str ( ) ) ;
2014-12-08 09:55:40 +01:00
Flush ( ) ;
2014-08-17 17:57:20 +02:00
}
2013-09-30 20:34:55 +02:00
}
2014-04-03 15:36:13 +02:00
void CompatLogger : : EventCommandHandler ( const Checkable : : Ptr & checkable )
2013-11-05 18:33:57 +01:00
{
2014-04-03 15:36:13 +02:00
Host : : Ptr host ;
Service : : Ptr service ;
2014-04-07 21:19:07 +02:00
tie ( host , service ) = GetHostService ( checkable ) ;
2014-04-03 15:36:13 +02:00
2014-04-07 21:19:07 +02:00
EventCommand : : Ptr event_command = checkable - > GetEventCommand ( ) ;
2013-11-05 18:33:57 +01:00
String event_command_name = event_command - > GetName ( ) ;
2014-04-07 21:19:07 +02:00
long current_attempt = checkable - > GetCheckAttempt ( ) ;
2013-11-05 18:33:57 +01:00
2014-08-17 17:57:20 +02:00
std : : ostringstream msgbuf ;
2013-11-05 18:33:57 +01:00
2014-04-07 21:19:07 +02:00
if ( service ) {
2014-04-03 15:36:13 +02:00
msgbuf < < " SERVICE EVENT HANDLER: "
< < host - > GetName ( ) < < " ; "
< < service - > GetShortName ( ) < < " ; "
< < Service : : StateToString ( service - > GetState ( ) ) < < " ; "
< < Service : : StateTypeToString ( service - > GetStateType ( ) ) < < " ; "
< < current_attempt < < " ; "
< < event_command_name ;
} else {
msgbuf < < " HOST EVENT HANDLER: "
2014-08-17 17:57:20 +02:00
< < host - > GetName ( ) < < " ; "
2017-12-21 09:24:04 +01:00
< < GetHostStateString ( host ) < < " ; "
2014-04-03 15:36:13 +02:00
< < Host : : StateTypeToString ( host - > GetStateType ( ) ) < < " ; "
2013-11-05 18:33:57 +01:00
< < current_attempt < < " ; "
< < event_command_name ;
2014-04-03 15:36:13 +02:00
}
2013-11-05 18:33:57 +01:00
2014-04-03 15:36:13 +02:00
{
ObjectLock oLock ( this ) ;
WriteLine ( msgbuf . str ( ) ) ;
2014-08-17 17:57:20 +02:00
Flush ( ) ;
}
2013-11-05 18:33:57 +01:00
}
2017-12-21 09:24:04 +01:00
String CompatLogger : : GetHostStateString ( const Host : : Ptr & host )
{
if ( host - > GetState ( ) ! = HostUp & & ! host - > IsReachable ( ) )
return " UNREACHABLE " ; /* hardcoded compat state */
return Host : : StateToString ( host - > GetState ( ) ) ;
}
2013-09-25 10:55:50 +02:00
void CompatLogger : : WriteLine ( const String & line )
2013-03-19 13:04:30 +01:00
{
ASSERT ( OwnsLock ( ) ) ;
if ( ! m_OutputFile . good ( ) )
return ;
m_OutputFile < < " [ " < < ( long ) Utility : : GetTime ( ) < < " ] " < < line < < " \n " ;
}
2018-01-04 04:25:35 +01:00
void CompatLogger : : Flush ( )
2013-03-19 13:04:30 +01:00
{
ASSERT ( OwnsLock ( ) ) ;
if ( ! m_OutputFile . good ( ) )
return ;
m_OutputFile < < std : : flush ;
}
/**
* @ threadsafety Always .
*/
2013-09-25 10:55:50 +02:00
void CompatLogger : : ReopenFile ( bool rotate )
2013-03-19 13:04:30 +01:00
{
ObjectLock olock ( this ) ;
String tempFile = GetLogDir ( ) + " /icinga.log " ;
2013-03-20 11:11:46 +01:00
if ( m_OutputFile ) {
2013-03-19 13:04:30 +01:00
m_OutputFile . close ( ) ;
2013-03-20 11:11:46 +01:00
if ( rotate ) {
String archiveFile = GetLogDir ( ) + " /archives/icinga- " + Utility : : FormatDateTime ( " %m-%d-%Y-%H " , Utility : : GetTime ( ) ) + " .log " ;
2014-10-19 17:52:17 +02:00
Log ( LogNotice , " CompatLogger " )
2017-12-19 15:50:05 +01:00
< < " Rotating compat log file ' " < < tempFile < < " ' -> ' " < < archiveFile < < " ' " ;
2014-10-19 17:52:17 +02:00
2013-03-20 11:11:46 +01:00
( void ) rename ( tempFile . CStr ( ) , archiveFile . CStr ( ) ) ;
}
2013-03-19 13:04:30 +01:00
}
2013-03-20 11:11:46 +01:00
m_OutputFile . open ( tempFile . CStr ( ) , std : : ofstream : : app ) ;
2013-03-19 13:04:30 +01:00
2014-10-19 17:52:17 +02:00
if ( ! m_OutputFile ) {
Log ( LogWarning , " CompatLogger " )
2017-12-19 15:50:05 +01:00
< < " Could not open compat log file ' " < < tempFile < < " ' for writing. Log output will be lost. " ;
2013-03-19 13:04:30 +01:00
return ;
}
2013-03-20 11:11:46 +01:00
WriteLine ( " LOG ROTATION: " + GetRotationMethod ( ) ) ;
2013-03-19 13:04:30 +01:00
WriteLine ( " LOG VERSION: 2.0 " ) ;
2016-08-25 06:19:44 +02:00
for ( const Host : : Ptr & host : ConfigType : : GetObjectsByType < Host > ( ) ) {
2013-10-31 14:18:20 +01:00
String output ;
2014-04-03 15:36:13 +02:00
CheckResult : : Ptr cr = host - > GetLastCheckResult ( ) ;
2013-10-31 14:18:20 +01:00
2013-12-05 14:54:16 +01:00
if ( cr )
output = CompatUtility : : GetCheckResultOutput ( cr ) ;
2013-10-31 14:18:20 +01:00
2013-03-19 13:04:30 +01:00
std : : ostringstream msgbuf ;
2013-10-29 13:44:43 +01:00
msgbuf < < " CURRENT HOST STATE: "
2017-12-19 15:50:05 +01:00
< < host - > GetName ( ) < < " ; "
2017-12-21 09:24:04 +01:00
< < GetHostStateString ( host ) < < " ; "
2017-12-19 15:50:05 +01:00
< < Host : : StateTypeToString ( host - > GetStateType ( ) ) < < " ; "
< < host - > GetCheckAttempt ( ) < < " ; "
< < output < < " " ;
2013-03-19 13:04:30 +01:00
WriteLine ( msgbuf . str ( ) ) ;
}
2016-08-25 06:19:44 +02:00
for ( const Service : : Ptr & service : ConfigType : : GetObjectsByType < Service > ( ) ) {
2013-03-19 13:04:30 +01:00
Host : : Ptr host = service - > GetHost ( ) ;
2013-10-31 14:18:20 +01:00
String output ;
2013-11-09 14:22:38 +01:00
CheckResult : : Ptr cr = service - > GetLastCheckResult ( ) ;
2013-10-31 14:18:20 +01:00
2013-12-05 14:54:16 +01:00
if ( cr )
output = CompatUtility : : GetCheckResultOutput ( cr ) ;
2013-10-31 14:18:20 +01:00
2013-03-19 13:04:30 +01:00
std : : ostringstream msgbuf ;
2013-10-29 13:44:43 +01:00
msgbuf < < " CURRENT SERVICE STATE: "
2017-12-19 15:50:05 +01:00
< < host - > GetName ( ) < < " ; "
< < service - > GetShortName ( ) < < " ; "
< < Service : : StateToString ( service - > GetState ( ) ) < < " ; "
< < Service : : StateTypeToString ( service - > GetStateType ( ) ) < < " ; "
< < service - > GetCheckAttempt ( ) < < " ; "
< < output < < " " ;
2013-03-19 13:04:30 +01:00
WriteLine ( msgbuf . str ( ) ) ;
}
Flush ( ) ;
}
2018-01-04 04:25:35 +01:00
void CompatLogger : : ScheduleNextRotation ( )
2013-03-20 11:11:46 +01:00
{
2018-01-04 09:07:03 +01:00
auto now = ( time_t ) Utility : : GetTime ( ) ;
2013-03-20 11:11:46 +01:00
String method = GetRotationMethod ( ) ;
tm tmthen ;
# ifdef _MSC_VER
tm * temp = localtime ( & now ) ;
2017-12-14 15:37:20 +01:00
if ( ! temp ) {
2013-03-20 11:11:46 +01:00
BOOST_THROW_EXCEPTION ( posix_error ( )
2017-12-19 15:50:05 +01:00
< < boost : : errinfo_api_function ( " localtime " )
< < boost : : errinfo_errno ( errno ) ) ;
2013-03-20 11:11:46 +01:00
}
tmthen = * temp ;
# else /* _MSC_VER */
2017-12-14 15:37:20 +01:00
if ( ! localtime_r ( & now , & tmthen ) ) {
2013-03-20 11:11:46 +01:00
BOOST_THROW_EXCEPTION ( posix_error ( )
2017-12-19 15:50:05 +01:00
< < boost : : errinfo_api_function ( " localtime_r " )
< < boost : : errinfo_errno ( errno ) ) ;
2013-03-20 11:11:46 +01:00
}
# endif /* _MSC_VER */
tmthen . tm_min = 0 ;
tmthen . tm_sec = 0 ;
if ( method = = " HOURLY " ) {
tmthen . tm_hour + + ;
} else if ( method = = " DAILY " ) {
tmthen . tm_mday + + ;
tmthen . tm_hour = 0 ;
} else if ( method = = " WEEKLY " ) {
tmthen . tm_mday + = 7 - tmthen . tm_wday ;
tmthen . tm_hour = 0 ;
} else if ( method = = " MONTHLY " ) {
tmthen . tm_mon + + ;
tmthen . tm_mday = 1 ;
tmthen . tm_hour = 0 ;
}
time_t ts = mktime ( & tmthen ) ;
2014-10-19 17:52:17 +02:00
Log ( LogNotice , " CompatLogger " )
2017-12-19 15:50:05 +01:00
< < " Rescheduling rotation timer for compat log ' "
< < GetName ( ) < < " ' to ' " < < Utility : : FormatDateTime ( " %Y/%m/%d %H:%M:%S %z " , ts ) < < " ' " ;
2014-10-19 17:52:17 +02:00
2013-03-20 11:11:46 +01:00
m_RotationTimer - > Reschedule ( ts ) ;
}
2013-03-19 13:04:30 +01:00
/**
* @ threadsafety Always .
*/
2018-01-04 04:25:35 +01:00
void CompatLogger : : RotationTimerHandler ( )
2013-03-19 13:04:30 +01:00
{
2013-03-20 11:11:46 +01:00
try {
ReopenFile ( true ) ;
} catch ( . . . ) {
ScheduleNextRotation ( ) ;
throw ;
}
ScheduleNextRotation ( ) ;
}
2018-01-11 07:08:09 +01:00
void CompatLogger : : ValidateRotationMethod ( const Lazy < String > & lvalue , const ValidationUtils & utils )
2013-03-20 11:11:46 +01:00
{
2018-01-11 07:08:09 +01:00
ObjectImpl < CompatLogger > : : ValidateRotationMethod ( lvalue , utils ) ;
2013-03-20 11:11:46 +01:00
2018-01-11 07:08:09 +01:00
if ( lvalue ( ) ! = " HOURLY " & & lvalue ( ) ! = " DAILY " & &
lvalue ( ) ! = " WEEKLY " & & lvalue ( ) ! = " MONTHLY " & & lvalue ( ) ! = " NONE " ) {
BOOST_THROW_EXCEPTION ( ValidationError ( this , { " rotation_method " } , " Rotation method ' " + lvalue ( ) + " ' is invalid. " ) ) ;
2013-03-20 11:11:46 +01:00
}
2013-03-19 13:04:30 +01:00
}