4use \Bitrix\Main\Localization\Loc;
22 $dir = new \Bitrix\Main\IO\Directory(static::$rrdPath.
"/".$hostname);
23 return $dir->isExists();
33 $command =
"sudo -u root /opt/webdir/bin/bx-monitor -o json";
37 $action =
new Action(
"is_monitoring_enabled", array(
38 "START_COMMAND_TEMPLATE" => $command,
52 $actRes = $action->getResult();
53 if(isset($actRes[
"is_monitoring_enabled"][
"OUTPUT"][
"DATA"][
"params"][
"monitor"][
"monitoring_status"]))
55 $result = ($actRes[
"is_monitoring_enabled"][
"OUTPUT"][
"DATA"][
"params"][
"monitor"][
"monitoring_status"] ==
"enable");
76 if (!extension_loaded(
'rrd'))
77 throw new \Exception(
"Extension rrd not loaded!");
80 throw new \Bitrix\Main\ArgumentNullException(
"hostname");
83 throw new \Bitrix\Main\ArgumentNullException(
"roleId");
88 throw new \Exception(
"Role with id = ".$roleId.
" was not defined.");
90 if(!isset($role[
"LOADBAR_INFO"]) || $role[
"LOADBAR_INFO"] ==
'')
91 throw new \Exception(
"Role ".$roleId.
" has no correctly defined LOADBAR_INFO param .");
93 $rrdFile = str_replace(
'##HOSTNAME##', $hostname, $role[
"LOADBAR_INFO"]);
94 $rrdPath =
"/var/lib/munin/".$hostname.
"/".$rrdFile;
95 $file = new \Bitrix\Main\IO\File(
$rrdPath);
97 if(!$file->isExists())
98 throw new \Bitrix\Main\IO\FileNotFoundException(
$rrdPath);
102 $result = static::extractRrdValue($data);
110 throw new \Bitrix\Main\ArgumentNullException(
"hostname");
112 if($categoryId ==
'')
113 throw new \Bitrix\Main\ArgumentNullException(
"paramId");
118 if(isset($categories[$categoryId]))
119 $result = $categories[$categoryId];
128 $result[
"HDD"] = array(
130 "PARAMS" => static::getHddsParams($hostname)
133 $result[
"NET"] = array(
135 "PARAMS" => static::getNetParams($hostname)
138 $result[
"HDDACT"] = array(
140 "PARAMS" => static::getHddsUtilization($hostname)
143 $result[
"MEMORY"] = array(
151 "VALUE_FUNC" =>
'\Bitrix\Scale\Monitoring::getMemoryUsage',
152 "FUNC_PARAMS" => array($hostname)
155 "VALUE_FUNC" =>
'\Bitrix\Scale\Monitoring::getMemoryUsageValue',
156 "FUNC_PARAMS" => array($hostname),
164 $result[
"AVG_LOAD"] = array(
169 "RRD" =>
"##HOSTNAME##-load-load-g.rrd",
175 "RRD" =>
"##HOSTNAME##-load-load-g.rrd",
181 "RRD" =>
"##HOSTNAME##-load-load-g.rrd",
191 public static function getValue($hostname, $categoryId, $param)
194 throw new \Bitrix\Main\ArgumentNullException(
"hostname");
196 if($categoryId ==
'')
197 throw new \Bitrix\Main\ArgumentNullException(
"categoryId");
200 throw new \Bitrix\Main\ArgumentNullException(
"param");
202 $arCat = static::getInfoTableCategory($hostname, $categoryId);
205 throw new \Exception(
"Monitoring category ".$categoryId.
" not exist.");
207 if(!$arCat[
"PARAMS"][$param])
208 throw new \Exception(
"Monitoring param ".$param.
" in category ".$categoryId.
" not exist.");
210 $monParam = $arCat[
"PARAMS"][$param];
212 if(isset($monParam[
"TYPE"]) && $monParam[
"TYPE"] ==
"ARRAY")
214 if(!isset($monParam[
"ITEMS"]) || !is_array($monParam[
"ITEMS"]))
215 throw new \Exception(
"Monitoring param ".$param.
" in category ".$categoryId.
" hasn't field ITEMS.");
218 foreach($monParam[
"ITEMS"] as $item)
220 $result[] = static::getItemValue($hostname, $categoryId, $item, $param);
225 $result = static::getItemValue($hostname, $categoryId, $monParam, $param);
231 protected static function getItemValue($hostname, $categoryId, $item, $param)
233 if(isset($item[
"VALUE"]))
234 return $item[
"VALUE"];
236 if(isset($item[
"VALUE_FUNC"]))
238 return call_user_func_array($item[
"VALUE_FUNC"], (isset($item[
"FUNC_PARAMS"]) ? $item[
"FUNC_PARAMS"] : array()));
241 if((!$item[
"RRD"] || !$item[
"CF"]) && !$item[
"OPTIONS"])
242 throw new \Exception(
"Monitoring param item in category ".$categoryId.
" has no RRD or CF fields.");
244 if(isset($item[
"RRD"]))
246 $rrdFile = str_replace(
'##HOSTNAME##', $hostname, $item[
"RRD"]);
247 $rrdPath = static::$rrdPath.
"/".$hostname.
"/".$rrdFile;
248 $file = new \Bitrix\Main\IO\File(
$rrdPath);
250 if(!$file->isExists())
251 throw new \Bitrix\Main\IO\FileNotFoundException(
$rrdPath);
257 if(isset($item[
"OPTIONS"]))
259 $arOpts = $item[
"OPTIONS"];
260 $arOpts = str_replace(
'##HOSTNAME##', $hostname, $arOpts);
264 if($item[
"CF"] ==
"MIN")
268 elseif($item[
"CF"] ==
"MAX")
277 if($item[
"CF"] ==
"LAST")
278 $item[
"CF"] =
"AVERAGE";
280 $format = isset($item[
"FORMAT"]) ? $item[
"FORMAT"] :
"%6.2lf";
283 "DEF:val=".
$rrdPath.
":42:".$item[
"CF"],
284 "VDEF:vval=val,".$agr,
285 "PRINT:vval:".$format);
288 if(isset($item[
"RRD"]))
290 $arOpts[] =
"--start";
296 $data = \rrd_graph(
"/dev/null", $arOpts);
298 if(isset($item[
"DATA_FUNC"]) && is_callable($item[
"DATA_FUNC"]))
300 $result = $item[
"DATA_FUNC"]($data);
304 if(isset($data[
"calcpr"]))
306 $data[
"data"] = $data[
"calcpr"];
309 $result = static::extractRrdValue($data);
319 if(isset($data[
"data"]) && is_array($data[
"data"]))
321 reset($data[
"data"]);
322 $result = current($data[
"data"]);
325 return trim($result);
330 static $info = array();
332 if(!isset($info[$hostname]))
335 $execRes = $shellAdapter->syncExec(
"sudo -u root /usr/bin/ansible ".$hostname.
" -m setup");
336 $serversData = $shellAdapter->getLastOutput();
337 $serversData = mb_substr($serversData, mb_strpos($serversData,
"{"));
341 $info[$hostname] = json_decode($serversData,
true);
345 $info[$hostname] = array();
349 return $info[$hostname];
355 $arData = static::getAnsibleSetup($hostname);
357 if(isset($arData[
"ansible_facts"][
"ansible_mounts"]))
359 foreach($arData[
"ansible_facts"][
"ansible_mounts"] as $mountId => $mount)
361 $result[$mountId] = array(
362 "NAME" => $mount[
"device"].
" ".
Loc::getMessage(
"SCALE_MONITORING_HDD_PARAMS"),
366 "VALUE_FUNC" =>
'\Bitrix\Scale\Monitoring::getHddsValues',
367 "FUNC_PARAMS" => array(
374 "VALUE_FUNC" =>
'\Bitrix\Scale\Monitoring::getHddsUsed',
375 "FUNC_PARAMS" => array(
391 $arData = static::getAnsibleSetup($hostname);
393 if(isset($arData[
"ansible_facts"][
"ansible_mounts"][$param][
"size_total"]) && isset($arData[
"ansible_facts"][
"ansible_mounts"][$param][
"size_available"]))
395 $mount = $arData[
"ansible_facts"][
"ansible_mounts"][$param];
396 $result = ($mount[
"size_total"]-$mount[
"size_available"])/$mount[
"size_total"]*100;
408 $arData = static::getAnsibleSetup($hostname);
410 if(isset($arData[
"ansible_facts"][
"ansible_mounts"][$param][
"size_total"]) && isset($arData[
"ansible_facts"][
"ansible_mounts"][$param][
"size_available"]))
412 $mount = $arData[
"ansible_facts"][
"ansible_mounts"][$param];
414 $result = static::formatSize($mount[
"size_total"], 2).
" / ".
415 static::formatSize(($mount[
"size_total"]-$mount[
"size_available"]), 2).
" / ".
416 static::formatSize($mount[
"size_available"], 2);
428 $dir = new \Bitrix\Main\IO\Directory(static::$rrdPath.
"/".$hostname);
430 if(!$dir->isExists())
433 $arChildren = $dir->getChildren();
436 foreach ($arChildren as $child)
438 if(!$child->isFile())
441 $name = $child->getName();
442 $pos1 = mb_strpos($name,
"-if_");
443 $pos2 = mb_strpos($name,
"-up-");
445 if($pos1 !==
false && $pos2 !==
false)
448 $dev = mb_substr($name, $pos1, $pos2 - $pos1);
450 $result[$dev] = array(
452 "RRD" => $hostname.
"-if_".$dev.
"-up-d.rrd",
454 "DEF:in=".static::$rrdPath.
"/".$hostname.
"/".$hostname.
"-if_".$dev.
"-up-d.rrd:42:AVERAGE:start=now-600;end=now",
455 "DEF:out=".static::$rrdPath.
"/".$hostname.
"/".$hostname.
"-if_".$dev.
"-down-d.rrd:42:AVERAGE:start=now-600;end=now",
457 "VDEF:vout=out,TOTAL",
461 "DATA_FUNC" =>
'\Bitrix\Scale\Monitoring::formatNetParamsValue'
473 if(isset($data[
'calcpr'][0], $data[
'calcpr'][1]))
475 $result = static::formatSize((
int)$data[
'calcpr'][0]/600) .
477 static::formatSize((
int)$data[
'calcpr'][1]/600).
' ' .
486 $dir = new \Bitrix\Main\IO\Directory(static::$rrdPath.
"/".$hostname);
488 if(!$dir->isExists())
491 $arChildren = $dir->getChildren();
494 foreach ($arChildren as $child)
496 if(!$child->isFile())
499 $name = $child->getName();
500 $pos1 = mb_strpos($name,
"-diskstats_utilization-");
501 $pos2 = mb_strpos($name,
"-util-");
502 if($pos1 !==
false && $pos2 !==
false)
505 $dev = mb_substr($name, $pos1, $pos2 - $pos1);
507 $result[$dev] = array(
513 "DEF:r=".static::$rrdPath.
"/".$hostname.
"/".$hostname.
"-diskstats_throughput-".$dev.
"-rdbytes-g.rrd:42:AVERAGE:start=now-600;end=now",
514 "DEF:w=".static::$rrdPath.
"/".$hostname.
"/".$hostname.
"-diskstats_throughput-".$dev.
"-wrbytes-g.rrd:42:AVERAGE:start=now-600;end=now",
520 "DATA_FUNC" =>
'\Bitrix\Scale\Monitoring::formatHddsUtilizationValue'
523 "RRD" => $hostname.
"-diskstats_utilization-".$dev.
"-util-g.rrd",
525 "FORMAT" =>
"%2.2lf",
540 if(isset($data[
'calcpr'][0], $data[
'calcpr'][1]))
542 $result = static::formatSize((
int)$data[
'calcpr'][0]/600) .
544 static::formatSize((
int)$data[
'calcpr'][1]/600) .
545 ' '.Loc::getMessage(
'SCALE_MONITORING_NET_SEC');
558 static $a = array(
"b",
"Kb",
"Mb",
"Gb",
"Tb");
560 while($size >= 1024 && $pos < 4)
565 return round($size, $precision).
" ".$a[$pos];
571 $arData = static::getAnsibleSetup($hostname);
573 if(isset($arData[
"ansible_facts"][
"ansible_memtotal_mb"]) && isset($arData[
"ansible_facts"][
"ansible_memfree_mb"]))
574 $result = $arData[
"ansible_facts"][
"ansible_memtotal_mb"].
" / ".(intval($arData[
"ansible_facts"][
"ansible_memtotal_mb"])-intval($arData[
"ansible_facts"][
"ansible_memfree_mb"])).
" / ".$arData[
"ansible_facts"][
"ansible_memfree_mb"].
" Mb";
582 $arData = static::getAnsibleSetup($hostname);
584 if(isset($arData[
"ansible_facts"][
"ansible_memtotal_mb"]) && isset($arData[
"ansible_facts"][
"ansible_memfree_mb"]) && intval($arData[
"ansible_facts"][
"ansible_memtotal_mb"]) != 0)
585 $result = (intval($arData[
"ansible_facts"][
"ansible_memtotal_mb"]) - intval($arData[
"ansible_facts"][
"ansible_memfree_mb"]))/intval($arData[
"ansible_facts"][
"ansible_memtotal_mb"])*100;
static loadMessages($file)
static getMessage($code, $replace=null, $language=null)
static getHddsUtilization($hostname)
static getMemoryUsage($hostname)
static formatHddsUtilizationValue(array $data)
static isDatabaseCreated($hostname)
static getMemoryUsageValue($hostname)
static getHddsValues($hostname, $param)
static getLoadBarValue($hostname, $roleId)
static getHddsUsed($hostname, $param)
static extractRrdValue($data)
static formatNetParamsValue(array $data)
static formatSize($size, $precision=2)
static getItemValue($hostname, $categoryId, $item, $param)
static getHddsParams($hostname)
static getInfoTableCategory($hostname, $categoryId)
static getInfoTableCategoriesList($hostname)
static getAnsibleSetup($hostname)
static getValue($hostname, $categoryId, $param)
static getNetParams($hostname)
static $monitoringCategories