예제 #1
0
def home():
    
    connection=WebModel.connection()
    #Fix, make local variable
    
    s=get_session()
    
    if check_login():
        
        getpostfiles=GetPostFiles()
        
        getpostfiles.obtain_get()

        parent_id=getpostfiles.get.get('parent_id', '0')
        
        parent_id=int(parent_id)
        
        #Load menu
        
        menu=get_menu(config_admin.modules_admin)
    
        lang_selected=get_language(s)
        
        groups=servers.ServerGroup(connection)
        
        groups.create_forms()
        
        groups.forms['parent_id'].default_value=parent_id
        
        hierarchy=HierarchyModelLinks(groups, 'All groups', 'name', 'parent_id', make_url('pastafari/groups'))
        
        hierarchy.prepare()
        
        group_list=GenerateAdminClass(groups, make_url(pastafari_folder+'/groups', {'parent_id': str(parent_id)}), t)
        
        groups.set_conditions('WHERE parent_id=%s', [parent_id])
        
        group_list.list.fields_showed=['name']
        
        group_list.list.arr_extra_options=[task_options]
        
        group_list.list.s['order']='0'
        group_list.list.s['order_field']='name'
        
        group_list.list.yes_search=False
        
        content_index=t.load_template('pastafari/groups.phtml', group_list=group_list, hierarchy_links=hierarchy, son_id=parent_id)
        #group_list.show()
        
        return t.load_template('admin/content.html', title=I18n.lang('pastafari', 'servers_groups', 'Server\'s Group'), content_index=content_index, menu=menu, lang_selected=lang_selected, arr_i18n=I18n.dict_i18n)
        
    else:
        redirect(make_url(config.admin_folder))
예제 #2
0
 def check(self, value):
     
     value=super().check(value)
     
     if self.model!=None:
         if self.model.updated==True:
             if self.model.name_field_id in self.model.post:
                 GetPostFiles.obtain_get()
                 
                 model_id=GetPostFiles.get.get(self.model.name_field_id, '0')
                 
                 if model_id==value:
                     self.error=True
                     self.txt_error='A field cannot be its own father'
                     value=0
                     return value
                     
             
     return value
예제 #3
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 ""
예제 #4
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 ""
예제 #5
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.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 ""
예제 #6
0
def gettasks():
    
    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()

        getpostfiles=GetPostFiles()

        getpostfiles.obtain_get()
        
        getpostfiles.get['op']=getpostfiles.get.get('op', '')
        getpostfiles.get['task_id']=getpostfiles.get.get('task_id', '0')
        getpostfiles.get['position']=getpostfiles.get.get('position', '0')
        getpostfiles.get['server']=getpostfiles.get.get('server', '')
        
        try:
        
            task_id=int(getpostfiles.get['task_id'])
        except:
            task_id=0
            
        try:
            position=int(getpostfiles.get['position'])
        except:
            position=0
        
        task=Task(conn)
        logtask=LogTask(conn)
        
        arr_task=task.select_a_row(task_id)
        
        t.show_basic_template=False
        
        arr_rows={'wait': 1}
        
        if arr_task:
            
            logtask.set_limit([position, 20])
                
            logtask.set_order({'id': 0})
            
            logtask.conditions=['WHERE task_id=%s', [task_id]]
            
            if getpostfiles.get['server']!='':
                logtask.conditions=['WHERE task_id=%s and logtask.server=%s', [task_id, getpostfiles.get['server']]]
                
            #logtask.set_limit([position, 1])
            
            #arr_row=logtask.select_a_row_where([], 1, position)
            
            logtask.yes_reset_conditions=False
            
            c=logtask.select_count()
            
            if c>0:
                
                arr_rows=[]
                
                with logtask.select([], True) as cursor:            
                    for arr_row in cursor:
                        arr_rows.append(arr_row)
                
            else:
                arr_rows=[]
                
                with logtask.set_conditions('WHERE task_id=%s and logtask.error=1', [task_id]).select([], True) as cursor:            
                    for arr_row in cursor:
                        arr_rows.append(arr_row)

                if arr_rows==0:
                    
                    arr_rows={'wait': 1}
                else:

                    with logtask.set_conditions('WHERE task_id=%s and logtask.server=%s and logtask.error=1', [task_id, getpostfiles.get['server']]).select([], True) as cursor:            
                        for arr_row in cursor:
                            arr_rows.append(arr_row)
                    
            """
                
                arr_rows=[]
                
                with logtask.select() as cursor:            
                    for arr_row in cursor:
                        arr_rows.append(arr_row)
                
                if len(arr_rows)==0:
                    logtask.set_limit([1])
                
                    logtask.set_order({'id': 0})
                    
                    logtask.conditions=['WHERE task_id=%s and status=1 and error=1  and server=""', [task_id]]
                    
                    if logtask.select_count('id', True)==0:
                        
                        if arr_task['status']=='0' or arr_task['status']==0:
                            conn.close()
                            return filter_ajax({'wait': 1})
                        else:
                            conn.close()
                            return filter_ajax({})
                    else:
                        
                        logtask.set_limit([1])
                    
                        logtask.set_order({'id': 0})
                        
                        logtask.conditions=['WHERE task_id=%s and status=1 and error=1  and server=""', [task_id]]
                        
                        arr_rows=logtask.select_to_array([], True)
                
                #response.set_header('Content-type', 'text/plain')
                conn.close()
                return filter_ajax(arr_rows)
                
            else:
                conn.close()
                return filter_ajax({'wait': 1})
            """
                    
        else:
            
            arr_task={'task_id': task_id, 'progress': 100, 'message': 'Error: no exists task', 'error': 1, 'status': 1}
            
        conn.close()
        
        return filter_ajax(arr_rows)

        
        
    else:

        redirect(make_url(config.admin_folder))
