2014-05-03 20:02:22 +02:00
/******************************************************************************
* Icinga 2 *
2015-01-22 12:00:23 +01:00
* Copyright ( C ) 2012 - 2015 Icinga Development Team ( http : //www.icinga.org) *
2014-05-03 20:02:22 +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 . *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2014-05-25 16:23:35 +02:00
# include "icinga/apievents.hpp"
# include "icinga/service.hpp"
2014-10-28 15:24:35 +01:00
# include "icinga/perfdatavalue.hpp"
2014-05-25 16:23:35 +02:00
# include "remote/apilistener.hpp"
# include "remote/endpoint.hpp"
# include "remote/messageorigin.hpp"
# include "remote/zone.hpp"
# include "remote/apifunction.hpp"
# include "base/application.hpp"
2015-08-15 20:28:05 +02:00
# include "base/configtype.hpp"
2014-05-25 16:23:35 +02:00
# include "base/utility.hpp"
# include "base/exception.hpp"
# include "base/initialize.hpp"
2014-10-26 19:59:49 +01:00
# include "base/serializer.hpp"
# include "base/json.hpp"
2014-05-03 20:02:22 +02:00
# include <fstream>
using namespace icinga ;
INITIALIZE_ONCE ( & ApiEvents : : StaticInitialize ) ;
REGISTER_APIFUNCTION ( CheckResult , event , & ApiEvents : : CheckResultAPIHandler ) ;
REGISTER_APIFUNCTION ( SetNextCheck , event , & ApiEvents : : NextCheckChangedAPIHandler ) ;
REGISTER_APIFUNCTION ( SetNextNotification , event , & ApiEvents : : NextNotificationChangedAPIHandler ) ;
REGISTER_APIFUNCTION ( SetForceNextCheck , event , & ApiEvents : : ForceNextCheckChangedAPIHandler ) ;
REGISTER_APIFUNCTION ( SetForceNextNotification , event , & ApiEvents : : ForceNextNotificationChangedAPIHandler ) ;
REGISTER_APIFUNCTION ( AddComment , event , & ApiEvents : : CommentAddedAPIHandler ) ;
REGISTER_APIFUNCTION ( RemoveComment , event , & ApiEvents : : CommentRemovedAPIHandler ) ;
REGISTER_APIFUNCTION ( AddDowntime , event , & ApiEvents : : DowntimeAddedAPIHandler ) ;
REGISTER_APIFUNCTION ( RemoveDowntime , event , & ApiEvents : : DowntimeRemovedAPIHandler ) ;
REGISTER_APIFUNCTION ( SetAcknowledgement , event , & ApiEvents : : AcknowledgementSetAPIHandler ) ;
REGISTER_APIFUNCTION ( ClearAcknowledgement , event , & ApiEvents : : AcknowledgementClearedAPIHandler ) ;
REGISTER_APIFUNCTION ( UpdateRepository , event , & ApiEvents : : UpdateRepositoryAPIHandler ) ;
2014-11-13 11:23:57 +01:00
REGISTER_APIFUNCTION ( ExecuteCommand , event , & ApiEvents : : ExecuteCommandAPIHandler ) ;
2014-05-03 20:02:22 +02:00
static Timer : : Ptr l_RepositoryTimer ;
void ApiEvents : : StaticInitialize ( void )
{
Checkable : : OnNewCheckResult . connect ( & ApiEvents : : CheckResultHandler ) ;
Checkable : : OnNextCheckChanged . connect ( & ApiEvents : : NextCheckChangedHandler ) ;
Notification : : OnNextNotificationChanged . connect ( & ApiEvents : : NextNotificationChangedHandler ) ;
Checkable : : OnForceNextCheckChanged . connect ( & ApiEvents : : ForceNextCheckChangedHandler ) ;
Checkable : : OnForceNextNotificationChanged . connect ( & ApiEvents : : ForceNextNotificationChangedHandler ) ;
2015-09-28 19:03:13 +02:00
2014-05-03 20:02:22 +02:00
Checkable : : OnCommentAdded . connect ( & ApiEvents : : CommentAddedHandler ) ;
Checkable : : OnCommentRemoved . connect ( & ApiEvents : : CommentRemovedHandler ) ;
Checkable : : OnDowntimeAdded . connect ( & ApiEvents : : DowntimeAddedHandler ) ;
Checkable : : OnDowntimeRemoved . connect ( & ApiEvents : : DowntimeRemovedHandler ) ;
Checkable : : OnAcknowledgementSet . connect ( & ApiEvents : : AcknowledgementSetHandler ) ;
Checkable : : OnAcknowledgementCleared . connect ( & ApiEvents : : AcknowledgementClearedHandler ) ;
2014-11-08 21:17:16 +01:00
l_RepositoryTimer = new Timer ( ) ;
2014-05-08 15:46:47 +02:00
l_RepositoryTimer - > SetInterval ( 30 ) ;
2014-05-03 20:02:22 +02:00
l_RepositoryTimer - > OnTimerExpired . connect ( boost : : bind ( & ApiEvents : : RepositoryTimerHandler ) ) ;
l_RepositoryTimer - > Start ( ) ;
l_RepositoryTimer - > Reschedule ( 0 ) ;
}
2014-11-13 11:23:57 +01:00
Dictionary : : Ptr ApiEvents : : MakeCheckResultMessage ( const Checkable : : Ptr & checkable , const CheckResult : : Ptr & cr )
2014-05-03 20:02:22 +02:00
{
2014-11-08 21:17:16 +01:00
Dictionary : : Ptr message = new Dictionary ( ) ;
2014-05-03 20:02:22 +02:00
message - > Set ( " jsonrpc " , " 2.0 " ) ;
message - > Set ( " method " , " event::CheckResult " ) ;
Host : : Ptr host ;
Service : : Ptr service ;
tie ( host , service ) = GetHostService ( checkable ) ;
2014-11-08 21:17:16 +01:00
Dictionary : : Ptr params = new Dictionary ( ) ;
2014-08-22 12:59:24 +02:00
params - > Set ( " host " , host - > GetName ( ) ) ;
2014-05-03 20:02:22 +02:00
if ( service )
params - > Set ( " service " , service - > GetShortName ( ) ) ;
2014-11-13 11:23:57 +01:00
else {
Value agent_service_name = checkable - > GetExtension ( " agent_service_name " ) ;
if ( ! agent_service_name . IsEmpty ( ) )
params - > Set ( " service " , agent_service_name ) ;
}
2014-05-03 20:02:22 +02:00
params - > Set ( " cr " , Serialize ( cr ) ) ;
message - > Set ( " params " , params ) ;
2014-11-13 11:23:57 +01:00
return message ;
}
2015-08-04 14:47:44 +02:00
void ApiEvents : : CheckResultHandler ( const Checkable : : Ptr & checkable , const CheckResult : : Ptr & cr , const MessageOrigin : : Ptr & origin )
2014-11-13 11:23:57 +01:00
{
ApiListener : : Ptr listener = ApiListener : : GetInstance ( ) ;
if ( ! listener )
return ;
Dictionary : : Ptr message = MakeCheckResultMessage ( checkable , cr ) ;
2014-05-03 20:02:22 +02:00
listener - > RelayMessage ( origin , checkable , message , true ) ;
}
2015-08-04 14:47:44 +02:00
Value ApiEvents : : CheckResultAPIHandler ( const MessageOrigin : : Ptr & origin , const Dictionary : : Ptr & params )
2014-05-03 20:02:22 +02:00
{
2015-08-04 14:47:44 +02:00
Endpoint : : Ptr endpoint = origin - > FromClient - > GetEndpoint ( ) ;
2014-11-13 11:23:57 +01:00
2015-07-02 13:04:38 +02:00
if ( ! endpoint ) {
Log ( LogNotice , " ApiEvents " )
2015-08-04 14:47:44 +02:00
< < " Discarding 'check result' message from ' " < < origin - > FromClient - > GetIdentity ( ) < < " ': Invalid endpoint origin (client not allowed). " ;
2014-10-16 13:04:04 +02:00
return Empty ;
2015-07-02 13:04:38 +02:00
}
2014-10-16 13:04:04 +02:00
2014-05-03 20:02:22 +02:00
if ( ! params )
return Empty ;
2014-11-08 21:17:16 +01:00
CheckResult : : Ptr cr = new CheckResult ( ) ;
2014-10-28 15:24:35 +01:00
Dictionary : : Ptr vcr = params - > Get ( " cr " ) ;
Array : : Ptr vperf = vcr - > Get ( " performance_data " ) ;
vcr - > Remove ( " performance_data " ) ;
2014-10-25 09:14:56 +02:00
Deserialize ( cr , params - > Get ( " cr " ) , true ) ;
2014-05-03 20:02:22 +02:00
2014-11-08 21:17:16 +01:00
Array : : Ptr rperf = new Array ( ) ;
2014-10-28 15:24:35 +01:00
2014-11-05 09:58:43 +01:00
if ( vperf ) {
ObjectLock olock ( vperf ) ;
BOOST_FOREACH ( const Value & vp , vperf ) {
Value p ;
if ( vp . IsObjectType < Dictionary > ( ) ) {
2014-11-08 21:17:16 +01:00
PerfdataValue : : Ptr val = new PerfdataValue ( ) ;
2014-11-05 09:58:43 +01:00
Deserialize ( val , vp , true ) ;
rperf - > Add ( val ) ;
} else
rperf - > Add ( vp ) ;
}
2014-10-28 15:24:35 +01:00
}
cr - > SetPerformanceData ( rperf ) ;
2014-10-28 14:05:17 +01:00
Host : : Ptr host = Host : : GetByName ( params - > Get ( " host " ) ) ;
2014-05-03 20:02:22 +02:00
if ( ! host )
return Empty ;
Checkable : : Ptr checkable ;
if ( params - > Contains ( " service " ) )
checkable = host - > GetServiceByShortName ( params - > Get ( " service " ) ) ;
else
checkable = host ;
if ( ! checkable )
return Empty ;
2015-08-04 14:47:44 +02:00
if ( origin - > FromZone & & ! origin - > FromZone - > CanAccessObject ( checkable ) & & endpoint ! = checkable - > GetCommandEndpoint ( ) ) {
2015-07-02 13:04:38 +02:00
Log ( LogNotice , " ApiEvents " )
2015-08-04 14:47:44 +02:00
< < " Discarding 'check result' message from ' " < < origin - > FromClient - > GetIdentity ( ) < < " ': Unauthorized access. " ;
2014-05-03 20:02:22 +02:00
return Empty ;
2015-07-02 13:04:38 +02:00
}
2014-05-03 20:02:22 +02:00
2015-03-30 13:39:24 +02:00
if ( endpoint = = checkable - > GetCommandEndpoint ( ) )
checkable - > ProcessCheckResult ( cr ) ;
else
checkable - > ProcessCheckResult ( cr , origin ) ;
2014-05-03 20:02:22 +02:00
return Empty ;
}
2015-08-04 14:47:44 +02:00
void ApiEvents : : NextCheckChangedHandler ( const Checkable : : Ptr & checkable , const MessageOrigin : : Ptr & origin )
2014-05-03 20:02:22 +02:00
{
ApiListener : : Ptr listener = ApiListener : : GetInstance ( ) ;
if ( ! listener )
return ;
Host : : Ptr host ;
Service : : Ptr service ;
tie ( host , service ) = GetHostService ( checkable ) ;
2014-11-08 21:17:16 +01:00
Dictionary : : Ptr params = new Dictionary ( ) ;
2014-08-22 12:59:24 +02:00
params - > Set ( " host " , host - > GetName ( ) ) ;
2014-05-03 20:02:22 +02:00
if ( service )
params - > Set ( " service " , service - > GetShortName ( ) ) ;
2015-08-04 14:47:44 +02:00
params - > Set ( " next_check " , checkable - > GetNextCheck ( ) ) ;
2014-05-03 20:02:22 +02:00
2014-11-08 21:17:16 +01:00
Dictionary : : Ptr message = new Dictionary ( ) ;
2014-05-03 20:02:22 +02:00
message - > Set ( " jsonrpc " , " 2.0 " ) ;
message - > Set ( " method " , " event::SetNextCheck " ) ;
message - > Set ( " params " , params ) ;
listener - > RelayMessage ( origin , checkable , message , true ) ;
}
2015-08-04 14:47:44 +02:00
Value ApiEvents : : NextCheckChangedAPIHandler ( const MessageOrigin : : Ptr & origin , const Dictionary : : Ptr & params )
2014-05-03 20:02:22 +02:00
{
2015-08-04 14:47:44 +02:00
Endpoint : : Ptr endpoint = origin - > FromClient - > GetEndpoint ( ) ;
2015-07-02 13:04:38 +02:00
if ( ! endpoint ) {
Log ( LogNotice , " ApiEvents " )
2015-08-04 14:47:44 +02:00
< < " Discarding 'next check changed' message from ' " < < origin - > FromClient - > GetIdentity ( ) < < " ': Invalid endpoint origin (client not allowed). " ;
2014-10-16 13:04:04 +02:00
return Empty ;
2015-07-02 13:04:38 +02:00
}
2014-10-16 13:04:04 +02:00
2014-05-03 20:02:22 +02:00
if ( ! params )
return Empty ;
2014-10-28 14:05:17 +01:00
Host : : Ptr host = Host : : GetByName ( params - > Get ( " host " ) ) ;
2014-05-03 20:02:22 +02:00
if ( ! host )
return Empty ;
Checkable : : Ptr checkable ;
if ( params - > Contains ( " service " ) )
checkable = host - > GetServiceByShortName ( params - > Get ( " service " ) ) ;
else
checkable = host ;
if ( ! checkable )
return Empty ;
2015-08-04 14:47:44 +02:00
if ( origin - > FromZone & & ! origin - > FromZone - > CanAccessObject ( checkable ) ) {
2015-07-02 13:04:38 +02:00
Log ( LogNotice , " ApiEvents " )
2015-08-04 14:47:44 +02:00
< < " Discarding 'next check changed' message from ' " < < origin - > FromClient - > GetIdentity ( ) < < " ': Unauthorized access. " ;
2014-05-03 20:02:22 +02:00
return Empty ;
2015-07-02 13:04:38 +02:00
}
2014-05-03 20:02:22 +02:00
2015-08-04 14:47:44 +02:00
checkable - > SetNextCheck ( params - > Get ( " next_check " ) , false , origin ) ;
2014-05-03 20:02:22 +02:00
return Empty ;
}
2015-08-04 14:47:44 +02:00
void ApiEvents : : NextNotificationChangedHandler ( const Notification : : Ptr & notification , const MessageOrigin : : Ptr & origin )
2014-05-03 20:02:22 +02:00
{
ApiListener : : Ptr listener = ApiListener : : GetInstance ( ) ;
if ( ! listener )
return ;
2014-11-08 21:17:16 +01:00
Dictionary : : Ptr params = new Dictionary ( ) ;
2014-05-03 20:02:22 +02:00
params - > Set ( " notification " , notification - > GetName ( ) ) ;
2015-08-04 14:47:44 +02:00
params - > Set ( " next_notification " , notification - > GetNextNotification ( ) ) ;
2014-05-03 20:02:22 +02:00
2014-11-08 21:17:16 +01:00
Dictionary : : Ptr message = new Dictionary ( ) ;
2014-05-03 20:02:22 +02:00
message - > Set ( " jsonrpc " , " 2.0 " ) ;
message - > Set ( " method " , " event::SetNextNotification " ) ;
message - > Set ( " params " , params ) ;
listener - > RelayMessage ( origin , notification , message , true ) ;
}
2015-08-04 14:47:44 +02:00
Value ApiEvents : : NextNotificationChangedAPIHandler ( const MessageOrigin : : Ptr & origin , const Dictionary : : Ptr & params )
2014-05-03 20:02:22 +02:00
{
2015-08-04 14:47:44 +02:00
Endpoint : : Ptr endpoint = origin - > FromClient - > GetEndpoint ( ) ;
2015-07-02 13:04:38 +02:00
if ( ! endpoint ) {
Log ( LogNotice , " ApiEvents " )
2015-08-04 14:47:44 +02:00
< < " Discarding 'next notification changed' message from ' " < < origin - > FromClient - > GetIdentity ( ) < < " ': Invalid endpoint origin (client not allowed). " ;
2014-10-16 13:04:04 +02:00
return Empty ;
2015-07-02 13:04:38 +02:00
}
2014-10-16 13:04:04 +02:00
2014-05-03 20:02:22 +02:00
if ( ! params )
return Empty ;
Notification : : Ptr notification = Notification : : GetByName ( params - > Get ( " notification " ) ) ;
if ( ! notification )
return Empty ;
2015-08-04 14:47:44 +02:00
if ( origin - > FromZone & & ! origin - > FromZone - > CanAccessObject ( notification ) ) {
2015-07-02 13:04:38 +02:00
Log ( LogNotice , " ApiEvents " )
2015-08-04 14:47:44 +02:00
< < " Discarding 'next notification changed' message from ' " < < origin - > FromClient - > GetIdentity ( ) < < " ': Unauthorized access. " ;
2014-05-03 20:02:22 +02:00
return Empty ;
2015-07-02 13:04:38 +02:00
}
2014-05-03 20:02:22 +02:00
2015-08-04 14:47:44 +02:00
notification - > SetNextNotification ( params - > Get ( " next_notification " ) , false , origin ) ;
2014-05-03 20:02:22 +02:00
return Empty ;
}
2015-08-04 14:47:44 +02:00
void ApiEvents : : ForceNextCheckChangedHandler ( const Checkable : : Ptr & checkable , const MessageOrigin : : Ptr & origin )
2014-05-03 20:02:22 +02:00
{
ApiListener : : Ptr listener = ApiListener : : GetInstance ( ) ;
if ( ! listener )
return ;
Host : : Ptr host ;
Service : : Ptr service ;
tie ( host , service ) = GetHostService ( checkable ) ;
2014-11-08 21:17:16 +01:00
Dictionary : : Ptr params = new Dictionary ( ) ;
2014-08-22 12:59:24 +02:00
params - > Set ( " host " , host - > GetName ( ) ) ;
2014-05-03 20:02:22 +02:00
if ( service )
params - > Set ( " service " , service - > GetShortName ( ) ) ;
2015-08-04 14:47:44 +02:00
params - > Set ( " forced " , checkable - > GetForceNextCheck ( ) ) ;
2014-05-03 20:02:22 +02:00
2014-11-08 21:17:16 +01:00
Dictionary : : Ptr message = new Dictionary ( ) ;
2014-05-03 20:02:22 +02:00
message - > Set ( " jsonrpc " , " 2.0 " ) ;
message - > Set ( " method " , " event::SetForceNextCheck " ) ;
message - > Set ( " params " , params ) ;
listener - > RelayMessage ( origin , checkable , message , true ) ;
}
2015-08-04 14:47:44 +02:00
Value ApiEvents : : ForceNextCheckChangedAPIHandler ( const MessageOrigin : : Ptr & origin , const Dictionary : : Ptr & params )
2014-05-03 20:02:22 +02:00
{
2015-08-04 14:47:44 +02:00
Endpoint : : Ptr endpoint = origin - > FromClient - > GetEndpoint ( ) ;
2015-07-02 13:04:38 +02:00
if ( ! endpoint ) {
Log ( LogNotice , " ApiEvents " )
2015-08-04 14:47:44 +02:00
< < " Discarding 'force next check changed' message from ' " < < origin - > FromClient - > GetIdentity ( ) < < " ': Invalid endpoint origin (client not allowed). " ;
2014-10-16 13:04:04 +02:00
return Empty ;
2015-07-02 13:04:38 +02:00
}
2014-10-16 13:04:04 +02:00
2014-05-03 20:02:22 +02:00
if ( ! params )
return Empty ;
2014-10-28 14:05:17 +01:00
Host : : Ptr host = Host : : GetByName ( params - > Get ( " host " ) ) ;
2014-05-03 20:02:22 +02:00
if ( ! host )
return Empty ;
Checkable : : Ptr checkable ;
if ( params - > Contains ( " service " ) )
checkable = host - > GetServiceByShortName ( params - > Get ( " service " ) ) ;
else
checkable = host ;
if ( ! checkable )
return Empty ;
2015-08-04 14:47:44 +02:00
if ( origin - > FromZone & & ! origin - > FromZone - > CanAccessObject ( checkable ) ) {
2015-07-02 13:04:38 +02:00
Log ( LogNotice , " ApiEvents " )
2015-08-04 14:47:44 +02:00
< < " Discarding 'force next check' message from ' " < < origin - > FromClient - > GetIdentity ( ) < < " ': Unauthorized access. " ;
2014-05-03 20:02:22 +02:00
return Empty ;
2015-07-02 13:04:38 +02:00
}
2014-05-03 20:02:22 +02:00
2015-08-04 14:47:44 +02:00
checkable - > SetForceNextCheck ( params - > Get ( " forced " ) , false , origin ) ;
2014-05-03 20:02:22 +02:00
return Empty ;
}
2015-08-04 14:47:44 +02:00
void ApiEvents : : ForceNextNotificationChangedHandler ( const Checkable : : Ptr & checkable , const MessageOrigin : : Ptr & origin )
2014-05-03 20:02:22 +02:00
{
ApiListener : : Ptr listener = ApiListener : : GetInstance ( ) ;
if ( ! listener )
return ;
Host : : Ptr host ;
Service : : Ptr service ;
tie ( host , service ) = GetHostService ( checkable ) ;
2014-11-08 21:17:16 +01:00
Dictionary : : Ptr params = new Dictionary ( ) ;
2014-08-22 12:59:24 +02:00
params - > Set ( " host " , host - > GetName ( ) ) ;
2014-05-03 20:02:22 +02:00
if ( service )
params - > Set ( " service " , service - > GetShortName ( ) ) ;
2015-08-04 14:47:44 +02:00
params - > Set ( " forced " , checkable - > GetForceNextNotification ( ) ) ;
2014-05-03 20:02:22 +02:00
2014-11-08 21:17:16 +01:00
Dictionary : : Ptr message = new Dictionary ( ) ;
2014-05-03 20:02:22 +02:00
message - > Set ( " jsonrpc " , " 2.0 " ) ;
message - > Set ( " method " , " event::SetForceNextNotification " ) ;
message - > Set ( " params " , params ) ;
listener - > RelayMessage ( origin , checkable , message , true ) ;
}
2015-08-04 14:47:44 +02:00
Value ApiEvents : : ForceNextNotificationChangedAPIHandler ( const MessageOrigin : : Ptr & origin , const Dictionary : : Ptr & params )
2014-05-03 20:02:22 +02:00
{
2015-08-04 14:47:44 +02:00
Endpoint : : Ptr endpoint = origin - > FromClient - > GetEndpoint ( ) ;
2015-07-02 13:04:38 +02:00
if ( ! endpoint ) {
Log ( LogNotice , " ApiEvents " )
2015-08-04 14:47:44 +02:00
< < " Discarding 'force next notification changed' message from ' " < < origin - > FromClient - > GetIdentity ( ) < < " ': Invalid endpoint origin (client not allowed). " ;
2014-10-16 13:04:04 +02:00
return Empty ;
2015-07-02 13:04:38 +02:00
}
2014-10-16 13:04:04 +02:00
2014-05-03 20:02:22 +02:00
if ( ! params )
return Empty ;
2014-10-28 14:05:17 +01:00
Host : : Ptr host = Host : : GetByName ( params - > Get ( " host " ) ) ;
2014-05-03 20:02:22 +02:00
if ( ! host )
return Empty ;
Checkable : : Ptr checkable ;
if ( params - > Contains ( " service " ) )
checkable = host - > GetServiceByShortName ( params - > Get ( " service " ) ) ;
else
checkable = host ;
if ( ! checkable )
return Empty ;
2015-08-04 14:47:44 +02:00
if ( origin - > FromZone & & ! origin - > FromZone - > CanAccessObject ( checkable ) ) {
2015-07-02 13:04:38 +02:00
Log ( LogNotice , " ApiEvents " )
2015-08-04 14:47:44 +02:00
< < " Discarding 'force next notification' message from ' " < < origin - > FromClient - > GetIdentity ( ) < < " ': Unauthorized access. " ;
2014-05-03 20:02:22 +02:00
return Empty ;
2015-07-02 13:04:38 +02:00
}
2014-05-03 20:02:22 +02:00
2015-08-04 14:47:44 +02:00
checkable - > SetForceNextNotification ( params - > Get ( " forced " ) , false , origin ) ;
2014-05-03 20:02:22 +02:00
return Empty ;
}
2015-08-04 14:47:44 +02:00
void ApiEvents : : CommentAddedHandler ( const Checkable : : Ptr & checkable , const Comment : : Ptr & comment , const MessageOrigin : : Ptr & origin )
2014-05-03 20:02:22 +02:00
{
ApiListener : : Ptr listener = ApiListener : : GetInstance ( ) ;
if ( ! listener )
return ;
Host : : Ptr host ;
Service : : Ptr service ;
tie ( host , service ) = GetHostService ( checkable ) ;
2014-11-08 21:17:16 +01:00
Dictionary : : Ptr params = new Dictionary ( ) ;
2014-08-22 12:59:24 +02:00
params - > Set ( " host " , host - > GetName ( ) ) ;
2014-05-03 20:02:22 +02:00
if ( service )
params - > Set ( " service " , service - > GetShortName ( ) ) ;
params - > Set ( " comment " , Serialize ( comment ) ) ;
2014-11-08 21:17:16 +01:00
Dictionary : : Ptr message = new Dictionary ( ) ;
2014-05-03 20:02:22 +02:00
message - > Set ( " jsonrpc " , " 2.0 " ) ;
message - > Set ( " method " , " event::AddComment " ) ;
message - > Set ( " params " , params ) ;
listener - > RelayMessage ( origin , checkable , message , true ) ;
}
2015-08-04 14:47:44 +02:00
Value ApiEvents : : CommentAddedAPIHandler ( const MessageOrigin : : Ptr & origin , const Dictionary : : Ptr & params )
2014-05-03 20:02:22 +02:00
{
2015-08-04 14:47:44 +02:00
Endpoint : : Ptr endpoint = origin - > FromClient - > GetEndpoint ( ) ;
2015-07-02 13:04:38 +02:00
if ( ! endpoint ) {
Log ( LogNotice , " ApiEvents " )
2015-08-04 14:47:44 +02:00
< < " Discarding 'comment added' message from ' " < < origin - > FromClient - > GetIdentity ( ) < < " ': Invalid endpoint origin (client not allowed). " ;
2014-10-16 13:04:04 +02:00
return Empty ;
2015-07-02 13:04:38 +02:00
}
2014-10-16 13:04:04 +02:00
2014-05-03 20:02:22 +02:00
if ( ! params )
return Empty ;
2014-10-28 14:05:17 +01:00
Host : : Ptr host = Host : : GetByName ( params - > Get ( " host " ) ) ;
2014-05-03 20:02:22 +02:00
if ( ! host )
return Empty ;
Checkable : : Ptr checkable ;
if ( params - > Contains ( " service " ) )
checkable = host - > GetServiceByShortName ( params - > Get ( " service " ) ) ;
else
checkable = host ;
if ( ! checkable )
return Empty ;
2015-08-04 14:47:44 +02:00
if ( origin - > FromZone & & ! origin - > FromZone - > CanAccessObject ( checkable ) ) {
2015-07-02 13:04:38 +02:00
Log ( LogNotice , " ApiEvents " )
2015-08-04 14:47:44 +02:00
< < " Discarding 'comment added' message from ' " < < origin - > FromClient - > GetIdentity ( ) < < " ': Unauthorized access. " ;
2014-05-03 20:02:22 +02:00
return Empty ;
2015-07-02 13:04:38 +02:00
}
2014-05-03 20:02:22 +02:00
2014-11-08 21:17:16 +01:00
Comment : : Ptr comment = new Comment ( ) ;
2014-10-25 09:14:56 +02:00
Deserialize ( comment , params - > Get ( " comment " ) , true ) ;
2014-05-03 20:02:22 +02:00
checkable - > AddComment ( comment - > GetEntryType ( ) , comment - > GetAuthor ( ) ,
comment - > GetText ( ) , comment - > GetExpireTime ( ) , comment - > GetId ( ) , origin ) ;
return Empty ;
}
2015-08-04 14:47:44 +02:00
void ApiEvents : : CommentRemovedHandler ( const Checkable : : Ptr & checkable , const Comment : : Ptr & comment , const MessageOrigin : : Ptr & origin )
2014-05-03 20:02:22 +02:00
{
ApiListener : : Ptr listener = ApiListener : : GetInstance ( ) ;
if ( ! listener )
return ;
Host : : Ptr host ;
Service : : Ptr service ;
tie ( host , service ) = GetHostService ( checkable ) ;
2014-11-08 21:17:16 +01:00
Dictionary : : Ptr params = new Dictionary ( ) ;
2014-08-22 12:59:24 +02:00
params - > Set ( " host " , host - > GetName ( ) ) ;
2014-05-03 20:02:22 +02:00
if ( service )
params - > Set ( " service " , service - > GetShortName ( ) ) ;
params - > Set ( " id " , comment - > GetId ( ) ) ;
2014-11-08 21:17:16 +01:00
Dictionary : : Ptr message = new Dictionary ( ) ;
2014-05-03 20:02:22 +02:00
message - > Set ( " jsonrpc " , " 2.0 " ) ;
message - > Set ( " method " , " event::RemoveComment " ) ;
message - > Set ( " params " , params ) ;
listener - > RelayMessage ( origin , checkable , message , true ) ;
}
2015-08-04 14:47:44 +02:00
Value ApiEvents : : CommentRemovedAPIHandler ( const MessageOrigin : : Ptr & origin , const Dictionary : : Ptr & params )
2014-05-03 20:02:22 +02:00
{
2015-08-04 14:47:44 +02:00
Endpoint : : Ptr endpoint = origin - > FromClient - > GetEndpoint ( ) ;
2015-07-02 13:04:38 +02:00
if ( ! endpoint ) {
Log ( LogNotice , " ApiEvents " )
2015-08-04 14:47:44 +02:00
< < " Discarding 'comment removed' message from ' " < < origin - > FromClient - > GetIdentity ( ) < < " ': Invalid endpoint origin (client not allowed). " ;
2014-10-16 13:04:04 +02:00
return Empty ;
2015-07-02 13:04:38 +02:00
}
2014-10-16 13:04:04 +02:00
2014-05-03 20:02:22 +02:00
if ( ! params )
return Empty ;
2014-10-28 14:05:17 +01:00
Host : : Ptr host = Host : : GetByName ( params - > Get ( " host " ) ) ;
2014-05-03 20:02:22 +02:00
if ( ! host )
return Empty ;
Checkable : : Ptr checkable ;
if ( params - > Contains ( " service " ) )
checkable = host - > GetServiceByShortName ( params - > Get ( " service " ) ) ;
else
checkable = host ;
if ( ! checkable )
return Empty ;
2015-08-04 14:47:44 +02:00
if ( origin - > FromZone & & ! origin - > FromZone - > CanAccessObject ( checkable ) ) {
2015-07-02 13:04:38 +02:00
Log ( LogNotice , " ApiEvents " )
2015-08-04 14:47:44 +02:00
< < " Discarding 'comment removed' message from ' " < < origin - > FromClient - > GetIdentity ( ) < < " ': Unauthorized access. " ;
2014-05-03 20:02:22 +02:00
return Empty ;
2015-07-02 13:04:38 +02:00
}
2014-05-03 20:02:22 +02:00
checkable - > RemoveComment ( params - > Get ( " id " ) , origin ) ;
return Empty ;
}
2015-08-04 14:47:44 +02:00
void ApiEvents : : DowntimeAddedHandler ( const Checkable : : Ptr & checkable , const Downtime : : Ptr & downtime , const MessageOrigin : : Ptr & origin )
2014-05-03 20:02:22 +02:00
{
ApiListener : : Ptr listener = ApiListener : : GetInstance ( ) ;
if ( ! listener )
return ;
Host : : Ptr host ;
Service : : Ptr service ;
tie ( host , service ) = GetHostService ( checkable ) ;
2014-11-08 21:17:16 +01:00
Dictionary : : Ptr params = new Dictionary ( ) ;
2014-08-22 12:59:24 +02:00
params - > Set ( " host " , host - > GetName ( ) ) ;
2014-05-03 20:02:22 +02:00
if ( service )
params - > Set ( " service " , service - > GetShortName ( ) ) ;
params - > Set ( " downtime " , Serialize ( downtime ) ) ;
2014-11-08 21:17:16 +01:00
Dictionary : : Ptr message = new Dictionary ( ) ;
2014-05-03 20:02:22 +02:00
message - > Set ( " jsonrpc " , " 2.0 " ) ;
message - > Set ( " method " , " event::AddDowntime " ) ;
message - > Set ( " params " , params ) ;
listener - > RelayMessage ( origin , checkable , message , true ) ;
}
2015-08-04 14:47:44 +02:00
Value ApiEvents : : DowntimeAddedAPIHandler ( const MessageOrigin : : Ptr & origin , const Dictionary : : Ptr & params )
2014-05-03 20:02:22 +02:00
{
2015-08-04 14:47:44 +02:00
Endpoint : : Ptr endpoint = origin - > FromClient - > GetEndpoint ( ) ;
2015-07-02 13:04:38 +02:00
if ( ! endpoint ) {
Log ( LogNotice , " ApiEvents " )
2015-08-04 14:47:44 +02:00
< < " Discarding 'downtime added' message from ' " < < origin - > FromClient - > GetIdentity ( ) < < " ': Invalid endpoint origin (client not allowed). " ;
2014-10-16 13:04:04 +02:00
return Empty ;
2015-07-02 13:04:38 +02:00
}
2014-10-16 13:04:04 +02:00
2014-05-03 20:02:22 +02:00
if ( ! params )
return Empty ;
2014-10-28 14:05:17 +01:00
Host : : Ptr host = Host : : GetByName ( params - > Get ( " host " ) ) ;
2014-05-03 20:02:22 +02:00
if ( ! host )
return Empty ;
Checkable : : Ptr checkable ;
if ( params - > Contains ( " service " ) )
checkable = host - > GetServiceByShortName ( params - > Get ( " service " ) ) ;
else
checkable = host ;
if ( ! checkable )
return Empty ;
2015-08-04 14:47:44 +02:00
if ( origin - > FromZone & & ! origin - > FromZone - > CanAccessObject ( checkable ) ) {
2015-07-02 13:04:38 +02:00
Log ( LogNotice , " ApiEvents " )
2015-08-04 14:47:44 +02:00
< < " Discarding 'downtime added' message from ' " < < origin - > FromClient - > GetIdentity ( ) < < " ': Unauthorized access. " ;
2014-05-03 20:02:22 +02:00
return Empty ;
2015-07-02 13:04:38 +02:00
}
2014-05-03 20:02:22 +02:00
2014-11-08 21:17:16 +01:00
Downtime : : Ptr downtime = new Downtime ( ) ;
2014-10-25 09:14:56 +02:00
Deserialize ( downtime , params - > Get ( " downtime " ) , true ) ;
2014-05-03 20:02:22 +02:00
checkable - > AddDowntime ( downtime - > GetAuthor ( ) , downtime - > GetComment ( ) ,
downtime - > GetStartTime ( ) , downtime - > GetEndTime ( ) ,
downtime - > GetFixed ( ) , downtime - > GetTriggeredBy ( ) ,
downtime - > GetDuration ( ) , downtime - > GetScheduledBy ( ) ,
downtime - > GetId ( ) , origin ) ;
return Empty ;
}
2015-08-04 14:47:44 +02:00
void ApiEvents : : DowntimeRemovedHandler ( const Checkable : : Ptr & checkable , const Downtime : : Ptr & downtime , const MessageOrigin : : Ptr & origin )
2014-05-03 20:02:22 +02:00
{
ApiListener : : Ptr listener = ApiListener : : GetInstance ( ) ;
if ( ! listener )
return ;
Host : : Ptr host ;
Service : : Ptr service ;
tie ( host , service ) = GetHostService ( checkable ) ;
2014-11-08 21:17:16 +01:00
Dictionary : : Ptr params = new Dictionary ( ) ;
2014-08-22 12:59:24 +02:00
params - > Set ( " host " , host - > GetName ( ) ) ;
2014-05-03 20:02:22 +02:00
if ( service )
params - > Set ( " service " , service - > GetShortName ( ) ) ;
params - > Set ( " id " , downtime - > GetId ( ) ) ;
2014-11-08 21:17:16 +01:00
Dictionary : : Ptr message = new Dictionary ( ) ;
2014-05-03 20:02:22 +02:00
message - > Set ( " jsonrpc " , " 2.0 " ) ;
message - > Set ( " method " , " event::RemoveDowntime " ) ;
message - > Set ( " params " , params ) ;
listener - > RelayMessage ( origin , checkable , message , true ) ;
}
2015-08-04 14:47:44 +02:00
Value ApiEvents : : DowntimeRemovedAPIHandler ( const MessageOrigin : : Ptr & origin , const Dictionary : : Ptr & params )
2014-05-03 20:02:22 +02:00
{
2015-08-04 14:47:44 +02:00
Endpoint : : Ptr endpoint = origin - > FromClient - > GetEndpoint ( ) ;
2015-07-02 13:04:38 +02:00
if ( ! endpoint ) {
Log ( LogNotice , " ApiEvents " )
2015-08-04 14:47:44 +02:00
< < " Discarding 'downtime removed' message from ' " < < origin - > FromClient - > GetIdentity ( ) < < " ': Invalid endpoint origin (client not allowed). " ;
2014-10-16 13:04:04 +02:00
return Empty ;
2015-07-02 13:04:38 +02:00
}
2014-10-16 13:04:04 +02:00
2014-05-03 20:02:22 +02:00
if ( ! params )
return Empty ;
2014-10-28 14:05:17 +01:00
Host : : Ptr host = Host : : GetByName ( params - > Get ( " host " ) ) ;
2014-05-03 20:02:22 +02:00
if ( ! host )
return Empty ;
Checkable : : Ptr checkable ;
if ( params - > Contains ( " service " ) )
checkable = host - > GetServiceByShortName ( params - > Get ( " service " ) ) ;
else
checkable = host ;
if ( ! checkable )
return Empty ;
2015-08-04 14:47:44 +02:00
if ( origin - > FromZone & & ! origin - > FromZone - > CanAccessObject ( checkable ) ) {
2015-07-02 13:04:38 +02:00
Log ( LogNotice , " ApiEvents " )
2015-08-04 14:47:44 +02:00
< < " Discarding 'downtime removed' message from ' " < < origin - > FromClient - > GetIdentity ( ) < < " ': Unauthorized access. " ;
2014-05-03 20:02:22 +02:00
return Empty ;
2015-07-02 13:04:38 +02:00
}
2014-05-03 20:02:22 +02:00
checkable - > RemoveDowntime ( params - > Get ( " id " ) , false , origin ) ;
return Empty ;
}
void ApiEvents : : AcknowledgementSetHandler ( const Checkable : : Ptr & checkable ,
const String & author , const String & comment , AcknowledgementType type ,
2015-08-04 14:47:44 +02:00
bool notify , double expiry , const MessageOrigin : : Ptr & origin )
2014-05-03 20:02:22 +02:00
{
ApiListener : : Ptr listener = ApiListener : : GetInstance ( ) ;
if ( ! listener )
return ;
Host : : Ptr host ;
Service : : Ptr service ;
tie ( host , service ) = GetHostService ( checkable ) ;
2014-11-08 21:17:16 +01:00
Dictionary : : Ptr params = new Dictionary ( ) ;
2014-08-22 12:59:24 +02:00
params - > Set ( " host " , host - > GetName ( ) ) ;
2014-05-03 20:02:22 +02:00
if ( service )
params - > Set ( " service " , service - > GetShortName ( ) ) ;
params - > Set ( " author " , author ) ;
params - > Set ( " comment " , comment ) ;
params - > Set ( " acktype " , type ) ;
2015-02-07 21:39:19 +01:00
params - > Set ( " notify " , notify ) ;
2014-05-03 20:02:22 +02:00
params - > Set ( " expiry " , expiry ) ;
2014-11-08 21:17:16 +01:00
Dictionary : : Ptr message = new Dictionary ( ) ;
2014-05-03 20:02:22 +02:00
message - > Set ( " jsonrpc " , " 2.0 " ) ;
message - > Set ( " method " , " event::SetAcknowledgement " ) ;
message - > Set ( " params " , params ) ;
listener - > RelayMessage ( origin , checkable , message , true ) ;
}
2015-08-04 14:47:44 +02:00
Value ApiEvents : : AcknowledgementSetAPIHandler ( const MessageOrigin : : Ptr & origin , const Dictionary : : Ptr & params )
2014-05-03 20:02:22 +02:00
{
2015-08-04 14:47:44 +02:00
Endpoint : : Ptr endpoint = origin - > FromClient - > GetEndpoint ( ) ;
2015-07-02 13:04:38 +02:00
if ( ! endpoint ) {
Log ( LogNotice , " ApiEvents " )
2015-08-04 14:47:44 +02:00
< < " Discarding 'acknowledgement set' message from ' " < < origin - > FromClient - > GetIdentity ( ) < < " ': Invalid endpoint origin (client not allowed). " ;
2014-10-16 13:04:04 +02:00
return Empty ;
2015-07-02 13:04:38 +02:00
}
2014-10-16 13:04:04 +02:00
2014-05-03 20:02:22 +02:00
if ( ! params )
return Empty ;
2014-10-28 14:05:17 +01:00
Host : : Ptr host = Host : : GetByName ( params - > Get ( " host " ) ) ;
2014-05-03 20:02:22 +02:00
if ( ! host )
return Empty ;
Checkable : : Ptr checkable ;
if ( params - > Contains ( " service " ) )
checkable = host - > GetServiceByShortName ( params - > Get ( " service " ) ) ;
else
checkable = host ;
if ( ! checkable )
return Empty ;
2015-08-04 14:47:44 +02:00
if ( origin - > FromZone & & ! origin - > FromZone - > CanAccessObject ( checkable ) ) {
2015-07-02 13:04:38 +02:00
Log ( LogNotice , " ApiEvents " )
2015-08-04 14:47:44 +02:00
< < " Discarding 'acknowledgement set' message from ' " < < origin - > FromClient - > GetIdentity ( ) < < " ': Unauthorized access. " ;
2014-05-03 20:02:22 +02:00
return Empty ;
2015-07-02 13:04:38 +02:00
}
2014-05-03 20:02:22 +02:00
checkable - > AcknowledgeProblem ( params - > Get ( " author " ) , params - > Get ( " comment " ) ,
static_cast < AcknowledgementType > ( static_cast < int > ( params - > Get ( " acktype " ) ) ) ,
2015-02-07 21:39:19 +01:00
params - > Get ( " notify " ) , params - > Get ( " expiry " ) , origin ) ;
2014-05-03 20:02:22 +02:00
return Empty ;
}
2015-08-04 14:47:44 +02:00
void ApiEvents : : AcknowledgementClearedHandler ( const Checkable : : Ptr & checkable , const MessageOrigin : : Ptr & origin )
2014-05-03 20:02:22 +02:00
{
ApiListener : : Ptr listener = ApiListener : : GetInstance ( ) ;
if ( ! listener )
return ;
Host : : Ptr host ;
Service : : Ptr service ;
tie ( host , service ) = GetHostService ( checkable ) ;
2014-11-08 21:17:16 +01:00
Dictionary : : Ptr params = new Dictionary ( ) ;
2014-08-22 12:59:24 +02:00
params - > Set ( " host " , host - > GetName ( ) ) ;
2014-05-03 20:02:22 +02:00
if ( service )
params - > Set ( " service " , service - > GetShortName ( ) ) ;
2014-11-08 21:17:16 +01:00
Dictionary : : Ptr message = new Dictionary ( ) ;
2014-05-03 20:02:22 +02:00
message - > Set ( " jsonrpc " , " 2.0 " ) ;
message - > Set ( " method " , " event::ClearAcknowledgement " ) ;
message - > Set ( " params " , params ) ;
listener - > RelayMessage ( origin , checkable , message , true ) ;
}
2015-08-04 14:47:44 +02:00
Value ApiEvents : : AcknowledgementClearedAPIHandler ( const MessageOrigin : : Ptr & origin , const Dictionary : : Ptr & params )
2014-05-03 20:02:22 +02:00
{
2015-08-04 14:47:44 +02:00
Endpoint : : Ptr endpoint = origin - > FromClient - > GetEndpoint ( ) ;
2015-07-02 13:04:38 +02:00
if ( ! endpoint ) {
Log ( LogNotice , " ApiEvents " )
2015-08-04 14:47:44 +02:00
< < " Discarding 'acknowledgement cleared' message from ' " < < origin - > FromClient - > GetIdentity ( ) < < " ': Invalid endpoint origin (client not allowed). " ;
2014-10-16 13:04:04 +02:00
return Empty ;
2015-07-02 13:04:38 +02:00
}
2014-10-16 13:04:04 +02:00
2014-05-03 20:02:22 +02:00
if ( ! params )
return Empty ;
2014-10-28 14:05:17 +01:00
Host : : Ptr host = Host : : GetByName ( params - > Get ( " host " ) ) ;
2014-05-03 20:02:22 +02:00
if ( ! host )
return Empty ;
Checkable : : Ptr checkable ;
if ( params - > Contains ( " service " ) )
checkable = host - > GetServiceByShortName ( params - > Get ( " service " ) ) ;
else
checkable = host ;
if ( ! checkable )
return Empty ;
2015-08-04 14:47:44 +02:00
if ( origin - > FromZone & & ! origin - > FromZone - > CanAccessObject ( checkable ) ) {
2015-07-02 13:04:38 +02:00
Log ( LogNotice , " ApiEvents " )
2015-08-04 14:47:44 +02:00
< < " Discarding 'acknowledgement cleared' message from ' " < < origin - > FromClient - > GetIdentity ( ) < < " ': Unauthorized access. " ;
2014-05-03 20:02:22 +02:00
return Empty ;
2015-07-02 13:04:38 +02:00
}
2014-05-03 20:02:22 +02:00
checkable - > ClearAcknowledgement ( origin ) ;
return Empty ;
}
2015-08-04 14:47:44 +02:00
Value ApiEvents : : ExecuteCommandAPIHandler ( const MessageOrigin : : Ptr & origin , const Dictionary : : Ptr & params )
2014-11-13 11:23:57 +01:00
{
2015-08-04 14:47:44 +02:00
Endpoint : : Ptr sourceEndpoint = origin - > FromClient - > GetEndpoint ( ) ;
2014-11-13 11:23:57 +01:00
2015-08-04 14:47:44 +02:00
if ( ! sourceEndpoint | | ( origin - > FromZone & & ! Zone : : GetLocalZone ( ) - > IsChildOf ( origin - > FromZone ) ) ) {
2015-07-02 13:04:38 +02:00
Log ( LogNotice , " ApiEvents " )
2015-08-04 14:47:44 +02:00
< < " Discarding 'execute command' message from ' " < < origin - > FromClient - > GetIdentity ( ) < < " ': Invalid endpoint origin (client not allowed). " ;
2014-11-13 11:23:57 +01:00
return Empty ;
2015-07-02 13:04:38 +02:00
}
2014-11-13 11:23:57 +01:00
ApiListener : : Ptr listener = ApiListener : : GetInstance ( ) ;
if ( ! listener ) {
Log ( LogCritical , " ApiListener " , " No instance available. " ) ;
return Empty ;
}
if ( ! listener - > GetAcceptCommands ( ) ) {
Log ( LogWarning , " ApiListener " )
< < " Ignoring command. ' " < < listener - > GetName ( ) < < " ' does not accept commands. " ;
2015-02-05 15:17:56 +01:00
Host : : Ptr host = new Host ( ) ;
Dictionary : : Ptr attrs = new Dictionary ( ) ;
attrs - > Set ( " __name " , params - > Get ( " host " ) ) ;
attrs - > Set ( " type " , " Host " ) ;
Deserialize ( host , attrs , false , FAConfig ) ;
2015-03-05 20:50:27 +01:00
if ( params - > Contains ( " service " ) )
host - > SetExtension ( " agent_service_name " , params - > Get ( " service " ) ) ;
2015-02-05 15:17:56 +01:00
CheckResult : : Ptr cr = new CheckResult ( ) ;
cr - > SetState ( ServiceUnknown ) ;
2015-03-05 20:50:27 +01:00
cr - > SetOutput ( " Endpoint ' " + Endpoint : : GetLocalEndpoint ( ) - > GetName ( ) + " ' does not accept commands. " ) ;
2015-02-05 15:17:56 +01:00
Dictionary : : Ptr message = MakeCheckResultMessage ( host , cr ) ;
2015-01-18 22:15:35 +01:00
listener - > SyncSendMessage ( sourceEndpoint , message ) ;
2015-02-05 15:17:56 +01:00
2014-11-13 11:23:57 +01:00
return Empty ;
}
2015-01-18 22:15:35 +01:00
/* use a virtual host object for executing the command */
2014-11-13 11:23:57 +01:00
Host : : Ptr host = new Host ( ) ;
Dictionary : : Ptr attrs = new Dictionary ( ) ;
attrs - > Set ( " __name " , params - > Get ( " host " ) ) ;
attrs - > Set ( " type " , " Host " ) ;
2015-03-05 20:50:27 +01:00
Deserialize ( host , attrs , false , FAConfig ) ;
if ( params - > Contains ( " service " ) )
host - > SetExtension ( " agent_service_name " , params - > Get ( " service " ) ) ;
2014-11-13 11:23:57 +01:00
String command = params - > Get ( " command " ) ;
String command_type = params - > Get ( " command_type " ) ;
if ( command_type = = " check_command " ) {
if ( ! CheckCommand : : GetByName ( command ) ) {
CheckResult : : Ptr cr = new CheckResult ( ) ;
cr - > SetState ( ServiceUnknown ) ;
cr - > SetOutput ( " Check command ' " + command + " ' does not exist. " ) ;
Dictionary : : Ptr message = MakeCheckResultMessage ( host , cr ) ;
2015-01-18 22:15:35 +01:00
listener - > SyncSendMessage ( sourceEndpoint , message ) ;
2014-11-13 11:23:57 +01:00
return Empty ;
}
} else if ( command_type = = " event_command " ) {
2015-07-31 16:04:03 +02:00
if ( ! EventCommand : : GetByName ( command ) ) {
Log ( LogWarning , " ApiEvents " )
< < " Event command ' " < < command < < " ' does not exist. " ;
2014-11-13 11:23:57 +01:00
return Empty ;
2015-07-31 16:04:03 +02:00
}
2014-11-13 11:23:57 +01:00
} else
return Empty ;
attrs - > Set ( command_type , params - > Get ( " command " ) ) ;
2015-01-18 22:15:35 +01:00
attrs - > Set ( " command_endpoint " , sourceEndpoint - > GetName ( ) ) ;
2014-11-13 11:23:57 +01:00
Deserialize ( host , attrs , false , FAConfig ) ;
host - > SetExtension ( " agent_check " , true ) ;
Dictionary : : Ptr macros = params - > Get ( " macros " ) ;
2015-02-05 15:36:17 +01:00
if ( command_type = = " check_command " ) {
try {
2015-01-18 22:15:35 +01:00
host - > ExecuteRemoteCheck ( macros ) ;
2015-02-05 15:36:17 +01:00
} catch ( const std : : exception & ex ) {
CheckResult : : Ptr cr = new CheckResult ( ) ;
cr - > SetState ( ServiceUnknown ) ;
String output = " Exception occured while checking ' " + host - > GetName ( ) + " ': " + DiagnosticInformation ( ex ) ;
cr - > SetOutput ( output ) ;
double now = Utility : : GetTime ( ) ;
cr - > SetScheduleStart ( now ) ;
cr - > SetScheduleEnd ( now ) ;
cr - > SetExecutionStart ( now ) ;
cr - > SetExecutionEnd ( now ) ;
Dictionary : : Ptr message = MakeCheckResultMessage ( host , cr ) ;
2015-01-18 22:15:35 +01:00
listener - > SyncSendMessage ( sourceEndpoint , message ) ;
2015-02-05 15:36:17 +01:00
Log ( LogCritical , " checker " , output ) ;
}
} else if ( command_type = = " event_command " ) {
2014-11-13 11:23:57 +01:00
host - > ExecuteEventHandler ( macros , true ) ;
2015-02-05 15:36:17 +01:00
}
2014-11-13 11:23:57 +01:00
return Empty ;
}
2014-05-03 20:02:22 +02:00
void ApiEvents : : RepositoryTimerHandler ( void )
{
ApiListener : : Ptr listener = ApiListener : : GetInstance ( ) ;
if ( ! listener )
return ;
2014-11-08 21:17:16 +01:00
Dictionary : : Ptr repository = new Dictionary ( ) ;
2014-05-03 20:02:22 +02:00
2015-08-15 20:28:05 +02:00
BOOST_FOREACH ( const Host : : Ptr & host , ConfigType : : GetObjectsByType < Host > ( ) ) {
2014-11-08 21:17:16 +01:00
Array : : Ptr services = new Array ( ) ;
2014-05-03 20:02:22 +02:00
BOOST_FOREACH ( const Service : : Ptr & service , host - > GetServices ( ) ) {
2014-05-08 12:17:21 +02:00
services - > Add ( service - > GetShortName ( ) ) ;
2014-05-03 20:02:22 +02:00
}
2014-08-22 12:59:24 +02:00
repository - > Set ( host - > GetName ( ) , services ) ;
2014-05-03 20:02:22 +02:00
}
Endpoint : : Ptr my_endpoint = Endpoint : : GetLocalEndpoint ( ) ;
2014-08-04 14:03:37 +02:00
if ( ! my_endpoint ) {
Log ( LogWarning , " ApiEvents " , " No local endpoint defined. Bailing out. " ) ;
return ;
}
2014-05-03 20:02:22 +02:00
Zone : : Ptr my_zone = my_endpoint - > GetZone ( ) ;
2015-02-25 13:59:18 +01:00
if ( ! my_zone )
return ;
2014-11-08 21:17:16 +01:00
Dictionary : : Ptr params = new Dictionary ( ) ;
2014-05-08 12:17:21 +02:00
params - > Set ( " seen " , Utility : : GetTime ( ) ) ;
2014-05-03 20:02:22 +02:00
params - > Set ( " endpoint " , my_endpoint - > GetName ( ) ) ;
Zone : : Ptr parent_zone = my_zone - > GetParent ( ) ;
if ( parent_zone )
params - > Set ( " parent_zone " , parent_zone - > GetName ( ) ) ;
params - > Set ( " zone " , my_zone - > GetName ( ) ) ;
params - > Set ( " repository " , repository ) ;
2014-11-08 21:17:16 +01:00
Dictionary : : Ptr message = new Dictionary ( ) ;
2014-05-03 20:02:22 +02:00
message - > Set ( " jsonrpc " , " 2.0 " ) ;
message - > Set ( " method " , " event::UpdateRepository " ) ;
message - > Set ( " params " , params ) ;
2015-08-04 14:47:44 +02:00
listener - > RelayMessage ( MessageOrigin : : Ptr ( ) , my_zone , message , false ) ;
2014-05-03 20:02:22 +02:00
}
String ApiEvents : : GetRepositoryDir ( void )
{
return Application : : GetLocalStateDir ( ) + " /lib/icinga2/api/repository/ " ;
}
2015-08-04 14:47:44 +02:00
Value ApiEvents : : UpdateRepositoryAPIHandler ( const MessageOrigin : : Ptr & origin , const Dictionary : : Ptr & params )
2014-05-03 20:02:22 +02:00
{
if ( ! params )
return Empty ;
2014-08-22 12:59:24 +02:00
Value vrepository = params - > Get ( " repository " ) ;
if ( vrepository . IsEmpty ( ) | | ! vrepository . IsObjectType < Dictionary > ( ) )
return Empty ;
2014-10-18 21:06:28 +02:00
String repositoryFile = GetRepositoryDir ( ) + SHA256 ( params - > Get ( " endpoint " ) ) + " .repo " ;
2014-05-03 20:02:22 +02:00
String repositoryTempFile = repositoryFile + " .tmp " ;
std : : ofstream fp ( repositoryTempFile . CStr ( ) , std : : ofstream : : out | std : : ostream : : trunc ) ;
2014-10-26 19:59:49 +01:00
fp < < JsonEncode ( params ) ;
2014-05-03 20:02:22 +02:00
fp . close ( ) ;
# ifdef _WIN32
2014-05-10 11:26:56 +02:00
_unlink ( repositoryFile . CStr ( ) ) ;
2014-05-03 20:02:22 +02:00
# endif /* _WIN32 */
if ( rename ( repositoryTempFile . CStr ( ) , repositoryFile . CStr ( ) ) < 0 ) {
BOOST_THROW_EXCEPTION ( posix_error ( )
< < boost : : errinfo_api_function ( " rename " )
< < boost : : errinfo_errno ( errno )
< < boost : : errinfo_file_name ( repositoryTempFile ) ) ;
}
ApiListener : : Ptr listener = ApiListener : : GetInstance ( ) ;
if ( ! listener )
return Empty ;
2014-11-08 21:17:16 +01:00
Dictionary : : Ptr message = new Dictionary ( ) ;
2014-05-03 20:02:22 +02:00
message - > Set ( " jsonrpc " , " 2.0 " ) ;
message - > Set ( " method " , " event::UpdateRepository " ) ;
message - > Set ( " params " , params ) ;
listener - > RelayMessage ( origin , Zone : : GetLocalZone ( ) , message , true ) ;
return Empty ;
}
2014-05-08 13:08:34 +02:00