This commit is contained in:
emre.akkaya 2016-06-27 12:04:08 +03:00
commit bf5ddb7aff
15 changed files with 157 additions and 119 deletions

2
debian/control vendored
View file

@ -8,5 +8,5 @@ Homepage: http://www.liderahenk.org.tr
Package: ahenk Package: ahenk
Architecture: any 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. 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.

View file

@ -114,12 +114,12 @@ class AhenkDeamon(BaseDaemon):
def check_registration(self): def check_registration(self):
max_attemp_number = int(System.Hardware.Network.interface_size()) * 3 max_attemp_number = int(System.Hardware.Network.interface_size()) * 3
logger = Scope.getInstance().getLogger() logger = Scope.getInstance().getLogger()
try: try:
while Scope.getInstance().getRegistration().is_registered() is False: while Scope.getInstance().getRegistration().is_registered() is False:
max_attemp_number -= 1 max_attemp_number -= 1
logger.debug('[AhenkDeamon] Ahenk is not registered. Attempting for registration') 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() Scope.getInstance().getRegistration().registration_request()
if max_attemp_number < 0: if max_attemp_number < 0:
logger.warning('[AhenkDeamon] Number of Attempting for registration is over') 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))) logger.error('[AhenkDeamon] Registration failed. Error message: {}'.format(str(e)))
def registration_failed(self): def registration_failed(self):
# TODO registration fail protocol implement self.logger.error('[AhenkDeamon] Registration failed. All registration attemps were failed. Ahenk is stopping...')
pass print('Registration failed. Ahenk is stopping..')
ahenkdaemon.stop()
def reload_plugins(self): def reload_plugins(self):
Scope.getInstance().getPluginManager().reloadPlugins() Scope.getInstance().getPluginManager().reloadPlugins()
@ -178,6 +179,9 @@ class AhenkDeamon(BaseDaemon):
self.init_plugin_manager() self.init_plugin_manager()
self.logger.info('[AhenkDeamon] Plugin Manager was set') self.logger.info('[AhenkDeamon] Plugin Manager was set')
self.init_scheduler()
self.logger.info('[AhenkDeamon] Scheduler was set')
self.init_task_manager() self.init_task_manager()
self.logger.info('[AhenkDeamon] Task Manager was set') self.logger.info('[AhenkDeamon] Task Manager was set')
@ -232,7 +236,6 @@ class AhenkDeamon(BaseDaemon):
self.logger.debug('[AhenkDeamon] Signal handled') self.logger.debug('[AhenkDeamon] Signal handled')
self.logger.debug('[AhenkDeamon] Signal is :{}'.format(str(json_data['event']))) self.logger.debug('[AhenkDeamon] Signal is :{}'.format(str(json_data['event'])))
print('event:{}'.format(str(json_data['event'])))
if 'login' == str(json_data['event']): if 'login' == str(json_data['event']):
self.logger.info('[AhenkDeamon] login event is handled for user: {}'.format(json_data['username'])) self.logger.info('[AhenkDeamon] login event is handled for user: {}'.format(json_data['username']))

View file