예제 #7
0
def home(server_id, task_id):
    
    connection=WebModel.connection()
    #Fix, make local variable
    
    t=PTemplate(env)
    
    s=get_session()
    
    if check_login():
                
        task=Task(connection)
        logtask=LogTask(connection)
        server=Server(connection)
        
        getpost=GetPostFiles() 
        
        getpost.obtain_get()
        
        #Load menu
            
        menu=get_menu(config_admin.modules_admin)
    
        lang_selected=get_language(s)           
        
        # server_id=getpost.get.get('id', 0)
        
        server_hostname=''
        
        arr_server=server.select_a_row(server_id)
        
        if arr_server:
            
            server_hostname=' - '+arr_server['hostname']
        
            ip=arr_server['ip']
            
            logtask.set_conditions('WHERE server=%s', [ip])
            
            logtask_list=SimpleList(logtask, make_url(pastafari_folder+'/serverslogs', {'id':str(server_id) }), t)
            
            logtask_list.limit_pages=100
            
            logtask_list.yes_search=False
            
            logtask_list.order_field='id'
            logtask_list.order=1
            
            logtask_list.fields_showed=['id', 'server', 'message', 'error', 'status']
            
            logtask_list.arr_extra_fields=[]
            logtask_list.arr_extra_options=[]
            
            return_url=make_url('pastafari/servers')
            
            content_index=t.load_template('pastafari/admin/logs_list.phtml', logtask_list=logtask_list, server=arr_server, return_url=return_url)
            #logtask_list.show()
            
            # Send request to server
                           
        else:
        
            content_index='Log no exists'
            
        connection.close()
        return t.load_template('admin/content.html', title='Servers log'+ server_hostname, content_index=content_index, menu=menu, lang_selected=lang_selected, arr_i18n=I18n.dict_i18n)
        
    else:
        connection.close()
        redirect(make_url(config.admin_folder))
