File: //usr/local/qcloud/monitor/barad/plugin/collector/vm/barad_health.py
import sys
import time
import os
if __name__ == "__main__":
try:
sys.path.append(os.getcwd() + '/../')
sys.path.append(os.getcwd() + '/../../base/')
sys.path.append(os.getcwd() + '/../../../comm/')
except Exception as e:
sys.stderr.write("Set Module Directory Failed")
sys.exit(1)
from utils.metric_handler import MetricHandler
from phealth import PluginHealth
from plugin_base import VmBaseCollector
class baradInfo(object):
def __init__(self, logger):
self.logger = logger
self.plugin = PluginHealth()
def do_collect(self):
barad_map = {}
try:
#barad_map["cpu_usage"] = self.plugin.get_cpu_percent()
barad_mem = self.plugin.get_rss_memory() / 1024.0
barad_map["memory_used"] = "%.3f" % barad_mem
except Exception as e:
self.logger.info("failed to collect barad_agent component, error: %s" % e)
return barad_map
class PluginCollector(VmBaseCollector):
def init(self):
self.set_frequency(60)
self.handler = MetricHandler()
self.handler.namespace = 'qce/cvm'
self.handler.dimensions = ['vm_uuid']
self._barad_collector = baradInfo(self.logger())
def do_collect(self):
vm_uuid = self.get_vm_uuid()
barad_data = self._barad_collector.do_collect()
batch_metric = []
#metric = {"name": "barad_cpu_usage", "value": barad_data.get("cpu_usage", 0.0), "unit": "%"}
#batch_metric.append(metric)
metric = {"name": "barad_mem_used", "value": barad_data.get("memory_used", 0.0), "unit": "MB"}
batch_metric.append(metric)
dimensions = {'vm_uuid': vm_uuid}
self.handler.add_batch_metric(batch=batch_metric, dimensions=dimensions)
if (len( self.handler.get_metrics()) > 0) :
data = {'sender':'nws_sender', 'datas': self.handler.pop_metrics()}
self.put_data(data)
def main():
collector = PluginCollector()
while True:
beg = time.time()
collector.collect()
collector.dump_data()
time.sleep(10)
if __name__ == "__main__":
main()