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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
<?php
/**
* A class with SLA calculation logic.
*
* Class CServicesSlaCalculator
*/
class CServicesSlaCalculator {
/**
* Calculates the SLA for the given service during the given period.
*
* Returns the following information:
* - ok - the percentage of time the service was in OK state;
* - problem - the percentage of time the service was in problem state;
* - okTime - the time the service was in OK state, in seconds;
* - problemTime - the time the service was in problem state, in seconds;
* - downtimeTime - the time the service was down, in seconds;
* - dt;
* - ut.
*
* @param array $serviceAlarms
* @param array $serviceTimes
* @param int $periodStart
* @param int $periodEnd
* @param int $startValue the value of the last service alarm
*
* @return array
*/
public function calculateSla(array $serviceAlarms, array $serviceTimes, $periodStart, $periodEnd, $startValue) {
/**
* structure of "$data":
* - alarm - on/off status (0,1 - off; >1 - on)
* - dt_s - count of downtime starts
* - dt_e - count of downtime ends
* - ut_s - count of uptime starts
* - ut_e - count of uptime ends
* - clock - time stamp
*
* Key in $data array contains unique value to sort by.
*/
$data = [];
$latest = 0; // Timestamp of last database record.
foreach ($serviceAlarms as $alarm) {
if ($alarm['clock'] >= $periodStart && $alarm['clock'] <= $periodEnd) {
$data[$alarm['servicealarmid']] = [
'alarm' => $alarm['value'],
'clock' => $alarm['clock']
];
if ($alarm['clock'] > $latest) {
$latest = $alarm['clock'];
}
}
}
if ($periodEnd != $latest) {
$data[] = ['clock' => $periodEnd];
}
$unmarkedPeriodType = 'ut';
$service_time_data = [];
foreach ($serviceTimes as $time) {
if ($time['type'] == SERVICE_TIME_TYPE_UPTIME) {
$this->expandPeriodicalTimes($service_time_data, $periodStart, $periodEnd, $time['ts_from'],
$time['ts_to'], 'ut'
);
// if an uptime period exists - unmarked time is downtime
$unmarkedPeriodType = 'dt';
}
elseif ($time['type'] == SERVICE_TIME_TYPE_DOWNTIME) {
$this->expandPeriodicalTimes($service_time_data, $periodStart, $periodEnd, $time['ts_from'],
$time['ts_to'], 'dt'
);
}
elseif($time['type'] == SERVICE_TIME_TYPE_ONETIME_DOWNTIME && $time['ts_to'] >= $periodStart
&& $time['ts_from'] <= $periodEnd) {
if ($time['ts_from'] < $periodStart) {
$time['ts_from'] = $periodStart;
}
if ($time['ts_to'] > $periodEnd) {
$time['ts_to'] = $periodEnd;
}
if (isset($service_time_data[$time['ts_from']]['dt_s'])) {
$service_time_data[$time['ts_from']]['dt_s']++;
}
else {
$service_time_data[$time['ts_from']]['dt_s'] = 1;
}
if (isset($service_time_data[$time['ts_to']]['dt_e'])) {
$service_time_data[$time['ts_to']]['dt_e']++;
}
else {
$service_time_data[$time['ts_to']]['dt_e'] = 1;
}
}
}
// Put service times between alarms at right positions.
if ($service_time_data) {
ksort($service_time_data);
$prev_time = $periodStart;
$prev_alarmid = 0;
foreach ($data as $alarmid => $val) {
/**
* Search what service times was in force during the alarm interval and put selected services right
* before the end of service alarm interval.
*/
$service_times = CArrayHelper::getByKeysRange($service_time_data, $prev_time, $val['clock']);
foreach ($service_times as $ts => $service_time) {
$data[$prev_alarmid.'.'.$ts] = $service_time + ['clock' => $ts];
}
$prev_time = $val['clock'] + 1; // Next range begins in next second.
$prev_alarmid = $alarmid;
}
}
// Sort chronologically.
ksort($data);
// calculate times
$dtCnt = 0;
$utCnt = 0;
$slaTime = [
'dt' => ['problemTime' => 0, 'okTime' => 0],
'ut' => ['problemTime' => 0, 'okTime' => 0]
];
$prevTime = $periodStart;
if (isset($data[$periodStart]['ut_s'])) {
$utCnt += $data[$periodStart]['ut_s'];
}
if (isset($data[$periodStart]['ut_e'])) {
$utCnt -= $data[$periodStart]['ut_e'];
}
if (isset($data[$periodStart]['dt_s'])) {
$dtCnt += $data[$periodStart]['dt_s'];
}
if (isset($data[$periodStart]['dt_e'])) {
$dtCnt -= $data[$periodStart]['dt_e'];
}
foreach ($data as $val) {
// skip first data [already read]
if ($val['clock'] == $periodStart) {
continue;
}
if ($dtCnt > 0) {
$periodType = 'dt';
}
elseif ($utCnt > 0) {
$periodType = 'ut';
}
else {
$periodType = $unmarkedPeriodType;
}
// Calculate the duration of current state. Negative durations are ignored.
$duration = max($val['clock'] - $prevTime, 0);
// state=0,1 [OK] (1 - information severity of trigger), >1 [PROBLEMS] (trigger severity)
if ($startValue > 1) {
$slaTime[$periodType]['problemTime'] += $duration;
}
else {
$slaTime[$periodType]['okTime'] += $duration;
}
if (isset($val['ut_s'])) {
$utCnt += $val['ut_s'];
}
if (isset($val['ut_e'])) {
$utCnt -= $val['ut_e'];
}
if (isset($val['dt_s'])) {
$dtCnt += $val['dt_s'];
}
if (isset($val['dt_e'])) {
$dtCnt -= $val['dt_e'];
}
if (isset($val['alarm'])) {
$startValue = $val['alarm'];
}
$prevTime = $val['clock'];
}
$slaTime['problemTime'] = &$slaTime['ut']['problemTime'];
$slaTime['okTime'] = &$slaTime['ut']['okTime'];
$slaTime['downtimeTime'] = $slaTime['dt']['okTime'] + $slaTime['dt']['problemTime'];
$fullTime = $slaTime['problemTime'] + $slaTime['okTime'];
if ($fullTime > 0) {
$slaTime['problem'] = 100 * $slaTime['problemTime'] / $fullTime;
$slaTime['ok'] = 100 * $slaTime['okTime'] / $fullTime;
}
else {
$slaTime['problem'] = 100;
$slaTime['ok'] = 100;
}
return $slaTime;
}
/**
* Adds information about a weekly scheduled uptime or downtime to the $data array.
*
* @param array $data
* @param int $period_start start of the SLA calculation period
* @param int $period_end end of the SLA calculation period
* @param int $ts_from start of the scheduled uptime or downtime
* @param int $ts_to end of the scheduled uptime or downtime
* @param string $type "ut" for uptime and "dt" for downtime
*/
protected function expandPeriodicalTimes(array &$data, $period_start, $period_end, $ts_from, $ts_to, $type) {
$weekStartDate = new DateTime();
$weekStartDate->setTimestamp($period_start);
$days = $weekStartDate->format('w');
$hours = $weekStartDate->format('H');
$minutes = $weekStartDate->format('i');
$seconds = $weekStartDate->format('s');
$weekStartDate->modify('-'.$days.' day -'.$hours.' hour -'.$minutes.' minute -'.$seconds.' second');
$weekStartTimestamp = $weekStartDate->getTimestamp();
for (; $weekStartTimestamp < $period_end; $weekStartTimestamp += $this->secondsPerNextWeek($weekStartTimestamp)) {
$weekStartDate->setTimestamp($weekStartTimestamp);
$weekStartDate->modify('+'.$ts_from.' second');
$_s = $weekStartDate->getTimestamp();
$weekStartDate->setTimestamp($weekStartTimestamp);
$weekStartDate->modify('+'.$ts_to.' second');
$_e = $weekStartDate->getTimestamp();
if ($period_end < $_s || $period_start >= $_e) {
continue;
}
if ($_s < $period_start) {
$_s = $period_start;
}
if ($_e > $period_end) {
$_e = $period_end;
}
if (isset($data[$_s][$type.'_s'])) {
$data[$_s][$type.'_s']++;
}
else {
$data[$_s][$type.'_s'] = 1;
}
if (isset($data[$_e][$type.'_e'])) {
$data[$_e][$type.'_e']++;
}
else {
$data[$_e][$type.'_e'] = 1;
}
}
}
/**
* Return seconds in next week relative to given week start timestamp.
*
* @param int $currentWeekStartTimestamp
*
* @return int
*/
protected function secondsPerNextWeek($currentWeekStartTimestamp) {
$currentWeekStartDate = new DateTime();
$currentWeekStartDate->setTimestamp($currentWeekStartTimestamp);
$currentWeekStartDate->modify('+7 day');
$result = $currentWeekStartDate->getTimestamp() - $currentWeekStartTimestamp;
return $result;
}
}
|