예제 #8
0
def home():
    
    connection=WebModel.connection()
    #Fix, make local variable
    
    t=PTemplate(env)
    
    getpost=GetPostFiles()            
    
    getpost.obtain_get()
    
    try:
        group_id=int(getpost.get.get('group_id', '0'))
    except:
        group_id=0

    if request_type()=='POST':
        getpost.obtain_post()
    
    s=get_session()
    
    if 'login' in s:
        
        if s['privileges']==2:
            
            task=Task(connection)
            logtask=LogTask(connection)
            server=Server(connection)
            
            #Load menu
            
            menu=get_menu(config_admin.modules_admin)
        
            lang_selected=get_language(s)
            
            content_index=''
            
            # Send request to server
            
            arr_servers=[]
            
            where_sql='WHERE num_updates>0'
            
            for server_id in getpost.post.values():
                
                try:
                    server_id=int(server_id)
                    
                    if server_id>0:
                        arr_servers.append(str(server_id))
                except:
                    pass
            
            if len(arr_servers)>0:
                where_sql='WHERE id IN (%s)' % ",".join(arr_servers)
            
            if group_id>0:
                where_sql+=' AND id IN (select server_id from servergroupitem where group_id='+str(group_id)+')'
            
            commands_to_execute=[['bin/upgrade.sh', '']]
            
            task.create_forms()
            
            if task.insert({'name_task': 'update_server','description_task': I18n.lang('pastafari', 'update_servers', 'Updating servers...'), 'url_return': '', 'commands_to_execute': commands_to_execute, 'server': '', 'where_sql_server': where_sql}):
                                                
                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):
                        
                        content_index="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..."
                    else:
                        
                        server.set_conditions(where_sql, [])
                        
                        num_servers=server.select_count()
                        
                        #content_index=t.load_template('pastafari/updates.phtml', task_id=task_id, title_task=I18n.lang('pastafari', 'servers_updating', 'Servers updating'), num_servers=num_servers)
                        content_index=t.load_template('pastafari/updates.phtml', task_id=task_id, title_task=I18n.lang('pastafari', 'servers_updating', 'Servers updating'), description_task=I18n.lang('pastafari', 'servers_updating_txt', 'All servers selected will be updated...'), num_servers=num_servers)
                
                except:
                    
                    #logtask.conditions=['WHERE id=%s', [task_id]]
                    
                    task.update({'status': 1, 'error': 1})
                    
                    content_index="Error:cannot connect to task server, check the url for it..."+traceback.format_exc()
                    
            else:
                content_index="Error: cannot insert the task: "+task.show_errors()

            return t.load_template('admin/content.html', title='Updating servers', content_index=content_index, menu=menu, lang_selected=lang_selected, arr_i18n=I18n.dict_i18n)
            
        else:
            redirect(make_url(config.admin_folder))
    
    else:
    
        redirect(make_url(config.admin_folder))
예제 #9
0
    def check_all_fields(self, dict_values, external_agent, yes_update=False, errors_set="insert"):
        
        error=0
        
        try:
            
            fields, values, update_values=super().check_all_fields(dict_values, external_agent, yes_update, errors_set)
        except: 
            
            error+=1
        
        if self.check_user==True:
        
            # Check if passwords matches
            
            if self.password_field in dict_values:
                
                dict_values['repeat_password']=dict_values.get('repeat_password', '')
                
                if dict_values[self.password_field].strip()!="":
                
                    if dict_values['repeat_password']!=dict_values[self.password_field]:
                        
                        self.fields[self.password_field].error=True
                        self.fields[self.password_field].txt_error=I18n.lang('common', 'error_passwords_no_match', 'Error: passwords doesn\'t match')
                    
                        error+=1

            # Check if exists user with same email or password
            
            get_id=0
            
            if self.updated:
                # Need the id
                #GetPostFiles.obtain_get()
                #GetPostFiles.obtain_post()
                
                getpostfiles=GetPostFiles()
                
                getpostfiles.obtain_get()
                
                get_id=getpostfiles.get.get(self.name_field_id, '0')
                
                post_id=getpostfiles.post.get(self.name_field_id, '0')
                
                if get_id!='0':
                    get_id=int(get_id)
                
                if post_id!='0':
                    get_id=int(post_id)
                
                pass
            
            get_id=int(get_id)
            
            sql_id=''
            
            original_conditions=self.conditions
            
            self.reset_conditions()
            
            if self.username_field in dict_values:
            
                self.conditions=['WHERE (username=%s', [dict_values[self.username_field]]]

            
            if self.email_field in dict_values:
            
                if len(self.conditions[1])>0:
            
                    self.conditions[0]+=' OR email=%s)'
                else:
                    self.conditions[0]='WHERE (email=%s)'
                    self.conditions[1]=[]
            
                self.conditions[1].append([dict_values[self.email_field]])
            
            if get_id>0:
                
                self.conditions[0]+=' AND '+self.name_field_id+'!=%s'
                self.conditions[1].append(get_id)
            
            
            if self.select_count()>0:
                
                self.fields[self.username_field].error=True
                self.fields[self.username_field].txt_error=I18n.lang('common', 'error_username_or_password_exists', 'Error: username or email exists in database')
                
                error+=1
            
            self.conditions=original_conditions

        if error>0:
            self.query_error+='Error:if is not expected, please, check that you disabled the special checkings of this model'
            return False

        return fields, values, update_values
