def show_formatted(self, value): now=datetime.now(gmt=True) timestamp_now=datetime.obtain_timestamp(now) timestamp_value=datetime.obtain_timestamp(value) five_minutes=int(timestamp_now)-300 if timestamp_value<five_minutes: return '<img src="'+make_media_url_module('images/status_red.png', 'pastafari')+'" />' else: return '<img src="'+make_media_url_module('images/status_green.png', 'pastafari')+'" />'
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 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 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
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))
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.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 post(ip, api_key): getpost=GetPostFiles() if config_task.api_key==api_key: conn=WebModel.connection() server=servers.Server(conn) data_server=servers.DataServer(conn) ipcheck=IpField('', '') ip=ipcheck.check(ip) server.set_conditions('WHERE ip=%s', [ip]) now=datetime.now() c=server.select_count() if ipcheck.error!=True and c>0: status_disk=servers.StatusDisk(conn) status_net=servers.StatusNet(conn) status_cpu=servers.StatusCpu(conn) status_mem=servers.StatusMemory(conn) server.set_conditions('where ip=%s', [ip]) server.yes_reset_conditions=False arr_server=server.select_a_row_where(['id']) if arr_server: arr_update={'status': 1, 'monitoring':1, 'date': now} getpost.obtain_post(['data_json'], True) try: arr_info=json.loads(getpost.post['data_json']) except: return 'Ouch' server_id=arr_server['id'] net_id=None memory_id=None cpu_id=None arr_disk_id=[] if 'net_info' in arr_info: net_info=arr_info['net_info'] if type(net_info).__name__=='list': post={'bytes_sent': net_info[0], 'bytes_recv': net_info[1], 'errin': net_info[2], 'errout': net_info[3], 'dropin': net_info[4], 'dropout': net_info[5], 'date': now, 'ip': ip, 'last_updated': 1} status_net.reset_require() status_net.create_forms() status_net.set_order({'id': 1}) status_net.set_limit([1]) status_net.set_conditions('WHERE ip=%s', [ip]) status_net.update({'last_updated': 0}) status_net.insert(post) net_id=status_net.insert_id() if 'mem_info' in arr_info: mem_info=arr_info['mem_info'] if type(mem_info).__name__=='list': #svmem(total=518418432, available=413130752, percent=20.3, used=208052224, free=310366208, active=137457664, inactive=40919040, buffers=20692992, cached=82071552, shared=4820992) post={'total': mem_info[0], 'available': mem_info[1], 'percent': mem_info[2], 'used': mem_info[3], 'free': mem_info[4], 'active': mem_info[5], 'inactive': mem_info[6], 'buffers': mem_info[7], 'cached': mem_info[8], 'shared': mem_info[9], 'date': now, 'ip': ip, 'last_updated': 1} status_mem.reset_require() status_mem.create_forms() status_mem.set_order({'id': 1}) status_mem.set_limit([1]) status_mem.set_conditions('WHERE ip=%s', [ip]) status_mem.update({'last_updated': 0}) status_mem.insert(post) memory_id=status_mem.insert_id() if 'cpu_idle' in arr_info: status_cpu.reset_require() status_cpu.create_forms() status_cpu.set_order({'id': 1}) status_cpu.set_limit([1]) status_cpu.set_conditions('WHERE ip=%s', [ip]) status_cpu.update({'last_updated': 0}) status_cpu.insert({'ip': ip, 'idle': arr_info['cpu_idle'], 'date': now, 'last_updated': 1, 'num_cpu': arr_info['cpu_number']}) arr_update['actual_idle']=arr_info['cpu_idle'] cpu_id=status_cpu.insert_id() # Need optimitation if 'disks_info' in arr_info: status_disk.create_forms() status_disk.set_conditions('WHERE ip=%s', [ip]) method_update=status_disk.insert if status_disk.select_count()>0: method_update=status_disk.update for disk, data in arr_info['disks_info'].items(): status_disk.set_conditions('where ip=%s and disk=%s', [ip, disk]) method_update({'ip' : ip, 'disk' : disk, 'date' : now, 'size' : data[0], 'used' : data[1], 'free' : data[2], 'percent' : data[3]}) status_disk.set_conditions('where ip=%s and disk=%s', [ip, disk]) arr_disk_id=status_disk.select_to_array(['id'], True) #Save status server.reset_require() server.create_forms() server.update(arr_update) # Save middle table for all statuses of a server data_server.create_forms() post={'server_id': server_id, 'net_id': net_id, 'memory_id': memory_id, 'cpu_id': cpu_id, 'ip': ip, 'date': now } z=0 final_id=0 for disk_id in arr_disk_id: if z<6: post['disk'+str(z)+'_id']=disk_id['id'] z+=1 #for z in range(z, 6): #post['disk'+str(z)+'_id']=final_id data_server.yes_reset_conditions=False method_data_update=data_server.insert data_server.set_conditions('where server_id=%s', [server_id]) if data_server.select_count()>0: method_data_update=data_server.update method_data_update(post) return 'Ok' return "Ouch"
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 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 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 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: arr_server.append(s['hostname'])
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
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({})