From 1e3377c048fa09b4fb4c83a70396744bc91eebc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20=C5=9Eahin?= Date: Tue, 21 Jun 2016 15:30:01 +0300 Subject: [PATCH 01/13] minor bug fixing --- debian/control | 2 +- opt/ahenk/ahenkd.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/debian/control b/debian/control index 91e8ecf..e17dd32 100644 --- a/debian/control +++ b/debian/control @@ -8,5 +8,5 @@ Homepage: http://www.liderahenk.org.tr Package: ahenk Architecture: any -Depends:python3 (>= 3), cython, libidn11, libidn11-dev, python3-dev, python3-pip, libffi-dev, libssl-dev, python3-paramiko, python3-cpuinfo, python3-psutil (>= 4), libpam-script, python-sleekxmpp +Depends:python3 (>= 3), cython, libidn11, libidn11-dev, python3-dev, python3-pip, libffi-dev, libssl-dev, python3-paramiko, python3-cpuinfo, python3-psutil (>= 4), libpam-script, python3-sleekxmpp 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. diff --git a/opt/ahenk/ahenkd.py b/opt/ahenk/ahenkd.py index 41767a8..a903c5c 100755 --- a/opt/ahenk/ahenkd.py +++ b/opt/ahenk/ahenkd.py @@ -232,7 +232,6 @@ class AhenkDeamon(BaseDaemon): self.logger.debug('[AhenkDeamon] Signal handled') self.logger.debug('[AhenkDeamon] Signal is :{}'.format(str(json_data['event']))) - print('event:{}'.format(str(json_data['event']))) if 'login' == str(json_data['event']): self.logger.info('[AhenkDeamon] login event is handled for user: {}'.format(json_data['username'])) From 98784905dd7bdcc93fffe473247001db4e7e7955 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20=C5=9Eahin?= Date: Tue, 21 Jun 2016 17:47:25 +0300 Subject: [PATCH 02/13] registration failed handled --- opt/ahenk/ahenkd.py | 9 +++++---- opt/ahenk/base/util/util.py | 8 ++++---- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/opt/ahenk/ahenkd.py b/opt/ahenk/ahenkd.py index a903c5c..42ea1c8 100755 --- a/opt/ahenk/ahenkd.py +++ b/opt/ahenk/ahenkd.py @@ -114,12 +114,12 @@ class AhenkDeamon(BaseDaemon): def check_registration(self): max_attemp_number = int(System.Hardware.Network.interface_size()) * 3 logger = Scope.getInstance().getLogger() + try: while Scope.getInstance().getRegistration().is_registered() is False: max_attemp_number -= 1 logger.debug('[AhenkDeamon] Ahenk is not registered. Attempting for registration') - # TODO 'Could not reach Registration response from Lider. Be sure Lider is awake and it is connected to XMPP server!' - + # TODO 'Could not reach Registration response from Lider. Be sure Lider is running and it is connected to XMPP server!' Scope.getInstance().getRegistration().registration_request() if max_attemp_number < 0: logger.warning('[AhenkDeamon] Number of Attempting for registration is over') @@ -129,8 +129,9 @@ class AhenkDeamon(BaseDaemon): logger.error('[AhenkDeamon] Registration failed. Error message: {}'.format(str(e))) def registration_failed(self): - # TODO registration fail protocol implement - pass + self.logger.error('[AhenkDeamon] Registration failed. All registration attemps were failed. Ahenk is stopping...') + print('Registration failed. Ahenk is stopping..') + ahenkdaemon.stop() def reload_plugins(self): Scope.getInstance().getPluginManager().reloadPlugins() diff --git a/opt/ahenk/base/util/util.py b/opt/ahenk/base/util/util.py index 33eff7b..ca75a72 100644 --- a/opt/ahenk/base/util/util.py +++ b/opt/ahenk/base/util/util.py @@ -209,16 +209,16 @@ class Util: result_code, p_out, p_err = Util.execute('dpkg -s {}'.format(package_name)) try: - lines=str(p_out).split('\n') + lines = str(p_out).split('\n') for line in lines: - if len(line)>1: - if line.split(None, 1)[0].lower() =='status:': + 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 - + @staticmethod def get_md5_file(fname): hash_md5 = hashlib.md5() From 613cb68df3a7e078e7ca7bc03e10ff71efbaa06c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20=C5=9Eahin?= Date: Wed, 22 Jun 2016 18:10:16 +0300 Subject: [PATCH 03/13] minor line fixing --- opt/ahenk/base/system/system.py | 27 ++------------------------- opt/ahenk/base/util/util.py | 2 +- 2 files changed, 3 insertions(+), 26 deletions(-) diff --git a/opt/ahenk/base/system/system.py b/opt/ahenk/base/system/system.py index 043663e..dd20e21 100644 --- a/opt/ahenk/base/system/system.py +++ b/opt/ahenk/base/system/system.py @@ -13,10 +13,6 @@ import fcntl import struct from uuid import getnode as get_mac -""" -some functions closed because of dependency management -""" - class System: class Ahenk(object): @@ -191,21 +187,13 @@ class System: def user_details(): return psutil.users() + + @staticmethod def last_login_username(): # TODO pass - """ - @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 - """ - class Os(object): @staticmethod @@ -349,17 +337,6 @@ class System: return arr - """ - @staticmethod - def mac_addresses(): - arr=[] - for iface in psutil.net_io_counters(pernic=True): - 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() diff --git a/opt/ahenk/base/util/util.py b/opt/ahenk/base/util/util.py index ca75a72..7a7bc68 100644 --- a/opt/ahenk/base/util/util.py +++ b/opt/ahenk/base/util/util.py @@ -225,4 +225,4 @@ class Util: with open(fname, 'rb') as f: for chunk in iter(lambda: f.read(4096), b""): hash_md5.update(chunk) - return str(hash_md5.hexdigest()) + return str(hash_md5.hexdigest()) \ No newline at end of file From 2bcf476aa853223da3e7591d7b8f4c84525ab731 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20=C5=9Eahin?= Date: Wed, 22 Jun 2016 18:12:06 +0300 Subject: [PATCH 04/13] Storing tasks in database --- opt/ahenk/base/task/TaskManager.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/opt/ahenk/base/task/TaskManager.py b/opt/ahenk/base/task/TaskManager.py index 62601c0..2a94381 100644 --- a/opt/ahenk/base/task/TaskManager.py +++ b/opt/ahenk/base/task/TaskManager.py @@ -1,6 +1,7 @@ #!/usr/bin/python3 # -*- coding: utf-8 -*- # Author: İsmail BAŞARAN + from base.Scope import Scope from base.model.MessageFactory import MessageFactory from base.model.enum.MessageType import MessageType @@ -19,33 +20,34 @@ class TaskManager(object): def addTask(self, task): try: + self.saveTask(task) if task.get_cron_str() == None or task.get_cron_str() == '': - self.logger.debug('Adding task ... ') - #self.saveTask(task) - self.logger.info('Task saved ') - # TODO send task received message + self.logger.debug('[TaskManager] Adding task ... ') self.pluginManager.processTask(task) else: self.scheduler.save_and_add_job(task) except Exception as e: - # TODO error log here - self.logger.debug('Exception occured when adding task ' + str(e)) - pass + self.logger.debug('[TaskManager] Exception occured when adding task. Error Message: {}'.format(str(e))) def addPolicy(self, policy): try: self.pluginManager.processPolicy(policy) except Exception as e: - self.logger.error("Exception occured when adding policy. Error Message: {}".format(str(e))) + self.logger.error("[TaskManager] Exception occured when adding policy. Error Message: {}".format(str(e))) pass def saveTask(self, task): - - cols = ['id', 'create_date', 'modify_date', 'command_cls_id', 'parameter_map', 'deleted', 'plugin'] - values = [str(task.get_id()), str(task.get_create_date()), str(task.get_modify_date()), str(task.get_command_cls_id()), str(task.get_parameter_map()), str(task.get_deleted()), task.plugin.to_string()] - self.db_service.update('task', cols, values, None) - self.logger.debug('[TaskManager] Task has been saved to database (Task id:' + task.id + ')') + try: + task_cols = ['id', 'create_date', 'modify_date', 'command_cls_id', 'parameter_map', 'deleted', 'plugin','cron_expr'] + plu_cols = ['active', 'create_date', 'deleted', 'description', 'machine_oriented', 'modify_date', 'name', 'policy_plugin', 'user_oriented', 'version','task_plugin','x_based'] + plugin_args = [str(task.get_plugin().get_active()), str(task.get_plugin().get_create_date()), str(task.get_plugin().get_deleted()), str(task.get_plugin().get_description()), str(task.get_plugin().get_machine_oriented()), str(task.get_plugin().get_modify_date()), str(task.get_plugin().get_name()), str(task.get_plugin().get_policy_plugin()), str(task.get_plugin().get_user_oriented()), str(task.get_plugin().get_version()), str(task.get_plugin().get_task_plugin()), str(task.get_plugin().get_x_based())] + plugin_id = self.db_service.update('plugin', plu_cols, plugin_args) + values = [str(task.get_id()), str(task.get_create_date()), str(task.get_modify_date()), str(task.get_command_cls_id()), str(task.get_parameter_map()), str(task.get_deleted()), str(plugin_id),str(task.get_cron_str())] + self.db_service.update('task', task_cols, values, None) + # self.logger.debug('[TaskManager] Task has been saved to database (Task id:' + task.get_id() + ')') + except Exception as e: + print(str(e)) def updateTask(self, task): # TODO not implemented yet From bef981e1963c7aa4b562e1cbb394302700ab0fc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20=C5=9Eahin?= Date: Wed, 22 Jun 2016 18:14:10 +0300 Subject: [PATCH 05/13] log fixing --- opt/ahenk/base/messaging/Messenger.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/opt/ahenk/base/messaging/Messenger.py b/opt/ahenk/base/messaging/Messenger.py index c76d4d1..ef48832 100644 --- a/opt/ahenk/base/messaging/Messenger.py +++ b/opt/ahenk/base/messaging/Messenger.py @@ -65,7 +65,7 @@ class Messenger(ClientXMPP): return False def session_end(self): - print("disconnect") + self.logger.warning('[Messenger] DISCONNECTED') def session_start(self, event): self.logger.debug('[Messenger] Session was started') @@ -77,7 +77,7 @@ class Messenger(ClientXMPP): self.logger.debug('[Messenger] <<--------Sending message: {}'.format(msg)) self.send_message(mto=self.receiver, mbody=msg, mtype='normal') except Exception as e: - self.logger.debug('[Messenger] A problem occurred while sending direct message. Error Message: {}'.format(str(e))) + self.logger.error('[Messenger] A problem occurred while sending direct message. Error Message: {}'.format(str(e))) def recv_direct_message(self, msg): if msg['type'] in ('normal'): @@ -87,6 +87,5 @@ class Messenger(ClientXMPP): message_type = j['type'] self.event_manger.fireEvent(message_type, str(msg['body'])) self.logger.debug('[Messenger] Fired event is: {}'.format(message_type)) - except Exception as e: - self.logger.debug('[Messenger] A problem occurred while keeping message. Error Message: {}'.format(str(e))) + self.logger.error('[Messenger] A problem occurred while keeping message. Error Message: {}'.format(str(e))) From 93a3acf7f24eeec110f186e0c7104493c35198f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20=C5=9Eahin?= Date: Wed, 22 Jun 2016 18:15:19 +0300 Subject: [PATCH 06/13] missing parameters were added --- opt/ahenk/base/model/PluginBean.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/opt/ahenk/base/model/PluginBean.py b/opt/ahenk/base/model/PluginBean.py index 510bfd9..265b521 100644 --- a/opt/ahenk/base/model/PluginBean.py +++ b/opt/ahenk/base/model/PluginBean.py @@ -1,13 +1,12 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- # Author: Volkan Şahin -import json class PluginBean(object): """docstring for PluginBean""" - def __init__(self, p_id=None, active=None, create_date=None, deleted=None, description=None, machine_oriented=None, modify_date=None, name=None, policy_plugin=None, user_oriented=None, version=None): + def __init__(self, p_id=None, active=None, create_date=None, deleted=None, description=None, machine_oriented=None, modify_date=None, name=None, policy_plugin=None, task_plugin=None, user_oriented=None, version=None, x_based=None): self.id = p_id self.active = active self.create_date = create_date @@ -19,6 +18,8 @@ class PluginBean(object): self.policy_plugin = policy_plugin self.user_oriented = user_oriented self.version = version + self.task_plugin = task_plugin + self.x_based = x_based def get_user_oriented(self): return self.user_oriented @@ -85,3 +86,15 @@ class PluginBean(object): def set_version(self, version): self.version = version + + def get_x_based(self): + return self.x_based + + def set_x_based(self, x_based): + self.x_based = x_based + + def get_task_plugin(self): + return self.task_plugin + + def set_task_plugin(self, task_plugin): + self.task_plugin = task_plugin From dc6a74b528b10d0d6c41b170bca4aa6a17909347 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20=C5=9Eahin?= Date: Wed, 22 Jun 2016 18:17:16 +0300 Subject: [PATCH 07/13] missing columns added --- opt/ahenk/base/database/AhenkDbService.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/opt/ahenk/base/database/AhenkDbService.py b/opt/ahenk/base/database/AhenkDbService.py index fb39326..95a765a 100644 --- a/opt/ahenk/base/database/AhenkDbService.py +++ b/opt/ahenk/base/database/AhenkDbService.py @@ -22,10 +22,10 @@ class AhenkDbService(object): def initialize_table(self): - self.check_and_create_table('task', ['id INTEGER', 'create_date TEXT', 'modify_date TEXT', 'command_cls_id TEXT', 'parameter_map BLOB', 'deleted INTEGER', 'plugin TEXT']) + self.check_and_create_table('task', ['id INTEGER', 'create_date TEXT', 'modify_date TEXT', 'command_cls_id TEXT', 'parameter_map BLOB', 'deleted INTEGER', 'plugin TEXT','cron_expr TEXT']) self.check_and_create_table('policy', ['id INTEGER PRIMARY KEY AUTOINCREMENT', 'type TEXT', 'version TEXT', 'name TEXT', 'execution_id TEXT']) self.check_and_create_table('profile', ['id INTEGER', 'create_date TEXT', 'label TEXT', 'description TEXT', 'overridable INTEGER', 'active TEXT', 'deleted TEXT', 'profile_data TEXT', 'modify_date TEXT', 'plugin TEXT']) - self.check_and_create_table('plugin', ['id INTEGER PRIMARY KEY AUTOINCREMENT', 'active TEXT', 'create_date TEXT', 'deleted TEXT', 'description TEXT', 'machine_oriented TEXT', 'modify_date TEXT', 'name TEXT', 'policy_plugin TEXT', 'user_oriented TEXT', 'version TEXT']) + self.check_and_create_table('plugin', ['id INTEGER PRIMARY KEY AUTOINCREMENT', 'active TEXT', 'create_date TEXT', 'deleted TEXT', 'description TEXT', 'machine_oriented TEXT', 'modify_date TEXT', 'name TEXT', 'policy_plugin TEXT', 'user_oriented TEXT', 'version TEXT', 'task_plugin TEXT', 'x_based TEXT']) self.check_and_create_table('registration', ['jid TEXT', 'password TEXT', 'registered INTEGER', 'dn TEXT', 'params TEXT', 'timestamp TEXT']) def connect(self): From da896074eb66de22a3a39a4b5ed5d651ac1a743e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20=C5=9Eahin?= Date: Wed, 22 Jun 2016 18:18:11 +0300 Subject: [PATCH 08/13] converting obj to json func added --- opt/ahenk/base/model/TaskBean.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/opt/ahenk/base/model/TaskBean.py b/opt/ahenk/base/model/TaskBean.py index d0912eb..24fca81 100644 --- a/opt/ahenk/base/model/TaskBean.py +++ b/opt/ahenk/base/model/TaskBean.py @@ -65,6 +65,33 @@ class TaskBean(object): def set_cron_str(self, cron_str): self.cron_str = cron_str + def to_json(self): + plugin_data = {} + plugin_data['id'] = self.plugin.get_id() + plugin_data['name'] = self.plugin.get_name() + plugin_data['version'] = self.plugin.get_version() + plugin_data['description'] = self.plugin.get_description() + plugin_data['active'] = self.plugin.get_active() + plugin_data['deleted'] = self.plugin.get_deleted() + plugin_data['machineOriented'] = self.plugin.get_machine_oriented() + plugin_data['userOriented'] = self.plugin.get_user_oriented() + plugin_data['policyPlugin'] = self.plugin.get_policy_plugin() + plugin_data['taskPlugin'] = self.plugin.get_task_plugin() + plugin_data['xBased'] = self.plugin.get_x_based() + plugin_data['createDate'] = self.plugin.get_create_date() + plugin_data['modifyDate'] = self.plugin.get_modify_date() + + task_data = {} + task_data['id'] = self._id + task_data['plugin'] = plugin_data + task_data['commandClsId'] = self.command_cls_id + task_data['parameterMap'] = self.parameter_map + task_data['deleted'] = self.deleted + task_data['cronExpression'] = self.cron_str + task_data['createDate'] = self.create_date + task_data['modifyDate'] = self.modify_date + return task_data + @property def obj_name(self): return "TASK" From b3c6800f1d6a0549e348e77a8012619ff1bdc0c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20=C5=9Eahin?= Date: Wed, 22 Jun 2016 18:19:24 +0300 Subject: [PATCH 09/13] updated execution task-policy because of added new columns-params --- opt/ahenk/base/execution/ExecutionManager.py | 29 +++++++++----------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/opt/ahenk/base/execution/ExecutionManager.py b/opt/ahenk/base/execution/ExecutionManager.py index a33a64f..6f54da4 100644 --- a/opt/ahenk/base/execution/ExecutionManager.py +++ b/opt/ahenk/base/execution/ExecutionManager.py @@ -8,9 +8,8 @@ import os import shutil import stat import subprocess -import uuid import urllib.request -import errno +import uuid from base.Scope import Scope from base.messaging.ssh_file_transfer import FileTransfer @@ -108,7 +107,7 @@ class ExecutionManager(object): user_policy_version = self.db_service.select_one_result('policy', 'version', 'type = \'U\' and name = \'' + policy.get_username() + '\'') profile_columns = ['id', 'create_date', 'modify_date', 'label', 'description', 'overridable', 'active', 'deleted', 'profile_data', 'plugin'] - plugin_columns = ['active', 'create_date', 'deleted', 'description', 'machine_oriented', 'modify_date', 'name', 'policy_plugin', 'user_oriented', 'version'] + plugin_columns = ['active', 'create_date', 'deleted', 'description', 'machine_oriented', 'modify_date', 'name', 'policy_plugin', 'user_oriented', 'version', 'task_plugin', 'x_based'] if policy.get_ahenk_policy_version() != ahenk_policy_ver: ahenk_policy_id = self.db_service.select_one_result('policy', 'id', 'type = \'A\'') @@ -123,7 +122,7 @@ class ExecutionManager(object): for profile in policy.get_ahenk_profiles(): plugin = profile.get_plugin() - plugin_args = [str(plugin.get_active()), str(plugin.get_create_date()), str(plugin.get_deleted()), str(plugin.get_description()), str(plugin.get_machine_oriented()), str(plugin.get_modify_date()), str(plugin.get_name()), str(plugin.get_policy_plugin()), str(plugin.get_user_oriented()), str(plugin.get_version())] + plugin_args = [str(plugin.get_active()), str(plugin.get_create_date()), str(plugin.get_deleted()), str(plugin.get_description()), str(plugin.get_machine_oriented()), str(plugin.get_modify_date()), str(plugin.get_name()), str(plugin.get_policy_plugin()), str(plugin.get_user_oriented()), str(plugin.get_version()), str(plugin.get_task_plugin()), str(plugin.get_x_based())] plugin_id = self.db_service.update('plugin', plugin_columns, plugin_args) profile_args = [str(ahenk_policy_id), str(profile.get_create_date()), str(profile.get_modify_date()), str(profile.get_label()), str(profile.get_description()), str(profile.get_overridable()), str(profile.get_active()), str(profile.get_deleted()), str(profile.get_profile_data()), plugin_id] @@ -131,7 +130,7 @@ class ExecutionManager(object): else: self.logger.debug('[ExecutionManager] Already there is ahenk policy. Command Execution Id is updating') - self.db_service.update('policy', ['execution_id'], [policy.get_agent_execution_id()],'type = \'A\'') + self.db_service.update('policy', ['execution_id'], [policy.get_agent_execution_id()], 'type = \'A\'') if policy.get_user_policy_version() != user_policy_version: user_policy_id = self.db_service.select_one_result('policy', 'id', 'type = \'U\' and name=\'' + policy.get_username() + '\'') @@ -147,7 +146,7 @@ class ExecutionManager(object): for profile in policy.get_user_profiles(): plugin = profile.get_plugin() - plugin_args = [str(plugin.get_active()), str(plugin.get_create_date()), str(plugin.get_deleted()), str(plugin.get_description()), str(plugin.get_machine_oriented()), str(plugin.get_modify_date()), str(plugin.get_name()), str(plugin.get_policy_plugin()), str(plugin.get_user_oriented()), str(plugin.get_version())] + plugin_args = [str(plugin.get_active()), str(plugin.get_create_date()), str(plugin.get_deleted()), str(plugin.get_description()), str(plugin.get_machine_oriented()), str(plugin.get_modify_date()), str(plugin.get_name()), str(plugin.get_policy_plugin()), str(plugin.get_user_oriented()), str(plugin.get_version()), str(plugin.get_task_plugin()), str(plugin.get_x_based())] plugin_id = self.db_service.update('plugin', plugin_columns, plugin_args) profile_args = [str(user_policy_id), str(profile.get_create_date()), str(profile.get_modify_date()), str(profile.get_label()), str(profile.get_description()), str(profile.get_overridable()), str(profile.get_active()), str(profile.get_deleted()), str(profile.get_profile_data()), plugin_id] @@ -155,7 +154,7 @@ class ExecutionManager(object): else: self.logger.debug('[ExecutionManager] Already there is user policy. . Command Execution Id is updating') - self.db_service.update('policy', ['execution_id'], [policy.get_user_execution_id()],'type = \'U\'') + self.db_service.update('policy', ['execution_id'], [policy.get_user_execution_id()], 'type = \'U\'') policy = self.get_active_policies(policy.get_username()) self.task_manager.addPolicy(policy) @@ -165,7 +164,7 @@ class ExecutionManager(object): user_policy = self.db_service.select('policy', ['id', 'version', 'name'], ' type=\'U\' and name=\'' + username + '\'') ahenk_policy = self.db_service.select('policy', ['id', 'version'], ' type=\'A\' ') - plugin_columns = ['id', 'active', 'create_date', 'deleted', 'description', 'machine_oriented', 'modify_date', 'name', 'policy_plugin', 'user_oriented', 'version'] + plugin_columns = ['id', 'active', 'create_date', 'deleted', 'description', 'machine_oriented', 'modify_date', 'name', 'policy_plugin', 'user_oriented', 'version', 'task_plugin', 'x_based'] profile_columns = ['id', 'create_date', 'label', 'description', 'overridable', 'active', 'deleted', 'profile_data', 'modify_date', 'plugin'] policy = PolicyBean(username=username) @@ -198,7 +197,7 @@ class ExecutionManager(object): return policy - #from db + # from db def get_installed_plugins(self): plugins = self.db_service.select('plugin', ['name', 'version']) p_list = [] @@ -210,18 +209,16 @@ class ExecutionManager(object): str_task = json.loads(arg)['task'] json_task = json.loads(str_task) - task = self.json_to_TaskBean(json_task) + task = self.json_to_task_bean(json_task) self.logger.debug('[ExecutionManager] Adding new task...Task is:{}'.format(task.get_command_cls_id())) self.task_manager.addTask(task) self.logger.debug('[ExecutionManager] Task added') - def json_to_TaskBean(self, json_data): - + def json_to_task_bean(self, json_data): plu = json_data['plugin'] - plugin = PluginBean(p_id=plu['id'], active=plu['active'], create_date=plu['createDate'], deleted=plu['deleted'], description=plu['description'], machine_oriented=plu['machineOriented'], modify_date=plu['modifyDate'], name=plu['name'], policy_plugin=plu['policyPlugin'], user_oriented=plu['userOriented'], version=plu['version']) - + plugin = PluginBean(p_id=plu['id'], active=plu['active'], create_date=plu['createDate'], deleted=plu['deleted'], description=plu['description'], machine_oriented=plu['machineOriented'], modify_date=plu['modifyDate'], name=plu['name'], policy_plugin=plu['policyPlugin'], user_oriented=plu['userOriented'], version=plu['version'], task_plugin=plu['taskPlugin'], x_based=plu['xBased']) return TaskBean(_id=json_data['id'], create_date=json_data['createDate'], modify_date=json_data['modifyDate'], command_cls_id=json_data['commandClsId'], parameter_map=json_data['parameterMap'], deleted=json_data['deleted'], plugin=plugin, cron_str=json_data['cronExpression']) def move_file(self, arg): @@ -296,13 +293,13 @@ class ExecutionManager(object): if ahenk_prof_json_arr is not None: for prof in ahenk_prof_json_arr: plu = json.loads(json.dumps(prof['plugin'])) - plugin = PluginBean(p_id=plu['id'], active=plu['active'], create_date=plu['createDate'], deleted=plu['deleted'], description=plu['description'], machine_oriented=plu['machineOriented'], modify_date=plu['modifyDate'], name=plu['name'], policy_plugin=plu['policyPlugin'], user_oriented=plu['userOriented'], version=plu['version']) + plugin = PluginBean(p_id=plu['id'], active=plu['active'], create_date=plu['createDate'], deleted=plu['deleted'], description=plu['description'], machine_oriented=plu['machineOriented'], modify_date=plu['modifyDate'], name=plu['name'], policy_plugin=plu['policyPlugin'], user_oriented=plu['userOriented'], version=plu['version'], task_plugin=plu['taskPlugin'], x_based=plu['xBased']) ahenk_prof_arr.append(ProfileBean(prof['id'], prof['createDate'], prof['label'], prof['description'], prof['overridable'], prof['active'], prof['deleted'], json.dumps(prof['profileData']), prof['modifyDate'], plugin, username)) if user_prof_json_arr is not None: for prof in user_prof_json_arr: plu = json.loads(json.dumps(prof['plugin'])) - plugin = PluginBean(p_id=plu['id'], active=plu['active'], create_date=plu['createDate'], deleted=plu['deleted'], description=plu['description'], machine_oriented=plu['machineOriented'], modify_date=plu['modifyDate'], name=plu['name'], policy_plugin=plu['policyPlugin'], user_oriented=plu['userOriented'], version=plu['version']) + plugin = PluginBean(p_id=plu['id'], active=plu['active'], create_date=plu['createDate'], deleted=plu['deleted'], description=plu['description'], machine_oriented=plu['machineOriented'], modify_date=plu['modifyDate'], name=plu['name'], policy_plugin=plu['policyPlugin'], user_oriented=plu['userOriented'], version=plu['version'], task_plugin=plu['taskPlugin'], x_based=plu['xBased']) user_prof_arr.append(ProfileBean(prof['id'], prof['createDate'], prof['label'], prof['description'], prof['overridable'], prof['active'], prof['deleted'], json.dumps(prof['profileData']), prof['modifyDate'], plugin, username)) return PolicyBean(ahenk_policy_version=json_data['agentPolicyVersion'], user_policy_version=json_data['userPolicyVersion'], ahenk_profiles=ahenk_prof_arr, user_profiles=user_prof_arr, timestamp=json_data['timestamp'], username=json_data['username'], agent_execution_id=json_data['agentCommandExecutionId'], user_execution_id=json_data['userCommandExecutionId']) From 795caa22f1035f9e39b8424c266852592ea3f365 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20=C5=9Eahin?= Date: Wed, 22 Jun 2016 18:19:58 +0300 Subject: [PATCH 10/13] log fixing --- opt/ahenk/base/plugin/plugin_manager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opt/ahenk/base/plugin/plugin_manager.py b/opt/ahenk/base/plugin/plugin_manager.py index 9e2de11..9bffe83 100644 --- a/opt/ahenk/base/plugin/plugin_manager.py +++ b/opt/ahenk/base/plugin/plugin_manager.py @@ -63,7 +63,7 @@ class PluginManager(object): plugin.start() self.plugins.append(plugin) - self.logger.debug('[PluginManager] New plugin was loaded.') + self.logger.debug('[PluginManager] New plugin was loaded. Plugin Name: {}'.format(plugin_name)) if len(self.delayed_profiles) > 0: self.pluginQueueDict[plugin_name].put(self.delayed_profiles[plugin_name], 1) From 1e2dbede2bbfd841b6354b66d3d492b990ad6409 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20=C5=9Eahin?= Date: Wed, 22 Jun 2016 18:20:25 +0300 Subject: [PATCH 11/13] scheduler init added --- opt/ahenk/ahenkd.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/opt/ahenk/ahenkd.py b/opt/ahenk/ahenkd.py index 42ea1c8..21a60ef 100755 --- a/opt/ahenk/ahenkd.py +++ b/opt/ahenk/ahenkd.py @@ -179,6 +179,9 @@ class AhenkDeamon(BaseDaemon): self.init_plugin_manager() self.logger.info('[AhenkDeamon] Plugin Manager was set') + self.init_scheduler() + self.logger.info('[AhenkDeamon] Scheduler was set') + self.init_task_manager() self.logger.info('[AhenkDeamon] Task Manager was set') From 54c73ac04ba5ae7f2af2d3e017a03a03c0cf3c95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20=C5=9Eahin?= Date: Wed, 22 Jun 2016 18:22:07 +0300 Subject: [PATCH 12/13] scheduler fixing and adapting --- .../base/scheduler/custom/custom_scheduler.py | 24 ++++--- .../base/scheduler/custom/schedule_job.py | 69 +++++++++++-------- opt/ahenk/base/scheduler/custom/scheduledb.py | 14 ++-- opt/ahenk/base/scheduler/scheduler_factory.py | 3 +- 4 files changed, 66 insertions(+), 44 deletions(-) diff --git a/opt/ahenk/base/scheduler/custom/custom_scheduler.py b/opt/ahenk/base/scheduler/custom/custom_scheduler.py index f911d13..14c53c3 100644 --- a/opt/ahenk/base/scheduler/custom/custom_scheduler.py +++ b/opt/ahenk/base/scheduler/custom/custom_scheduler.py @@ -21,25 +21,33 @@ class CustomScheduler(BaseScheduler): def initialize(self): self.scheduledb.initialize() tasks = self.scheduledb.load() - for task in tasks: - self.add_job(ScheduleTaskJob(task)) + if tasks: + for task in tasks: + self.add_job(ScheduleTaskJob(task)) def add_job(self, job): self.events.append(job) def save_and_add_job(self, task): - self.scheduledb.save(task) - self.events.append(ScheduleTaskJob(task)) + try: + self.logger.debug('[CustomScheduler] Saving scheduled task to database...') + self.scheduledb.save(task) + self.logger.debug('[CustomScheduler] Adding scheduled task to events...') + self.events.append(ScheduleTaskJob(task)) + except Exception as e: + self.logger.error('[CustomScheduler] A problem occurred while saving and adding job. Error Message: {}'.format(str(e))) - def remove_job(self, task): + def remove_job(self, task_id): for event in self.events: - if event.task.id == task.id: - self.scheduledb.delete(task) + if event.task.get_id() == task_id: + self.scheduledb.delete(task_id) + self.logger.debug('[CustomScheduler] Task was deleted from scheduled tasks table') self.events.remove(event) + self.logger.debug('[CustomScheduler] Task was removed from events') def remove_job_via_task_id(self,task_id): for event in self.events: - if event.task.id == task_id: + if event.task.get_id() == task_id: self.scheduledb.delete(event.task) self.events.remove(event) diff --git a/opt/ahenk/base/scheduler/custom/schedule_job.py b/opt/ahenk/base/scheduler/custom/schedule_job.py index 92a2881..207c307 100644 --- a/opt/ahenk/base/scheduler/custom/schedule_job.py +++ b/opt/ahenk/base/scheduler/custom/schedule_job.py @@ -5,30 +5,40 @@ from base.scheduler.custom.all_match import AllMatch from base.Scope import Scope + +# TODO Need logs class ScheduleTaskJob(object): def __init__(self, task): - self.task = task - cron_sj = self.parse_cron_str(task.cron_str) - if cron_sj: - self.mins = self.conv_to_set(cron_sj[0]) - self.hours= self.conv_to_set(cron_sj[1]) - self.days = self.conv_to_set(cron_sj[2]) - self.months = self.conv_to_set(cron_sj[3]) - self.dow = self.conv_to_set(cron_sj[4]) - self.action = self.processTask scope = Scope.getInstance() self.logger = scope.getLogger() self.task_manager = scope.getTaskManager() - - def processTask(self): + self.plugin_manager = scope.getPluginManager() + self.task = task + cron_sj = self.parse_cron_str(task.get_cron_str()) try: - self.task_manager.addTask(self.task) - if self.is_single_shot(): - Scope.getInstance().get_scheduler().remove_job(self.task) + if cron_sj: + self.mins = self.conv_to_set(cron_sj[0]) + self.hours = self.conv_to_set(cron_sj[1]) + self.days = self.conv_to_set(cron_sj[2]) + self.months = self.conv_to_set(cron_sj[3]) + self.dow = self.conv_to_set(cron_sj[4]) + self.action = self.process_task + self.logger.debug('[ScheduleTaskJob] Instance created.') except Exception as e: - self.logger.error(e) + self.logger.error('[ScheduleTaskJob] A problem occurred while creating instance of ScheduleTaskJob. Error Message : {}'.format(str(e))) + + def process_task(self): + try: + self.logger.debug('[ScheduleTaskJob] Running scheduled task now...') + self.plugin_manager.processTask(self.task) + self.logger.debug('[ScheduleTaskJob] Scheduled Task was executed.') + if self.is_single_shot(): + Scope.getInstance().get_scheduler().remove_job(self.task.get_id()) + except Exception as e: + self.logger.error('[ScheduleTaskJob] A problem occurred while running scheduled task. Error Message: {}'.format(str(e))) def parse_cron_str(self, cron_str): + self.logger.debug('[ScheduleTaskJob] Parsing cron string...') try: cron_exp_arr = cron_str.split(" ") cron_sj = [] @@ -51,21 +61,24 @@ class ScheduleTaskJob(object): elif count == 3: cron_sj.append(range(0, 7, range_val)) else: - print("it is not supported") + self.logger.warning('[ScheduleTaskJob] Unsupported expression.') elif ',' in exp: cron_sj.append("(" + str(exp) + ")") else: - print("it is not supported") + self.logger.warning('[ScheduleTaskJob] Unsupported expression.') count = count + 1 return cron_sj except Exception as e: - self.logger.error(str(e)) + self.logger.error('[ScheduleTaskJob] A problem occurred while parsing cron expression. Error Message: {}'.format(str(e))) - def conv_to_set(obj): - if isinstance(obj, (int)): - return set([obj]) + def conv_to_set(self, obj): + self.logger.debug('[ScheduleTaskJob] Converting {} to set'.format(str(obj))) + + if str(obj).isdigit(): + return set([int(obj)]) if not isinstance(obj, set): obj = set(obj) + return obj def is_single_shot(self): @@ -76,12 +89,12 @@ class ScheduleTaskJob(object): def matchtime(self, t): """Return True if this event should trigger at the specified datetime""" - return ((t.minute in self.mins) and - (t.hour in self.hours) and - (t.day in self.days) and - (t.month in self.months) and - (t.weekday() in self.dow)) + return ((t.minute in self.mins) and + (t.hour in self.hours) and + (t.day in self.days) and + (t.month in self.months) and + (t.weekday() in self.dow)) def check(self, t): - if self.matchtime(t): - self.action(*self.args, **self.kwargs) \ No newline at end of file + if self.matchtime(t) is True: + self.action() diff --git a/opt/ahenk/base/scheduler/custom/scheduledb.py b/opt/ahenk/base/scheduler/custom/scheduledb.py index fa1bc65..a980eed 100644 --- a/opt/ahenk/base/scheduler/custom/scheduledb.py +++ b/opt/ahenk/base/scheduler/custom/scheduledb.py @@ -14,22 +14,22 @@ class ScheduleTaskDB(object): def initialize(self): self.logger.debug('[ScheduleTaskDB] Initializing scheduler database...') - self.db_service.check_and_create_table('schedule_task', ['id INTEGER', 'task_json TEXT']) + self.db_service.check_and_create_table('schedule_task', ['id INTEGER PRIMARY KEY AUTOINCREMENT', 'task_id TEXT']) self.logger.debug('[ScheduleTaskDB] Scheduler database is ok.') def save(self, task): self.logger.debug('[ScheduleTaskDB] Preparing schedule task for save operation... creating columns and values...') - cols = ['id', 'task_json'] - values = [str(task.id), str(task.to_json())] + cols = ['task_id'] + values = [task.get_id()] self.logger.debug('[ScheduleTaskDB] Saving scheduler task to db... ') self.db_service.update('schedule_task', cols, values, None) self.logger.debug('[ScheduleTaskDB] Scheduler task saved.') - def delete(self, task): + def delete(self, task_id): try: - self.logger.debug('[ScheduleTaskDB] Deleting schedule task. Task id=' + str(task.id)) - self.db_service.delete('schedule_task', 'id=' + str(task.id)) - self.logger.debug('[ScheduleTaskDB] Deleting schedule task deleted successfully. task id=' + str(task.id)) + self.logger.debug('[ScheduleTaskDB] Deleting schedule task. Task id=' + str(task_id)) + self.db_service.delete('schedule_task', 'task_id=' + str(task_id)) + self.logger.debug('[ScheduleTaskDB] Deleting schedule task deleted successfully. task id=' + str(task_id)) except Exception as e: self.logger.error('[ScheduleTaskDB] Exception occur when deleting schedule task ' + str(e)) diff --git a/opt/ahenk/base/scheduler/scheduler_factory.py b/opt/ahenk/base/scheduler/scheduler_factory.py index 047400a..3b55c9b 100644 --- a/opt/ahenk/base/scheduler/scheduler_factory.py +++ b/opt/ahenk/base/scheduler/scheduler_factory.py @@ -6,6 +6,7 @@ from base.scheduler.custom.custom_scheduler import CustomScheduler class SchedulerFactory(): - def get_intstance(self): + def get_intstance(): return CustomScheduler() + get_intstance = staticmethod(get_intstance) \ No newline at end of file From 6009755066f9786eea9391e3977d9e5433a55b42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20=C5=9Eahin?= Date: Thu, 23 Jun 2016 10:34:27 +0300 Subject: [PATCH 13/13] log fixing --- opt/ahenk/base/task/TaskManager.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/opt/ahenk/base/task/TaskManager.py b/opt/ahenk/base/task/TaskManager.py index 2a94381..5d9703b 100644 --- a/opt/ahenk/base/task/TaskManager.py +++ b/opt/ahenk/base/task/TaskManager.py @@ -28,14 +28,13 @@ class TaskManager(object): self.scheduler.save_and_add_job(task) except Exception as e: - self.logger.debug('[TaskManager] Exception occured when adding task. Error Message: {}'.format(str(e))) + self.logger.debug('[TaskManager] Exception occurred when adding task. Error Message: {}'.format(str(e))) def addPolicy(self, policy): try: self.pluginManager.processPolicy(policy) except Exception as e: - self.logger.error("[TaskManager] Exception occured when adding policy. Error Message: {}".format(str(e))) - pass + self.logger.error("[TaskManager] Exception occurred when adding policy. Error Message: {}".format(str(e))) def saveTask(self, task): try: @@ -45,9 +44,8 @@ class TaskManager(object): plugin_id = self.db_service.update('plugin', plu_cols, plugin_args) values = [str(task.get_id()), str(task.get_create_date()), str(task.get_modify_date()), str(task.get_command_cls_id()), str(task.get_parameter_map()), str(task.get_deleted()), str(plugin_id),str(task.get_cron_str())] self.db_service.update('task', task_cols, values, None) - # self.logger.debug('[TaskManager] Task has been saved to database (Task id:' + task.get_id() + ')') except Exception as e: - print(str(e)) + self.logger.error("[TaskManager] Exception occurred while saving task. Error Message: {}".format(str(e))) def updateTask(self, task): # TODO not implemented yet