예제 #10
0
def home():

    connection = WebModel.connection()
    #Fix, make local variable

    s = get_session()

    if check_login():

        getpostfiles = GetPostFiles()

        getpostfiles.obtain_get()

        parent_id = getpostfiles.get.get('parent_id', '0')

        parent_id = int(parent_id)

        #Load menu

        menu = get_menu(config_admin.modules_admin)

        lang_selected = get_language(s)

        groups = servers.ServerGroup(connection)

        groups.create_forms()

        groups.forms['parent_id'].default_value = parent_id

        hierarchy = HierarchyModelLinks(groups, 'All groups', 'name',
                                        'parent_id',
                                        make_url('pastafari/groups'))

        hierarchy.prepare()

        group_list = GenerateAdminClass(
            groups,
            make_url(pastafari_folder + '/groups',
                     {'parent_id': str(parent_id)}), t)

        groups.set_conditions('WHERE parent_id=%s', [parent_id])

        group_list.list.fields_showed = ['name']

        group_list.list.arr_extra_options = [task_options]

        group_list.list.s['order'] = '0'
        group_list.list.s['order_field'] = 'name'

        group_list.list.yes_search = False

        content_index = t.load_template('pastafari/groups.phtml',
                                        group_list=group_list,
                                        hierarchy_links=hierarchy,
                                        son_id=parent_id)
        #group_list.show()

        return t.load_template('admin/content.html',
                               title=I18n.lang('pastafari', 'servers_groups',
                                               'Server\'s Group'),
                               content_index=content_index,
                               menu=menu,
                               lang_selected=lang_selected,
                               arr_i18n=I18n.dict_i18n)

    else:
        redirect(make_url(config.admin_folder))
예제 #11
0
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)
예제 #12
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.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 ""
예제 #13
0
    def show(self):
        
        getpostfiles=GetPostFiles()
        
        getpostfiles.obtain_get()
        
        getpostfiles.get['op_admin']=getpostfiles.get.get('op_admin', '0')
        
        getpostfiles.get['id']=getpostfiles.get.get('id', '0')
        
        if len(self.model.forms)==0:

            self.model.create_forms()
        
        edit_forms=OrderedDict()
            
        for key_form in self.arr_fields_edit:
            edit_forms[key_form]=self.model.forms[key_form]
        
        if getpostfiles.get['op_admin']=='1':
            
            post=None
            
            title_edit=I18n.lang('common', 'add_new_item', 'Add new item')
            
            if getpostfiles.get['id']!='0':
                post=self.model.select_a_row(getpostfiles.get['id'], [], True)
                title_edit=I18n.lang('common', 'edit_new_item', 'Edit item')
            
            if post==None or post==False:
                
                if getpostfiles.get['id']=='0':
                    post={}
                else:
                    return ""
            
            form=show_form(post, edit_forms, self.t, False)
                
            return self.t.load_template(self.template_insert, admin=self, title_edit=title_edit, form=form, model=self.model, id=getpostfiles.get['id'])
        
        elif getpostfiles.get['op_admin']=='2':
            
            getpostfiles.obtain_post()
            
            #post=getpostfiles.post
            
            self.model.reset_conditions()
            
            insert_row=self.model.insert
            
            try:
                
                getpostfiles.get['id']=str(int(getpostfiles.get['id']))
            
            except:
                
                getpostfiles.get['id']='0'
            
            title_edit=I18n.lang('common', 'add_new_item', 'Add new item')
                
            
            if getpostfiles.get['id']!='0':
                insert_row=self.model.update
                title_edit=I18n.lang('common', 'edit_new_item', 'Edit item')
                self.model.conditions=['WHERE `'+self.model.name+'`.`'+self.model.name_field_id+'`=%s', [getpostfiles.get['id']]]
            
            if insert_row(getpostfiles.post):
                set_flash_message(I18n.lang('common', 'task_successful', 'Task successful'))
                redirect(self.url)
            else:
                form=show_form(getpostfiles.post, edit_forms, self.t, True)
                return self.t.load_template(self.template_insert, admin=self, title_edit=title_edit, form=form, model=self.model, id=getpostfiles.get['id'])

            
            pass
            
        elif getpostfiles.get['op_admin']=='3':
            
            verified=getpostfiles.get.get('verified', '0')
            
            if verified=='1':
    
                if getpostfiles.get['id']!='0':
                    self.model.conditions=['WHERE `'+self.model.name+'`.`'+self.model.name_field_id+'`=%s', [getpostfiles.get['id']]]
                    self.model.delete()
                    set_flash_message(I18n.lang('common', 'task_successful', 'Task successful'))
                    redirect(self.url)
    
            else:
                
                return self.t.load_template(self.template_verify_delete, url=self.url, item_id=getpostfiles.get['id'], op_admin=3, verified=1)
    
        else:
            return self.t.load_template(self.template_admin, admin=self)
