def disk_status(server_id): if check_login(): s = get_session() conn = WebModel.connection() server = servers.Server(conn) arr_server = server.select_a_row(server_id) if arr_server: if 'ip' in arr_server: ip = arr_server['ip'] status_disk = servers.StatusDisk(conn) status_disk.set_conditions('where ip=%s', [ip]) arr_disk = status_disk.select_to_array( ['disk', 'used', 'free', 'date']) return filter_ajax(arr_disk) return filter_ajax({})
def graphs(server_id): if check_login(): s = get_session() #Load menu menu = get_menu(config_admin.modules_admin) lang_selected = get_language(s) t = PTemplate(env) conn = WebModel.connection() server = servers.Server(conn) arr_server = server.select_a_row(server_id) if arr_server: content_index = t.load_template('pastafari/admin/graphs.phtml', server=arr_server, api_key=config_task.api_key) else: content_index = '' if t.show_basic_template == True: return t.load_template('admin/content.html', title='Servers', content_index=content_index, menu=menu, lang_selected=lang_selected, arr_i18n=I18n.dict_i18n) else: return content_index else: redirect(make_url(config.admin_folder))
def index(token, ip): if config.api_key == token and config.api_key != '': now = datetime.now() servermodel = servers.Server() servermodel.create_forms() servermodel.reset_require() servermodel.conditions = ['WHERE ip=%s', ip] servermodel.update({'status': 1, 'last_updated': now}) return "Updated server" return "Nothing to see here"
def admin(t): GetPostFiles.obtain_get() GetPostFiles.get['op'] = GetPostFiles.get.get('op', '') server = servers.Server() url = make_url(config.admin_folder + '/pastafari/servers') if GetPostFiles.get['op'] == 'add_new_server': #Check if the server can be access with god module using ssh, if yes, install idea(aka virus or platon) using protozoo or similar program server.create_forms() return "" pass elif GetPostFiles.get['op'] == 'view_status': GetPostFiles.get['id'] = GetPostFiles.get.get('id', '0') server_id = int(GetPostFiles.get['id']) server_view = server.select_a_row(server_id) GetPostFiles.get['show_data'] = GetPostFiles.get.get('show_data', '0') if server_view != False: if GetPostFiles.get['show_data'] == '0': return ts.load_template('server_status.phtml', server_view=server_view) elif GetPostFiles.get['show_data'] == '1': get_c = 60 ptemplate.show_basic_template = False network_status = servers.ServerInfoNet() c = network_status.select_count() begin_c = c - get_c if begin_c < 0: begin_c = 0 network_status.set_conditions('where server=%s', [server_view['ip']]) network_status.set_order(['date'], ['ASC']) network_status.set_limit([begin_c, get_c]) network_cur = network_status.select() #arr_dates=[] arr_dates = {} for net_info in network_cur: arr_dates[net_info['device']] = arr_dates.get( net_info['device'], []) net_info['date'] = datetime.format_fulldate( net_info['date']) arr_dates[net_info['device']].append(net_info) #arr_dates.append(arr_date) arr_final_dates = {} for dev in arr_dates: substract_up = arr_dates[dev][0]['network_up'] substract_down = arr_dates[dev][0]['network_down'] arr_final_dates[dev] = [] for x in range(1, len(arr_dates[dev])): up = arr_dates[dev][x]['network_up'] - substract_up down = arr_dates[dev][x][ 'network_down'] - substract_down arr_final_dates[dev].append({ 'date': arr_dates[dev][x]['date'], 'network_up': up, 'network_down': down }) substract_up = arr_dates[dev][x]['network_up'] substract_down = arr_dates[dev][x]['network_down'] pass return json.dumps(arr_final_dates) else: # Obtain uptimes server.conditions = ['where status=%s', [0]] num_servers_failed = server.select_count() server.conditions = ['where status=%s', [1]] num_servers_uptime = server.select_count() # Obtain profiles server.distinct = 'DISTINCT' server.order_by = 'order by profile ASC' arr_profiles = server.select_to_array(['profile']) profiles = SelectForm('profile', '') profiles.arr_select[''] = '' for profile in arr_profiles.values(): profiles.arr_select[profile['profile']] = profile['profile'] server.distinct = '' GetPostFiles.get['profile'] = GetPostFiles.get.get('profile', '') url = add_get_parameters(url, profile=GetPostFiles.get['profile']) if GetPostFiles.get[ 'profile'] in profiles.arr_select and GetPostFiles.get[ 'profile'] != '': server.conditions = [ 'where profile=%s', [GetPostFiles.get['profile']] ] profiles.default_value = GetPostFiles.get['profile'] server_list = SimpleList(server, url, t) #server_list.fields=['id', 'type'] server_list.fields_showed = [ 'hostname', 'ip', 'profile', 'type', 'last_updated' ] server_list.yes_search = False server_list.arr_extra_fields = [ I18n.lang('common', 'options', 'Options') ] server_list.arr_extra_options = [server_options] return ts.load_template('servers.phtml', server_list=server_list, url=url, profiles=profiles, num_servers_failed=num_servers_failed, num_servers_uptime=num_servers_uptime)
def start(): parser = argparse.ArgumentParser(description='A daemon used for make a task in a server.The results are saved in a sql database using task class') parser.add_argument('--task_id', help='The task to execute', required=True) args = parser.parse_args() task_id=int(args.task_id) conn=WebModel.connection() task_model=tasks.Task(conn) logtask=tasks.LogTask(conn) arr_task=task_model.select_a_row(task_id) if arr_task: if arr_task['user']!='' and arr_task['password']!='' and arr_task['path']!='': config_task.remote_user=arr_task['user'] config_task.remote_password=arr_task['password'] config_task.remote_path=arr_task['path'] task_model.create_forms() task_model.reset_require() task_model.set_conditions('WHERE id=%s', [task_id]) task_model.update({'password': ''}) commands_to_execute=json.loads(arr_task['commands_to_execute']) if not commands_to_execute: print('Error: no task files') exit(1) if arr_task['where_sql_server']=='': task=generate_task(arr_task, task_id) task.exec() else: # Select the servers and make all tasks asynchronous server_model=servers.Server(conn) server_model.set_conditions(arr_task['where_sql_server'], []) server_model.yes_reset_conditions=False c=server_model.select_count() #Update task with number of servers task_model.set_conditions('WHERE id=%s', [task_id]) task_model.reset_require() task_model.valid_fields=['num_servers'] task_model.update({'num_servers': c}) z=0 while z<c: # Set the num of pools server_model.set_limit([z, num_tasks]) arr_servers=server_model.select_to_array() num_pools=len(arr_servers) with Pool(processes=num_pools) as pool: arr_task_exec=[] for server in arr_servers: arr_task_exec.append([arr_task, task_id, server['ip'], server['os_codename']]) pool.map(execute_multitask, arr_task_exec) #for x in range(num_pools) #pool. pass z+=num_tasks pass # Task done task_model.set_conditions('WHERE id=%s', [task_id]) task_model.reset_require() task_model.valid_fields=['status'] task_model.update({'num_servers': 1}) conn.close() exit(0)
def admin(**args): t = args['t'] conn = args['connection'] server = servers.Server(conn) os_model = servers.OsServer(conn) task = tasks.Task(conn) logtask = tasks.LogTask(conn) group_server = servers.ServerGroup(conn) group_server_item = servers.ServerGroupItem(conn) getpostfiles = GetPostFiles() getpostfiles.obtain_get() getpostfiles.get['op'] = getpostfiles.get.get('op', '') getpostfiles.get['group_id'] = getpostfiles.get.get('group_id', '0') request_type = formsutils.request_type() server.fields['os_codename'].name_form = SelectForm server.create_forms(['hostname', 'ip', 'os_codename']) arr_os = {} with os_model.select() as cur: arr_os = {v['codename']: v['name'] for v in cur} server.forms['os_codename'].arr_select = arr_os server.forms['password'] = PasswordForm('password', '') server.forms['password'].required = True server.forms['password'].label = I18n.lang('pastafari', 'password', 'Password') server.forms['delete_root_password'] = SelectForm('delete_root_password', '', { '0': 'No', '1': 'Yes' }) server.forms['delete_root_password'].label = I18n.lang( 'pastafari', 'delete_root_password', 'Disable root password') server.forms['group_id'] = SelectModelForm('group_id', getpostfiles.get['group_id'], group_server, 'name', 'id', 'parent_id') server.forms['group_id'].field = group_server_item.fields['server_id'] server.forms['group_id'].required = True server.forms['group_id'].label = label = I18n.lang('pastafari', 'principal_group', 'Principal group') if getpostfiles.get['op'] == '1': if request_type != "POST": forms = formsutils.show_form({}, server.forms, t, False, False) return t.load_template('pastafari/admin/add_servers.phtml', group_id=getpostfiles.get['group_id'], form_server=forms, url=url + '?op=1&group_id=' + getpostfiles.get['group_id']) else: #if insert then send task to servertask getpostfiles.obtain_post() post = {} post['password'] = getpostfiles.post.get('password', '') post['delete_root_password'] = getpostfiles.post.get( 'delete_root_password', '0') post['ip'] = getpostfiles.post.get('ip', '') try: post['group_id'] = getpostfiles.post.get('group_id', '0') except: post['group_id'] = '0' check_form = formsutils.CheckForm() (post, pass_form) = check_form.check( post, { 'password': server.forms['password'], 'delete_root_password': server.forms['delete_root_password'], 'ip': server.forms['ip'], 'group_id': server.forms['group_id'] }) server.set_conditions('WHERE ip=%s', [post['ip']]) c_ip = server.select_count() if c_ip == 0: if check_form.error == 0: #try connect to the server task_ssh = Task(post['ip']) testing_task = copy.copy(config_task) testing_task.remote_user = '******' testing_task.remote_password = post['password'] task_ssh.config = testing_task if task_ssh.prepare_connection(): task_ssh.ssh.close() if not server.insert(getpostfiles.post): forms = formsutils.show_form( getpostfiles.post, server.forms, t, True) return t.load_template( 'pastafari/admin/add_servers.phtml', group_id=getpostfiles.get['group_id'], form_server=forms, url=url + '?op=1') else: server_id = server.insert_id() # Insert in server group group_server_item.valid_fields = [ 'group_id', 'server_id' ] if group_server_item.insert({ 'group_id': post['group_id'], 'server_id': server_id }): task.create_forms() os_server = getpostfiles.post[ 'os_codename'].replace('/', '').replace( '.', '') ip = server.fields['ip'].check( getpostfiles.post['ip']) files = [] files.append([ 'modules/pastafari/scripts/standard/' + os_server + '/install_python.sh', 0o750 ]) files.append([ 'modules/pastafari/scripts/standard/' + os_server + '/install_curl.sh', 0o750 ]) files.append([ 'modules/pastafari/scripts/standard/' + os_server + '/install_psutil.sh', 0o750 ]) files.append([ 'modules/pastafari/scripts/standard/' + os_server + '/upgrade.sh', 0o750 ]) files.append([ 'modules/pastafari/scripts/monit/' + os_server + '/alive.py', 0o750 ]) #files.append(['monit/'+os_server+'/files/alive.sh', 0o750]; files.append([ 'modules/pastafari/scripts/monit/' + os_server + '/files/get_info.py', 0o750 ]) files.append([ 'modules/pastafari/scripts/monit/' + os_server + '/files/get_updates.py', 0o750 ]) files.append([ 'modules/pastafari/scripts/monit/' + os_server + '/files/crontab/alive', 0o640 ]) files.append([ 'modules/pastafari/scripts/monit/' + os_server + '/files/sudoers.d/spanel', 0o640 ]) files.append([config_task.public_key, 0o600]) commands_to_execute = [] commands_to_execute.append([ 'modules/pastafari/scripts/standard/' + os_server + '/install_python.sh', '' ]) commands_to_execute.append([ 'modules/pastafari/scripts/standard/' + os_server + '/install_curl.sh', '' ]) commands_to_execute.append([ 'modules/pastafari/scripts/standard/' + os_server + '/install_psutil.sh', '' ]) commands_to_execute.append([ 'modules/pastafari/scripts/monit/' + os_server + '/alive.py', '--url=' + config_task.url_monit + '/' + ip + '/' + config_task.api_key + ' --user='******' --pub_key=' + config_task.public_key ]) delete_files = [] delete_files.append( 'modules/pastafari/scripts/standard/' + os_server + '/install_python.sh') delete_files.append( 'modules/pastafari/scripts/standard/' + os_server + '/install_curl.sh') delete_files.append( 'modules/pastafari/scripts/standard/' + os_server + '/install_psutil.sh') delete_files.append(config_task.public_key) delete_directories = ['modules/pastafari'] if post['delete_root_password'] == '1': #delete_root_passwd.sh files.append([ 'modules/pastafari/scripts/standard/' + os_server + '/delete_root_passwd.sh', 0o750 ]) commands_to_execute.append([ 'modules/pastafari/scripts/standard/' + os_server + '/delete_root_passwd.sh', '' ]) if task.insert({ 'name_task': 'monit_server', 'description_task': I18n.lang( 'pastafari', 'add_monit', 'Adding monitoritation to the server...' ), 'url_return': url, 'files': files, 'commands_to_execute': commands_to_execute, 'delete_files': delete_files, 'delete_directories': delete_directories, 'server': ip, 'user': '******', 'password': post['password'], 'path': '/root' }): task_id = task.insert_id() try: r = requests.get(server_task + str(task_id)) arr_data = r.json() arr_data['task_id'] = task_id logtask.create_forms() if not logtask.insert(arr_data): return "Error:Wrong format of json data..." #return t_admin.load_template('pastafari/ajax_progress.phtml', title='Adding monitoritation to the server...') #"Load template with ajax..." except: logtask.conditions = [ 'WHERE id=%s', [task_id] ] task.update({'status': 1, 'error': 1}) server.conditions = [ 'WHERE id=%s', [server_id] ] group_server_item.conditions = [ 'WHERE server_id=%s', [server_id] ] group_server_item.delete() server.delete() return "Error:cannot connect to task server, check the url for it..." return t.load_template( 'pastafari/progress.phtml', name_task=I18n.lang( 'pastafari', 'add_monit', 'Adding monitoritation to the server...' ), description_task=I18n.lang( 'pastafari', 'add_monit_explain', 'Installing the basic scripts for send info from server to monit module' ), task_id=task_id, server=ip, position=0) #return "Server is building..." #redirect('servers?op=2&task_id='+str(task_id)) else: server.conditions = [ 'WHERE id=%s', [server_id] ] server.delete() group_server_item.conditions = [ 'WHERE server_id=%s', [server_id] ] group_server_item.delete() return "Error: cannot create the new task" else: server.conditions = [ 'WHERE id=%s', [server_id] ] server.delete() return "Error: you need a initial group for your server" else: task_ssh.ssh.close() return "Cannot connect to the new server " + task_ssh.txt_error else: forms = formsutils.show_form(getpostfiles.post, server.forms, t, True) return t.load_template( 'pastafari/admin/add_servers.phtml', group_id=getpostfiles.get['group_id'], form_server=forms, url=url + '?op=1&group_id=' + getpostfiles.get['group_id']) else: server.fields['ip'].duplicated_ip = True forms = formsutils.show_form(getpostfiles.post, server.forms, t, True) return t.load_template('pastafari/admin/add_servers.phtml', group_id=getpostfiles.get['group_id'], form_server=forms, url=url + '?op=1&group_id=' + getpostfiles.get['group_id']) #elif getpostfiles.get['op']=='2': #return "" elif getpostfiles.get['op'] == '2': getpostfiles = GetPostFiles() getpostfiles.obtain_get() server_id = int(getpostfiles.get.get('id', '0')) arr_server = server.select_a_row(server_id) if arr_server: return t.load_template('pastafari/admin/graphs.phtml', server=arr_server, api_key=config_task.api_key) else: return "" elif getpostfiles.get['op'] == '3': t.show_basic_template = False getpost = GetPostFiles() getpost.obtain_get() server_id = int(getpostfiles.get.get('id', '0')) arr_server = server.select_a_row(server_id) if 'ip' in arr_server: ip = arr_server['ip'] now = datetime.obtain_timestamp(datetime.now()) hours12 = now - 21600 date_now = datetime.timestamp_to_datetime(now) date_hours12 = datetime.timestamp_to_datetime(hours12) status_cpu = servers.StatusCpu(conn) status_cpu.set_conditions('where ip=%s and date>=%s and date<=%s', [ip, date_hours12, date_now]) #arr_cpu=status_cpu.select_to_array(['idle', 'date']) cur = status_cpu.select(['idle', 'date']) x = 0 arr_cpu = [] cur.fetchone() for cpu_info in cur: arr_cpu.append(cpu_info['idle']) cur.close() status_mem = servers.StatusMemory(conn) status_mem.set_conditions('where ip=%s and date>=%s and date<=%s', [ip, date_hours12, date_now]) #status_mem.set_order(['id', 'ASC']) #arr_mem=status_mem.select_to_array(['used', 'free', 'date']) arr_mem = [] with status_mem.select(['used', 'free', 'cached', 'date']) as cur: #cur.fetchone() for mem_info in cur: mem_info['used'] = ( (mem_info['used'] / 1024) / 1024) / 1024 mem_info['free'] = ( (mem_info['free'] / 1024) / 1024) / 1024 mem_info['cached'] = ( (mem_info['cached'] / 1024) / 1024) / 1024 arr_mem.append(mem_info) if len(arr_mem) > 2: arr_mem.pop(0) #arr_cpu=status_cpu.select_to_array(['idle', 'date']) cur = status_cpu.select(['idle', 'date']) arr_net = {} status_net = servers.StatusNet(conn) status_net.set_conditions('where ip=%s and date>=%s and date<=%s', [ip, date_hours12, date_now]) arr_net = [] cur = status_net.select(['bytes_sent', 'bytes_recv', 'date']) substract_time = 0 #datetime.obtain_timestamp(datetime.now()) c_hours12 = now c_elements = 0 if cur.rowcount > 0: data_net = cur.fetchone() first_recv = data_net['bytes_recv'] first_sent = data_net['bytes_sent'] for data_net in cur: timestamp = datetime.obtain_timestamp( data_net['date'], True) diff_time = timestamp - substract_time if substract_time != 0 and diff_time > 300: count_time = timestamp while substract_time <= count_time: form_time = datetime.timestamp_to_datetime( substract_time) arr_net.append( {'date': datetime.format_time(form_time)}) substract_time += 60 bytes_sent = round( (data_net['bytes_sent'] - first_sent) / 1024) bytes_recv = round( (data_net['bytes_recv'] - first_recv) / 1024) cpu = arr_cpu[x] memory_used = arr_mem[x]['used'] memory_free = arr_mem[x]['free'] memory_cached = arr_mem[x]['cached'] arr_net.append({ 'bytes_sent': bytes_sent, 'bytes_recv': bytes_recv, 'date': datetime.format_time(data_net['date']), 'cpu': cpu, 'memory_used': memory_used, 'memory_free': memory_free, 'memory_cached': memory_cached }) first_sent = data_net['bytes_sent'] first_recv = data_net['bytes_recv'] c_hours12 = timestamp substract_time = int(timestamp) c_elements += 1 x += 1 # If the last time is more little that now make a loop while c_hours12 <= now: form_time = datetime.timestamp_to_datetime(c_hours12) seconds = form_time[-2:] #print(form_time) if seconds == '00': arr_net.append( {'date': datetime.format_time(form_time)}) # if secons is 00 and z=1 put value #arr_net.append({'date': datetime.format_time(form_time)}) pass c_hours12 += 1 cur.close() if c_elements > 2: return json.dumps(arr_net) else: return {} return {} return {} elif getpostfiles.get['op'] == '4': t.show_basic_template = False getpost = GetPostFiles() getpost.obtain_get() server_id = int(getpostfiles.get.get('id', '0')) arr_server = server.select_a_row(server_id) if 'ip' in arr_server: ip = arr_server['ip'] status_disk = servers.StatusDisk(conn) status_disk.set_conditions('where ip=%s', [ip]) arr_disk = status_disk.select_to_array( ['disk', 'used', 'free', 'date']) return json.dumps(arr_disk) elif getpostfiles.get['op'] == '5': getpostfiles.get['id'] = getpostfiles.get.get('id', '0') getpostfiles.get['delete'] = getpostfiles.get.get('delete', '0') try: getpostfiles.get['id'] = int(getpostfiles.get['id']) except: getpostfiles.get['id'] = 0 if getpostfiles.get['delete'] != '0': group_server_item.set_conditions('WHERE server_id=%s', [getpostfiles.get['id']]) group_server_item.delete() server.set_conditions('WHERE id=%s', [getpostfiles.get['id']]) server.delete() set_flash_message('Deleted the server sucessfully') redirect(make_admin_url('pastafari/servers')) pass else: return "It deleted the server in database <strong>only</strong><p>You need delete the server with the tool of your election: <input type=\"button\" value=\"Do you are sure?\" onclick=\"javascript:location.href='" + make_admin_url( 'pastafari/servers', { 'op': '5', 'id': str(getpostfiles.get['id']), 'delete': '1' }) + "';\"></p>" pass else: """ $actual_timestamp=time(); $past_timestamp=time()-300; $actual_time=PhangoApp\PhaTime\DateTime::format_timestamp($actual_timestamp, $localtime=false); $past_time=PhangoApp\PhaTime\DateTime::format_timestamp($past_timestamp, $localtime=false); //$m->server->set_order(['date' => 1]); //$m->server->set_conditions(['where date<?', [$past_time]]); $admin->where_sql=['where date <?', [$past_time]]; """ getpost = GetPostFiles() getpost.obtain_get() try: group_id = int(getpost.get.get('group_id', '0')) except: group_id = 0 select_form_group = SelectModelForm('group_id', group_id, servers.ServerGroup(conn), 'name', 'id', 'parent_id') select_form_group.name_field_id = 'change_group_id_form' servers_list = SimpleList(server, url, t) yes_form = 0 type_op = '' #servers_list.arr_extra_fields=[I18n.lang('common', 'options', 'Options')] servers_list.arr_extra_options = [server_options] select_task = None if 'type' in getpost.get: if getpost.get['type'] == 'down': actual_timestamp = datetime.obtain_timestamp(datetime.now()) past_timestamp = actual_timestamp - 300 actual_time = datetime.timestamp_to_datetime(actual_timestamp) past_time = datetime.timestamp_to_datetime(past_timestamp) servers_list.model.set_conditions('WHERE date<%s', [past_time]) elif getpost.get['type'] == 'heavy': servers_list.model.set_conditions("where actual_idle>%s", [80]) elif getpost.get['type'] == 'disks': servers_list.model.set_conditions( "where ip IN (select ip from statusdisk where percent>90)", []) elif getpost.get['type'] == 'update_servers': servers_list.model.set_conditions("where num_updates>0", []) servers_list.arr_extra_fields = [ I18n.lang('common', 'update_server', 'Update server') ] servers_list.arr_extra_options = [server_update_options] servers_list.yes_search = False yes_form = 1 elif getpost.get['type'] == 'task_servers': servers_list.arr_extra_fields = [ I18n.lang('pastafari', 'choose_server', 'Choose server') ] servers_list.arr_extra_options = [server_update_options] servers_list.yes_search = False yes_form = 2 # Get tasks and put in select_task # Folders are tasks/ and modules/pastafari/tasks base_path = 'modules/pastafari/tasks' config_parser = configparser.ConfigParser() select_task = scandir(base_path, config_parser, OrderedDict(), 'tasks') #OrderedDict([('tasks', {'servers': [['Servers', 'tasks', 0]]}), ('servers', {'databases': [['Database servers', 'servers', 0]], 'mail': [['Mail', 'servers', 0], ['Standalone postfix server, Install on your servers a simple and secure postfix mail server', 'mail', 'modules/pastafari/tasks/servers/mail/postfix/standalone_postfix.py', 1]]})]) type_op = getpost.get['type'] if group_id > 0: servers_list.model.conditions[ 0] += ' AND id IN (select server_id from servergroupitem where group_id=%s)' servers_list.model.conditions[1].append(group_id) servers_list.fields_showed = ['hostname', 'ip', 'num_updates', 'date'] servers_list.limit_pages = 100 show_servers = servers_list.show() return t.load_template('pastafari/admin/servers.phtml', show_servers=show_servers, type_op=type_op, yes_form=yes_form, csrf_token=csrf_token(), select_form_group=select_form_group, group_id=group_id, select_task=select_task) return ""
def admin(**args): t = args['t'] conn = args['connection'] server = servers.Server(conn) os_model = servers.OsServer(conn) task = tasks.Task(conn) logtask = tasks.LogTask(conn) group_server = servers.ServerGroup(conn) group_server_item = servers.ServerGroupItem(conn) getpostfiles = GetPostFiles() getpostfiles.obtain_get() getpostfiles.get['op'] = getpostfiles.get.get('op', '') getpostfiles.get['group_id'] = getpostfiles.get.get('group_id', '0') request_type = formsutils.request_type() server.fields['os_codename'].name_form = SelectForm server.create_forms(['hostname', 'ip', 'os_codename']) arr_os = {} with os_model.select() as cur: arr_os = {v['codename']: v['name'] for v in cur} server.fields['date'].label = I18n.lang('pastafari', 'server_status', 'Server status') server.forms['os_codename'].arr_select = arr_os server.forms['password'] = PasswordForm('password', '') server.forms['password'].required = True server.forms['password'].label = I18n.lang('pastafari', 'password', 'Password') server.forms['delete_root_password'] = SelectForm('delete_root_password', '1', { '0': 'No', '1': 'Yes' }) server.forms['delete_root_password'].label = I18n.lang( 'pastafari', 'delete_root_password', 'Disable root password') server.forms['clean_gcc'] = SelectForm('clean_gcc', '1', { '0': 'No', '1': 'Yes' }) server.forms['clean_gcc'].label = I18n.lang( 'pastafari', 'clean_gcc', 'Clean build dependencies for soft monitoring install?,if you want gcc in your server, answer NO' ) server.forms['group_id'] = SelectModelForm('group_id', getpostfiles.get['group_id'], group_server, 'name', 'id', 'parent_id') server.forms['group_id'].field = group_server_item.fields['server_id'] server.forms['group_id'].required = True server.forms['group_id'].label = label = I18n.lang('pastafari', 'principal_group', 'Principal group') if getpostfiles.get['op'] == '1': if request_type != "POST": forms = formsutils.show_form({}, server.forms, t, False, False) return t.load_template('pastafari/admin/add_servers.phtml', group_id=getpostfiles.get['group_id'], form_server=forms, url=url + '?op=1&group_id=' + getpostfiles.get['group_id']) else: #if insert then send task to servertask getpostfiles.obtain_post() post = {} post['password'] = getpostfiles.post.get('password', '') post['delete_root_password'] = getpostfiles.post.get( 'delete_root_password', '0') post['ip'] = getpostfiles.post.get('ip', '') post['clean_gcc'] = getpostfiles.post.get('clean_gcc', '1') try: post['group_id'] = getpostfiles.post.get('group_id', '0') except: post['group_id'] = '0' check_form = formsutils.CheckForm() (post, pass_form) = check_form.check( post, { 'password': server.forms['password'], 'delete_root_password': server.forms['delete_root_password'], 'ip': server.forms['ip'], 'group_id': server.forms['group_id'] }) server.set_conditions('WHERE ip=%s', [post['ip']]) c_ip = server.select_count() if c_ip == 0: if check_form.error == 0: #try connect to the server task_ssh = Task(post['ip']) testing_task = copy.copy(config_task) testing_task.remote_user = '******' testing_task.remote_password = post['password'] task_ssh.config = testing_task if task_ssh.prepare_connection(): task_ssh.ssh.close() if not server.insert(getpostfiles.post): forms = formsutils.show_form( getpostfiles.post, server.forms, t, True) return t.load_template( 'pastafari/admin/add_servers.phtml', group_id=getpostfiles.get['group_id'], form_server=forms, url=url + '?op=1') else: server_id = server.insert_id() # Insert in server group group_server_item.valid_fields = [ 'group_id', 'server_id' ] if group_server_item.insert({ 'group_id': post['group_id'], 'server_id': server_id }): task.create_forms() os_server = getpostfiles.post[ 'os_codename'].replace('/', '').replace( '.', '') ip = server.fields['ip'].check( getpostfiles.post['ip']) files = [] files.append([ 'modules/pastafari/scripts/standard/' + os_server + '/install_python.sh', 0o750 ]) files.append([ 'modules/pastafari/scripts/standard/' + os_server + '/install_curl.sh', 0o750 ]) files.append([ 'modules/pastafari/scripts/standard/' + os_server + '/install_psutil.sh', 0o750 ]) files.append([ 'modules/pastafari/scripts/standard/' + os_server + '/upgrade.sh', 0o750 ]) files.append([ 'modules/pastafari/scripts/standard/' + os_server + '/clean_gcc.sh', 0o750 ]) files.append([ 'modules/pastafari/scripts/monit/' + os_server + '/alive.py', 0o750 ]) #files.append(['monit/'+os_server+'/files/alive.sh', 0o750]; files.append([ 'modules/pastafari/scripts/monit/' + os_server + '/files/get_info.py', 0o750 ]) files.append([ 'modules/pastafari/scripts/monit/' + os_server + '/files/get_updates.py', 0o750 ]) files.append([ 'modules/pastafari/scripts/monit/' + os_server + '/files/crontab/alive', 0o640 ]) files.append([ 'modules/pastafari/scripts/monit/' + os_server + '/files/sudoers.d/spanel', 0o640 ]) files.append([config_task.public_key, 0o600]) commands_to_execute = [] commands_to_execute.append([ 'modules/pastafari/scripts/standard/' + os_server + '/install_curl.sh', '' ]) commands_to_execute.append([ 'modules/pastafari/scripts/standard/' + os_server + '/install_python.sh', '' ]) commands_to_execute.append([ 'modules/pastafari/scripts/standard/' + os_server + '/install_psutil.sh', '' ]) commands_to_execute.append([ 'modules/pastafari/scripts/monit/' + os_server + '/alive.py', '--url=' + config_task.url_monit + '/' + ip + '/' + config_task.api_key + ' --user='******' --pub_key=' + config_task.public_key ]) delete_files = [] delete_files.append( 'modules/pastafari/scripts/standard/' + os_server + '/install_python.sh') delete_files.append( 'modules/pastafari/scripts/standard/' + os_server + '/install_curl.sh') delete_files.append( 'modules/pastafari/scripts/standard/' + os_server + '/install_psutil.sh') delete_files.append(config_task.public_key) delete_directories = ['modules/pastafari'] delete_directories = [ os.path.dirname(config_task.public_key) ] if post['delete_root_password'] == '1': #delete_root_passwd.sh files.append([ 'modules/pastafari/scripts/standard/' + os_server + '/delete_root_passwd.sh', 0o750 ]) commands_to_execute.append([ 'modules/pastafari/scripts/standard/' + os_server + '/delete_root_passwd.sh', '' ]) if post['clean_gcc'] == '1': #delete_root_passwd.sh files.append([ 'modules/pastafari/scripts/standard/' + os_server + '/clean_gcc.sh', 0o750 ]) commands_to_execute.append([ 'modules/pastafari/scripts/standard/' + os_server + '/clean_gcc.sh', '' ]) #'modules.pastafari.tasks.system.install.task_functions' if task.insert({ 'name_task': 'monit_server', 'description_task': I18n.lang( 'pastafari', 'add_monit', 'Adding monitoritation to the server...' ), 'url_return': url, 'files': files, 'commands_to_execute': commands_to_execute, 'delete_files': delete_files, 'delete_directories': delete_directories, 'server': ip, 'user': '******', 'password': post['password'], 'path': '/root', 'error_func': '', 'extra_data': { 'server_id': server_id } }): task_id = task.insert_id() try: r = requests.get(server_task + str(task_id)) arr_data = r.json() arr_data['task_id'] = task_id logtask.create_forms() if not logtask.insert(arr_data): return "Error:Wrong format of json data..." #return t_admin.load_template('pastafari/ajax_progress.phtml', title='Adding monitoritation to the server...') #"Load template with ajax..." except: logtask.conditions = [ 'WHERE id=%s', [task_id] ] task.update({'status': 1, 'error': 1}) server.conditions = [ 'WHERE id=%s', [server_id] ] group_server_item.conditions = [ 'WHERE server_id=%s', [server_id] ] group_server_item.delete() server.delete() return "Error:cannot connect to task server, check the url for it..." #return t.load_template('pastafari/progress.phtml', name_task=I18n.lang('pastafari', 'add_monit', 'Adding monitoritation to the server...'), description_task=I18n.lang('pastafari', 'add_monit_explain', 'Installing the basic scripts for send info from server to monit module'), task_id=task_id, server=ip, position=0) #return "Server is building..." #redirect('servers?op=2&task_id='+str(task_id)) #@get('/'+pastafari_folder+'/showprogress/<task_id:int>/<server>') redirect( make_url(pastafari_folder + '/showprogress/' + str(task_id) + '/' + ip)) else: server.conditions = [ 'WHERE id=%s', [server_id] ] server.delete() group_server_item.conditions = [ 'WHERE server_id=%s', [server_id] ] group_server_item.delete() return "Error: cannot create the new task" else: server.conditions = [ 'WHERE id=%s', [server_id] ] server.delete() return "Error: you need a initial group for your server" else: task_ssh.ssh.close() return "Cannot connect to the new server " + task_ssh.txt_error else: forms = formsutils.show_form(getpostfiles.post, server.forms, t, True) return t.load_template( 'pastafari/admin/add_servers.phtml', group_id=getpostfiles.get['group_id'], form_server=forms, url=url + '?op=1&group_id=' + getpostfiles.get['group_id']) else: server.fields['ip'].duplicated_ip = True forms = formsutils.show_form(getpostfiles.post, server.forms, t, True) return t.load_template('pastafari/admin/add_servers.phtml', group_id=getpostfiles.get['group_id'], form_server=forms, url=url + '?op=1&group_id=' + getpostfiles.get['group_id']) #elif getpostfiles.get['op']=='2': #return "" elif getpostfiles.get['op'] == '2': getpostfiles = GetPostFiles() getpostfiles.obtain_get() server_id = int(getpostfiles.get.get('id', '0')) arr_server = server.select_a_row(server_id) if arr_server: return t.load_template('pastafari/admin/graphs.phtml', server=arr_server, api_key=config_task.api_key) else: return "" elif getpostfiles.get['op'] == '5': getpostfiles.get['id'] = getpostfiles.get.get('id', '0') getpostfiles.get['delete'] = getpostfiles.get.get('delete', '0') try: getpostfiles.get['id'] = int(getpostfiles.get['id']) except: getpostfiles.get['id'] = 0 if getpostfiles.get['delete'] != '0': group_server_item.set_conditions('WHERE server_id=%s', [getpostfiles.get['id']]) group_server_item.delete() server.set_conditions('WHERE id=%s', [getpostfiles.get['id']]) server.delete() set_flash_message('Deleted the server sucessfully') redirect(make_url(pastafari_folder + '/servers')) pass else: return "It deleted the server in database <strong>only</strong><p>You need delete the server with the tool of your election: <input type=\"button\" value=\"Do you are sure?\" onclick=\"javascript:location.href='" + make_url( pastafari_folder + '/servers', { 'op': '5', 'id': str(getpostfiles.get['id']), 'delete': '1' }) + "';\"></p>" pass else: getpost = GetPostFiles() getpost.obtain_get() try: group_id = int(getpost.get.get('group_id', '0')) except: group_id = 0 select_form_group = SelectModelForm('group_id', group_id, servers.ServerGroup(conn), 'name', 'id', 'parent_id') select_form_group.name_field_id = 'change_group_id_form' servers_list = SimpleList(server, url, t) yes_form = 0 type_op = '' #servers_list.arr_extra_fields=[I18n.lang('common', 'options', 'Options')] servers_list.arr_extra_options = [server_options] servers_list.yes_search = False select_task = None if 'type' in getpost.get: if getpost.get['type'] == 'down': actual_timestamp = datetime.obtain_timestamp(datetime.now()) past_timestamp = actual_timestamp - 300 actual_time = datetime.timestamp_to_datetime(actual_timestamp) past_time = datetime.timestamp_to_datetime(past_timestamp) servers_list.model.set_conditions('WHERE date<%s', [past_time]) elif getpost.get['type'] == 'heavy': servers_list.model.set_conditions("where actual_idle>%s", [80]) elif getpost.get['type'] == 'disks': servers_list.model.set_conditions( "where ip IN (select ip from statusdisk where percent>90)", []) elif getpost.get['type'] == 'update_servers': servers_list.model.set_conditions("where num_updates>0", []) servers_list.arr_extra_fields = [ I18n.lang('common', 'update_server', 'Update server') ] servers_list.arr_extra_options = [server_update_options] servers_list.yes_search = False yes_form = 1 elif getpost.get['type'] == 'task_servers': servers_list.arr_extra_fields = [ I18n.lang('pastafari', 'choose_server', 'Choose server') ] servers_list.arr_extra_options = [server_update_options] servers_list.yes_search = False yes_form = 2 # Get tasks and put in select_task # Folders are tasks/ and modules/pastafari/tasks base_path = 'modules/pastafari/tasks' config_parser = configparser.ConfigParser() select_task = scandir(base_path, config_parser, OrderedDict(), 'tasks') #OrderedDict([('tasks', {'servers': [['Servers', 'tasks', 0]]}), ('servers', {'databases': [['Database servers', 'servers', 0]], 'mail': [['Mail', 'servers', 0], ['Standalone postfix server, Install on your servers a simple and secure postfix mail server', 'mail', 'modules/pastafari/tasks/servers/mail/postfix/standalone_postfix.py', 1]]})]) type_op = getpost.get['type'] if group_id > 0: servers_list.model.conditions[ 0] += ' AND id IN (select server_id from servergroupitem where group_id=%s)' servers_list.model.conditions[1].append(group_id) servers_list.fields_showed = ['hostname', 'ip', 'num_updates', 'date'] servers_list.limit_pages = 100 servers_list.s['order'] = '0' servers_list.s['order_field'] = 'hostname' show_servers = servers_list.show() return t.load_template('pastafari/admin/servers.phtml', show_servers=show_servers, type_op=type_op, yes_form=yes_form, csrf_token=csrf_token(), select_form_group=select_form_group, group_id=group_id, select_task=select_task) return ""
def net_cpu_status(server_id): if check_login(): s = get_session() conn = WebModel.connection() server = servers.Server(conn) arr_server = server.select_a_row(server_id) if arr_server: if 'ip' in arr_server: ip = arr_server['ip'] now = datetime.obtain_timestamp(datetime.now(True)) hours12 = now - 21600 date_now = datetime.timestamp_to_datetime(now) date_hours12 = datetime.timestamp_to_datetime(hours12) status_cpu = servers.StatusCpu(conn) status_cpu.set_conditions( 'where ip=%s and date>=%s and date<=%s', [ip, date_hours12, date_now]) #arr_cpu=status_cpu.select_to_array(['idle', 'date']) cur = status_cpu.select(['idle', 'date']) x = 0 arr_cpu = [] cur.fetchone() for cpu_info in cur: arr_cpu.append(cpu_info['idle']) cur.close() status_mem = servers.StatusMemory(conn) status_mem.set_conditions( 'where ip=%s and date>=%s and date<=%s', [ip, date_hours12, date_now]) #status_mem.set_order(['id', 'ASC']) #arr_mem=status_mem.select_to_array(['used', 'free', 'date']) arr_mem = [] with status_mem.select(['used', 'free', 'cached', 'date']) as cur: #cur.fetchone() for mem_info in cur: mem_info['used'] = ( (mem_info['used'] / 1024) / 1024) / 1024 mem_info['free'] = ( (mem_info['free'] / 1024) / 1024) / 1024 mem_info['cached'] = ( (mem_info['cached'] / 1024) / 1024) / 1024 arr_mem.append(mem_info) if len(arr_mem) > 2: arr_mem.pop(0) #arr_cpu=status_cpu.select_to_array(['idle', 'date']) cur = status_cpu.select(['idle', 'date']) arr_net = {} status_net = servers.StatusNet(conn) status_net.set_conditions( 'where ip=%s and date>=%s and date<=%s', [ip, date_hours12, date_now]) arr_net = [] cur = status_net.select(['bytes_sent', 'bytes_recv', 'date']) substract_time = 0 #datetime.obtain_timestamp(datetime.now()) c_hours12 = now c_elements = 0 c_count = cur.rowcount if c_count > 0: data_net = cur.fetchone() first_recv = data_net['bytes_recv'] first_sent = data_net['bytes_sent'] if len(arr_cpu) < (c_count - 1): arr_cpu.append(arr_cpu[1:]) for data_net in cur: timestamp = datetime.obtain_timestamp( data_net['date'], True) diff_time = timestamp - substract_time if substract_time != 0 and diff_time > 300: count_time = timestamp while substract_time <= count_time: form_time = datetime.timestamp_to_datetime( substract_time) arr_net.append( {'date': datetime.format_time(form_time)}) substract_time += 60 bytes_sent = round( (data_net['bytes_sent'] - first_sent) / 1024) bytes_recv = round( (data_net['bytes_recv'] - first_recv) / 1024) cpu = arr_cpu[x] memory_used = arr_mem[x]['used'] memory_free = arr_mem[x]['free'] memory_cached = arr_mem[x]['cached'] arr_net.append({ 'bytes_sent': bytes_sent, 'bytes_recv': bytes_recv, 'date': datetime.format_time(data_net['date']), 'cpu': cpu, 'memory_used': memory_used, 'memory_free': memory_free, 'memory_cached': memory_cached }) first_sent = data_net['bytes_sent'] first_recv = data_net['bytes_recv'] c_hours12 = timestamp substract_time = int(timestamp) c_elements += 1 x += 1 # If the last time is more little that now make a loop while c_hours12 <= now: form_time = datetime.timestamp_to_datetime(c_hours12) seconds = form_time[-2:] #print(form_time) if seconds == '00': arr_net.append( {'date': datetime.format_time(form_time)}) # if secons is 00 and z=1 put value #arr_net.append({'date': datetime.format_time(form_time)}) pass c_hours12 += 1 cur.close() if c_elements > 2: return filter_ajax(arr_net) else: return filter_ajax({}) return filter_ajax({}) return filter_ajax({})
def start(): parser = argparse.ArgumentParser( description= 'An daemon used for make a task in a server.The results are saved in a sql database' ) parser.add_argument('--task', help='The task to execute', required=True) parser.add_argument('--id', help='The id of server where the task must be done', required=True) parser.add_argument('--uuid', help='The uuid for identify this process in database', required=True) info = {} args = parser.parse_args() servertask = servers.ServerTask() servertask.create_forms() server = servers.Server() pid = getpid() #servertask.conditions=['where id=%s', [args.id]] arr_server = server.select_a_row(args.id) if arr_server == False: info = { 'ERROR': 1, 'CODE_ERROR': 4, 'TXT_ERROR': 'Error: server not exists', 'STATUS': 1, 'PROGRESS': 100 } servertask.insert({ 'task': args.task, 'uuid': args.uuid, 'status': 1, 'error': 1, 'pid': pid, 'info': json.dumps(info) }) exit(1) arr_server['ip'] = arr_server.get('ip', '') if arr_server['ip'] == '': info = { 'ERROR': 1, 'CODE_ERROR': 2, 'TXT_ERROR': 'Error: server not exists', 'STATUS': 1, 'PROGRESS': 100 } servertask.insert({ 'task': args.task, 'uuid': args.uuid, 'status': 1, 'error': 1, 'pid': pid, 'info': json.dumps(info) }) exit(1) ip = arr_server['ip'] try: server = str(ipaddress.ip_address(ip)) except: info = { 'ERROR': 1, 'CODE_ERROR': 1, 'TXT_ERROR': 'Error: ip not valid', 'STATUS': 1, 'PROGRESS': 100 } servertask.insert({ 'task': args.task, 'uuid': args.uuid, 'status': 1, 'error': 1, 'pid': pid, 'info': json.dumps(info) }) exit(1) # Prepare basic configuration home = getenv("HOME") if ConfigClass.public_key == '': ConfigClass.public_key = home + '/.ssh/id_rsa.pub' if ConfigClass.private_key == '': ConfigClass.private_key = home + '/.ssh/id_rsa' # Prepare task task_name = path.basename(args.task) task = args.task.replace('.', '_') task = args.task.replace('/', '.') #Seeking task path for p in ConfigClass.tasks_path: task_path = p + '.' + task + '.config' task_path_route = task_path.replace('.', '/') + '.py' #tpath=Path(task_path_route) if path.isfile(task_path_route): break try: config_task = import_module(task_path) except SyntaxError as e: info = { 'ERROR': 1, 'CODE_ERROR': 1, 'TXT_ERROR': 'Error: error in task: ' + str(e), 'STATUS': 1, 'PROGRESS': 100 } servertask.insert({ 'task': args.task, 'uuid': args.uuid, 'status': 1, 'error': 3, 'pid': pid, 'info': json.dumps(info) }) exit(1) except: info = { 'ERROR': 1, 'CODE_ERROR': 1, 'TXT_ERROR': 'Error: error in task: ' + traceback.format_exc(), 'STATUS': 1, 'PROGRESS': 100 } servertask.insert({ 'task': args.task, 'uuid': args.uuid, 'status': 1, 'error': 3, 'info': json.dumps(info) }) exit(1) # Load locals # Prepare ssh connection ssh = paramiko.SSHClient() ssh.load_system_host_keys() #Check if the unknown host keys are rejected or not if ConfigClass.deny_missing_host_key == False: ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #Prepare ssh keys rsa = prepare_ssh_keys(ConfigClass.password_key) if rsa == None: exit(1) try: ssh.connect(server, port=ConfigClass.port, username=ConfigClass.remote_user, password=None, pkey=rsa, key_filename=ConfigClass.private_key, timeout=None, allow_agent=True, look_for_keys=True, compress=False, sock=None, gss_auth=False, gss_kex=False, gss_deleg_creds=True, gss_host=None, banner_timeout=None) except paramiko.SSHException as e: info = { 'ERROR': 1, 'CODE_ERROR': 1, 'TXT_ERROR': 'Error: Cannot connect: ' + str(e), 'STATUS': 1, 'PROGRESS': 100 } servertask.insert({ 'task': args.task, 'uuid': args.uuid, 'status': 1, 'error': 1, 'pid': pid, 'info': json.dumps(info) }) exit(1) except paramiko.AuthenticationException as e: info = { 'ERROR': 1, 'CODE_ERROR': 1, 'TXT_ERROR': 'Error: Cannot connect: ' + str(e), 'STATUS': 1, 'PROGRESS': 100 } servertask.insert({ 'task': args.task, 'uuid': args.uuid, 'status': 1, 'error': 1, 'pid': pid, 'info': json.dumps(info) }) exit(1) except paramiko.BadHostKeyException as e: info = { 'ERROR': 1, 'CODE_ERROR': 1, 'TXT_ERROR': 'Error: Cannot connect: ' + str(e), 'STATUS': 1, 'PROGRESS': 100 } servertask.insert({ 'task': args.task, 'uuid': args.uuid, 'status': 1, 'error': 1, 'pid': pid, 'info': json.dumps(info) }) exit(1) except OSError as e: info = { 'ERROR': 1, 'CODE_ERROR': 1, 'TXT_ERROR': 'Error: Cannot connect: ' + str(e), 'STATUS': 1, 'PROGRESS': 100 } servertask.insert({ 'task': args.task, 'uuid': args.uuid, 'status': 1, 'error': 1, 'pid': pid, 'info': json.dumps(info) }) exit(1) except: info = { 'ERROR': 1, 'CODE_ERROR': 1, 'TXT_ERROR': 'Error: Cannot connect: ' + traceback.format_exc(), 'STATUS': 1, 'PROGRESS': 100 } servertask.insert({ 'task': args.task, 'uuid': args.uuid, 'status': 1, 'error': 1, 'pid': pid, 'info': json.dumps(info) }) exit(1) # Check that exists the script in server if len(ConfigTask.action) == 0: logging.warning( 'Error, check your task. The number of actions in task is zero') exit(1) # TODO Add external config for this task for action in ConfigTask.action: # Check if exists the files path_file = 'protozoo/scripts/' + arr_server[ 'os_codename'] + '/' + action.script_path stdin, stdout, stderr = ssh.exec_command('if [ ! -f "' + path_file + '" ] ; then false ; fi') if stdout.channel.recv_exit_status() > 0: # Uploading the files first time if not exists # TODO Use md5 hash for check the file. # print('File '+path_file+' doesn\'t exists') # Upload the script of this task try: stdin, stdout, stderr = ssh.exec_command( 'mkdir -p ' + path.dirname(path_file)) sftp = ssh.open_sftp() sftp.put(path_file, path_file) # TODO Upload extra files except: info = { 'ERROR': 1, 'CODE_ERROR': 5, 'TXT_ERROR': 'Error: Cannot connect: ' + traceback.format_exc(), 'STATUS': 1, 'PROGRESS': 100 } servertask.insert({ 'task': args.task, 'uuid': args.uuid, 'status': 1, 'pid': pid, 'error': 1, 'info': json.dumps(info) }) exit(1) # Obtain the script command action.script_interpreter = '' file_line = open(path_file) execute_line = file_line.readline() file_line.close() if execute_line.find("#!") == 0: action.script_interpreter = execute_line.replace('#!', '').strip() + ' ' #Execute the script command_to_execute = action.script_interpreter + path_file + " " + action.parameters try: #, get_pty=True stdin, stdout, stderr = ssh.exec_command(command_to_execute) for line in stdout: servertask.insert({ 'task': args.task, 'uuid': args.uuid, 'status': 1, 'pid': pid, 'error': 1, 'info': line }) for line in stderr: #logging.warning(action.codename+" WARNING: "+line) pass if stdout.channel.recv_exit_status() > 0: #line=stdout.readlines() #logging.warning(action.codename+" WARNING: "+line) info = { 'ERROR': 1, 'CODE_ERROR': 6, 'TXT_ERROR': 'Error: The script ' + action.codename + ' show an error: ' + traceback.format_exc(), 'STATUS': 1, 'PROGRESS': 100 } servertask.insert({ 'task': args.task, 'uuid': args.uuid, 'status': 1, 'pid': pid, 'error': 1, 'info': json.dumps(info) }) exit(1) except: info = { 'ERROR': 1, 'CODE_ERROR': 6, 'TXT_ERROR': 'Error: The script show an error: ' + traceback.format_exc(), 'STATUS': 1, 'PROGRESS': 100 } servertask.insert({ 'task': args.task, 'uuid': args.uuid, 'status': 1, 'pid': pid, 'error': 1, 'info': json.dumps(info) }) exit(1) ssh.close()
def getinfo(): if check_login(): connection = WebModel.connection() server = servers.Server(connection) status_disk = servers.StatusDisk(connection) status_net = servers.StatusNet(connection) status_cpu = servers.StatusCpu(connection) c = server.select_count() now = datetime.now(True) timestamp_now = datetime.obtain_timestamp(now) five_minutes = int(timestamp_now) - 300 five_minutes_date = datetime.timestamp_to_datetime(five_minutes) server.set_conditions('WHERE date<%s', [five_minutes_date]) c_down = server.select_count() server.set_conditions('WHERE num_updates>%s', [0]) c_updates = server.select_count() with status_disk.query( 'select sum(used) as total_used, sum(free) as total_free from statusdisk' ) as cur: arr_disk = cur.fetchone() status_disk.set_conditions('WHERE percent>%s', [85]) c_bloated_disk = status_disk.select_count() # Network use twelve_hours = int(timestamp_now) - 43200 twelve_hours_date = datetime.timestamp_to_datetime(twelve_hours) #status_net.set_conditions('WHERE date>%s', [twelve_hours_date]) #SELECT(t2.sub1 - t1.sub1) AS sub1, (t2.sub2 - t1.sub2) AS sub2 #FROM table t1 CROSS JOIN # table t2 #WHERE t1.date = '2014-11-08' AND t2.id = '2014-11-07'; # select (t1.bytes_sent+t2,bytes_sent) as bytes_sent from statusnet t1 CROSS JOIN statusnet t2 arr_net = {'total_bytes_recv': 0, 'total_bytes_sent': 0} #status_net.set_conditions('WHERE date>%s', [twelve_hours_date]) #status_net.set_order('date', 'ASC') #select bytes_sent, bytes_recv, ip from statusnet WHERE date>'20160606093229' and last_updated=1 group by ip status_net.set_conditions( 'WHERE date>%s and last_updated=1 group by ip', [twelve_hours_date]) with status_net.select(['bytes_recv', 'bytes_sent']) as cur: # I think that this loop can be optimized for net in cur: arr_net['total_bytes_recv'] += net['bytes_recv'] arr_net['total_bytes_sent'] += net['bytes_sent'] arr_cpu = {'0-30': 0, '30-70': 0, '70-100': 0} status_cpu.set_conditions( 'WHERE date>%s and last_updated=1 group by ip', [twelve_hours_date]) with status_cpu.select(['idle']) as cur: for cpu in cur: if cpu['idle'] > 70: arr_cpu['70-100'] += 1 elif cpu['idle'] > 30: arr_cpu['30-70'] += 1 else: arr_cpu['0-30'] += 1 #print(c_net) arr_json = { 'num_servers': c, 'num_servers_down': c_down, 'num_servers_updates': c_updates, 'disk_info': arr_disk, 'num_servers_bloated': c_bloated_disk, 'net_info': arr_net, 'cpu_info': arr_cpu } return arr_json else: redirect(make_url(config.admin_folder))
from paramecio.citoplasma.i18n import I18n from paramecio.citoplasma.urls import make_url from settings import config email_address = 'localhost' if hasattr(config, 'email_address'): email_address = config.email_address if not hasattr(config, 'email_notification'): print('You need an email address configured for notifications') exit(1) conn = WebModel.connection() server = servers.Server(conn) now = datetime.now(True) timestamp_now = datetime.obtain_timestamp(now) five_minutes = int(timestamp_now) - 300 five_minutes_date = datetime.timestamp_to_datetime(five_minutes) server.set_conditions('WHERE date<%s', [five_minutes_date]) arr_server = [] with server.select(['hostname']) as cur: for s in cur:
def info(token, ip): if config.api_key == token and config.api_key != '': now = datetime.now() GetPostFiles.obtain_post() servermodel = servers.Server() servermodel.conditions = ['WHERE ip=%s', [ip]] arr_server = servermodel.select_a_row_where() arr_server['id'] = arr_server.get('id', 0) if arr_server['id'] != 0: servernet = servers.ServerInfoNet() servercpu = servers.ServerInfoCPU() #{"device_info": {"eth0": [0, 0], "lo": [593521, 593521], "wlan0": [21354106, 376953085], "vboxnet0": [10847, 0]}, "cpu_info": 6.1} try: GetPostFiles.post['data_json'] = GetPostFiles.post.get( 'data_json', '') info = json.loads(GetPostFiles.post['data_json']) #print(info) if type(info).__name__ == 'dict': #print('pepe') info['cpu_info'] = info.get('cpu_info', 0) servercpu.create_forms() if servercpu.insert({ 'server': ip, 'cpu_use': info['cpu_info'], 'date': now }): print('Done') else: print(servercpu.fields_errors) info['net_info'] = info.get('net_info', {}) servernet.create_forms() for dev, data in info['net_info'].items(): servernet.insert({ 'server': ip, 'device': dev, 'network_up': data[0], 'network_down': data[1], 'date': now }) except: return 'Error' #for #print(GetPostFiles.post['data_json']) pass