@ -22,10 +22,10 @@ class AhenkDbService(object):
def initialize_table(self): 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('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('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']) self.check_and_create_table('registration', ['jid TEXT', 'password TEXT', 'registered INTEGER', 'dn TEXT', 'params TEXT', 'timestamp TEXT'])
def connect(self): def connect(self):

View file

@ -8,9 +8,8 @@ import os
import shutil import shutil
import stat import stat
import subprocess import subprocess
import uuid
import urllib.request import urllib.request
import errno import uuid
from base.Scope import Scope from base.Scope import Scope
from base.messaging.ssh_file_transfer import FileTransfer 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() + '\'') 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'] 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: if policy.get_ahenk_policy_version() != ahenk_policy_ver:
ahenk_policy_id = self.db_service.select_one_result('policy', 'id', 'type = \'A\'') 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(): for profile in policy.get_ahenk_profiles():
plugin = profile.get_plugin() 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) 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] 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: else:
self.logger.debug('[ExecutionManager] Already there is ahenk policy. Command Execution Id is updating') 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: 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() + '\'') 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(): for profile in policy.get_user_profiles():
plugin = profile.get_plugin() 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) 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] 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: else:
self.logger.debug('[ExecutionManager] Already there is user policy. . Command Execution Id is updating') 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()) policy = self.get_active_policies(policy.get_username())
self.task_manager.addPolicy(policy) 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 + '\'') 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\' ') 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'] profile_columns = ['id', 'create_date', 'label', 'description', 'overridable', 'active', 'deleted', 'profile_data', 'modify_date', 'plugin']
policy = PolicyBean(username=username) policy = PolicyBean(username=username)
@ -198,7 +197,7 @@ class ExecutionManager(object):
return policy return policy
#from db # from db
def get_installed_plugins(self): def get_installed_plugins(self):
plugins = self.db_service.select('plugin', ['name', 'version']) plugins = self.db_service.select('plugin', ['name', 'version'])
p_list = [] p_list = []
@ -210,18 +209,16 @@ class ExecutionManager(object):
str_task = json.loads(arg)['task'] str_task = json.loads(arg)['task']
json_task = json.loads(str_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.logger.debug('[ExecutionManager] Adding new task...Task is:{}'.format(task.get_command_cls_id()))
self.task_manager.addTask(task) self.task_manager.addTask(task)
self.logger.debug('[ExecutionManager] Task added') 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'] 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']) 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): def move_file(self, arg):
@ -296,13 +293,13 @@ class ExecutionManager(object):
if ahenk_prof_json_arr is not None: if ahenk_prof_json_arr is not None:
for prof in ahenk_prof_json_arr: for prof in ahenk_prof_json_arr:
plu = json.loads(json.dumps(prof['plugin'])) 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)) 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: if user_prof_json_arr is not None:
for prof in user_prof_json_arr: for prof in user_prof_json_arr:
plu = json.loads(json.dumps(prof['plugin'])) 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)) 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']) 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'])

View file

@ -65,7 +65,7 @@ class Messenger(ClientXMPP):
return False return False
def session_end(self): def session_end(self):
print("disconnect") self.logger.warning('[Messenger] DISCONNECTED')
def session_start(self, event): def session_start(self, event):
self.logger.debug('[Messenger] Session was started') self.logger.debug('[Messenger] Session was started')
@ -77,7 +77,7 @@ class Messenger(ClientXMPP):
self.logger.debug('[Messenger] <<--------Sending message: {}'.format(msg)) self.logger.debug('[Messenger] <<--------Sending message: {}'.format(msg))
self.send_message(mto=self.receiver, mbody=msg, mtype='normal') self.send_message(mto=self.receiver, mbody=msg, mtype='normal')
except Exception as e: 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): def recv_direct_message(self, msg):
if msg['type'] in ('normal'): if msg['type'] in ('normal'):
@ -87,6 +87,5 @@ class Messenger(ClientXMPP):
message_type = j['type'] message_type = j['type']
self.event_manger.fireEvent(message_type, str(msg['body'])) self.event_manger.fireEvent(message_type, str(msg['body']))
self.logger.debug('[Messenger] Fired event is: {}'.format(message_type)) self.logger.debug('[Messenger] Fired event is: {}'.format(message_type))
except Exception as e: 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)))

View file