예제 #14
0
def admin(**args):

    conn = args['connection']

    t = args['t']

    getpostfiles = GetPostFiles()

    getpostfiles.obtain_get()

    getpostfiles.get['op'] = getpostfiles.get.get('op', '')
    getpostfiles.get['task_id'] = getpostfiles.get.get('task_id', '0')
    getpostfiles.get['position'] = getpostfiles.get.get('position', '0')
    getpostfiles.get['server'] = getpostfiles.get.get('server', '')

    try:

        task_id = int(getpostfiles.get['task_id'])
    except:
        task_id = 0

    try:
        position = int(getpostfiles.get['position'])
    except:
        position = 0

    task = tasks.Task(conn)
    tasklog = tasks.LogTask(conn)

    arr_task = task.select_a_row(task_id)

    t.show_basic_template = False

    if arr_task:

        tasklog.set_limit([position, 20])

        tasklog.set_order(['id'], ['ASC'])

        tasklog.conditions = ['WHERE task_id=%s', [task_id]]

        if getpostfiles.get['server'] != '':
            tasklog.conditions = [
                'WHERE task_id=%s and logtask.server=%s',
                [task_id, getpostfiles.get['server']]
            ]

        #tasklog.set_limit([position, 1])

        #arr_row=tasklog.select_a_row_where([], 1, position)

        tasklog.yes_reset_conditions = False

        c = tasklog.select_count()

        if c > 0:

            arr_rows = []

            cursor = tasklog.select()

            for arr_row in cursor:
                arr_rows.append(arr_row)

            if len(arr_rows) == 0:
                tasklog.set_limit([1])

                tasklog.set_order(['id'], ['ASC'])

                tasklog.conditions = [
                    'WHERE task_id=%s and status=1 and error=1  and server=""',
                    [task_id]
                ]

                if tasklog.select_count('id', True) == 0:

                    if arr_task['status'] == '0' or arr_task['status'] == 0:
                        return {'wait': 1}
                    else:
                        return {}
                else:

                    tasklog.set_limit([1])

                    tasklog.set_order(['id'], ['ASC'])

                    tasklog.conditions = [
                        'WHERE task_id=%s and status=1 and error=1  and server=""',
                        [task_id]
                    ]

                    arr_rows = tasklog.select_to_array([], True)

            response.set_header('Content-type', 'text/plain')

            return json.dumps(arr_rows)

        else:
            return {'wait': 1}

    else:
        return {}
예제 #15
0
def admin(**args):

    conn=args['connection']

    t=args['t']

    getpostfiles=GetPostFiles()

    getpostfiles.obtain_get()
    
    getpostfiles.get['op']=getpostfiles.get.get('op', '')
    getpostfiles.get['task_id']=getpostfiles.get.get('task_id', '0')
    getpostfiles.get['position']=getpostfiles.get.get('position', '0')
    getpostfiles.get['server']=getpostfiles.get.get('server', '')
    
    try:
    
        task_id=int(getpostfiles.get['task_id'])
    except:
        task_id=0
        
    try:
        position=int(getpostfiles.get['position'])
    except:
        position=0
    
    task=tasks.Task(conn)
    tasklog=tasks.LogTask(conn)
    
    arr_task=task.select_a_row(task_id)
    
    t.show_basic_template=False
    
    if arr_task:
        
        tasklog.set_limit([position, 20])
            
        tasklog.set_order(['id'], ['ASC'])
        
        tasklog.conditions=['WHERE task_id=%s', [task_id]]
        
        if getpostfiles.get['server']!='':
            tasklog.conditions=['WHERE task_id=%s and logtask.server=%s', [task_id, getpostfiles.get['server']]]
            
        #tasklog.set_limit([position, 1])
        
        #arr_row=tasklog.select_a_row_where([], 1, position)
        
        tasklog.yes_reset_conditions=False
        
        c=tasklog.select_count()
        
        if c>0:
            
            arr_rows=[]
            
            cursor=tasklog.select()
            
            for arr_row in cursor:
                arr_rows.append(arr_row)
            
            if len(arr_rows)==0:
                tasklog.set_limit([1])
            
                tasklog.set_order(['id'], ['ASC'])
                
                tasklog.conditions=['WHERE task_id=%s and status=1 and error=1  and server=""', [task_id]]
                
                if tasklog.select_count('id', True)==0:
                    
                    if arr_task['status']=='0' or arr_task['status']==0:
                        return {'wait': 1}
                    else:
                        return {}
                else:
                    
                    tasklog.set_limit([1])
                
                    tasklog.set_order(['id'], ['ASC'])
                    
                    tasklog.conditions=['WHERE task_id=%s and status=1 and error=1  and server=""', [task_id]]
                    
                    arr_rows=tasklog.select_to_array([], True)
            
            response.set_header('Content-type', 'text/plain')
            
            return json.dumps(arr_rows)
            
        else:
            return {'wait': 1}
                
                
    else:
        return {}
