-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.knowitop-monitoring-api.php
137 lines (126 loc) · 4.36 KB
/
main.knowitop-monitoring-api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/**
* @author Vladimir Kunin <[email protected]>
*/
use Knowitop\iTop\MonitoringAPI\MonitoringContext;
require_once('classes/autoload.php');
/**
* Class MonitoringServices
*/
class MonitoringServices implements iRestServiceProvider
{
/**
* @param string $sVersion
*
* @return array
*/
public function ListOperations($sVersion)
{
$aOperations = array();
$aOperations[] = array(
'verb' => 'monitoring/alarm',
'description' => 'Handle an alarm state change',
);
return $aOperations;
}
/**
* @param string $sVersion
* @param string $sVerb
* @param \stdClass $oParams
*
* @return \RestResult|\RestResultWithObjects
* @throws \CoreException
* @throws \CoreUnexpectedValue
* @throws \MissingQueryArgument
* @throws \MySQLException
* @throws \MySQLHasGoneAwayException
* @throws \OQLException
*/
public function ExecOperation($sVersion, $sVerb, $oParams)
{
// $alarm->state$, $alarm->message$, $alarm->ci_key$
// $ci->att_code$
// $ticket->att_code$
$oResult = new RestResultWithObjects();
switch ($sVerb)
{
case 'monitoring/alarm':
RestUtils::InitTrackingComment($oParams);
$sContextName = RestUtils::GetMandatoryParam($oParams, 'context');
$sCIKey = RestUtils::GetMandatoryParam($oParams, 'ci_key');
$iAlarmState = (int)RestUtils::GetMandatoryParam($oParams, 'state');
$sAlarmMessage = RestUtils::GetMandatoryParam($oParams, 'message');
$sDescription = RestUtils::GetOptionalParam($oParams, 'description', $sAlarmMessage);
// $aAlarmFields = (array)RestUtils::GetOptionalParam($oParams, 'fields', array());
$aContextConfig = utils::GetCurrentModuleSetting($sContextName, array());
// $aContextConfig = $aConfig[$sContextName];
if (empty($aContextConfig) || !is_array($aContextConfig))
{
throw new Exception(utils::GetCurrentModuleName().": '$sContextName' context not found in the module config.");
}
if (empty($aContextConfig['actions_on_problem']) || empty($aContextConfig['actions_on_ok']))
{
throw new Exception(utils::GetCurrentModuleName().": 'actions_on_problem' or 'actions_on_ok' is missing or empty in the module config.");
}
if (!empty($aContextConfig['ci_key_regexp']))
{
if (preg_match($aContextConfig['ci_key_regexp'], $sCIKey, $aMatches))
{
$sCIKey = $aMatches[1];
}
else
{
// todo: log warning/error
}
};
$oMonitoringContext = new MonitoringContext($sContextName, $iAlarmState, $aContextConfig['actions_on_problem'],
$aContextConfig['actions_on_ok']);
$oMonitoringContext->AddQueryParams(array(
'alarm->ci_key' => $sCIKey,
'alarm->state' => $iAlarmState,
'alarm->message' => $sAlarmMessage,
'alarm->description' => $sDescription,
));
// $oMonitoringContext->SetAlarmFields($aAlarmFields);
if (!empty($aContextConfig['ci_oql']))
{
$oMonitoringContext->InitConfigItemFromOql($aContextConfig['ci_oql']);
}
if (!empty($aContextConfig['ticket_oql']))
{
$oMonitoringContext->InitTicketFromOql($aContextConfig['ticket_oql']);
}
$sTicketClass = $oMonitoringContext->GetTicketClass();
if (UserRights::IsActionAllowed($sTicketClass, UR_ACTION_MODIFY) != UR_ALLOWED_YES)
{
$oResult->code = RestResult::UNAUTHORIZED;
$oResult->message = "The current user does not have enough permissions for creating data of class $sTicketClass";
}
elseif (UserRights::IsActionAllowed($sTicketClass, UR_ACTION_BULK_MODIFY) != UR_ALLOWED_YES)
{
$oResult->code = RestResult::UNAUTHORIZED;
$oResult->message = "The current user does not have enough permissions for massively creating data of class $sTicketClass";
}
else
{
$oMonitoringContext->Execute();
if ($oTicket = $oMonitoringContext->GetTicket())
{
$aShowFields = RestUtils::GetFieldList($oMonitoringContext->GetTicketClass(), $oParams, 'output_fields');
$bExtendedOutput = (RestUtils::GetOptionalParam($oParams, 'output_fields', '*') == '*+');
$sMessage = 'Performed actions: '.join(', ', $oMonitoringContext->GetPerformedActions());
$oResult->AddObject(0, $sMessage, $oTicket, $aShowFields, $bExtendedOutput);
}
else
{
$oResult = new RestResult();
$oResult->message = 'No ticket to perform actions.';
}
}
break;
default:
// unknown operation: handled at a higher level
}
return $oResult;
}
}