2013-07-17 14:10:28 +02:00
/******************************************************************************
* Icinga 2 *
2013-09-25 07:43:57 +02:00
* Copyright ( C ) 2012 - 2013 Icinga Development Team ( http : //www.icinga.org/) *
2013-07-17 14:10:28 +02: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 . *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2013-09-25 10:41:59 +02:00
# include "db_ido/servicedbobject.h"
# include "db_ido/dbtype.h"
# include "db_ido/dbvalue.h"
2013-08-09 13:55:50 +02:00
# include "base/convert.h"
2013-07-23 09:50:42 +02:00
# include "base/objectlock.h"
2013-08-07 15:39:09 +02:00
# include "base/initialize.h"
# include "base/dynamictype.h"
2013-09-17 16:51:55 +02:00
# include "base/utility.h"
2013-07-31 18:06:54 +02:00
# include "icinga/notification.h"
2013-07-22 12:08:49 +02:00
# include "icinga/checkcommand.h"
2013-07-31 18:06:54 +02:00
# include "icinga/eventcommand.h"
2013-09-30 19:32:32 +02:00
# include "icinga/externalcommandprocessor.h"
2013-07-23 09:50:42 +02:00
# include "icinga/compatutility.h"
2013-08-05 17:25:40 +02:00
# include <boost/foreach.hpp>
2013-09-30 19:32:32 +02:00
# include <boost/algorithm/string/join.hpp>
2013-07-17 14:10:28 +02:00
using namespace icinga ;
2013-08-02 17:12:07 +02:00
REGISTER_DBTYPE ( Service , " service " , DbObjectTypeService , " service_object_id " , ServiceDbObject ) ;
2013-07-17 14:10:28 +02:00
2013-11-08 21:12:47 +01:00
INITIALIZE_ONCE ( & ServiceDbObject : : StaticInitialize ) ;
2013-08-07 15:39:09 +02:00
void ServiceDbObject : : StaticInitialize ( void )
{
2013-09-24 11:49:02 +02:00
/* Status */
2013-08-28 14:59:41 +02:00
Service : : OnCommentAdded . connect ( boost : : bind ( & ServiceDbObject : : AddComment , _1 , _2 ) ) ;
Service : : OnCommentRemoved . connect ( boost : : bind ( & ServiceDbObject : : RemoveComment , _1 , _2 ) ) ;
2013-08-28 16:08:22 +02:00
Service : : OnDowntimeAdded . connect ( boost : : bind ( & ServiceDbObject : : AddDowntime , _1 , _2 ) ) ;
Service : : OnDowntimeRemoved . connect ( boost : : bind ( & ServiceDbObject : : RemoveDowntime , _1 , _2 ) ) ;
Service : : OnDowntimeTriggered . connect ( boost : : bind ( & ServiceDbObject : : TriggerDowntime , _1 , _2 ) ) ;
2013-09-24 11:49:02 +02:00
/* History */
2013-09-25 16:59:08 +02:00
Service : : OnCommentAdded . connect ( boost : : bind ( & ServiceDbObject : : AddCommentHistory , _1 , _2 ) ) ;
Service : : OnDowntimeAdded . connect ( boost : : bind ( & ServiceDbObject : : AddDowntimeHistory , _1 , _2 ) ) ;
Service : : OnAcknowledgementSet . connect ( boost : : bind ( & ServiceDbObject : : AddAcknowledgementHistory , _1 , _2 , _3 , _4 , _5 ) ) ;
2013-10-01 12:45:58 +02:00
Service : : OnNotificationSentToUser . connect ( bind ( & ServiceDbObject : : AddContactNotificationHistory , _1 , _2 ) ) ;
2013-09-25 16:59:08 +02:00
Service : : OnNotificationSentToAllUsers . connect ( bind ( & ServiceDbObject : : AddNotificationHistory , _1 , _2 , _3 , _4 , _5 , _6 ) ) ;
2013-09-25 18:01:08 +02:00
Service : : OnStateChange . connect ( boost : : bind ( & ServiceDbObject : : AddStateChangeHistory , _1 , _2 , _3 ) ) ;
2013-09-25 19:47:23 +02:00
Service : : OnNewCheckResult . connect ( bind ( & ServiceDbObject : : AddCheckResultLogHistory , _1 , _2 ) ) ;
Service : : OnNotificationSentToUser . connect ( bind ( & ServiceDbObject : : AddNotificationSentLogHistory , _1 , _2 , _3 , _4 , _5 , _6 ) ) ;
Service : : OnFlappingChanged . connect ( bind ( & ServiceDbObject : : AddFlappingLogHistory , _1 , _2 ) ) ;
Service : : OnDowntimeTriggered . connect ( boost : : bind ( & ServiceDbObject : : AddTriggerDowntimeLogHistory , _1 , _2 ) ) ;
Service : : OnDowntimeRemoved . connect ( boost : : bind ( & ServiceDbObject : : AddRemoveDowntimeLogHistory , _1 , _2 ) ) ;
2013-09-30 17:36:14 +02:00
Service : : OnFlappingChanged . connect ( bind ( & ServiceDbObject : : AddFlappingHistory , _1 , _2 ) ) ;
2013-09-30 18:39:13 +02:00
Service : : OnNewCheckResult . connect ( bind ( & ServiceDbObject : : AddServiceCheckHistory , _1 , _2 ) ) ;
2013-09-30 19:32:32 +02:00
2013-10-01 11:04:30 +02:00
Service : : OnEventCommandExecuted . connect ( bind ( & ServiceDbObject : : AddEventHandlerHistory , _1 ) ) ;
2013-10-10 23:06:28 +02:00
ExternalCommandProcessor : : OnNewExternalCommand . connect ( boost : : bind ( & ServiceDbObject : : AddExternalCommandHistory , _1 , _2 , _3 ) ) ;
2013-08-07 15:39:09 +02:00
}
2013-08-01 13:20:30 +02:00
ServiceDbObject : : ServiceDbObject ( const DbType : : Ptr & type , const String & name1 , const String & name2 )
: DbObject ( type , name1 , name2 )
2013-07-17 14:10:28 +02:00
{ }
2013-07-22 12:08:49 +02:00
Dictionary : : Ptr ServiceDbObject : : GetConfigFields ( void ) const
2013-07-17 14:10:28 +02:00
{
2013-11-06 08:51:56 +01:00
Dictionary : : Ptr fields = make_shared < Dictionary > ( ) ;
2013-07-17 14:10:28 +02:00
Service : : Ptr service = static_pointer_cast < Service > ( GetObject ( ) ) ;
2013-07-23 11:02:47 +02:00
Host : : Ptr host = service - > GetHost ( ) ;
fields - > Set ( " host_object_id " , host ) ;
2013-07-17 14:10:28 +02:00
fields - > Set ( " display_name " , service - > GetDisplayName ( ) ) ;
2013-07-22 12:08:49 +02:00
fields - > Set ( " check_command_object_id " , service - > GetCheckCommand ( ) ) ;
fields - > Set ( " check_command_args " , Empty ) ;
2013-07-31 18:06:54 +02:00
fields - > Set ( " eventhandler_command_object_id " , service - > GetEventCommand ( ) ) ;
2013-07-22 12:08:49 +02:00
fields - > Set ( " eventhandler_command_args " , Empty ) ;
2013-12-05 14:54:16 +01:00
fields - > Set ( " notification_timeperiod_object_id " , Notification : : GetByName ( CompatUtility : : GetServiceNotificationNotificationPeriod ( service ) ) ) ;
2013-07-31 18:06:54 +02:00
fields - > Set ( " check_timeperiod_object_id " , service - > GetCheckPeriod ( ) ) ;
2013-07-22 12:08:49 +02:00
fields - > Set ( " failure_prediction_options " , Empty ) ;
2013-12-05 14:54:16 +01:00
fields - > Set ( " check_interval " , CompatUtility : : GetServiceCheckInterval ( service ) ) ;
fields - > Set ( " retry_interval " , CompatUtility : : GetServiceRetryInterval ( service ) ) ;
fields - > Set ( " max_check_attempts " , service - > GetMaxCheckAttempts ( ) ) ;
2013-07-22 12:08:49 +02:00
fields - > Set ( " first_notification_delay " , Empty ) ;
2013-12-05 14:54:16 +01:00
fields - > Set ( " notification_interval " , CompatUtility : : GetServiceNotificationNotificationInterval ( service ) ) ;
fields - > Set ( " notify_on_warning " , CompatUtility : : GetServiceNotifyOnWarning ( service ) ) ;
fields - > Set ( " notify_on_unknown " , CompatUtility : : GetServiceNotifyOnUnknown ( service ) ) ;
fields - > Set ( " notify_on_critical " , CompatUtility : : GetServiceNotifyOnCritical ( service ) ) ;
fields - > Set ( " notify_on_recovery " , CompatUtility : : GetServiceNotifyOnRecovery ( service ) ) ;
fields - > Set ( " notify_on_flapping " , CompatUtility : : GetServiceNotifyOnFlapping ( service ) ) ;
fields - > Set ( " notify_on_downtime " , CompatUtility : : GetServiceNotifyOnDowntime ( service ) ) ;
2013-07-22 12:08:49 +02:00
fields - > Set ( " stalk_on_ok " , 0 ) ;
fields - > Set ( " stalk_on_warning " , 0 ) ;
fields - > Set ( " stalk_on_unknown " , 0 ) ;
fields - > Set ( " stalk_on_critical " , 0 ) ;
2013-12-05 14:54:16 +01:00
fields - > Set ( " is_volatile " , CompatUtility : : GetServiceIsVolatile ( service ) ) ;
fields - > Set ( " flap_detection_enabled " , CompatUtility : : GetServiceFlapDetectionEnabled ( service ) ) ;
2013-07-22 12:08:49 +02:00
fields - > Set ( " flap_detection_on_ok " , Empty ) ;
fields - > Set ( " flap_detection_on_warning " , Empty ) ;
fields - > Set ( " flap_detection_on_unknown " , Empty ) ;
fields - > Set ( " flap_detection_on_critical " , Empty ) ;
2013-12-05 14:54:16 +01:00
fields - > Set ( " low_flap_threshold " , CompatUtility : : GetServiceLowFlapThreshold ( service ) ) ;
fields - > Set ( " high_flap_threshold " , CompatUtility : : GetServiceHighFlapThreshold ( service ) ) ;
fields - > Set ( " process_performance_data " , CompatUtility : : GetServiceProcessPerformanceData ( service ) ) ;
fields - > Set ( " freshness_checks_enabled " , CompatUtility : : GetServiceFreshnessChecksEnabled ( service ) ) ;
fields - > Set ( " freshness_threshold " , CompatUtility : : GetServiceFreshnessThreshold ( service ) ) ;
fields - > Set ( " passive_checks_enabled " , CompatUtility : : GetServicePassiveChecksEnabled ( service ) ) ;
fields - > Set ( " event_handler_enabled " , CompatUtility : : GetServiceEventHandlerEnabled ( service ) ) ;
fields - > Set ( " active_checks_enabled " , CompatUtility : : GetServiceActiveChecksEnabled ( service ) ) ;
2013-07-22 12:08:49 +02:00
fields - > Set ( " retain_status_information " , Empty ) ;
fields - > Set ( " retain_nonstatus_information " , Empty ) ;
2013-12-05 14:54:16 +01:00
fields - > Set ( " notifications_enabled " , CompatUtility : : GetServiceNotificationsEnabled ( service ) ) ;
2013-07-22 12:08:49 +02:00
fields - > Set ( " obsess_over_service " , Empty ) ;
fields - > Set ( " failure_prediction_enabled " , Empty ) ;
2013-12-05 14:54:16 +01:00
fields - > Set ( " notes " , CompatUtility : : GetCustomAttributeConfig ( service , " notes " ) ) ;
fields - > Set ( " notes_url " , CompatUtility : : GetCustomAttributeConfig ( service , " notes_url " ) ) ;
fields - > Set ( " action_url " , CompatUtility : : GetCustomAttributeConfig ( service , " action_url " ) ) ;
fields - > Set ( " icon_image " , CompatUtility : : GetCustomAttributeConfig ( service , " icon_image " ) ) ;
fields - > Set ( " icon_image_alt " , CompatUtility : : GetCustomAttributeConfig ( service , " icon_image_alt " ) ) ;
2013-07-17 14:10:28 +02:00
return fields ;
}
2013-07-22 12:08:49 +02:00
Dictionary : : Ptr ServiceDbObject : : GetStatusFields ( void ) const
{
2013-11-06 08:51:56 +01:00
Dictionary : : Ptr fields = make_shared < Dictionary > ( ) ;
2013-07-22 12:08:49 +02:00
Service : : Ptr service = static_pointer_cast < Service > ( GetObject ( ) ) ;
2013-12-05 14:54:16 +01:00
CheckResult : : Ptr cr = service - > GetLastCheckResult ( ) ;
2013-07-23 09:50:42 +02:00
2013-12-05 14:54:16 +01:00
if ( cr ) {
fields - > Set ( " output " , CompatUtility : : GetCheckResultOutput ( cr ) ) ;
fields - > Set ( " long_output " , CompatUtility : : GetCheckResultLongOutput ( cr ) ) ;
fields - > Set ( " perfdata " , CompatUtility : : GetCheckResultPerfdata ( cr ) ) ;
fields - > Set ( " check_source " , cr - > GetCheckSource ( ) ) ;
2013-07-23 09:50:42 +02:00
}
2013-12-05 14:54:16 +01:00
fields - > Set ( " current_state " , CompatUtility : : GetServiceCurrentState ( service ) ) ;
fields - > Set ( " has_been_checked " , CompatUtility : : GetServiceHasBeenChecked ( service ) ) ;
fields - > Set ( " should_be_scheduled " , CompatUtility : : GetServiceShouldBeScheduled ( service ) ) ;
fields - > Set ( " current_check_attempt " , service - > GetCheckAttempt ( ) ) ;
fields - > Set ( " max_check_attempts " , service - > GetMaxCheckAttempts ( ) ) ;
if ( cr )
fields - > Set ( " last_check " , DbValue : : FromTimestamp ( cr - > GetScheduleEnd ( ) ) ) ;
fields - > Set ( " next_check " , DbValue : : FromTimestamp ( service - > GetNextCheck ( ) ) ) ;
fields - > Set ( " check_type " , CompatUtility : : GetServiceCheckType ( service ) ) ;
fields - > Set ( " last_state_change " , DbValue : : FromTimestamp ( service - > GetLastStateChange ( ) ) ) ;
fields - > Set ( " last_hard_state_change " , DbValue : : FromTimestamp ( service - > GetLastHardStateChange ( ) ) ) ;
fields - > Set ( " last_time_ok " , DbValue : : FromTimestamp ( static_cast < int > ( service - > GetLastStateOK ( ) ) ) ) ;
fields - > Set ( " last_time_warning " , DbValue : : FromTimestamp ( static_cast < int > ( service - > GetLastStateWarning ( ) ) ) ) ;
fields - > Set ( " last_time_critical " , DbValue : : FromTimestamp ( static_cast < int > ( service - > GetLastStateCritical ( ) ) ) ) ;
fields - > Set ( " last_time_unknown " , DbValue : : FromTimestamp ( static_cast < int > ( service - > GetLastStateUnknown ( ) ) ) ) ;
fields - > Set ( " state_type " , service - > GetStateType ( ) ) ;
fields - > Set ( " last_notification " , DbValue : : FromTimestamp ( CompatUtility : : GetServiceNotificationLastNotification ( service ) ) ) ;
fields - > Set ( " next_notification " , DbValue : : FromTimestamp ( CompatUtility : : GetServiceNotificationNextNotification ( service ) ) ) ;
2013-07-31 18:06:54 +02:00
fields - > Set ( " no_more_notifications " , Empty ) ;
2013-12-05 14:54:16 +01:00
fields - > Set ( " notifications_enabled " , CompatUtility : : GetServiceNotificationsEnabled ( service ) ) ;
fields - > Set ( " problem_has_been_acknowledged " , CompatUtility : : GetServiceProblemHasBeenAcknowledged ( service ) ) ;
fields - > Set ( " acknowledgement_type " , CompatUtility : : GetServiceAcknowledgementType ( service ) ) ;
fields - > Set ( " current_notification_number " , CompatUtility : : GetServiceNotificationNotificationNumber ( service ) ) ;
fields - > Set ( " passive_checks_enabled " , CompatUtility : : GetServicePassiveChecksEnabled ( service ) ) ;
fields - > Set ( " active_checks_enabled " , CompatUtility : : GetServiceActiveChecksEnabled ( service ) ) ;
fields - > Set ( " event_handler_enabled " , CompatUtility : : GetServiceEventHandlerEnabled ( service ) ) ;
fields - > Set ( " flap_detection_enabled " , CompatUtility : : GetServiceFlapDetectionEnabled ( service ) ) ;
fields - > Set ( " is_flapping " , CompatUtility : : GetServiceIsFlapping ( service ) ) ;
fields - > Set ( " percent_state_change " , CompatUtility : : GetServicePercentStateChange ( service ) ) ;
if ( cr ) {
fields - > Set ( " latency " , Service : : CalculateLatency ( cr ) ) ;
fields - > Set ( " execution_time " , Service : : CalculateExecutionTime ( cr ) ) ;
}
fields - > Set ( " scheduled_downtime_depth " , service - > GetDowntimeDepth ( ) ) ;
fields - > Set ( " process_performance_data " , CompatUtility : : GetServiceProcessPerformanceData ( service ) ) ;
fields - > Set ( " event_handler " , CompatUtility : : GetServiceEventHandler ( service ) ) ;
fields - > Set ( " check_command " , CompatUtility : : GetServiceCheckCommand ( service ) ) ;
fields - > Set ( " normal_check_interval " , CompatUtility : : GetServiceCheckInterval ( service ) ) ;
fields - > Set ( " retry_check_interval " , CompatUtility : : GetServiceRetryInterval ( service ) ) ;
2013-07-31 18:06:54 +02:00
fields - > Set ( " check_timeperiod_object_id " , service - > GetCheckPeriod ( ) ) ;
2013-12-05 14:54:16 +01:00
fields - > Set ( " modified_service_attributes " , service - > GetModifiedAttributes ( ) ) ;
2013-07-22 12:08:49 +02:00
return fields ;
2013-07-24 10:55:04 +02:00
}
bool ServiceDbObject : : IsStatusAttribute ( const String & attribute ) const
{
return ( attribute = = " last_result " ) ;
2013-07-25 08:30:02 +02:00
}
void ServiceDbObject : : OnConfigUpdate ( void )
{
Service : : Ptr service = static_pointer_cast < Service > ( GetObject ( ) ) ;
2013-08-05 17:25:40 +02:00
/* service dependencies */
2013-09-25 13:08:15 +02:00
Log ( LogDebug , " db_ido " , " service dependencies for ' " + service - > GetName ( ) + " ' " ) ;
2013-08-05 17:25:40 +02:00
BOOST_FOREACH ( const Service : : Ptr & parent , service - > GetParentServices ( ) ) {
2013-09-25 13:08:15 +02:00
Log ( LogDebug , " db_ido " , " service parents: " + parent - > GetName ( ) ) ;
2013-08-05 17:25:40 +02:00
/* service dependencies */
2013-11-06 08:51:56 +01:00
Dictionary : : Ptr fields1 = make_shared < Dictionary > ( ) ;
2013-08-05 17:25:40 +02:00
fields1 - > Set ( " service_object_id " , parent ) ;
fields1 - > Set ( " dependent_service_object_id " , service ) ;
fields1 - > Set ( " instance_id " , 0 ) ; /* DbConnection class fills in real ID */
DbQuery query1 ;
query1 . Table = GetType ( ) - > GetTable ( ) + " dependencies " ;
query1 . Type = DbQueryInsert ;
2013-10-29 15:54:43 +01:00
query1 . Category = DbCatConfig ;
2013-08-05 17:25:40 +02:00
query1 . Fields = fields1 ;
OnQuery ( query1 ) ;
}
2013-10-01 16:43:25 +02:00
/* service contacts, contactgroups */
2013-10-02 21:09:13 +02:00
Log ( LogDebug , " db_ido " , " service contacts: " + service - > GetName ( ) ) ;
2013-10-01 16:43:25 +02:00
2013-10-02 21:09:13 +02:00
BOOST_FOREACH ( const User : : Ptr & user , CompatUtility : : GetServiceNotificationUsers ( service ) ) {
Log ( LogDebug , " db_ido " , " service contacts: " + user - > GetName ( ) ) ;
2013-11-06 08:51:56 +01:00
Dictionary : : Ptr fields_contact = make_shared < Dictionary > ( ) ;
2013-10-02 21:09:13 +02:00
fields_contact - > Set ( " service_id " , DbValue : : FromObjectInsertID ( service ) ) ;
fields_contact - > Set ( " contact_object_id " , user ) ;
fields_contact - > Set ( " instance_id " , 0 ) ; /* DbConnection class fills in real ID */
DbQuery query_contact ;
query_contact . Table = GetType ( ) - > GetTable ( ) + " _contacts " ;
query_contact . Type = DbQueryInsert ;
2013-10-29 15:54:43 +01:00
query_contact . Category = DbCatConfig ;
2013-10-02 21:09:13 +02:00
query_contact . Fields = fields_contact ;
OnQuery ( query_contact ) ;
2013-10-01 16:43:25 +02:00
}
2013-10-02 21:09:13 +02:00
Log ( LogDebug , " db_ido " , " service contactgroups: " + service - > GetName ( ) ) ;
BOOST_FOREACH ( const UserGroup : : Ptr & usergroup , CompatUtility : : GetServiceNotificationUserGroups ( service ) ) {
Log ( LogDebug , " db_ido " , " service contactgroups: " + usergroup - > GetName ( ) ) ;
2013-11-06 08:51:56 +01:00
Dictionary : : Ptr fields_contact = make_shared < Dictionary > ( ) ;
2013-10-02 21:09:13 +02:00
fields_contact - > Set ( " service_id " , DbValue : : FromObjectInsertID ( service ) ) ;
fields_contact - > Set ( " contactgroup_object_id " , usergroup ) ;
fields_contact - > Set ( " instance_id " , 0 ) ; /* DbConnection class fills in real ID */
DbQuery query_contact ;
query_contact . Table = GetType ( ) - > GetTable ( ) + " _contactgroups " ;
query_contact . Type = DbQueryInsert ;
2013-10-29 15:54:43 +01:00
query_contact . Category = DbCatConfig ;
2013-10-02 21:09:13 +02:00
query_contact . Fields = fields_contact ;
OnQuery ( query_contact ) ;
2013-10-01 16:43:25 +02:00
}
2013-08-09 13:55:50 +02:00
/* custom variables */
2013-09-25 13:08:15 +02:00
Log ( LogDebug , " db_ido " , " service customvars for ' " + service - > GetName ( ) + " ' " ) ;
2013-08-09 13:55:50 +02:00
Dictionary : : Ptr customvars ;
2013-08-09 14:26:17 +02:00
2013-08-09 13:55:50 +02:00
{
ObjectLock olock ( service ) ;
customvars = CompatUtility : : GetCustomVariableConfig ( service ) ;
}
if ( customvars ) {
2013-11-21 16:09:21 +01:00
ObjectLock olock ( customvars ) ;
2013-08-09 13:55:50 +02:00
2013-11-30 17:42:50 +01:00
BOOST_FOREACH ( const Dictionary : : Pair & kv , customvars ) {
Log ( LogDebug , " db_ido " , " service customvar key: ' " + kv . first + " ' value: ' " + Convert : : ToString ( kv . second ) + " ' " ) ;
2013-08-09 13:55:50 +02:00
2013-11-06 08:51:56 +01:00
Dictionary : : Ptr fields2 = make_shared < Dictionary > ( ) ;
2013-11-30 17:42:50 +01:00
fields2 - > Set ( " varname " , Convert : : ToString ( kv . first ) ) ;
fields2 - > Set ( " varvalue " , Convert : : ToString ( kv . second ) ) ;
2013-08-09 13:55:50 +02:00
fields2 - > Set ( " config_type " , 1 ) ;
fields2 - > Set ( " has_been_modified " , 0 ) ;
fields2 - > Set ( " object_id " , service ) ;
fields2 - > Set ( " instance_id " , 0 ) ; /* DbConnection class fills in real ID */
DbQuery query2 ;
query2 . Table = " customvariables " ;
query2 . Type = DbQueryInsert ;
2013-10-29 15:54:43 +01:00
query2 . Category = DbCatConfig ;
2013-08-09 13:55:50 +02:00
query2 . Fields = fields2 ;
OnQuery ( query2 ) ;
}
}
2013-08-12 11:08:29 +02:00
/* update comments and downtimes on config change */
2013-08-28 14:59:41 +02:00
AddComments ( service ) ;
2013-08-28 16:08:22 +02:00
AddDowntimes ( service ) ;
2013-07-25 08:30:02 +02:00
}
void ServiceDbObject : : OnStatusUpdate ( void )
{
Service : : Ptr service = static_pointer_cast < Service > ( GetObject ( ) ) ;
Host : : Ptr host = service - > GetHost ( ) ;
2013-08-12 11:08:29 +02:00
/* update the host if hostcheck service */
2013-09-25 09:12:15 +02:00
if ( host - > GetCheckService ( ) ! = service )
2013-07-25 08:30:02 +02:00
return ;
DbObject : : Ptr dbobj = GetOrCreateByObject ( host ) ;
if ( ! dbobj )
return ;
dbobj - > SendStatusUpdate ( ) ;
2013-07-30 18:17:58 +02:00
}
2013-08-07 15:39:09 +02:00
2013-09-30 17:36:14 +02:00
/* comments */
2013-08-09 14:22:56 +02:00
void ServiceDbObject : : AddComments ( const Service : : Ptr & service )
2013-08-07 17:36:54 +02:00
{
/* dump all comments */
Dictionary : : Ptr comments = service - > GetComments ( ) ;
2013-08-07 15:39:09 +02:00
2013-12-09 12:22:54 +01:00
if ( comments - > GetLength ( ) > 0 )
RemoveComments ( service ) ;
2013-12-09 13:22:50 +01:00
ObjectLock olock ( comments ) ;
2013-11-30 17:42:50 +01:00
BOOST_FOREACH ( const Dictionary : : Pair & kv , comments ) {
AddComment ( service , kv . second ) ;
2013-08-07 17:36:54 +02:00
}
}
2013-08-07 15:39:09 +02:00
2013-11-09 21:19:52 +01:00
void ServiceDbObject : : AddComment ( const Service : : Ptr & service , const Comment : : Ptr & comment )
2013-09-25 16:59:08 +02:00
{
AddCommentInternal ( service , comment , false ) ;
}
2013-11-09 21:19:52 +01:00
void ServiceDbObject : : AddCommentHistory ( const Service : : Ptr & service , const Comment : : Ptr & comment )
2013-09-25 16:59:08 +02:00
{
AddCommentInternal ( service , comment , true ) ;
}
2013-11-09 21:19:52 +01:00
void ServiceDbObject : : AddCommentInternal ( const Service : : Ptr & service , const Comment : : Ptr & comment , bool historical )
2013-08-07 17:36:54 +02:00
{
Host : : Ptr host = service - > GetHost ( ) ;
2013-08-07 15:39:09 +02:00
2013-08-07 17:36:54 +02:00
if ( ! comment ) {
2013-09-25 13:08:15 +02:00
Log ( LogWarning , " db_ido " , " comment does not exist. not adding it. " ) ;
2013-08-07 17:36:54 +02:00
return ;
}
2013-08-07 15:39:09 +02:00
2013-11-09 21:19:52 +01:00
Log ( LogDebug , " db_ido " , " adding service comment (id = " + Convert : : ToString ( comment - > GetLegacyId ( ) ) + " ) for ' " + service - > GetName ( ) + " ' " ) ;
2013-08-07 15:39:09 +02:00
2013-08-07 18:24:16 +02:00
/* add the service comment */
2013-09-25 16:59:08 +02:00
AddCommentByType ( service , comment , historical ) ;
2013-08-07 15:39:09 +02:00
2013-08-07 18:24:16 +02:00
/* add the hostcheck service comment to the host as well */
2013-09-25 09:12:15 +02:00
if ( host - > GetCheckService ( ) = = service ) {
2013-11-09 21:19:52 +01:00
Log ( LogDebug , " db_ido " , " adding host comment (id = " + Convert : : ToString ( comment - > GetLegacyId ( ) ) + " ) for ' " + host - > GetName ( ) + " ' " ) ;
2013-09-25 16:59:08 +02:00
AddCommentByType ( host , comment , historical ) ;
2013-08-07 18:24:16 +02:00
}
}
2013-11-09 21:19:52 +01:00
void ServiceDbObject : : AddCommentByType ( const DynamicObject : : Ptr & object , const Comment : : Ptr & comment , bool historical )
2013-08-07 18:24:16 +02:00
{
2013-11-09 21:19:52 +01:00
unsigned long entry_time = static_cast < long > ( comment - > GetEntryTime ( ) ) ;
unsigned long entry_time_usec = ( comment - > GetEntryTime ( ) - entry_time ) * 1000 * 1000 ;
2013-08-07 15:39:09 +02:00
2013-11-06 08:51:56 +01:00
Dictionary : : Ptr fields1 = make_shared < Dictionary > ( ) ;
2013-08-07 17:36:54 +02:00
fields1 - > Set ( " entry_time " , DbValue : : FromTimestamp ( entry_time ) ) ;
fields1 - > Set ( " entry_time_usec " , entry_time_usec ) ;
2013-11-09 21:19:52 +01:00
fields1 - > Set ( " entry_type " , comment - > GetEntryType ( ) ) ;
2013-08-09 15:30:28 +02:00
fields1 - > Set ( " object_id " , object ) ;
2013-08-07 15:39:09 +02:00
2013-08-07 18:24:16 +02:00
if ( object - > GetType ( ) = = DynamicType : : GetByName ( " Host " ) ) {
2013-08-07 17:36:54 +02:00
fields1 - > Set ( " comment_type " , 2 ) ;
2013-09-17 15:30:21 +02:00
/* requires idoutils 1.10 schema fix */
2013-11-09 21:19:52 +01:00
fields1 - > Set ( " internal_comment_id " , comment - > GetLegacyId ( ) ) ;
2013-08-07 18:24:16 +02:00
} else if ( object - > GetType ( ) = = DynamicType : : GetByName ( " Service " ) ) {
2013-08-07 17:36:54 +02:00
fields1 - > Set ( " comment_type " , 1 ) ;
2013-11-09 21:19:52 +01:00
fields1 - > Set ( " internal_comment_id " , comment - > GetLegacyId ( ) ) ;
2013-08-07 18:24:16 +02:00
} else {
2013-09-25 13:08:15 +02:00
Log ( LogDebug , " db_ido " , " unknown object type for adding comment. " ) ;
2013-08-07 18:24:16 +02:00
return ;
2013-08-07 15:39:09 +02:00
}
2013-08-07 17:36:54 +02:00
2013-08-07 19:03:22 +02:00
fields1 - > Set ( " comment_time " , DbValue : : FromTimestamp ( entry_time ) ) ; /* same as entry_time */
2013-11-09 21:19:52 +01:00
fields1 - > Set ( " author_name " , comment - > GetAuthor ( ) ) ;
fields1 - > Set ( " comment_data " , comment - > GetText ( ) ) ;
2013-08-07 17:36:54 +02:00
fields1 - > Set ( " is_persistent " , 1 ) ;
fields1 - > Set ( " comment_source " , 1 ) ; /* external */
2013-11-09 21:19:52 +01:00
fields1 - > Set ( " expires " , ( comment - > GetExpireTime ( ) > 0 ) ? 1 : 0 ) ;
fields1 - > Set ( " expiration_time " , comment - > GetExpireTime ( ) ) ;
2013-08-07 17:36:54 +02:00
fields1 - > Set ( " instance_id " , 0 ) ; /* DbConnection class fills in real ID */
DbQuery query1 ;
2013-09-25 16:59:08 +02:00
if ( ! historical ) {
query1 . Table = " comments " ;
} else {
query1 . Table = " commenthistory " ;
}
2013-08-07 17:36:54 +02:00
query1 . Type = DbQueryInsert ;
2013-10-29 15:54:43 +01:00
query1 . Category = DbCatComment ;
2013-08-07 17:36:54 +02:00
query1 . Fields = fields1 ;
OnQuery ( query1 ) ;
}
2013-09-17 15:30:21 +02:00
void ServiceDbObject : : RemoveComments ( const Service : : Ptr & service )
{
Host : : Ptr host = service - > GetHost ( ) ;
2013-09-25 13:08:15 +02:00
Log ( LogDebug , " db_ido " , " removing service comments for ' " + service - > GetName ( ) + " ' " ) ;
2013-09-17 15:30:21 +02:00
DbQuery query1 ;
query1 . Table = " comments " ;
query1 . Type = DbQueryDelete ;
2013-10-29 15:54:43 +01:00
query1 . Category = DbCatComment ;
2013-11-06 08:51:56 +01:00
query1 . WhereCriteria = make_shared < Dictionary > ( ) ;
2013-09-17 15:30:21 +02:00
query1 . WhereCriteria - > Set ( " object_id " , service ) ;
OnQuery ( query1 ) ;
/* delete hostcheck service's host comments */
2013-09-25 09:12:15 +02:00
if ( host - > GetCheckService ( ) = = service ) {
2013-11-19 12:55:37 +01:00
query1 . WhereCriteria = query1 . WhereCriteria - > ShallowClone ( ) ;
2013-09-25 18:24:22 +02:00
query1 . WhereCriteria - > Set ( " object_id " , host ) ;
OnQuery ( query1 ) ;
2013-09-17 15:30:21 +02:00
}
}
2013-11-09 21:19:52 +01:00
void ServiceDbObject : : RemoveComment ( const Service : : Ptr & service , const Comment : : Ptr & comment )
2013-08-28 14:59:41 +02:00
{
2013-09-17 15:30:21 +02:00
Host : : Ptr host = service - > GetHost ( ) ;
if ( ! comment ) {
2013-09-25 13:08:15 +02:00
Log ( LogWarning , " db_ido " , " comment does not exist. not deleting it. " ) ;
2013-09-17 15:30:21 +02:00
return ;
}
2013-11-09 21:19:52 +01:00
Log ( LogDebug , " db_ido " , " removing service comment (id = " + Convert : : ToString ( comment - > GetLegacyId ( ) ) + " ) for ' " + service - > GetName ( ) + " ' " ) ;
2013-09-17 15:30:21 +02:00
2013-09-24 12:45:29 +02:00
/* Status */
2013-09-17 15:30:21 +02:00
DbQuery query1 ;
query1 . Table = " comments " ;
query1 . Type = DbQueryDelete ;
2013-10-29 15:54:43 +01:00
query1 . Category = DbCatComment ;
2013-11-06 08:51:56 +01:00
query1 . WhereCriteria = make_shared < Dictionary > ( ) ;
2013-09-17 15:30:21 +02:00
query1 . WhereCriteria - > Set ( " object_id " , service ) ;
2013-11-09 21:19:52 +01:00
query1 . WhereCriteria - > Set ( " internal_comment_id " , comment - > GetLegacyId ( ) ) ;
2013-09-17 15:30:21 +02:00
OnQuery ( query1 ) ;
/* delete hostcheck service's host comments */
2013-09-25 09:12:15 +02:00
if ( host - > GetCheckService ( ) = = service ) {
2013-11-19 12:55:37 +01:00
query1 . WhereCriteria = query1 . WhereCriteria - > ShallowClone ( ) ;
2013-09-25 18:24:22 +02:00
query1 . WhereCriteria - > Set ( " object_id " , host ) ;
OnQuery ( query1 ) ;
2013-09-17 15:30:21 +02:00
}
2013-09-24 12:45:29 +02:00
/* History - update deletion time for service (and host in case) */
2013-11-09 21:19:52 +01:00
unsigned long entry_time = static_cast < long > ( comment - > GetEntryTime ( ) ) ;
2013-10-31 14:18:20 +01:00
2013-09-24 12:45:29 +02:00
double now = Utility : : GetTime ( ) ;
2013-11-11 15:06:23 +01:00
std : : pair < unsigned long , unsigned long > time_bag = CompatUtility : : ConvertTimestamp ( now ) ;
2013-09-24 12:45:29 +02:00
DbQuery query2 ;
query2 . Table = " commenthistory " ;
query2 . Type = DbQueryUpdate ;
2013-10-29 15:54:43 +01:00
query2 . Category = DbCatComment ;
2013-09-24 12:45:29 +02:00
2013-11-06 08:51:56 +01:00
Dictionary : : Ptr fields2 = make_shared < Dictionary > ( ) ;
2013-11-11 15:06:23 +01:00
fields2 - > Set ( " deletion_time " , DbValue : : FromTimestamp ( time_bag . first ) ) ;
fields2 - > Set ( " deletion_time_usec " , time_bag . second ) ;
2013-09-24 12:45:29 +02:00
query2 . Fields = fields2 ;
2013-11-06 08:51:56 +01:00
query2 . WhereCriteria = make_shared < Dictionary > ( ) ;
2013-11-09 21:19:52 +01:00
query2 . WhereCriteria - > Set ( " internal_comment_id " , comment - > GetLegacyId ( ) ) ;
2013-09-24 12:45:29 +02:00
query2 . WhereCriteria - > Set ( " comment_time " , DbValue : : FromTimestamp ( entry_time ) ) ;
query2 . WhereCriteria - > Set ( " instance_id " , 0 ) ; /* DbConnection class fills in real ID */
OnQuery ( query2 ) ;
2013-08-28 14:59:41 +02:00
}
2013-09-30 17:36:14 +02:00
/* downtimes */
2013-08-09 15:30:28 +02:00
void ServiceDbObject : : AddDowntimes ( const Service : : Ptr & service )
{
/* dump all downtimes */
Dictionary : : Ptr downtimes = service - > GetDowntimes ( ) ;
2013-12-09 12:22:54 +01:00
if ( downtimes - > GetLength ( ) > 0 )
RemoveDowntimes ( service ) ;
2013-12-09 13:22:50 +01:00
ObjectLock olock ( downtimes ) ;
2013-11-30 17:42:50 +01:00
BOOST_FOREACH ( const Dictionary : : Pair & kv , downtimes ) {
AddDowntime ( service , kv . second ) ;
2013-08-09 15:30:28 +02:00
}
}
2013-11-09 22:08:26 +01:00
void ServiceDbObject : : AddDowntime ( const Service : : Ptr & service , const Downtime : : Ptr & downtime )
2013-09-25 16:59:08 +02:00
{
AddDowntimeInternal ( service , downtime , false ) ;
}
2013-11-09 22:08:26 +01:00
void ServiceDbObject : : AddDowntimeHistory ( const Service : : Ptr & service , const Downtime : : Ptr & downtime )
2013-09-25 16:59:08 +02:00
{
AddDowntimeInternal ( service , downtime , true ) ;
}
2013-11-09 22:08:26 +01:00
void ServiceDbObject : : AddDowntimeInternal ( const Service : : Ptr & service , const Downtime : : Ptr & downtime , bool historical )
2013-08-09 15:30:28 +02:00
{
Host : : Ptr host = service - > GetHost ( ) ;
if ( ! downtime ) {
2013-09-25 13:08:15 +02:00
Log ( LogWarning , " db_ido " , " downtime does not exist. not adding it. " ) ;
2013-08-09 15:30:28 +02:00
return ;
}
2013-11-09 22:08:26 +01:00
Log ( LogDebug , " db_ido " , " adding service downtime (id = " + Convert : : ToString ( downtime - > GetLegacyId ( ) ) + " ) for ' " + service - > GetName ( ) + " ' " ) ;
2013-08-09 15:30:28 +02:00
/* add the service downtime */
2013-09-25 16:59:08 +02:00
AddDowntimeByType ( service , downtime , historical ) ;
2013-08-09 15:30:28 +02:00
/* add the hostcheck service downtime to the host as well */
2013-09-25 09:12:15 +02:00
if ( host - > GetCheckService ( ) = = service ) {
2013-11-09 22:08:26 +01:00
Log ( LogDebug , " db_ido " , " adding host downtime (id = " + Convert : : ToString ( downtime - > GetLegacyId ( ) ) + " ) for ' " + host - > GetName ( ) + " ' " ) ;
2013-09-25 16:59:08 +02:00
AddDowntimeByType ( host , downtime , historical ) ;
2013-08-09 15:30:28 +02:00
}
}
2013-08-07 18:24:16 +02:00
2013-11-09 22:08:26 +01:00
void ServiceDbObject : : AddDowntimeByType ( const DynamicObject : : Ptr & object , const Downtime : : Ptr & downtime , bool historical )
2013-08-09 15:30:28 +02:00
{
2013-11-06 08:51:56 +01:00
Dictionary : : Ptr fields1 = make_shared < Dictionary > ( ) ;
2013-11-09 22:08:26 +01:00
fields1 - > Set ( " entry_time " , DbValue : : FromTimestamp ( downtime - > GetEntryTime ( ) ) ) ;
2013-08-09 15:30:28 +02:00
fields1 - > Set ( " object_id " , object ) ;
if ( object - > GetType ( ) = = DynamicType : : GetByName ( " Host " ) ) {
fields1 - > Set ( " downtime_type " , 2 ) ;
2013-09-17 15:30:21 +02:00
/* requires idoutils 1.10 schema fix */
2013-11-09 22:08:26 +01:00
fields1 - > Set ( " internal_downtime_id " , downtime - > GetLegacyId ( ) ) ;
2013-08-09 15:30:28 +02:00
} else if ( object - > GetType ( ) = = DynamicType : : GetByName ( " Service " ) ) {
fields1 - > Set ( " downtime_type " , 1 ) ;
2013-11-09 22:08:26 +01:00
fields1 - > Set ( " internal_downtime_id " , downtime - > GetLegacyId ( ) ) ;
2013-08-09 15:30:28 +02:00
} else {
2013-09-25 13:08:15 +02:00
Log ( LogDebug , " db_ido " , " unknown object type for adding downtime. " ) ;
2013-08-09 15:30:28 +02:00
return ;
}
2013-11-09 22:08:26 +01:00
fields1 - > Set ( " author_name " , downtime - > GetAuthor ( ) ) ;
2013-12-09 13:20:46 +01:00
fields1 - > Set ( " comment_data " , downtime - > GetComment ( ) ) ;
2013-12-09 09:22:04 +01:00
fields1 - > Set ( " triggered_by_id " , Service : : GetDowntimeByID ( downtime - > GetTriggeredBy ( ) ) ) ;
2013-11-09 22:08:26 +01:00
fields1 - > Set ( " is_fixed " , downtime - > GetFixed ( ) ) ;
fields1 - > Set ( " duration " , downtime - > GetDuration ( ) ) ;
fields1 - > Set ( " scheduled_start_time " , DbValue : : FromTimestamp ( downtime - > GetStartTime ( ) ) ) ;
fields1 - > Set ( " scheduled_end_time " , DbValue : : FromTimestamp ( downtime - > GetEndTime ( ) ) ) ;
2013-08-09 15:30:28 +02:00
fields1 - > Set ( " was_started " , Empty ) ;
fields1 - > Set ( " actual_start_time " , Empty ) ;
fields1 - > Set ( " actual_start_time_usec " , Empty ) ;
fields1 - > Set ( " is_in_effect " , Empty ) ;
2013-11-09 22:08:26 +01:00
fields1 - > Set ( " trigger_time " , DbValue : : FromTimestamp ( downtime - > GetTriggerTime ( ) ) ) ;
2013-08-09 15:30:28 +02:00
fields1 - > Set ( " instance_id " , 0 ) ; /* DbConnection class fills in real ID */
DbQuery query1 ;
2013-09-25 16:59:08 +02:00
if ( ! historical ) {
query1 . Table = " scheduleddowntime " ;
} else {
query1 . Table = " downtimehistory " ;
}
2013-08-09 15:30:28 +02:00
query1 . Type = DbQueryInsert ;
2013-10-29 15:54:43 +01:00
query1 . Category = DbCatDowntime ;
2013-08-09 15:30:28 +02:00
query1 . Fields = fields1 ;
OnQuery ( query1 ) ;
}
2013-09-17 15:30:21 +02:00
void ServiceDbObject : : RemoveDowntimes ( const Service : : Ptr & service )
{
Host : : Ptr host = service - > GetHost ( ) ;
2013-09-25 13:08:15 +02:00
Log ( LogDebug , " db_ido " , " removing service downtimes for ' " + service - > GetName ( ) + " ' " ) ;
2013-09-17 15:30:21 +02:00
DbQuery query1 ;
query1 . Table = " scheduleddowntime " ;
query1 . Type = DbQueryDelete ;
2013-10-29 15:54:43 +01:00
query1 . Category = DbCatDowntime ;
2013-11-06 08:51:56 +01:00
query1 . WhereCriteria = make_shared < Dictionary > ( ) ;
2013-09-17 15:30:21 +02:00
query1 . WhereCriteria - > Set ( " object_id " , service ) ;
OnQuery ( query1 ) ;
/* delete hostcheck service's host downtimes */
2013-09-25 09:12:15 +02:00
if ( host - > GetCheckService ( ) = = service ) {
2013-11-19 12:55:37 +01:00
query1 . WhereCriteria = query1 . WhereCriteria - > ShallowClone ( ) ;
2013-09-25 18:24:22 +02:00
query1 . WhereCriteria - > Set ( " object_id " , host ) ;
OnQuery ( query1 ) ;
2013-09-17 15:30:21 +02:00
}
}
2013-11-09 22:08:26 +01:00
void ServiceDbObject : : RemoveDowntime ( const Service : : Ptr & service , const Downtime : : Ptr & downtime )
2013-08-09 15:30:28 +02:00
{
2013-09-17 15:30:21 +02:00
Host : : Ptr host = service - > GetHost ( ) ;
if ( ! downtime ) {
2013-09-25 13:08:15 +02:00
Log ( LogWarning , " db_ido " , " downtime does not exist. not adding it. " ) ;
2013-09-17 15:30:21 +02:00
return ;
}
2013-11-09 22:08:26 +01:00
Log ( LogDebug , " db_ido " , " removing service downtime (id = " + Convert : : ToString ( downtime - > GetLegacyId ( ) ) + " ) for ' " + service - > GetName ( ) + " ' " ) ;
2013-09-17 15:30:21 +02:00
2013-09-24 13:27:02 +02:00
/* Status */
2013-09-17 15:30:21 +02:00
DbQuery query1 ;
query1 . Table = " scheduleddowntime " ;
query1 . Type = DbQueryDelete ;
2013-10-29 15:54:43 +01:00
query1 . Category = DbCatDowntime ;
2013-11-06 08:51:56 +01:00
query1 . WhereCriteria = make_shared < Dictionary > ( ) ;
2013-09-17 15:30:21 +02:00
query1 . WhereCriteria - > Set ( " object_id " , service ) ;
2013-11-09 22:08:26 +01:00
query1 . WhereCriteria - > Set ( " internal_downtime_id " , downtime - > GetLegacyId ( ) ) ;
2013-09-17 15:30:21 +02:00
OnQuery ( query1 ) ;
/* delete hostcheck service's host comments */
2013-09-25 09:12:15 +02:00
if ( host - > GetCheckService ( ) = = service ) {
2013-11-19 12:55:37 +01:00
query1 . WhereCriteria = query1 . WhereCriteria - > ShallowClone ( ) ;
2013-09-25 18:24:22 +02:00
query1 . WhereCriteria - > Set ( " object_id " , host ) ;
OnQuery ( query1 ) ;
2013-09-17 15:30:21 +02:00
}
2013-09-24 13:27:02 +02:00
/* History - update actual_end_time, was_cancelled for service (and host in case) */
double now = Utility : : GetTime ( ) ;
2013-11-11 15:06:23 +01:00
std : : pair < unsigned long , unsigned long > time_bag = CompatUtility : : ConvertTimestamp ( now ) ;
2013-09-24 13:27:02 +02:00
DbQuery query3 ;
query3 . Table = " downtimehistory " ;
query3 . Type = DbQueryUpdate ;
2013-10-29 15:54:43 +01:00
query3 . Category = DbCatDowntime ;
2013-09-24 13:27:02 +02:00
2013-11-06 08:51:56 +01:00
Dictionary : : Ptr fields3 = make_shared < Dictionary > ( ) ;
2013-11-09 22:08:26 +01:00
fields3 - > Set ( " was_cancelled " , downtime - > GetWasCancelled ( ) ? 1 : 0 ) ;
2013-11-11 15:06:23 +01:00
fields3 - > Set ( " actual_end_time " , DbValue : : FromTimestamp ( time_bag . first ) ) ;
fields3 - > Set ( " actual_end_time_usec " , time_bag . second ) ;
2013-09-24 13:27:02 +02:00
query3 . Fields = fields3 ;
2013-11-06 08:51:56 +01:00
query3 . WhereCriteria = make_shared < Dictionary > ( ) ;
2013-11-09 22:08:26 +01:00
query3 . WhereCriteria - > Set ( " internal_downtime_id " , downtime - > GetLegacyId ( ) ) ;
query3 . WhereCriteria - > Set ( " entry_time " , DbValue : : FromTimestamp ( downtime - > GetEntryTime ( ) ) ) ;
query3 . WhereCriteria - > Set ( " scheduled_start_time " , DbValue : : FromTimestamp ( downtime - > GetStartTime ( ) ) ) ;
query3 . WhereCriteria - > Set ( " scheduled_end_time " , DbValue : : FromTimestamp ( downtime - > GetEndTime ( ) ) ) ;
2013-09-24 13:27:02 +02:00
query3 . WhereCriteria - > Set ( " instance_id " , 0 ) ; /* DbConnection class fills in real ID */
OnQuery ( query3 ) ;
2013-08-07 15:39:09 +02:00
}
2013-08-09 14:26:17 +02:00
2013-11-09 22:08:26 +01:00
void ServiceDbObject : : TriggerDowntime ( const Service : : Ptr & service , const Downtime : : Ptr & downtime )
2013-08-28 16:08:22 +02:00
{
2013-09-17 16:51:55 +02:00
Host : : Ptr host = service - > GetHost ( ) ;
if ( ! downtime ) {
2013-09-25 13:08:15 +02:00
Log ( LogWarning , " db_ido " , " downtime does not exist. not updating it. " ) ;
2013-09-17 16:51:55 +02:00
return ;
}
2013-11-09 22:08:26 +01:00
Log ( LogDebug , " db_ido " , " updating triggered service downtime (id = " + Convert : : ToString ( downtime - > GetLegacyId ( ) ) + " ) for ' " + service - > GetName ( ) + " ' " ) ;
2013-09-17 16:51:55 +02:00
double now = Utility : : GetTime ( ) ;
2013-11-11 15:06:23 +01:00
std : : pair < unsigned long , unsigned long > time_bag = CompatUtility : : ConvertTimestamp ( now ) ;
2013-09-17 16:51:55 +02:00
2013-09-24 13:27:02 +02:00
/* Status */
2013-09-17 16:51:55 +02:00
DbQuery query1 ;
query1 . Table = " scheduleddowntime " ;
query1 . Type = DbQueryUpdate ;
2013-10-29 15:54:43 +01:00
query1 . Category = DbCatDowntime ;
2013-09-17 16:51:55 +02:00
2013-11-06 08:51:56 +01:00
Dictionary : : Ptr fields1 = make_shared < Dictionary > ( ) ;
2013-09-17 16:51:55 +02:00
fields1 - > Set ( " was_started " , 1 ) ;
2013-11-11 15:06:23 +01:00
fields1 - > Set ( " actual_start_time " , DbValue : : FromTimestamp ( time_bag . first ) ) ;
fields1 - > Set ( " actual_start_time_usec " , time_bag . second ) ;
2013-09-17 16:51:55 +02:00
fields1 - > Set ( " is_in_effect " , 1 ) ;
2013-11-09 22:08:26 +01:00
fields1 - > Set ( " trigger_time " , DbValue : : FromTimestamp ( downtime - > GetTriggerTime ( ) ) ) ;
2013-09-24 11:49:02 +02:00
fields1 - > Set ( " instance_id " , 0 ) ; /* DbConnection class fills in real ID */
2013-09-17 16:51:55 +02:00
2013-11-06 08:51:56 +01:00
query1 . WhereCriteria = make_shared < Dictionary > ( ) ;
2013-09-17 16:51:55 +02:00
query1 . WhereCriteria - > Set ( " object_id " , service ) ;
2013-11-09 22:08:26 +01:00
query1 . WhereCriteria - > Set ( " internal_downtime_id " , downtime - > GetLegacyId ( ) ) ;
2013-09-17 16:51:55 +02:00
query1 . Fields = fields1 ;
OnQuery ( query1 ) ;
/* delete hostcheck service's host comments */
2013-09-25 09:12:15 +02:00
if ( host - > GetCheckService ( ) = = service ) {
2013-11-19 12:55:37 +01:00
query1 . WhereCriteria = query1 . WhereCriteria - > ShallowClone ( ) ;
2013-09-25 18:24:22 +02:00
query1 . WhereCriteria - > Set ( " object_id " , host ) ;
OnQuery ( query1 ) ;
2013-09-17 16:55:29 +02:00
}
2013-09-24 13:27:02 +02:00
/* History - downtime was started for service (and host in case) */
DbQuery query3 ;
query3 . Table = " downtimehistory " ;
query3 . Type = DbQueryUpdate ;
2013-10-29 15:54:43 +01:00
query3 . Category = DbCatDowntime ;
2013-09-24 13:27:02 +02:00
2013-11-06 08:51:56 +01:00
Dictionary : : Ptr fields3 = make_shared < Dictionary > ( ) ;
2013-09-24 13:27:02 +02:00
fields3 - > Set ( " was_started " , 1 ) ;
fields3 - > Set ( " is_in_effect " , 1 ) ;
2013-11-11 15:06:23 +01:00
fields3 - > Set ( " actual_start_time " , DbValue : : FromTimestamp ( time_bag . first ) ) ;
fields3 - > Set ( " actual_start_time_usec " , time_bag . second ) ;
2013-11-09 22:08:26 +01:00
fields3 - > Set ( " trigger_time " , DbValue : : FromTimestamp ( downtime - > GetTriggerTime ( ) ) ) ;
2013-09-24 13:27:02 +02:00
query3 . Fields = fields3 ;
2013-11-06 08:51:56 +01:00
query3 . WhereCriteria = make_shared < Dictionary > ( ) ;
2013-11-09 22:08:26 +01:00
query3 . WhereCriteria - > Set ( " internal_downtime_id " , downtime - > GetLegacyId ( ) ) ;
query3 . WhereCriteria - > Set ( " entry_time " , DbValue : : FromTimestamp ( downtime - > GetEntryTime ( ) ) ) ;
query3 . WhereCriteria - > Set ( " scheduled_start_time " , DbValue : : FromTimestamp ( downtime - > GetStartTime ( ) ) ) ;
query3 . WhereCriteria - > Set ( " scheduled_end_time " , DbValue : : FromTimestamp ( downtime - > GetEndTime ( ) ) ) ;
2013-09-24 13:27:02 +02:00
query3 . WhereCriteria - > Set ( " instance_id " , 0 ) ; /* DbConnection class fills in real ID */
OnQuery ( query3 ) ;
2013-08-28 16:08:22 +02:00
}
2013-09-24 11:49:02 +02:00
2013-09-30 17:36:14 +02:00
/* acknowledgements */
2013-09-25 16:59:08 +02:00
void ServiceDbObject : : AddAcknowledgementHistory ( const Service : : Ptr & service , const String & author , const String & comment ,
2013-09-25 11:31:35 +02:00
AcknowledgementType type , double expiry )
2013-09-24 11:49:02 +02:00
{
Host : : Ptr host = service - > GetHost ( ) ;
2013-10-01 12:45:58 +02:00
Log ( LogDebug , " db_ido " , " add acknowledgement history for ' " + service - > GetName ( ) + " ' " ) ;
2013-09-24 11:49:02 +02:00
double now = Utility : : GetTime ( ) ;
2013-11-11 15:06:23 +01:00
std : : pair < unsigned long , unsigned long > time_bag = CompatUtility : : ConvertTimestamp ( now ) ;
2013-10-31 14:18:20 +01:00
2013-09-24 11:49:02 +02:00
unsigned long end_time = static_cast < long > ( expiry ) ;
DbQuery query1 ;
query1 . Table = " acknowledgements " ;
query1 . Type = DbQueryInsert ;
2013-10-29 15:54:43 +01:00
query1 . Category = DbCatAcknowledgement ;
2013-09-24 11:49:02 +02:00
2013-11-06 08:51:56 +01:00
Dictionary : : Ptr fields1 = make_shared < Dictionary > ( ) ;
2013-11-11 15:06:23 +01:00
fields1 - > Set ( " entry_time " , DbValue : : FromTimestamp ( time_bag . first ) ) ;
fields1 - > Set ( " entry_time_usec " , time_bag . second ) ;
2013-09-24 11:49:02 +02:00
fields1 - > Set ( " acknowledgement_type " , type ) ;
fields1 - > Set ( " object_id " , service ) ;
2013-12-05 14:54:16 +01:00
fields1 - > Set ( " state " , CompatUtility : : GetServiceCurrentState ( service ) ) ;
2013-09-24 11:49:02 +02:00
fields1 - > Set ( " author_name " , author ) ;
fields1 - > Set ( " comment_data " , comment ) ;
fields1 - > Set ( " is_sticky " , type = = AcknowledgementSticky ? 1 : 0 ) ;
fields1 - > Set ( " end_time " , DbValue : : FromTimestamp ( end_time ) ) ;
fields1 - > Set ( " instance_id " , 0 ) ; /* DbConnection class fills in real ID */
query1 . Fields = fields1 ;
OnQuery ( query1 ) ;
2013-09-25 09:12:15 +02:00
if ( host - > GetCheckService ( ) = = service ) {
2013-11-19 12:55:37 +01:00
fields1 = fields1 - > ShallowClone ( ) ;
2013-09-25 18:24:22 +02:00
fields1 - > Set ( " object_id " , host ) ;
fields1 - > Set ( " state " , host - > GetState ( ) ) ;
query1 . Fields = fields1 ;
OnQuery ( query1 ) ;
2013-09-24 11:49:02 +02:00
}
}
2013-09-30 17:36:14 +02:00
/* notifications */
2013-10-01 12:45:58 +02:00
void ServiceDbObject : : AddContactNotificationHistory ( const Service : : Ptr & service , const User : : Ptr & user )
{
Host : : Ptr host = service - > GetHost ( ) ;
Log ( LogDebug , " db_ido " , " add contact notification history for ' " + service - > GetName ( ) + " ' " ) ;
/* start and end happen at the same time */
double now = Utility : : GetTime ( ) ;
2013-11-11 15:06:23 +01:00
std : : pair < unsigned long , unsigned long > time_bag = CompatUtility : : ConvertTimestamp ( now ) ;
2013-10-01 12:45:58 +02:00
DbQuery query1 ;
query1 . Table = " contactnotifications " ;
query1 . Type = DbQueryInsert ;
2013-10-29 15:54:43 +01:00
query1 . Category = DbCatNotification ;
2013-10-01 12:45:58 +02:00
2013-11-06 08:51:56 +01:00
Dictionary : : Ptr fields1 = make_shared < Dictionary > ( ) ;
2013-10-01 12:45:58 +02:00
fields1 - > Set ( " contact_object_id " , user ) ;
2013-11-11 15:06:23 +01:00
fields1 - > Set ( " start_time " , DbValue : : FromTimestamp ( time_bag . first ) ) ;
fields1 - > Set ( " start_time_usec " , time_bag . second ) ;
fields1 - > Set ( " end_time " , DbValue : : FromTimestamp ( time_bag . first ) ) ;
fields1 - > Set ( " end_time_usec " , time_bag . second ) ;
2013-10-01 12:45:58 +02:00
fields1 - > Set ( " notification_id " , 0 ) ; /* DbConnection class fills in real ID */
fields1 - > Set ( " instance_id " , 0 ) ; /* DbConnection class fills in real ID */
query1 . Fields = fields1 ;
OnQuery ( query1 ) ;
}
2013-09-25 16:59:08 +02:00
void ServiceDbObject : : AddNotificationHistory ( const Service : : Ptr & service , const std : : set < User : : Ptr > & users , NotificationType type ,
2013-11-09 14:22:38 +01:00
const CheckResult : : Ptr & cr , const String & author , const String & text )
2013-09-24 18:50:42 +02:00
{
Host : : Ptr host = service - > GetHost ( ) ;
2013-10-01 12:45:58 +02:00
Log ( LogDebug , " db_ido " , " add notification history for ' " + service - > GetName ( ) + " ' " ) ;
2013-09-24 18:50:42 +02:00
/* start and end happen at the same time */
double now = Utility : : GetTime ( ) ;
2013-11-11 15:06:23 +01:00
std : : pair < unsigned long , unsigned long > time_bag = CompatUtility : : ConvertTimestamp ( now ) ;
2013-09-24 18:50:42 +02:00
DbQuery query1 ;
query1 . Table = " notifications " ;
query1 . Type = DbQueryInsert ;
2013-10-29 15:54:43 +01:00
query1 . Category = DbCatNotification ;
2013-09-24 18:50:42 +02:00
2013-11-06 08:51:56 +01:00
Dictionary : : Ptr fields1 = make_shared < Dictionary > ( ) ;
2013-09-24 18:50:42 +02:00
fields1 - > Set ( " notification_type " , 1 ) ; /* service */
fields1 - > Set ( " notification_reason " , CompatUtility : : MapNotificationReasonType ( type ) ) ;
fields1 - > Set ( " object_id " , service ) ;
2013-11-11 15:06:23 +01:00
fields1 - > Set ( " start_time " , DbValue : : FromTimestamp ( time_bag . first ) ) ;
fields1 - > Set ( " start_time_usec " , time_bag . second ) ;
fields1 - > Set ( " end_time " , DbValue : : FromTimestamp ( time_bag . first ) ) ;
fields1 - > Set ( " end_time_usec " , time_bag . second ) ;
2013-12-05 14:54:16 +01:00
fields1 - > Set ( " state " , CompatUtility : : GetServiceCurrentState ( service ) ) ;
2013-09-24 18:50:42 +02:00
if ( cr ) {
2013-12-05 14:54:16 +01:00
fields1 - > Set ( " output " , CompatUtility : : GetCheckResultOutput ( cr ) ) ;
fields1 - > Set ( " long_output " , CompatUtility : : GetCheckResultLongOutput ( cr ) ) ;
2013-09-24 18:50:42 +02:00
}
fields1 - > Set ( " escalated " , 0 ) ;
2013-10-10 23:46:45 +02:00
fields1 - > Set ( " contacts_notified " , static_cast < long > ( users . size ( ) ) ) ;
2013-09-24 18:50:42 +02:00
fields1 - > Set ( " instance_id " , 0 ) ; /* DbConnection class fills in real ID */
query1 . Fields = fields1 ;
OnQuery ( query1 ) ;
2013-09-25 09:12:15 +02:00
if ( host - > GetCheckService ( ) = = service ) {
2013-11-19 12:55:37 +01:00
fields1 = fields1 - > ShallowClone ( ) ;
2013-09-25 18:24:22 +02:00
fields1 - > Set ( " notification_type " , 2 ) ; /* host */
fields1 - > Set ( " object_id " , host ) ;
fields1 - > Set ( " state " , host - > GetState ( ) ) ;
query1 . Fields = fields1 ;
OnQuery ( query1 ) ;
2013-09-24 18:50:42 +02:00
}
}
2013-09-25 18:01:08 +02:00
2013-09-30 17:36:14 +02:00
/* statehistory */
2013-11-09 14:22:38 +01:00
void ServiceDbObject : : AddStateChangeHistory ( const Service : : Ptr & service , const CheckResult : : Ptr & cr , StateType type )
2013-09-25 18:01:08 +02:00
{
Host : : Ptr host = service - > GetHost ( ) ;
2013-10-01 12:45:58 +02:00
Log ( LogDebug , " db_ido " , " add state change history for ' " + service - > GetName ( ) + " ' " ) ;
2013-09-25 18:01:08 +02:00
double now = Utility : : GetTime ( ) ;
2013-11-11 15:06:23 +01:00
std : : pair < unsigned long , unsigned long > time_bag = CompatUtility : : ConvertTimestamp ( now ) ;
2013-09-25 18:01:08 +02:00
DbQuery query1 ;
query1 . Table = " statehistory " ;
query1 . Type = DbQueryInsert ;
2013-10-29 15:54:43 +01:00
query1 . Category = DbCatStateHistory ;
2013-09-25 18:01:08 +02:00
2013-11-06 08:51:56 +01:00
Dictionary : : Ptr fields1 = make_shared < Dictionary > ( ) ;
2013-11-11 15:06:23 +01:00
fields1 - > Set ( " state_time " , DbValue : : FromTimestamp ( time_bag . first ) ) ;
fields1 - > Set ( " state_time_usec " , time_bag . second ) ;
2013-09-25 18:01:08 +02:00
fields1 - > Set ( " object_id " , service ) ;
fields1 - > Set ( " state_change " , 1 ) ; /* service */
2013-12-05 14:54:16 +01:00
fields1 - > Set ( " state " , CompatUtility : : GetServiceCurrentState ( service ) ) ;
2013-09-25 18:01:08 +02:00
fields1 - > Set ( " state_type " , service - > GetStateType ( ) ) ;
2013-10-26 09:41:45 +02:00
fields1 - > Set ( " current_check_attempt " , service - > GetCheckAttempt ( ) ) ;
2013-09-25 18:01:08 +02:00
fields1 - > Set ( " max_check_attempts " , service - > GetMaxCheckAttempts ( ) ) ;
fields1 - > Set ( " last_state " , service - > GetLastState ( ) ) ;
fields1 - > Set ( " last_hard_state " , service - > GetLastHardState ( ) ) ;
if ( cr ) {
2013-12-05 14:54:16 +01:00
fields1 - > Set ( " output " , CompatUtility : : GetCheckResultOutput ( cr ) ) ;
fields1 - > Set ( " long_output " , CompatUtility : : GetCheckResultLongOutput ( cr ) ) ;
2013-09-25 18:01:08 +02:00
}
fields1 - > Set ( " instance_id " , 0 ) ; /* DbConnection class fills in real ID */
query1 . Fields = fields1 ;
OnQuery ( query1 ) ;
if ( host - > GetCheckService ( ) = = service ) {
2013-11-19 12:55:37 +01:00
fields1 = fields1 - > ShallowClone ( ) ;
2013-09-25 18:01:08 +02:00
fields1 - > Set ( " object_id " , host ) ;
fields1 - > Set ( " state_change " , 0 ) ; /* host */
/* get host states instead */
fields1 - > Set ( " state " , host - > GetState ( ) ) ;
fields1 - > Set ( " state_type " , host - > GetStateType ( ) ) ;
fields1 - > Set ( " last_state " , host - > GetLastState ( ) ) ;
fields1 - > Set ( " last_hard_state " , host - > GetLastHardState ( ) ) ;
query1 . Fields = fields1 ;
OnQuery ( query1 ) ;
}
}
2013-09-25 19:47:23 +02:00
2013-09-30 17:36:14 +02:00
/* logentries */
2013-11-09 14:22:38 +01:00
void ServiceDbObject : : AddCheckResultLogHistory ( const Service : : Ptr & service , const CheckResult : : Ptr & cr )
2013-09-25 19:47:23 +02:00
{
Host : : Ptr host = service - > GetHost ( ) ;
2013-11-09 14:22:38 +01:00
Dictionary : : Ptr vars_after = cr - > GetVarsAfter ( ) ;
2013-09-25 19:47:23 +02: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 " ) ;
bool host_reachable_after = vars_after - > Get ( " host_reachable " ) ;
2013-11-09 14:22:38 +01:00
Dictionary : : Ptr vars_before = cr - > GetVarsBefore ( ) ;
2013-09-25 19:47:23 +02: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 & &
attempt_before = = attempt_after & & reachable_before = = reachable_after )
return ; /* Nothing changed, ignore this checkresult. */
}
LogEntryType type ;
switch ( service - > GetState ( ) ) {
case StateOK :
type = LogEntryTypeServiceOk ;
break ;
case StateUnknown :
type = LogEntryTypeServiceUnknown ;
break ;
case StateWarning :
type = LogEntryTypeServiceWarning ;
break ;
case StateCritical :
type = LogEntryTypeServiceCritical ;
break ;
default :
Log ( LogCritical , " db_ido " , " Unknown service state: " + Convert : : ToString ( state_after ) ) ;
return ;
}
2013-11-09 14:22:38 +01:00
String output ;
2013-09-25 19:47:23 +02:00
2013-12-05 14:54:16 +01:00
if ( cr )
output = CompatUtility : : GetCheckResultOutput ( cr ) ;
2013-09-25 19:47:23 +02:00
std : : ostringstream msgbuf ;
msgbuf < < " SERVICE ALERT: "
< < host - > GetName ( ) < < " ; "
< < service - > GetShortName ( ) < < " ; "
< < Service : : StateToString ( static_cast < ServiceState > ( state_after ) ) < < " ; "
< < Service : : StateTypeToString ( static_cast < StateType > ( stateType_after ) ) < < " ; "
< < attempt_after < < " ; "
< < output < < " "
< < " " ;
AddLogHistory ( service , msgbuf . str ( ) , type ) ;
if ( service = = host - > GetCheckService ( ) ) {
std : : ostringstream msgbuf ;
msgbuf < < " HOST ALERT: "
< < host - > GetName ( ) < < " ; "
< < Host : : StateToString ( Host : : CalculateState ( static_cast < ServiceState > ( state_after ) , host_reachable_after ) ) < < " ; "
< < Service : : StateTypeToString ( static_cast < StateType > ( stateType_after ) ) < < " ; "
< < attempt_after < < " ; "
< < output < < " "
< < " " ;
switch ( host - > GetState ( ) ) {
case HostUp :
type = LogEntryTypeHostUp ;
break ;
case HostDown :
type = LogEntryTypeHostDown ;
break ;
case HostUnreachable :
type = LogEntryTypeHostUnreachable ;
break ;
default :
Log ( LogCritical , " db_ido " , " Unknown host state: " + Convert : : ToString ( state_after ) ) ;
return ;
}
AddLogHistory ( service , msgbuf . str ( ) , type ) ;
}
}
2013-11-09 22:08:26 +01:00
void ServiceDbObject : : AddTriggerDowntimeLogHistory ( const Service : : Ptr & service , const Downtime : : Ptr & downtime )
2013-09-25 19:47:23 +02:00
{
Host : : Ptr host = service - > GetHost ( ) ;
if ( ! downtime )
return ;
std : : ostringstream msgbuf ;
msgbuf < < " SERVICE DOWNTIME ALERT: "
< < host - > GetName ( ) < < " ; "
< < service - > GetShortName ( ) < < " ; "
< < " STARTED " < < " ; "
< < " Service has entered a period of scheduled downtime. "
< < " " ;
AddLogHistory ( service , msgbuf . str ( ) , LogEntryTypeInfoMessage ) ;
if ( service = = host - > GetCheckService ( ) ) {
std : : ostringstream msgbuf ;
msgbuf < < " HOST DOWNTIME ALERT: "
< < host - > GetName ( ) < < " ; "
< < " STARTED " < < " ; "
< < " Service has entered a period of scheduled downtime. "
< < " " ;
AddLogHistory ( service , msgbuf . str ( ) , LogEntryTypeInfoMessage ) ;
}
}
2013-11-09 22:08:26 +01:00
void ServiceDbObject : : AddRemoveDowntimeLogHistory ( const Service : : Ptr & service , const Downtime : : Ptr & downtime )
2013-09-25 19:47:23 +02:00
{
Host : : Ptr host = service - > GetHost ( ) ;
if ( ! downtime )
return ;
String downtime_output ;
String downtime_state_str ;
2013-11-09 22:08:26 +01:00
if ( downtime - > GetWasCancelled ( ) ) {
2013-09-25 19:47:23 +02:00
downtime_output = " Scheduled downtime for service has been cancelled. " ;
downtime_state_str = " CANCELLED " ;
} else {
downtime_output = " Service has exited from a period of scheduled downtime. " ;
downtime_state_str = " STOPPED " ;
}
std : : ostringstream msgbuf ;
msgbuf < < " SERVICE DOWNTIME ALERT: "
< < host - > GetName ( ) < < " ; "
< < service - > GetShortName ( ) < < " ; "
< < downtime_state_str < < " ; "
< < downtime_output
< < " " ;
AddLogHistory ( service , msgbuf . str ( ) , LogEntryTypeInfoMessage ) ;
if ( service = = host - > GetCheckService ( ) ) {
std : : ostringstream msgbuf ;
msgbuf < < " HOST DOWNTIME ALERT: "
< < host - > GetName ( ) < < " ; "
< < downtime_state_str < < " ; "
< < downtime_output
< < " " ;
AddLogHistory ( service , msgbuf . str ( ) , LogEntryTypeInfoMessage ) ;
}
}
void ServiceDbObject : : AddNotificationSentLogHistory ( const Service : : Ptr & service , const User : : Ptr & user ,
2013-11-09 14:22:38 +01:00
NotificationType notification_type , const CheckResult : : Ptr & cr ,
2013-09-25 19:47:23 +02:00
const String & author , const String & comment_text )
{
Host : : Ptr host = service - > GetHost ( ) ;
CheckCommand : : Ptr commandObj = service - > GetCheckCommand ( ) ;
String check_command = " " ;
if ( commandObj )
check_command = commandObj - > GetName ( ) ;
String notification_type_str = Notification : : NotificationTypeToString ( notification_type ) ;
String author_comment = " " ;
if ( notification_type = = NotificationCustom | | notification_type = = NotificationAcknowledgement ) {
author_comment = " ; " + author + " ; " + comment_text ;
}
2013-11-09 14:22:38 +01:00
if ( ! cr )
return ;
2013-09-25 19:47:23 +02:00
2013-11-09 14:22:38 +01:00
String output ;
2013-09-25 19:47:23 +02:00
2013-12-05 14:54:16 +01:00
if ( cr )
output = CompatUtility : : GetCheckResultOutput ( cr ) ;
2013-09-25 19:47:23 +02:00
2013-11-09 14:22:38 +01:00
std : : ostringstream msgbuf ;
msgbuf < < " SERVICE NOTIFICATION: "
< < user - > GetName ( ) < < " ; "
< < host - > GetName ( ) < < " ; "
< < service - > GetShortName ( ) < < " ; "
< < notification_type_str < < " "
< < " ( " < < Service : : StateToString ( service - > GetState ( ) ) < < " ); "
< < check_command < < " ; "
< < output < < author_comment
< < " " ;
2013-09-25 19:47:23 +02:00
2013-11-09 14:22:38 +01:00
AddLogHistory ( service , msgbuf . str ( ) , LogEntryTypeServiceNotification ) ;
2013-09-25 19:47:23 +02:00
2013-11-09 14:22:38 +01:00
if ( service = = host - > GetCheckService ( ) ) {
std : : ostringstream msgbuf ;
msgbuf < < " HOST NOTIFICATION: "
< < user - > GetName ( ) < < " ; "
< < host - > GetName ( ) < < " ; "
< < notification_type_str < < " "
< < " ( " < < Service : : StateToString ( service - > GetState ( ) ) < < " ); "
< < check_command < < " ; "
< < output < < author_comment
< < " " ;
2013-09-25 19:47:23 +02:00
2013-11-09 14:22:38 +01:00
AddLogHistory ( service , msgbuf . str ( ) , LogEntryTypeHostNotification ) ;
}
2013-09-25 19:47:23 +02:00
}
void ServiceDbObject : : AddFlappingLogHistory ( const Service : : Ptr & service , FlappingState flapping_state )
{
Host : : Ptr host = service - > GetHost ( ) ;
String flapping_state_str ;
String flapping_output ;
switch ( flapping_state ) {
case FlappingStarted :
flapping_output = " Service appears to have started flapping ( " + Convert : : ToString ( service - > GetFlappingCurrent ( ) ) + " % change >= " + Convert : : ToString ( service - > GetFlappingThreshold ( ) ) + " % threshold) " ;
flapping_state_str = " STARTED " ;
break ;
case FlappingStopped :
flapping_output = " Service appears to have stopped flapping ( " + Convert : : ToString ( service - > GetFlappingCurrent ( ) ) + " % change < " + Convert : : ToString ( service - > GetFlappingThreshold ( ) ) + " % threshold) " ;
flapping_state_str = " STOPPED " ;
break ;
case FlappingDisabled :
flapping_output = " Flap detection has been disabled " ;
flapping_state_str = " DISABLED " ;
break ;
default :
Log ( LogCritical , " db_ido " , " Unknown flapping state: " + Convert : : ToString ( flapping_state ) ) ;
return ;
}
std : : ostringstream msgbuf ;
msgbuf < < " SERVICE FLAPPING ALERT: "
< < host - > GetName ( ) < < " ; "
< < service - > GetShortName ( ) < < " ; "
< < flapping_state_str < < " ; "
< < flapping_output
< < " " ;
AddLogHistory ( service , msgbuf . str ( ) , LogEntryTypeInfoMessage ) ;
if ( service = = host - > GetCheckService ( ) ) {
std : : ostringstream msgbuf ;
msgbuf < < " HOST FLAPPING ALERT: "
< < host - > GetName ( ) < < " ; "
< < flapping_state_str < < " ; "
< < flapping_output
< < " " ;
AddLogHistory ( service , msgbuf . str ( ) , LogEntryTypeInfoMessage ) ;
}
}
void ServiceDbObject : : AddLogHistory ( const Service : : Ptr & service , String buffer , LogEntryType type )
{
Host : : Ptr host = service - > GetHost ( ) ;
2013-10-01 12:45:58 +02:00
Log ( LogDebug , " db_ido " , " add log entry history for ' " + service - > GetName ( ) + " ' " ) ;
2013-09-25 19:47:23 +02:00
double now = Utility : : GetTime ( ) ;
2013-11-11 15:06:23 +01:00
std : : pair < unsigned long , unsigned long > time_bag = CompatUtility : : ConvertTimestamp ( now ) ;
2013-09-25 19:47:23 +02:00
DbQuery query1 ;
query1 . Table = " logentries " ;
query1 . Type = DbQueryInsert ;
2013-10-29 15:54:43 +01:00
query1 . Category = DbCatLog ;
2013-09-25 19:47:23 +02:00
2013-11-06 08:51:56 +01:00
Dictionary : : Ptr fields1 = make_shared < Dictionary > ( ) ;
2013-11-11 15:06:23 +01:00
fields1 - > Set ( " logentry_time " , DbValue : : FromTimestamp ( time_bag . first ) ) ;
fields1 - > Set ( " entry_time " , DbValue : : FromTimestamp ( time_bag . first ) ) ;
fields1 - > Set ( " entry_time_usec " , time_bag . second ) ;
2013-10-03 03:41:47 +02:00
fields1 - > Set ( " object_id " , service ) ; // added in 1.10 see #4754
2013-09-25 19:47:23 +02:00
fields1 - > Set ( " logentry_type " , type ) ;
fields1 - > Set ( " logentry_data " , buffer ) ;
fields1 - > Set ( " instance_id " , 0 ) ; /* DbConnection class fills in real ID */
query1 . Fields = fields1 ;
OnQuery ( query1 ) ;
if ( host - > GetCheckService ( ) = = service ) {
2013-11-19 12:55:37 +01:00
fields1 = fields1 - > ShallowClone ( ) ;
2013-10-03 03:41:47 +02:00
fields1 - > Set ( " object_id " , host ) ; // added in 1.10 see #4754
2013-09-25 19:47:23 +02:00
query1 . Fields = fields1 ;
OnQuery ( query1 ) ;
}
}
2013-09-30 17:36:14 +02:00
/* flappinghistory */
void ServiceDbObject : : AddFlappingHistory ( const Service : : Ptr & service , FlappingState flapping_state )
{
Host : : Ptr host = service - > GetHost ( ) ;
Log ( LogDebug , " db_ido " , " add flapping history for ' " + service - > GetName ( ) + " ' " ) ;
double now = Utility : : GetTime ( ) ;
2013-11-11 15:06:23 +01:00
std : : pair < unsigned long , unsigned long > time_bag = CompatUtility : : ConvertTimestamp ( now ) ;
2013-09-30 17:36:14 +02:00
DbQuery query1 ;
query1 . Table = " flappinghistory " ;
query1 . Type = DbQueryInsert ;
2013-10-29 15:54:43 +01:00
query1 . Category = DbCatFlapping ;
2013-09-30 17:36:14 +02:00
2013-11-06 08:51:56 +01:00
Dictionary : : Ptr fields1 = make_shared < Dictionary > ( ) ;
2013-09-30 17:36:14 +02:00
2013-11-11 15:06:23 +01:00
fields1 - > Set ( " event_time " , DbValue : : FromTimestamp ( time_bag . first ) ) ;
fields1 - > Set ( " event_time_usec " , time_bag . second ) ;
2013-09-30 17:36:14 +02:00
switch ( flapping_state ) {
case FlappingStarted :
fields1 - > Set ( " event_type " , 1000 ) ;
break ;
case FlappingStopped :
fields1 - > Set ( " event_type " , 1001 ) ;
fields1 - > Set ( " reason_type " , 1 ) ;
break ;
case FlappingDisabled :
fields1 - > Set ( " event_type " , 1001 ) ;
fields1 - > Set ( " reason_type " , 2 ) ;
break ;
default :
Log ( LogDebug , " db_ido " , " Unhandled flapping state: " + Convert : : ToString ( flapping_state ) ) ;
return ;
}
fields1 - > Set ( " flapping_type " , 1 ) ; /* service */
fields1 - > Set ( " object_id " , service ) ;
fields1 - > Set ( " percent_state_change " , service - > GetFlappingCurrent ( ) ) ;
fields1 - > Set ( " low_threshold " , service - > GetFlappingThreshold ( ) ) ;
fields1 - > Set ( " high_threshold " , service - > GetFlappingThreshold ( ) ) ;
fields1 - > Set ( " instance_id " , 0 ) ; /* DbConnection class fills in real ID */
query1 . Fields = fields1 ;
OnQuery ( query1 ) ;
if ( host - > GetCheckService ( ) = = service ) {
2013-11-19 12:55:37 +01:00
fields1 = fields1 - > ShallowClone ( ) ;
2013-09-30 17:36:14 +02:00
fields1 - > Set ( " object_id " , host ) ;
fields1 - > Set ( " flapping_type " , 0 ) ; /* host */
query1 . Fields = fields1 ;
OnQuery ( query1 ) ;
}
}
2013-09-30 18:39:13 +02:00
/* servicechecks */
2013-11-09 14:22:38 +01:00
void ServiceDbObject : : AddServiceCheckHistory ( const Service : : Ptr & service , const CheckResult : : Ptr & cr )
2013-09-30 18:39:13 +02:00
{
2013-12-05 14:54:16 +01:00
if ( ! cr )
return ;
2013-09-30 18:39:13 +02:00
Host : : Ptr host = service - > GetHost ( ) ;
Log ( LogDebug , " db_ido " , " add service check history for ' " + service - > GetName ( ) + " ' " ) ;
DbQuery query1 ;
query1 . Table = " servicechecks " ;
query1 . Type = DbQueryInsert ;
2013-10-29 15:54:43 +01:00
query1 . Category = DbCatCheck ;
2013-09-30 18:39:13 +02:00
2013-11-06 08:51:56 +01:00
Dictionary : : Ptr fields1 = make_shared < Dictionary > ( ) ;
2013-12-05 14:54:16 +01:00
double execution_time = Service : : CalculateExecutionTime ( cr ) ;
2013-09-30 18:39:13 +02:00
2013-12-05 14:54:16 +01:00
fields1 - > Set ( " check_type " , CompatUtility : : GetServiceCheckType ( service ) ) ;
fields1 - > Set ( " current_check_attempt " , service - > GetCheckAttempt ( ) ) ;
fields1 - > Set ( " max_check_attempts " , service - > GetMaxCheckAttempts ( ) ) ;
fields1 - > Set ( " state " , CompatUtility : : GetServiceCurrentState ( service ) ) ;
fields1 - > Set ( " state_type " , service - > GetStateType ( ) ) ;
2013-09-30 18:39:13 +02:00
double now = Utility : : GetTime ( ) ;
2013-11-11 15:06:23 +01:00
std : : pair < unsigned long , unsigned long > time_bag = CompatUtility : : ConvertTimestamp ( now ) ;
2013-09-30 18:39:13 +02:00
2013-12-05 14:54:16 +01:00
double end = now + execution_time ;
std : : pair < unsigned long , unsigned long > time_bag_end = CompatUtility : : ConvertTimestamp ( end ) ;
2013-09-30 18:39:13 +02:00
2013-11-11 15:06:23 +01:00
fields1 - > Set ( " start_time " , DbValue : : FromTimestamp ( time_bag . first ) ) ;
fields1 - > Set ( " start_time_usec " , time_bag . second ) ;
2013-12-05 14:54:16 +01:00
fields1 - > Set ( " end_time " , DbValue : : FromTimestamp ( time_bag_end . first ) ) ;
fields1 - > Set ( " end_time_usec " , time_bag_end . second ) ;
2013-09-30 18:39:13 +02:00
fields1 - > Set ( " command_object_id " , service - > GetCheckCommand ( ) ) ;
fields1 - > Set ( " command_args " , Empty ) ;
2013-11-09 14:22:38 +01:00
fields1 - > Set ( " command_line " , cr - > GetCommand ( ) ) ;
2013-12-05 14:54:16 +01:00
fields1 - > Set ( " execution_time " , execution_time ) ;
fields1 - > Set ( " latency " , Service : : CalculateLatency ( cr ) ) ;
2013-11-09 14:22:38 +01:00
fields1 - > Set ( " return_code " , cr - > GetExitStatus ( ) ) ;
2013-12-05 14:54:16 +01:00
fields1 - > Set ( " output " , CompatUtility : : GetCheckResultOutput ( cr ) ) ;
fields1 - > Set ( " long_output " , CompatUtility : : GetCheckResultLongOutput ( cr ) ) ;
fields1 - > Set ( " perfdata " , CompatUtility : : GetCheckResultPerfdata ( cr ) ) ;
2013-09-30 18:39:13 +02:00
fields1 - > Set ( " instance_id " , 0 ) ; /* DbConnection class fills in real ID */
query1 . Fields = fields1 ;
OnQuery ( query1 ) ;
if ( host - > GetCheckService ( ) = = service ) {
query1 . Table = " hostchecks " ;
2013-11-19 12:55:37 +01:00
fields1 = fields1 - > ShallowClone ( ) ;
fields1 - > Remove ( " service_object_id " ) ;
fields1 - > Set ( " host_object_id " , host ) ;
fields1 - > Set ( " state " , host - > GetState ( ) ) ;
fields1 - > Set ( " state_type " , host - > GetStateType ( ) ) ;
query1 . Fields = fields1 ;
2013-09-30 18:39:13 +02:00
OnQuery ( query1 ) ;
}
}
2013-09-30 19:32:32 +02:00
2013-10-01 11:04:30 +02:00
/* eventhandlers */
void ServiceDbObject : : AddEventHandlerHistory ( const Service : : Ptr & service )
{
Host : : Ptr host = service - > GetHost ( ) ;
Log ( LogDebug , " db_ido " , " add eventhandler history for ' " + service - > GetName ( ) + " ' " ) ;
double now = Utility : : GetTime ( ) ;
2013-11-11 15:06:23 +01:00
std : : pair < unsigned long , unsigned long > time_bag = CompatUtility : : ConvertTimestamp ( now ) ;
2013-10-01 11:04:30 +02:00
DbQuery query1 ;
query1 . Table = " eventhandlers " ;
query1 . Type = DbQueryInsert ;
2013-10-29 15:54:43 +01:00
query1 . Category = DbCatEventHandler ;
2013-10-01 11:04:30 +02:00
2013-11-06 08:51:56 +01:00
Dictionary : : Ptr fields1 = make_shared < Dictionary > ( ) ;
2013-10-01 11:04:30 +02:00
fields1 - > Set ( " eventhandler_type " , 1 ) ; /* service */
fields1 - > Set ( " object_id " , service ) ;
2013-12-05 14:54:16 +01:00
fields1 - > Set ( " state " , CompatUtility : : GetServiceCurrentState ( service ) ) ;
2013-10-01 11:04:30 +02:00
fields1 - > Set ( " state_type " , service - > GetStateType ( ) ) ;
2013-11-11 15:06:23 +01:00
fields1 - > Set ( " start_time " , DbValue : : FromTimestamp ( time_bag . first ) ) ;
fields1 - > Set ( " start_time_usec " , time_bag . second ) ;
fields1 - > Set ( " end_time " , DbValue : : FromTimestamp ( time_bag . first ) ) ;
fields1 - > Set ( " end_time_usec " , time_bag . second ) ;
2013-10-01 11:04:30 +02:00
fields1 - > Set ( " command_object_id " , service - > GetEventCommand ( ) ) ;
fields1 - > Set ( " instance_id " , 0 ) ; /* DbConnection class fills in real ID */
query1 . Fields = fields1 ;
OnQuery ( query1 ) ;
if ( host - > GetCheckService ( ) = = service ) {
2013-11-19 12:55:37 +01:00
fields1 = fields1 - > ShallowClone ( ) ;
2013-10-01 11:04:30 +02:00
fields1 - > Set ( " eventhandler_type " , 0 ) ; /* host */
fields1 - > Set ( " object_id " , host ) ;
fields1 - > Set ( " state " , host - > GetState ( ) ) ;
fields1 - > Set ( " state_type " , host - > GetStateType ( ) ) ;
query1 . Fields = fields1 ;
OnQuery ( query1 ) ;
}
}
2013-09-30 19:32:32 +02:00
/* externalcommands */
void ServiceDbObject : : AddExternalCommandHistory ( double time , const String & command , const std : : vector < String > & arguments )
{
Log ( LogDebug , " db_ido " , " add external command history " ) ;
DbQuery query1 ;
query1 . Table = " externalcommands " ;
query1 . Type = DbQueryInsert ;
2013-10-29 15:54:43 +01:00
query1 . Category = DbCatExternalCommand ;
2013-09-30 19:32:32 +02:00
2013-11-06 08:51:56 +01:00
Dictionary : : Ptr fields1 = make_shared < Dictionary > ( ) ;
2013-09-30 19:32:32 +02:00
fields1 - > Set ( " entry_time " , DbValue : : FromTimestamp ( static_cast < long > ( time ) ) ) ;
2013-09-30 20:25:53 +02:00
fields1 - > Set ( " command_type " , CompatUtility : : MapExternalCommandType ( command ) ) ;
2013-09-30 19:32:32 +02:00
fields1 - > Set ( " command_name " , command ) ;
fields1 - > Set ( " command_args " , boost : : algorithm : : join ( arguments , " ; " ) ) ;
fields1 - > Set ( " instance_id " , 0 ) ; /* DbConnection class fills in real ID */
query1 . Fields = fields1 ;
OnQuery ( query1 ) ;
2013-10-03 03:41:47 +02:00
}