예제 #16
0
def home(server_id, task_id):

    connection = WebModel.connection()
    #Fix, make local variable

    t = PTemplate(env)

    s = get_session()

    if check_login():

        task = Task(connection)
        logtask = LogTask(connection)
        server = Server(connection)

        getpost = GetPostFiles()

        getpost.obtain_get()

        #Load menu

        menu = get_menu(config_admin.modules_admin)

        lang_selected = get_language(s)

        # server_id=getpost.get.get('id', 0)

        server_hostname = ''

        arr_server = server.select_a_row(server_id)

        if arr_server:

            server_hostname = ' - ' + arr_server['hostname']

            ip = arr_server['ip']

            logtask.set_conditions('WHERE server=%s', [ip])

            logtask_list = SimpleList(
                logtask,
                make_url(pastafari_folder + '/serverslogs',
                         {'id': str(server_id)}), t)

            logtask_list.limit_pages = 100

            logtask_list.yes_search = False

            logtask_list.order_field = 'id'
            logtask_list.order = 1

            logtask_list.fields_showed = [
                'id', 'server', 'message', 'error', 'status'
            ]

            logtask_list.arr_extra_fields = []
            logtask_list.arr_extra_options = []

            return_url = make_url('pastafari/servers')

            content_index = t.load_template('pastafari/admin/logs_list.phtml',
                                            logtask_list=logtask_list,
                                            server=arr_server,
                                            return_url=return_url)
            #logtask_list.show()

            # Send request to server

        else:

            content_index = 'Log no exists'

        connection.close()
        return t.load_template('admin/content.html',
                               title='Servers log' + server_hostname,
                               content_index=content_index,
                               menu=menu,
                               lang_selected=lang_selected,
                               arr_i18n=I18n.dict_i18n)

    else:
        connection.close()
        redirect(make_url(config.admin_folder))
예제 #17
0
def admin_task(connection, t, s, **args):
    
    task=Task(connection)
    
    getpost=GetPostFiles()            
    
    getpost.obtain_get()
    
    if request_type()=='POST':
        getpost.obtain_post()

    #Load task worker
    
    task_first, task_path=checktask(getpost.post, connection)
    
    if task_first is not None:
        
        post={'name_task': task_first.name_task, 'description_task': task_first.description_task, 'codename_task': task_first.codename_task, 'files': task_first.files, 'commands_to_execute': task_first.commands_to_execute, 'delete_files': task_first.delete_files, 'delete_directories': task_first.delete_directories, 'url_return': task_first.url_return, 'one_time': task_first.one_time, 'version': task_first.one_time}
            
        #post_task={'task': getpost.post['task']}
        
        where_sql_server='WHERE 1=1'
        
        post_task=[]
        
        try:
            group_id=int(getpost.get.get('group_id', '0'))
            
        except:
            group_id=0
            
        if group_id>0:
            
            where_sql_server+=' AND id IN (select server_id from servergroupitem where group_id='+str(group_id)+')'
        
        pattern=re.compile('^server_.*$')
            
        for k, server_id in getpost.post.items():
            
            if pattern.match(k):
                
                try:
                    server_id=int(server_id)
                    
                    if server_id>0:
                        
                        post_task.append(str(server_id))
                except:
                    pass
        
        #Create where
        if len(post_task)>0:
            where_sql_server+=' AND id IN ('+','.join(post_task)+')'
            
        post['where_sql_server']=where_sql_server
        
        #Create pàth
        
        post['path']=task_path
        
        #Insert task
        
        task.create_forms()
        
        if task.insert(post):
        
            task_id=task.insert_id()
        
            if task_first.yes_form:
                
                redirect(make_url('pastafari/formtask/'+str(task_id)))
                
            else:
                redirect(make_url('pastafari/executetask/'+str(task_id)))
        else:
            print(task.show_errors())
            return "Error: cannot insert the task"
