Current File : //opt/cloudlinux/venv/lib/python3.11/site-packages/cl_plus/collectors/lve_stats.py |
# coding=utf-8
#
# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2020 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENCE.TXT
#
import os
from .collector_base import CollectorBase
import json
import time
from clcommon.utils import get_file_lines
class LveStatsCollector(CollectorBase):
def __init__(self, _logger):
super(LveStatsCollector, self).__init__(_logger)
self._lve_stats_data_file = "/var/lve/cm_lve.json"
self._labels = ["ep", "pmem", "nproc", "iops", "cpu", "io"]
self._mysql_labels = ["cpu", "io"]
self._types = {"usage": "a", "limits": "l", "faults": "f"}
self._cl_errors = {"not_found": False, "external_error": False, "command_error": False,
"json_error": False, "file_error": False, "file_too_old": False, "file_empty": False}
def init(self):
"""
Initialize lve stats collector
:return: None
"""
self._aggregated_data = []
self._logger.info("Lve stats collector init")
def get_averages(self):
"""
Get collector's averages data
:return: dict
{
"lve_stats":
[
{
"lve_id": 1024,
"cpu": {"l": 20.0, "a": 19.89, "f": 2},
"mysql_cpu": {"l": 20.0, "a": 20.0},
"io": {"l": 1048576, "a": 0.0, "f": 0},
"mysql_io": {"l": 20.0, "a": 20.0},
"iops": {"l": 1024, "a": 0.0, "f": 0},
"ep": {"l": 20, "a": 0.0, "f": 0},
"nproc": {"l": 100, "a": 6, "f": 0},
"pmem": {"l": 1073741824, "a": 0.0, "f": 0},
"username": "cltest1", "domain": "cltest1.com"
}
]
}
or None if can't get data
"""
if not os.path.isfile(self._lve_stats_data_file):
if not self._cl_errors["not_found"]:
self._logger.warn("lve-stats data file %s absent. lve-stats collector will not work",
self._lve_stats_data_file)
self._cl_errors["not_found"] = True
return None
self._cl_errors["not_found"] = False
# Read lve-stats data file
try:
# Remove lve-stats data file if it older then 10 min
m_dt_ts = os.path.getmtime(self._lve_stats_data_file)
if time.time() - m_dt_ts > 600:
if not self._cl_errors["file_too_old"]:
self._logger.warn("lve-stats data file %s too old, please check lve-stats is working",
self._lve_stats_data_file)
self._cl_errors["file_too_old"] = True
return None
self._cl_errors["file_too_old"] = False
f_lines = get_file_lines(self._lve_stats_data_file)
except (OSError, IOError) as e:
if not self._cl_errors["file_error"]:
self._logger.warn("lve-stats data file %s read error, lve-stats data will not be sent. The reason is: %s",
(self._lve_stats_data_file, str(e)))
self._cl_errors["file_error"] = True
return None
else:
self._cl_errors["file_error"] = False
try:
json_data = json.loads(''.join(f_lines))
self._aggregated_data = json_data['lve_stats']
if not self._aggregated_data:
if not self._cl_errors["file_empty"]:
self._logger.warn("lve-stats data in file empty, will not be sent.")
self._cl_errors["file_empty"] = True
return None
self._cl_errors["file_empty"] = False
except (KeyError, ValueError, TypeError, json.decoder.JSONDecodeError) as e:
if not self._cl_errors["json_error"]:
self._logger.warn("Data in file corrupted, lve-stats data will not be sent. The reason is: %s", str(e))
self._cl_errors["json_error"] = True
return None
self._cl_errors["json_error"] = False
return {"lve_stats": self._aggregated_data}