1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
monitoring.php
См. документацию.
1<?php
4{
5 private static $instance = /*.(CBitrixCloudMonitoring).*/ null;
6 private $result = /*.(CBitrixCloudMonitoringResult).*/null;
7 private $interval = 0;
8
15 public static function getInstance()
16 {
17 if (!isset(self::$instance))
18 {
19 self::$instance = new CBitrixCloudMonitoring;
20 }
21
22 return self::$instance;
23 }
24
25 public function getConfiguredDomains()
26 {
27 $result = [];
28 $converter = CBXPunycode::GetConverter();
29
30 $domainName = COption::GetOptionString('main', 'server_name', '');
31 if ($domainName != '')
32 {
33 $result[$domainName] = $domainName;
34 }
35
36 $siteList = CSite::GetList('', '', ['ACTIVE' => 'Y']);
37 while ($site = $siteList->Fetch())
38 {
39 $domains = explode("\r\n", $site['DOMAINS']);
40 foreach ($domains as $domainName)
41 {
42 if ($domainName != '')
43 {
44 $punyName = $converter->Encode($domainName);
45 if ($punyName !== false)
46 {
47 $result[$punyName] = $domainName;
48 }
49 }
50 }
51 }
52
53 ksort($result);
54 return $result;
55 }
56
57 public function getList()
58 {
59 $web_service = new CBitrixCloudMonitoringWebService();
60 $xml = $web_service->actionGetList();
61 /* @var CDataXMLNode $node */
62 $node = $xml->SelectNodes('/control/domains');
63 if (is_object($node))
64 {
65 $result = [];
66 $children = $node->children();
67 if (is_array($children))
68 {
69 /* @var CDataXMLNode $domainXml */
70 foreach ($children as $domainXml)
71 {
72 $name = $domainXml->getAttribute('name');
73 $emails = $domainXml->elementsByName('emails');
74 $tests = $domainXml->elementsByName('tests');
75 $result[] = [
76 'DOMAIN' => $name,
77 'IS_HTTPS' => ($domainXml->getAttribute('https') === 'true' ? 'Y' : 'N'),
78 'LANG' => $domainXml->getAttribute('lang'),
79 'EMAILS' => (is_array($emails) ? explode(',', $emails[0]->textContent()) : []),
80 'TESTS' => (is_array($tests) ? explode(',', $tests[0]->textContent()) : []),
81 ];
82 }
83 }
84 return $result;
85 }
86 }
87
88 public function addDevice($domain, $deviceId)
89 {
90 if ($deviceId != '')
91 {
92 $option = CBitrixCloudOption::getOption('monitoring_devices');
93 $devices = $option->getArrayValue();
94 $devices[] = $domain . '|' . $deviceId;
95 $option->setArrayValue($devices);
96 }
97 }
98
99 public function deleteDevice($domain, $deviceId)
100 {
101 if ($deviceId != '')
102 {
103 $option = CBitrixCloudOption::getOption('monitoring_devices');
104 $devices = $option->getArrayValue();
105 $index = array_search($domain . '|' . $deviceId, $devices, true);
106 if ($index !== false)
107 {
108 unset($devices[$index]);
109 $option->setArrayValue($devices);
110 }
111 }
112 }
113
114 public function getDevices($domain)
115 {
116 $result = [];
117 $option = CBitrixCloudOption::getOption('monitoring_devices');
118 $devices = $option->getArrayValue();
119 foreach ($devices as $domain_device)
120 {
121 if (list ($myDomain, $myDevice) = explode('|', $domain_device, 2))
122 {
123 if ($myDomain === $domain)
124 {
125 $result[] = $myDevice;
126 }
127 }
128 }
129 return $result;
130 }
131
139 public function startMonitoring($domain, $is_https, $language_id, $emails, $tests)
140 {
141 try
142 {
143 $web_service = new CBitrixCloudMonitoringWebService();
144 $web_service->actionStart($domain, $is_https, $language_id, $emails, $tests);
146 return '';
147 }
148 catch (CBitrixCloudException $e)
149 {
150 return $e->getMessage();//."[".htmlspecialcharsEx($e->getErrorCode())."]";
151 }
152 }
153
161 public function stopMonitoring($domain)
162 {
163 try
164 {
165 $web_service = new CBitrixCloudMonitoringWebService();
166 $web_service->actionStop($domain);
168 return '';
169 }
170 catch (CBitrixCloudException $e)
171 {
172 return $e->getMessage();//."[".htmlspecialcharsEx($e->getErrorCode())."]";
173 }
174 }
175
176 public function setInterval($interval)
177 {
178 $interval = intval($interval);
179 if (
180 $interval != 7
181 && $interval != 30
182 && $interval != 90
183 && $interval != 365
184 )
185 {
186 $interval = 7;
187 }
188 $this->interval = $interval;
189 return $interval;
190 }
191
192 public function getInterval()
193 {
194 if ($this->interval <= 0)
195 {
196 $this->interval = intval(COption::GetOptionInt('bitrixcloud', 'monitoring_interval'));
197 if (
198 $this->interval != 7
199 && $this->interval != 30
200 && $this->interval != 90
201 && $this->interval != 365
202 )
203 {
204 $this->interval = 7;
205 }
206 }
207 return $this->interval;
208 }
209
210 public function getMonitoringResults($interval = false)
211 {
212 if ($interval === false)
213 {
214 $interval = $this->getInterval();
215 }
216 else
217 {
218 $interval = $this->setInterval($interval);
219 }
220
221 if ($this->result === null)
222 {
223 try
224 {
226 {
227 $web_service = new CBitrixCloudMonitoringWebService();
228 $xml = $web_service->actionGetInfo($interval);
229 $domains = $xml->SelectNodes('/control/domains');
230 if (is_object($domains))
231 {
232 $this->result = CBitrixCloudMonitoringResult::fromXMLNode($domains);
233 /* @var CDataXMLNode $control */
234 $control = $xml->SelectNodes('/control');
235 if (is_object($control))
236 {
237 $this->result->saveToOptions();
238 CBitrixCloudMonitoringResult::setExpirationTime(strtotime($control->getAttribute('expires')));
239 }
240 }
241 }
242 else
243 {
245 }
246 }
247 catch (CBitrixCloudException $e)
248 {
250 return $e->getMessage();//."[".htmlspecialcharsEx($e->getErrorCode())."]";
251 }
252 }
253 return $this->result;
254 }
255
256 public function getAlertsCurrentResult()
257 {
258 $alerts = false;
259 if ($this->result)
260 {
261 $alerts = [];
262 foreach ($this->result as $domainName => $domainResult)
263 {
264 foreach ($domainResult as $testId => $testResult)
265 {
266 if ($testResult->getStatus() === CBitrixCloudMonitoringResult::RED_LAMP)
267 {
268 $alerts[$domainName][$testId] = $testId;
269 }
270 }
271
272 if (isset($alerts[$domainName]))
273 {
274 ksort($alerts[$domainName]);
275 $alerts[$domainName] = implode(',', $alerts[$domainName]);
276 }
277 }
278 ksort($alerts);
279 }
280 return $alerts;
281 }
282
283 public function getAlertsStored()
284 {
285 return CBitrixCloudOption::getOption('monitoring_alert')->getArrayValue();
286 }
287
288 public function storeAlertsCurrentResult()
289 {
290 $alerts = $this->getAlertsCurrentResult();
291 if (is_array($alerts))
292 {
293 CBitrixCloudOption::getOption('monitoring_alert')->setArrayValue($alerts);
294 }
295 }
296
297 public function getWorstUptime($testId = '', $domainName = '')
298 {
299 $result = '';
300 $maxDiff = 0;
301
302 if ($this->result)
303 {
304 if ($domainName === '')
305 {
306 foreach ($this->result as $domainResult)
307 {
308 foreach ($domainResult as $testResult)
309 {
310 if (
311 ($testId === '' || $testId === $testResult->getName())
312 && $testResult->getStatus() === CBitrixCloudMonitoringResult::RED_LAMP
313 )
314 {
315 if ($testResult->getUptime())
316 {
317 $uptime = explode('/', $testResult->getUptime());
318 $diff = $uptime[1] - $uptime[0];
319 if ($diff > $maxDiff)
320 {
321 $maxDiff = $diff;
322 $result = $testResult->getUptime();
323 }
324 }
325 }
326 }
327 }
328 }
329 elseif (is_array($this->result[$domainName]))
330 {
331 foreach ($this->result[$domainName] as $testResult)
332 {
333 if (
334 ($testId === '' || $testId === $testResult->getName())
335 && $testResult->getStatus() === CBitrixCloudMonitoringResult::RED_LAMP
336 )
337 {
338 if ($testResult->getUptime())
339 {
340 $uptime = explode('/', $testResult->getUptime());
341 $diff = $uptime[1] - $uptime[0];
342 if ($diff > $maxDiff)
343 {
344 $maxDiff = $diff;
345 $result = $testResult->getUptime();
346 }
347 }
348 }
349 }
350 }
351 }
352
353 return $result;
354 }
355
356 public static function startMonitoringAgent()
357 {
359 $rsR = CLanguage::GetById('ru');
360 if ($rsR->Fetch())
361 {
362 $language_id = 'ru';
363 }
364 else
365 {
366 $rsD = CLanguage::GetById('de');
367 if ($rsD->Fetch())
368 {
369 $language_id = 'de';
370 }
371 else
372 {
373 $language_id = 'en';
374 }
375 }
376
377 $monitoring->startMonitoring(
378 COption::GetOptionString('main', 'server_name', ''),
379 false,
380 $language_id,
381 [
382 COption::GetOptionString('main', 'email_from', ''),
383 ],
384 [
385 'test_lic',
386 'test_domain_registration',
387 'test_http_response_time',
388 ]
389 );
390 return '';
391 }
392}
static GetConverter()
Определения punycode.php:25
deleteDevice($domain, $deviceId)
Определения monitoring.php:99
storeAlertsCurrentResult()
Определения monitoring.php:288
getDevices($domain)
Определения monitoring.php:114
getAlertsStored()
Определения monitoring.php:283
setInterval($interval)
Определения monitoring.php:176
getConfiguredDomains()
Определения monitoring.php:25
getList()
Определения monitoring.php:57
getInterval()
Определения monitoring.php:192
getMonitoringResults($interval=false)
Определения monitoring.php:210
static startMonitoringAgent()
Определения monitoring.php:356
getAlertsCurrentResult()
Определения monitoring.php:256
stopMonitoring($domain)
Определения monitoring.php:161
static getInstance()
Определения monitoring.php:15
startMonitoring($domain, $is_https, $language_id, $emails, $tests)
Определения monitoring.php:139
addDevice($domain, $deviceId)
Определения monitoring.php:88
getWorstUptime($testId='', $domainName='')
Определения monitoring.php:297
static fromXMLNode(CDataXMLNode $node)
Определения monitoring_result.php:397
static loadFromOptions()
Определения monitoring_result.php:366
static setExpirationTime($time)
Определения monitoring_result.php:359
static getOption($name)
Определения option.php:25
$children
Определения sync.php:12
$result
Определения get_property_values.php:14
$control
Определения iblock_catalog_edit.php:61
$emails
Определения mail_entry.php:72
IncludeModuleLangFile($filepath, $lang=false, $bReturnArray=false)
Определения tools.php:3778
$name
Определения menu_edit.php:35
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$siteList
Определения options.php:47
$option
Определения options.php:1711
$site
Определения yandex_run.php:614