예제 #18
0
def gettasks():

    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()

        getpostfiles = GetPostFiles()

        getpostfiles.obtain_get()

        getpostfiles.get['op'] = getpostfiles.get.get('op', '')
        getpostfiles.get['task_id'] = getpostfiles.get.get('task_id', '0')
        getpostfiles.get['position'] = getpostfiles.get.get('position', '0')
        getpostfiles.get['server'] = getpostfiles.get.get('server', '')

        try:

            task_id = int(getpostfiles.get['task_id'])
        except:
            task_id = 0

        try:
            position = int(getpostfiles.get['position'])
        except:
            position = 0

        task = Task(conn)
        logtask = LogTask(conn)

        arr_task = task.select_a_row(task_id)

        t.show_basic_template = False

        arr_rows = {'wait': 1}

        if arr_task:

            logtask.set_limit([position, 20])

            logtask.set_order({'id': 0})

            logtask.conditions = ['WHERE task_id=%s', [task_id]]

            if getpostfiles.get['server'] != '':
                logtask.conditions = [
                    'WHERE task_id=%s and logtask.server=%s',
                    [task_id, getpostfiles.get['server']]
                ]

            #logtask.set_limit([position, 1])

            #arr_row=logtask.select_a_row_where([], 1, position)

            logtask.yes_reset_conditions = False

            c = logtask.select_count()

            if c > 0:

                arr_rows = []

                with logtask.select([], True) as cursor:
                    for arr_row in cursor:
                        arr_rows.append(arr_row)

            else:
                arr_rows = []

                with logtask.set_conditions(
                        'WHERE task_id=%s and logtask.error=1',
                    [task_id]).select([], True) as cursor:
                    for arr_row in cursor:
                        arr_rows.append(arr_row)

                if arr_rows == 0:

                    arr_rows = {'wait': 1}
                else:

                    with logtask.set_conditions(
                            'WHERE task_id=%s and logtask.server=%s and logtask.error=1',
                        [task_id, getpostfiles.get['server']]).select(
                            [], True) as cursor:
                        for arr_row in cursor:
                            arr_rows.append(arr_row)
            """
                
                arr_rows=[]
                
                with logtask.select() as cursor:            
                    for arr_row in cursor:
                        arr_rows.append(arr_row)
                
                if len(arr_rows)==0:
                    logtask.set_limit([1])
                
                    logtask.set_order({'id': 0})
                    
                    logtask.conditions=['WHERE task_id=%s and status=1 and error=1  and server=""', [task_id]]
                    
                    if logtask.select_count('id', True)==0:
                        
                        if arr_task['status']=='0' or arr_task['status']==0:
                            conn.close()
                            return filter_ajax({'wait': 1})
                        else:
                            conn.close()
                            return filter_ajax({})
                    else:
                        
                        logtask.set_limit([1])
                    
                        logtask.set_order({'id': 0})
                        
                        logtask.conditions=['WHERE task_id=%s and status=1 and error=1  and server=""', [task_id]]
                        
                        arr_rows=logtask.select_to_array([], True)
                
                #response.set_header('Content-type', 'text/plain')
                conn.close()
                return filter_ajax(arr_rows)
                
            else:
                conn.close()
                return filter_ajax({'wait': 1})
            """

        else:

            arr_task = {
                'task_id': task_id,
                'progress': 100,
                'message': 'Error: no exists task',
                'error': 1,
                'status': 1
            }

        conn.close()

        return filter_ajax(arr_rows)

    else:

        redirect(make_url(config.admin_folder))
