diff --git a/src/base/messaging/messaging.py b/src/base/messaging/messaging.py index 7146451..e69f264 100644 --- a/src/base/messaging/messaging.py +++ b/src/base/messaging/messaging.py @@ -2,11 +2,12 @@ # -*- coding: utf-8 -*- # Author: Volkan Şahin import json +import os from base.scope import Scope from base.system.system import System from base.util.util import Util -import os +from base.system.disk_info import DiskInfo # TODO Message Factory @@ -66,6 +67,7 @@ class Messaging(object): return str(json_data) def login_msg(self, username,ip=None): + ssd_list, hdd_list = DiskInfo.get_all_disks() data = dict() data['type'] = 'LOGIN' data['username'] = username @@ -89,6 +91,12 @@ class Messaging(object): data['hardware.printers'] = str(System.Hardware.printers()), data['hardware.systemDefinitions'] = str(System.Hardware.system_definitions()), + if len(ssd_list) > 0: + data['hardware.disk.ssd.info'] = str(ssd_list) + + if len(hdd_list) > 0: + data['hardware.disk.hdd.info'] = str(hdd_list) + json_data = json.dumps(data) self.logger.debug('Login message was created') return json_data diff --git a/src/base/registration/registration.py b/src/base/registration/registration.py index f65b918..d682f9d 100644 --- a/src/base/registration/registration.py +++ b/src/base/registration/registration.py @@ -20,6 +20,7 @@ from base.registration.execute_cancel_sssd_authentication import ExecuteCancelSS from base.registration.execute_sssd_authentication import ExecuteSSSDAuthentication from base.registration.execute_sssd_ad_authentication import ExecuteSSSDAdAuthentication from base.registration.execute_cancel_sssd_ad_authentication import ExecuteCancelSSSDAdAuthentication +from base.system.disk_info import DiskInfo class Registration: def __init__(self): @@ -297,6 +298,12 @@ class Registration: 'agentVersion': Util.get_agent_version(), } + ssd_list, hdd_list = DiskInfo.get_all_disks() + if len(ssd_list) > 0: + params['hardware.disk.ssd.info'] = str(ssd_list) + if len(hdd_list) > 0: + params['hardware.disk.hdd.info'] = str(hdd_list) + return json.dumps(params) def unregister(self): diff --git a/src/base/system/disk_info.py b/src/base/system/disk_info.py new file mode 100644 index 0000000..ed0586c --- /dev/null +++ b/src/base/system/disk_info.py @@ -0,0 +1,115 @@ +# !/usr/bin/env python3 +# -*- coding: utf-8 -*- +# Author: Agah Hulusi ÖZ + +from base.util.util import Util +import psutil + +# HDD and SSD disk information +class DiskInfo(): + + @staticmethod + def total_disk_used(): + ssd_list, hdd_list = DiskInfo.get_all_disks() + total_disk_usage = 0 + if len(ssd_list) > 0: + for disk in ssd_list: + total_disk_usage += int(disk['used']) + if len(hdd_list) > 0: + for disk in hdd_list: + total_disk_usage += int(disk['used']) + return total_disk_usage + + @staticmethod + def total_disk(): + ssd_list, hdd_list = DiskInfo.get_all_disks() + total_size = 0 + for disk in ssd_list: + total_size += int(disk['total']) + for disk in hdd_list: + total_size += int(disk['total']) + return total_size + + @staticmethod + def get_all_disks(): + result_code, p_out, p_err = Util.execute("lsblk -b -o NAME,TYPE,ROTA,SIZE,RM,HOTPLUG,MOUNTPOINT,FSUSED | grep -v loop | awk '$5 == \"0\" { print $0 }'") + txt = p_out.split("\n") + while '' in txt: + txt.remove('') + detail_txt = [] + ssd_list = [] + hdd_list = [] + # Ignore USB from list + for item in txt: + item = item.split() + detail_txt.append(item) + # SSD and HDD list + for disk in detail_txt: + # Second element of disk equal to rotation type. + # Rotation type show that disk is SSD or HDD + # If it equals to "0" SSD + # If it equals to "1" HDD + if disk[2] == "0" and disk[1] == "disk": + ssd_list.append({ + "name": disk[0], + "type": "SSD", + "total": disk[3], + "used": 0, + }) + elif disk[2] == "1" and disk[1] == "disk": + hdd_list.append({ + "name": disk[0], + "type": "HDD", + "total": disk[3], + "used": 0, + }) + # Calculate the usage + used = 0 + ssd_list_counter = 0 + hdd_list_counter = 0 + is_first_disk = True + resource_name = 0 + resource_disk = 0 + for item in detail_txt: + if item[1] == "disk": + if is_first_disk: + total = item[3] + name = item[0] + type = item[2] + is_first_disk = False + else: + if type == "0": + ssd_list[ssd_list_counter]["used"] = used + ssd_list_counter += 1 + elif type == "1": + hdd_list[hdd_list_counter]["used"] = used + hdd_list_counter += 1 + name = item[0] + used = 0 + total = item[3] + type = item[2] + else: + if len(item) > 7 and item[0] != "NAME": + if item[6] == "/": + resource_disk = psutil.disk_usage(item[6])[0] + resource_name = name + used += int(item[7]) + for i in ssd_list: + if i["name"] == resource_name: + i["total"] = resource_disk + for i in hdd_list: + if i["name"] == resource_name: + i["total"] = resource_disk + if type == "0": + ssd_list[ssd_list_counter]["used"] = used + ssd_list_counter += 1 + elif type == "1": + hdd_list[hdd_list_counter]["used"] = used + hdd_list_counter += 1 + for item in ssd_list: + item["total"]= int(int(item["total"]) / (1000 * 1000)) + item["used"] = int(int(item["used"]) / (1000 * 1000)) + for item in hdd_list: + item["total"] = int(int(item["total"]) / (1000 * 1000)) + item["used"] = int(int(item["used"]) / (1000 * 1000)) + return ssd_list, hdd_list diff --git a/src/base/system/system.py b/src/base/system/system.py index c2d725b..999faa9 100644 --- a/src/base/system/system.py +++ b/src/base/system/system.py @@ -434,15 +434,18 @@ class System: @staticmethod def total(): - return int(int(psutil.disk_usage('/')[0]) / (1000 * 1000)) + return int(DiskInfo.total_disk()) +# return int(int(psutil.disk_usage('/')[0]) / (1000 * 1000)) @staticmethod def used(): - return int(int(psutil.disk_usage('/')[1]) / (1000 * 1000)) + return int(DiskInfo.total_disk_used()) +# return int(int(psutil.disk_usage('/')[1]) / (1000 * 1000)) @staticmethod def free(): - return int(int(psutil.disk_usage('/')[2]) / (1000 * 1000)) + return int(DiskInfo.total_disk_free()) +# return int(int(psutil.disk_usage('/')[2]) / (1000 * 1000)) @staticmethod def percent(): diff --git a/src/plugins/resource-usage/resource_info_fetcher.py b/src/plugins/resource-usage/resource_info_fetcher.py index d905f04..5e933bc 100644 --- a/src/plugins/resource-usage/resource_info_fetcher.py +++ b/src/plugins/resource-usage/resource_info_fetcher.py @@ -3,9 +3,12 @@ # Author: Cemre ALPSOY # Author: Emre Akkaya -from base.plugin.abstract_plugin import AbstractPlugin import json +from psutil import disk_io_counters +from base.plugin.abstract_plugin import AbstractPlugin +from base.system.disk_info import DiskInfo + class ResourceUsage(AbstractPlugin): def __init__(self, data, context): @@ -19,6 +22,7 @@ class ResourceUsage(AbstractPlugin): try: device = "" self.logger.debug("Gathering resource usage for disk, memory and CPU.") + ssd_list, hdd_list = DiskInfo.get_all_disks() for part in self.Hardware.Disk.partitions(): if len(device) != 0: device += ", " @@ -36,6 +40,12 @@ class ResourceUsage(AbstractPlugin): 'CPU Actual Hz': self.Hardware.Cpu.hz_actual(), 'CPU Advertised Hz': self.Hardware.Cpu.hz_advertised() } + if len(ssd_list) > 0: + data['hardware.disk.ssd.info'] = str(ssd_list) + + if len(hdd_list) > 0: + data['hardware.disk.hdd.info'] = str(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ı.',