From 696373d35885bcaebfd3d021507b298213c3baea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tuncay=20=C3=87OLAK?= Date: Tue, 27 Oct 2020 14:11:57 +0300 Subject: [PATCH] added agent version info and added agent_info command for updated agent properties --- src/base/messaging/messaging.py | 1 + src/base/registration/registration.py | 1 + src/base/util/util.py | 10 +++++ src/plugins/resource-usage/agent_info.py | 56 ++++++++++++++++++++++++ 4 files changed, 68 insertions(+) create mode 100644 src/plugins/resource-usage/agent_info.py diff --git a/src/base/messaging/messaging.py b/src/base/messaging/messaging.py index dca6a1a..caa239d 100644 --- a/src/base/messaging/messaging.py +++ b/src/base/messaging/messaging.py @@ -78,6 +78,7 @@ class Messaging(object): data['diskFree'] = System.Hardware.Disk.free() data['memory'] = System.Hardware.Memory.total() data['hostname'] = str(System.Os.hostname()) + data['agentVersion'] = str(Util.get_agent_version()) self.logger.debug('USER IP : '+ str(ip)+ ' IPADDRESSES : '+ str(System.Hardware.Network.ip_addresses()).replace('[', '').replace(']', '')) diff --git a/src/base/registration/registration.py b/src/base/registration/registration.py index 8058e25..cc1fcda 100644 --- a/src/base/registration/registration.py +++ b/src/base/registration/registration.py @@ -281,6 +281,7 @@ class Registration: 'hardware.baseboard.serialNumber': System.Hardware.BaseBoard.serial_number()[1].replace('\n', '') if System.Hardware.BaseBoard.serial_number()[0] == 0 else 'n/a', 'processor': System.Hardware.Cpu.brand(), + 'agentVersion': Util.get_agent_version(), } return json.dumps(params) diff --git a/src/base/util/util.py b/src/base/util/util.py index 21cd9b0..40ad67e 100644 --- a/src/base/util/util.py +++ b/src/base/util/util.py @@ -488,3 +488,13 @@ class Util: return pwd.getpwnam(username).pw_gid except: raise + + @staticmethod + def get_agent_version(): + result_code, result, p_err = Util.execute('dpkg -s {} | grep Version'.format("ahenk")) + data = result.split(': ') + if data[0] == 'Version': + version = data[1].strip('\n') + return version + else: + return None diff --git a/src/plugins/resource-usage/agent_info.py b/src/plugins/resource-usage/agent_info.py new file mode 100644 index 0000000..421d9f7 --- /dev/null +++ b/src/plugins/resource-usage/agent_info.py @@ -0,0 +1,56 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# Author: Tuncay ÇOLAK + +from base.plugin.abstract_plugin import AbstractPlugin +import json + + +class AgentInfo(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.") + for part in self.Hardware.Disk.partitions(): + if len(device) != 0: + device += ", " + device = device + part.device + + data = {'System': self.Os.name(), 'Release': self.Os.kernel_release(), + 'agentVersion': self.get_agent_version(), + 'hostname': self.Os.hostname(), + 'ipAddresses': str(self.Hardware.Network.ip_addresses()).replace('[', '').replace(']', ''), + 'os.name': self.Os.name(), + 'osVersion': self.Os.version(), + 'macAddresses': str(self.Hardware.Network.mac_addresses()).replace('[', '').replace(']', ''), + 'hardware.systemDefinitions': self.Hardware.system_definitions(), + 'hardware.monitors': self.Hardware.monitors(), + 'hardware.screens': self.Hardware.screens(), + 'hardware.usbDevices': self.Hardware.usb_devices(), + 'hardware.printers': self.Hardware.printers(), + 'diskTotal': self.Hardware.Disk.total(), + 'diskUsed': self.Hardware.Disk.used(), + 'diskFree': self.Hardware.Disk.free(), + 'memory': self.Hardware.Memory.total(), + 'Device': device, + } + self.logger.debug("Agent info gathered.") + self.context.create_response(code=self.message_code.TASK_PROCESSED.value, + message='Ahenk bilgileri başarıyla güncellendi.', + 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='Ahenk bilgileri güncellenirken hata oluştu: {0}'.format(str(e))) + + +def handle_task(task, context): + plugin = AgentInfo(task, context) + plugin.handle_task()