예제 #19
0
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)
예제 #20
0
class SimpleList:
    
    def __init__(self, model, url, t):
        
        self.raw_query=True
        
        self.getpostfiles=GetPostFiles()
            
        self.getpostfiles.obtain_get()
        
        self.t=t
        
        self.model=model
        
        #if len(self.model.forms)==0:
        
            #self.model.create_forms()
        
        self.fields=model.fields.keys()
        
        self.fields_showed=self.fields
        
        self.url=url
        
        self.limit_pages=20
        
        self.order_defaults=['ASC', 'DESC']
        
        self.order_class=['down', 'up']
        
        self.s=get_session()
        
        #clean session
        
        self.s['order']='0'
        
        self.s['order_field']=self.model.name_field_id
        
        self.order_by=self.order_defaults[0]
        
        self.change_order={}
        
        self.yes_search=True
        
        self.search_text=''
        
        self.initial_num_pages=20
        
        request.query.get('begin_page', '0')
        
        try: 
        
            self.begin_page=int(request.query.begin_page)
            
        except ValueError:
            self.begin_page=0
        
        if self.begin_page<0:
            self.begin_page=0
        
        self.search_fields=self.fields
        
        #self.yes_options=True
        
        self.arr_extra_fields=[I18n.lang('common', 'options', 'Options')]
        
        self.arr_extra_options=[SimpleList.standard_options]
        
        self.jln='<br />'

    def restore_fields(self):
        self.fields=self.model.fields.keys()
    
    def obtain_order(self):
        
        self.s['order']=self.getpostfiles.get.get('order', self.s['order'])
        
        order_k=int(self.s['order'])
        
        #Obtain from get
        """
        if 'order' in request.query.keys():
        """
        #order_k=int(request.query.get('order', 0))
        
        if order_k>1 or order_k<0:
            order_k=0
        
        self.order_by=self.order_defaults[ order_k ]
        
        self.s['order']=order_k
    
    def obtain_field_search(self):
        
        self.s['order_field']=self.getpostfiles.get.get('order_field', self.s['order_field'])
        
        field_k=self.s['order_field']
        
        """
        if 'order_field' in request.query.keys():
            field_k=request.query.order_field
        """
        
        if field_k in self.model.fields.keys():
            
            self.s['order_field']=field_k
        
        for field in self.fields:
            
            #Initialize foreignkeyfield fields too
            
            if type(self.model.fields[field]).__name__=='ForeignKeyField':
                name_related=self.model.fields[field].related_model.name
                for k in self.model.fields[field].related_model.fields.keys():
                    self.change_order[field+'_'+k]=self.s['order']
            
            self.change_order[field]=self.s['order']
        
        if self.s['order']==0:
            self.change_order[field_k]=1
        else:
            self.change_order[field_k]=0
        
        self.order_field=self.s['order_field']
        
    def search(self):
        
        self.getpostfiles.get['search_text']=self.getpostfiles.get.get('search_text', '')
        
        self.search_text=self.getpostfiles.get['search_text']
        
        self.search_text=self.search_text.replace('"', '&quot;')
        
        #self.model.conditions='AND 
        
        self.search_field=self.getpostfiles.get.get('search_field', '')
        
        if self.search_field not in self.model.fields.keys():
            self.search_field=''
        
        if self.search_field!='' and self.search_text!='':
            self.model.conditions[0]+=' AND '+self.search_field+' LIKE %s'
            self.model.conditions[1].append('%'+self.search_text+'%')
        
        pass
    
    def set_options(self, options_func, arr_row):
        #SimpleList.standard_options(arr_row)
        return self.jln.join(options_func(self.url, arr_row[self.model.name_field_id], arr_row)) 
    
    @staticmethod
    def standard_options(url, id, arr_row):
        options=[]
        options.append('<a href="'+add_get_parameters(url, op_admin=1, id=id)+'">'+I18n.lang('common', 'edit', 'Edit')+'</a>')
        options.append('<a href="'+add_get_parameters(url, op_admin=3, id=id)+'">'+I18n.lang('common', 'delete', 'Delete')+'</a>')
        return options
    
    def show(self):
        
        self.model.yes_reset_conditions=False
        
        self.obtain_order()
        
        self.obtain_field_search()
        
        self.search()
        
        total_elements=self.model.select_count()
        
        num_elements=self.limit_pages
        
        link=add_get_parameters(self.url, search_text=self.search_text, search_field=self.search_field, order=self.s['order'])
        
        begin_page=self.begin_page
        
        self.model.order_by='order by '+self.order_field+' '+self.order_by
        
        self.model.limit='limit '+str(begin_page)+','+str(self.limit_pages)
        
        list_items=self.model.select(self.fields, self.raw_query)
        
        #print(self.model.fields.keys())
        
        pages=Pages.show( begin_page, total_elements, num_elements, link ,initial_num_pages=self.initial_num_pages, variable='begin_page', label='', func_jscript='')
        
        self.begin_page=str(self.begin_page)
        
        self.model.yes_reset_conditions=True
        
        listing=self.t.load_template('utils/list.phtml', simplelist=self, list=list_items, pages=pages)
        
        list_items.close()
        
        return listing