Ahenk/usr/share/ahenk/plugins/resource-usage/resource_info_fetcher.py

64 lines
2.7 KiB
Python
Raw Normal View History

#!/usr/bin/python
# -*- coding: utf-8 -*-
# Author: Cemre ALPSOY <cemre.alpsoy@agem.com.tr>
# Author: Emre Akkaya <emre.akkaya@agem.com.tr>
2022-11-11 15:33:53 +03:00
from psutil import disk_io_counters
from base.plugin.abstract_plugin import AbstractPlugin
import json
2022-11-11 15:33:53 +03:00
from base.system.disk_info import DiskInfo
class ResourceUsage(AbstractPlugin):
def __init__(self, data, context):
super(AbstractPlugin, self).__init__()
self.data = data
self.context = context
self.logger = self.get_logger()
self.message_code = self.get_message_code()
def handle_task(self):
try:
device = ""
self.logger.debug("Gathering resource usage for disk, memory and CPU.")
2022-11-11 15:33:53 +03:00
ssd_list, hdd_list = DiskInfo.get_all_disks()
for part in self.Hardware.Disk.partitions():
if len(device) != 0:
device += ", "
device = device + part.device
2022-11-11 15:33:53 +03:00
data = {'System': self.Os.name(), 'Release': self.Os.kernel_release(),
'Version': self.Os.distribution_version(), 'Machine': self.Os.architecture(),
'CPU Physical Core Count': self.Hardware.Cpu.physical_core_count(),
'Total Memory': self.Hardware.Memory.total(),
'Usage': self.Hardware.Memory.used(),
'Total Disc': self.Hardware.Disk.total(),
'Usage Disc': self.Hardware.Disk.used(),
'Processor': self.Hardware.Cpu.brand(),
'Device': device,
'CPU Logical Core Count': self.Hardware.Cpu.logical_core_count(),
'CPU Actual Hz': self.Hardware.Cpu.hz_actual(),
'CPU Advertised Hz': self.Hardware.Cpu.hz_advertised()
}
2022-11-11 15:33:53 +03:00
if len(ssd_list) > 0:
data['hardware.disk.ssd.info'] = ssd_list
if len(hdd_list) > 0:
data['hardware.disk.hdd.info'] = hdd_list
self.logger.debug("Resource usage info gathered.")
self.context.create_response(code=self.message_code.TASK_PROCESSED.value,
message='Anlık kaynak kullanım bilgisi başarıyla toplandı.',
data=json.dumps(data), content_type=self.get_content_type().APPLICATION_JSON.value)
except Exception as e:
self.logger.error(str(e))
self.context.create_response(code=self.message_code.TASK_ERROR.value,
message='Anlık kaynak kullanım bilgisi toplanırken hata oluştu: {0}'.format(str(e)))
def handle_task(task, context):
plugin = ResourceUsage(task, context)
plugin.handle_task()