From 68db9eb342c89348eff4cad3c713d03dc2f7ee46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20=C5=9Eahin?= Date: Wed, 8 Jun 2016 14:43:07 +0300 Subject: [PATCH 1/5] Description added --- debian/control | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/debian/control b/debian/control index 6ed1a27..0e1cc77 100644 --- a/debian/control +++ b/debian/control @@ -9,5 +9,4 @@ Homepage: http://www.liderahenk.org.tr Package: ahenk Architecture: any Depends:python3 (>= 3), cython, libidn11, libidn11-dev, slixmpp, python3-dev, python3-pip, libffi-dev, libssl-dev, python3-paramiko, python3-psutil -Description: LiderAhenk agent application - Long Desc +Description: Lider Ahenk is an open source project which provides solutions to manage, monitor and audit unlimited number of different systems and users on a network. From 9e2fbac6a57e200ef2bb95b220e858cef08a46f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20=C5=9Eahin?= Date: Wed, 8 Jun 2016 14:43:51 +0300 Subject: [PATCH 2/5] already running problem fixed when call status --- opt/ahenk/base/command/commander.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opt/ahenk/base/command/commander.py b/opt/ahenk/base/command/commander.py index 3904f4b..da85512 100644 --- a/opt/ahenk/base/command/commander.py +++ b/opt/ahenk/base/command/commander.py @@ -180,7 +180,7 @@ class Commander(object): def status(self): ahenk_state = False - if System.Ahenk.get_pid_number(): + if System.Ahenk.is_running() is True: ahenk_state = True return "Ahenk Active:{0}\nInstalled Plugins:{1}".format(ahenk_state, str(System.Ahenk.installed_plugins())) From 944498af3db5d468e5640f7b71b90015ce5c65c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20=C5=9Eahin?= Date: Wed, 8 Jun 2016 14:52:37 +0300 Subject: [PATCH 3/5] some features blocked because of dependency management problems --- opt/ahenk/base/registration/Registration.py | 4 +- opt/ahenk/base/system/system.py | 197 +++++++++++++------- 2 files changed, 136 insertions(+), 65 deletions(-) diff --git a/opt/ahenk/base/registration/Registration.py b/opt/ahenk/base/registration/Registration.py index 2bce7a5..9cb3448 100644 --- a/opt/ahenk/base/registration/Registration.py +++ b/opt/ahenk/base/registration/Registration.py @@ -105,8 +105,8 @@ class Registration(): 'os.distributionVersion': System.Os.distribution_version(), 'os.architecture': System.Os.architecture(), 'hardware.cpu.architecture': System.Hardware.Cpu.architecture(), - 'hardware.cpu.logicalCoreCount': System.Hardware.Cpu.logical_core_count(), - 'hardware.cpu.physicalCoreCount': System.Hardware.Cpu.physical_core_count(), + # 'hardware.cpu.logicalCoreCount': System.Hardware.Cpu.logical_core_count(), + # 'hardware.cpu.physicalCoreCount': System.Hardware.Cpu.physical_core_count(), 'hardware.disk.total': System.Hardware.Disk.total(), 'hardware.disk.used': System.Hardware.Disk.used(), 'hardware.disk.free': System.Hardware.Disk.free(), diff --git a/opt/ahenk/base/system/system.py b/opt/ahenk/base/system/system.py index 761f4bd..9edccc9 100644 --- a/opt/ahenk/base/system/system.py +++ b/opt/ahenk/base/system/system.py @@ -4,11 +4,17 @@ import platform import psutil -import cpuinfo +# import cpuinfo import re import os import configparser - +import socket +import fcntl +import struct +from uuid import getnode as get_mac +""" +some functions closed because of dependency management +""" class System: class Ahenk(object): @@ -58,18 +64,14 @@ class System: @staticmethod def is_running(): - try: if System.Ahenk.get_pid_number() is not None: - if psutil.Process(int(System.Ahenk.get_pid_number())).is_running() is True: - return True + return psutil.pid_exists(int(System.Ahenk.get_pid_number())) else: return False except Exception as e: return False - - @staticmethod def config_path(): return '/etc/ahenk/ahenk.conf' @@ -80,6 +82,11 @@ class System: class Process(object): + @staticmethod + def process_by_pid(pid): + return psutil.Process(pid) + + """ @staticmethod def pids(): return psutil.pids() @@ -90,11 +97,7 @@ class System: if psutil.Process(id).name() == p_name: return id return None - - - @staticmethod - def process_by_pid(pid): - return psutil.Process(pid) + """ @staticmethod def kill_by_pid(pid): @@ -108,9 +111,11 @@ class System: def path(pid): return psutil.Process(pid).exe() + """ @staticmethod def working_directory(pid): return psutil.Process(pid).cwd() + """ @staticmethod def command_line(pid): @@ -128,6 +133,8 @@ class System: def create_time(pid): return psutil.Process(pid).create_time() + + """ @staticmethod def cpu_times(pid): return psutil.Process(pid).cpu_times() @@ -151,14 +158,17 @@ class System: @staticmethod def threads(pid): return psutil.Process(pid).threads() + """ @staticmethod def nice(pid): return psutil.Process(pid).nice() + """ @staticmethod def environment(pid): return psutil.Process(pid).environ() + """ @staticmethod def details(): @@ -166,6 +176,15 @@ class System: class Sessions(object): + @staticmethod + def user_name(): + arr = [] + for user in psutil.get_users(): + if str(user[0]) is not 'None' and user[0] not in arr: + arr.append(user[0]) + return arr + + """ @staticmethod def user_name(): arr = [] @@ -178,6 +197,10 @@ class System: def user_details(): return psutil.users() + """ + + + @staticmethod def last_login_username(): # TODO @@ -185,14 +208,17 @@ class System: class Os(object): - @staticmethod - def boot_time(): - return psutil.boot_time() @staticmethod def architecture(): return platform.architecture()[0] + """ + @staticmethod + def boot_time(): + return psutil.boot_time() + """ + @staticmethod def file_format(): return platform.architecture()[1] @@ -277,10 +303,6 @@ class System: def interface_size(): return len(psutil.net_io_counters(pernic=True)) - @staticmethod - def interfaces_details(): - return psutil.net_if_addrs() - @staticmethod def io_counter_detail(): return psutil.net_io_counters(pernic=True) @@ -288,7 +310,7 @@ class System: @staticmethod def interfaces(): arr = [] - for iface in psutil.net_if_addrs(): + for iface in psutil.net_io_counters(pernic=True): arr.append(str(iface)) return arr @@ -296,38 +318,71 @@ class System: def ip_addresses(): arr = [] for iface in psutil.net_io_counters(pernic=True): - ip = psutil.net_if_addrs()[str(iface)][0][1] - if re.match(r'^((\d{1,2}|1\d{2}|2[0-4]\d|25[0-5])\.){3}(\d{1,2}|1\d{2}|2[0-4]\d|25[0-5])$', ip) and str(ip) != 'localhost' and str(ip) != '127.0.0.1': + f = os.popen('ifconfig {} | grep "inet\ addr" | cut -d: -f2 | cut -d" " -f1'.format(iface)) + ip = str(f.read()).replace('\n', '') + if ip != '127.0.0.1': arr.append(ip) return arr + """ + @staticmethod + def getHwAddr(ifname): + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', ifname[:15])) + return ''.join(['%02x:' % ord(char) for char in info[18:24]])[:-1] + + @staticmethod + def getHwAddr(ifname): + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', ifname[:15])) + return ''.join(['%02x:' % ord(char) for char in info[18:24]])[:-1] + + + + @staticmethod + def mac_addresses(): + mac = get_mac() + ':'.join(("%012X" % mac)[i:i+2] for i in range(0, 12, 2) + + arr = [] + for iface in psutil.net_io_counters(pernic=True): + try: + addr_list = psutil.net_if_addrs() + mac = addr_list[str(iface)][2][1] + if re.match("[0-9a-f]{2}([-:])[0-9a-f]{2}(\\1[0-9a-f]{2}){4}$", mac.lower()) and str(mac) != '00:00:00:00:00:00': + arr.append(mac.lower()) + except Exception as e: + pass + + # return arr + """ + @staticmethod def mac_addresses(): - arr = [] + arr=[] for iface in psutil.net_io_counters(pernic=True): - try: - addr_list=psutil.net_if_addrs() - mac = addr_list[str(iface)][2][1] - if re.match("[0-9a-f]{2}([-:])[0-9a-f]{2}(\\1[0-9a-f]{2}){4}$", mac.lower()) and str(mac) != '00:00:00:00:00:00': - arr.append(mac.lower()) - except Exception as e: - pass + mac = open('/sys/class/net/' + iface + '/address').readline() + if str(mac[0:17]) != "00:00:00:00:00:00": + arr.append(mac[0:17]) return arr + """ + @staticmethod + def interfaces_details(): + return psutil.net_if_addrs() + + @staticmethod + def ip_addresses(): + arr = [] + for iface in psutil.net_io_counters(pernic=True): + ip = psutil.net_if_addrs()[str(iface)][0][1] + if re.match(r'^((\d{1,2}|1\d{2}|2[0-4]\d|25[0-5])\.){3}(\d{1,2}|1\d{2}|2[0-4]\d|25[0-5])$', ip) and str(ip) != 'localhost' and str(ip) != '127.0.0.1': + arr.append(ip) + return arr + """ + class Cpu(object): - @staticmethod - def physical_core_count(): - return psutil.cpu_count(logical=False) - - @staticmethod - def logical_core_count(): - return psutil.cpu_count(logical=True) - - @staticmethod - def stats(): - return psutil.cpu_stats() - @staticmethod def times(): return psutil.cpu_times() @@ -336,31 +391,47 @@ class System: def architecture(): return platform.processor() - @staticmethod - def vendor(): - return cpuinfo.get_cpu_info()['vendor_id'] + """ + @staticmethod + def physical_core_count(): + return psutil.cpu_count(logical=False) - @staticmethod - def brand(): - return cpuinfo.get_cpu_info()['brand'] + @staticmethod + def logical_core_count(): + return psutil.cpu_count(logical=True) - @staticmethod - def hz_advertised(): - return cpuinfo.get_cpu_info()['hz_advertised'] + @staticmethod + def stats(): + return psutil.cpu_stats() + """ - @staticmethod - def hz_actual(): - return cpuinfo.get_cpu_info()['hz_actual'] + """ + @staticmethod + def vendor(): + return cpuinfo.get_cpu_info()['vendor_id'] - @staticmethod - def bit(): - return cpuinfo.get_cpu_info()['bits'] + @staticmethod + def brand(): + return cpuinfo.get_cpu_info()['brand'] - @staticmethod - def family(): - return cpuinfo.get_cpu_info()['family'] + @staticmethod + def hz_advertised(): + return cpuinfo.get_cpu_info()['hz_advertised'] - @staticmethod - def model(): - return cpuinfo.get_cpu_info()['model'] + @staticmethod + def hz_actual(): + return cpuinfo.get_cpu_info()['hz_actual'] + @staticmethod + def bit(): + return cpuinfo.get_cpu_info()['bits'] + + @staticmethod + def family(): + return cpuinfo.get_cpu_info()['family'] + + @staticmethod + def model(): + return cpuinfo.get_cpu_info()['model'] + + """ From 3b9c5fa097d37b397c3d922d032a0a51378d7815 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20=C5=9Eahin?= Date: Thu, 9 Jun 2016 12:11:24 +0300 Subject: [PATCH 4/5] some abilities of installation package were added --- opt/ahenk/base/util/util.py | 46 +++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/opt/ahenk/base/util/util.py b/opt/ahenk/base/util/util.py index fec442c..20b1222 100644 --- a/opt/ahenk/base/util/util.py +++ b/opt/ahenk/base/util/util.py @@ -9,6 +9,7 @@ import pwd import shutil import stat import subprocess +import sys class Util: @@ -115,16 +116,19 @@ class Util: raise @staticmethod - def execute(command, stdin=None, env=None, cwd=None, shell=True): + def execute(command, stdin=None, env=None, cwd=None, shell=True, result=True): try: process = subprocess.Popen(command, stdin=stdin, env=env, cwd=cwd, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=shell) - result_code = process.wait() - p_out = process.stdout.read().decode("unicode_escape") - p_err = process.stderr.read().decode("unicode_escape") + if result is True: + result_code = process.wait() + p_out = process.stdout.read().decode("unicode_escape") + p_err = process.stderr.read().decode("unicode_escape") - return result_code, p_out, p_err + return result_code, p_out, p_err + else: + return None, None, None except Exception as e: return 1, 'Could not execute command: {0}. Error Message: {1}'.format(command, str(e)), '' @@ -183,3 +187,35 @@ class Util: return grp.getgrgid(gid)[0] except: raise + + @staticmethod + def install_with_gdebi(full_path): + try: + process = subprocess.Popen('gdebi -n ' + full_path, shell=True) + process.wait() + except: + raise + + @staticmethod + def install_with_apt_get(package_name): + try: + process = subprocess.Popen('apt-get install --yes --force-yes ' + package_name, shell=True) + process.wait() + except: + raise + + @staticmethod + def is_installed(package_name): + + result_code, p_out, p_err = Util.execute('dpkg -s {}'.format(package_name)) + try: + lines=str(p_out).split('\n') + for line in lines: + if len(line)>1: + if line.split(None, 1)[0].lower() =='status:': + if 'installed' in line.split(None, 1)[1].lower(): + return True + return False + except Exception as e: + return False + From 46047ac43b760481e9838b069714026057da49dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20=C5=9Eahin?= Date: Thu, 9 Jun 2016 12:12:30 +0300 Subject: [PATCH 5/5] finding pid and killing process abilities were extended --- opt/ahenk/base/system/system.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/opt/ahenk/base/system/system.py b/opt/ahenk/base/system/system.py index 9edccc9..1f57674 100644 --- a/opt/ahenk/base/system/system.py +++ b/opt/ahenk/base/system/system.py @@ -91,6 +91,8 @@ class System: def pids(): return psutil.pids() + + @staticmethod def find_pid_by_name(p_name): for id in psutil.pids(): @@ -99,10 +101,29 @@ class System: return None """ + @staticmethod + def find_pids_by_name(p_name): + arr = [] + + for pid in psutil.get_pid_list(): + if psutil.Process(pid).name==p_name: + arr.append(pid) + return arr + + + @staticmethod + def is_running(pid): + return psutil.pid_exists(pid) + @staticmethod def kill_by_pid(pid): return psutil.Process(pid).kill() + @staticmethod + def kill_by_pids(pids): + for pid in pids: + psutil.Process(pid).kill() + @staticmethod def find_name_by_pid(pid): return psutil.Process(pid).name() @@ -134,6 +155,7 @@ class System: return psutil.Process(pid).create_time() + """ @staticmethod def cpu_times(pid): @@ -434,4 +456,4 @@ class System: def model(): return cpuinfo.get_cpu_info()['model'] - """ + """ \ No newline at end of file