@ -1,13 +1,12 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Author: Volkan Şahin <volkansah.in> <bm.volkansahin@gmail.com> # Author: Volkan Şahin <volkansah.in> <bm.volkansahin@gmail.com>
import json
class PluginBean(object): class PluginBean(object):
"""docstring for PluginBean""" """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.id = p_id
self.active = active self.active = active
self.create_date = create_date self.create_date = create_date
@ -19,6 +18,8 @@ class PluginBean(object):
self.policy_plugin = policy_plugin self.policy_plugin = policy_plugin
self.user_oriented = user_oriented self.user_oriented = user_oriented
self.version = version self.version = version
self.task_plugin = task_plugin
self.x_based = x_based
def get_user_oriented(self): def get_user_oriented(self):
return self.user_oriented return self.user_oriented
@ -85,3 +86,15 @@ class PluginBean(object):
def set_version(self, version): def set_version(self, version):
self.version = 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

View file

@ -65,6 +65,33 @@ class TaskBean(object):
def set_cron_str(self, cron_str): def set_cron_str(self, cron_str):
self.cron_str = 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 @property
def obj_name(self): def obj_name(self):
return "TASK" return "TASK"

View file

@ -63,7 +63,7 @@ class PluginManager(object):
plugin.start() plugin.start()
self.plugins.append(plugin) 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: if len(self.delayed_profiles) > 0:
self.pluginQueueDict[plugin_name].put(self.delayed_profiles[plugin_name], 1) self.pluginQueueDict[plugin_name].put(self.delayed_profiles[plugin_name], 1)

View file

@ -21,25 +21,33 @@ class CustomScheduler(BaseScheduler):
def initialize(self): def initialize(self):
self.scheduledb.initialize() self.scheduledb.initialize()
tasks = self.scheduledb.load() tasks = self.scheduledb.load()
for task in tasks: if tasks:
self.add_job(ScheduleTaskJob(task)) for task in tasks:
self.add_job(ScheduleTaskJob(task))
def add_job(self, job): def add_job(self, job):
self.events.append(job) self.events.append(job)
def save_and_add_job(self, task): def save_and_add_job(self, task):
self.scheduledb.save(task) try:
self.events.append(ScheduleTaskJob(task)) 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: for event in self.events:
if event.task.id == task.id: if event.task.get_id() == task_id:
self.scheduledb.delete(task) self.scheduledb.delete(task_id)
self.logger.debug('[CustomScheduler] Task was deleted from scheduled tasks table')
self.events.remove(event) self.events.remove(event)
self.logger.debug('[CustomScheduler] Task was removed from events')
def remove_job_via_task_id(self,task_id): def remove_job_via_task_id(self,task_id):
for event in self.events: for event in self.events:
if event.task.id == task_id: if event.task.get_id() == task_id:
self.scheduledb.delete(event.task) self.scheduledb.delete(event.task)
self.events.remove(event) self.events.remove(event)

View file

@ -5,30 +5,40 @@
from base.scheduler.custom.all_match import AllMatch from base.scheduler.custom.all_match import AllMatch
from base.Scope import Scope from base.Scope import Scope
# TODO Need logs
class ScheduleTaskJob(object): class ScheduleTaskJob(object):
def __init__(self, task): 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() scope = Scope.getInstance()
self.logger = scope.getLogger() self.logger = scope.getLogger()
self.task_manager = scope.getTaskManager() self.task_manager = scope.getTaskManager()
self.plugin_manager = scope.getPluginManager()
def processTask(self): self.task = task
cron_sj = self.parse_cron_str(task.get_cron_str())
try: try:
self.task_manager.addTask(self.task) if cron_sj:
if self.is_single_shot(): self.mins = self.conv_to_set(cron_sj[0])
Scope.getInstance().get_scheduler().remove_job(self.task) 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: 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): def parse_cron_str(self, cron_str):
self.logger.debug('[ScheduleTaskJob] Parsing cron string...')
try: try:
cron_exp_arr = cron_str.split(" ") cron_exp_arr = cron_str.split(" ")
cron_sj = [] cron_sj = []
@ -51,21 +61,24 @@ class ScheduleTaskJob(object):
elif count == 3: elif count == 3:
cron_sj.append(range(0, 7, range_val)) cron_sj.append(range(0, 7, range_val))
else: else:
print("it is not supported") self.logger.warning('[ScheduleTaskJob] Unsupported expression.')
elif ',' in exp: elif ',' in exp:
cron_sj.append("(" + str(exp) + ")") cron_sj.append("(" + str(exp) + ")")
else: else:
print("it is not supported") self.logger.warning('[ScheduleTaskJob] Unsupported expression.')
count = count + 1 count = count + 1
return cron_sj return cron_sj
except Exception as e: 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): def conv_to_set(self, obj):
if isinstance(obj, (int)): self.logger.debug('[ScheduleTaskJob] Converting {} to set'.format(str(obj)))
return set([obj])
if str(obj).isdigit():
return set([int(obj)])
if not isinstance(obj, set): if not isinstance(obj, set):
obj = set(obj) obj = set(obj)
return obj return obj
def is_single_shot(self): def is_single_shot(self):
@ -76,12 +89,12 @@ class ScheduleTaskJob(object):
def matchtime(self, t): def matchtime(self, t):
"""Return True if this event should trigger at the specified datetime""" """Return True if this event should trigger at the specified datetime"""
return ((t.minute in self.mins) and return ((t.minute in self.mins) and
(t.hour in self.hours) and (t.hour in self.hours) and
(t.day in self.days) and (t.day in self.days) and
(t.month in self.months) and (t.month in self.months) and
(t.weekday() in self.dow)) (t.weekday() in self.dow))
def check(self, t): def check(self, t):
if self.matchtime(t): if self.matchtime(t) is True:
self.action(*self.args, **self.kwargs) self.action()

View file

@ -14,22 +14,22 @@ class ScheduleTaskDB(object):
def initialize(self): def initialize(self):
self.logger.debug('[ScheduleTaskDB] Initializing scheduler database...') 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.') self.logger.debug('[ScheduleTaskDB] Scheduler database is ok.')
def save(self, task): def save(self, task):
self.logger.debug('[ScheduleTaskDB] Preparing schedule task for save operation... creating columns and values...') self.logger.debug('[ScheduleTaskDB] Preparing schedule task for save operation... creating columns and values...')
cols = ['id', 'task_json'] cols = ['task_id']
values = [str(task.id), str(task.to_json())] values = [task.get_id()]
self.logger.debug('[ScheduleTaskDB] Saving scheduler task to db... ') self.logger.debug('[ScheduleTaskDB] Saving scheduler task to db... ')
self.db_service.update('schedule_task', cols, values, None) self.db_service.update('schedule_task', cols, values, None)
self.logger.debug('[ScheduleTaskDB] Scheduler task saved.') self.logger.debug('[ScheduleTaskDB] Scheduler task saved.')
def delete(self, task): def delete(self, task_id):
try: try:
self.logger.debug('[ScheduleTaskDB] Deleting schedule task. Task id=' + str(task.id)) self.logger.debug('[ScheduleTaskDB] Deleting schedule task. Task id=' + str(task_id))
self.db_service.delete('schedule_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)) self.logger.debug('[ScheduleTaskDB] Deleting schedule task deleted successfully. task id=' + str(task_id))
except Exception as e: except Exception as e:
self.logger.error('[ScheduleTaskDB] Exception occur when deleting schedule task ' + str(e)) self.logger.error('[ScheduleTaskDB] Exception occur when deleting schedule task ' + str(e))

View file

@ -6,6 +6,7 @@ from base.scheduler.custom.custom_scheduler import CustomScheduler
class SchedulerFactory(): class SchedulerFactory():
def get_intstance(self): def get_intstance():
return CustomScheduler() return CustomScheduler()
get_intstance = staticmethod(get_intstance) get_intstance = staticmethod(get_intstance)

View file

@ -13,10 +13,6 @@ import fcntl
import struct import struct
from uuid import getnode as get_mac from uuid import getnode as get_mac
"""
some functions closed because of dependency management
"""
class System: class System:
class Ahenk(object): class Ahenk(object):
@ -191,21 +187,13 @@ class System:
def user_details(): def user_details():
return psutil.users() return psutil.users()
@staticmethod @staticmethod
def last_login_username(): def last_login_username():
# TODO # TODO
pass 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): class Os(object):
@staticmethod @staticmethod
@ -349,17 +337,6 @@ class System:
return arr 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 @staticmethod
def interfaces_details(): def interfaces_details():
return psutil.net_if_addrs() return psutil.net_if_addrs()

View file

@ -1,6 +1,7 @@
#!/usr/bin/python3 #!/usr/bin/python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Author: İsmail BAŞARAN <ismail.basaran@tubitak.gov.tr> <basaran.ismaill@gmail.com> # Author: İsmail BAŞARAN <ismail.basaran@tubitak.gov.tr> <basaran.ismaill@gmail.com>
from base.Scope import Scope from base.Scope import Scope
from base.model.MessageFactory import MessageFactory from base.model.MessageFactory import MessageFactory
from base.model.enum.MessageType import MessageType from base.model.enum.MessageType import MessageType
@ -19,33 +20,32 @@ class TaskManager(object):
def addTask(self, task): def addTask(self, task):
try: try:
self.saveTask(task)
if task.get_cron_str() == None or task.get_cron_str() == '': if task.get_cron_str() == None or task.get_cron_str() == '':
self.logger.debug('Adding task ... ') self.logger.debug('[TaskManager] Adding task ... ')
#self.saveTask(task)
self.logger.info('Task saved ')
# TODO send task received message
self.pluginManager.processTask(task) self.pluginManager.processTask(task)
else: else:
self.scheduler.save_and_add_job(task) self.scheduler.save_and_add_job(task)
except Exception as e: except Exception as e:
# TODO error log here self.logger.debug('[TaskManager] Exception occurred when adding task. Error Message: {}'.format(str(e)))
self.logger.debug('Exception occured when adding task ' + str(e))
pass
def addPolicy(self, policy): def addPolicy(self, policy):
try: try:
self.pluginManager.processPolicy(policy) self.pluginManager.processPolicy(policy)
except Exception as e: except Exception as e:
self.logger.error("Exception occured when adding policy. Error Message: {}".format(str(e))) self.logger.error("[TaskManager] Exception occurred when adding policy. Error Message: {}".format(str(e)))
pass
def saveTask(self, task): def saveTask(self, task):
try:
cols = ['id', 'create_date', 'modify_date', 'command_cls_id', 'parameter_map', 'deleted', 'plugin'] task_cols = ['id', 'create_date', 'modify_date', 'command_cls_id', 'parameter_map', 'deleted', 'plugin','cron_expr']
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()] plu_cols = ['active', 'create_date', 'deleted', 'description', 'machine_oriented', 'modify_date', 'name', 'policy_plugin', 'user_oriented', 'version','task_plugin','x_based']
self.db_service.update('task', cols, values, None) 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())]
self.logger.debug('[TaskManager] Task has been saved to database (Task id:' + task.id + ')') 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)
except Exception as e:
self.logger.error("[TaskManager] Exception occurred while saving task. Error Message: {}".format(str(e)))
def updateTask(self, task): def updateTask(self, task):
# TODO not implemented yet # TODO not implemented yet

View file

@ -209,20 +209,20 @@ class Util:
result_code, p_out, p_err = Util.execute('dpkg -s {}'.format(package_name)) result_code, p_out, p_err = Util.execute('dpkg -s {}'.format(package_name))
try: try:
lines=str(p_out).split('\n') lines = str(p_out).split('\n')
for line in lines: for line in lines:
if len(line)>1: if len(line) > 1:
if line.split(None, 1)[0].lower() =='status:': if line.split(None, 1)[0].lower() == 'status:':
if 'installed' in line.split(None, 1)[1].lower(): if 'installed' in line.split(None, 1)[1].lower():
return True return True
return False return False
except Exception as e: except Exception as e:
return False return False
@staticmethod @staticmethod
def get_md5_file(fname): def get_md5_file(fname):
hash_md5 = hashlib.md5() hash_md5 = hashlib.md5()
with open(fname, 'rb') as f: with open(fname, 'rb') as f:
for chunk in iter(lambda: f.read(4096), b""): for chunk in iter(lambda: f.read(4096), b""):
hash_md5.update(chunk) hash_md5.update(chunk)
return str(hash_md5.hexdigest()) return str(hash_md5.hexdigest())