def agent_panel(request, agent_id):
    if 'user_id' not in request.session.keys():
        return redirect('/')

    res = get_manager_response({'type': 2, 'content': {'agentId': agent_id, 'userId': request.session['user_id']}})

    if res['type'] == 1:
        return redirect('/panel')
    elif res['type'] != 0:
        return HttpResponse('Agent not found', content_type='text/plain')

    context = {
        'data': res['content']['data']
    }

    res = get_manager_response({'type': 5, 'content': {'agentId': agent_id, 'userId': request.session['user_id']}})

    if res['type'] == 0:
        context['agent_name'] = res['content']['agent']['name']
        context['agent_id'] = res['content']['agent']['id']

    res = get_manager_response({'type': 5, 'content': {'agentId': agent_id, 'userId': request.session['user_id']}})

    if res['type'] == 0:
        pass

    res = get_manager_response({'type': 11, 'content': {'userId': request.session['user_id']}})

    if res['type'] == 0:
        context['name'] = res['content']['user']['name']
    else:
        context['name'] = 'usuário'

    return render(request, 'core/agent_panel.html', context)
def results(request):
    if 'user_id' not in request.session.keys():
        return redirect('/')

    if request.GET['agent'] == '-1':
        return redirect('/records')

    context = {
        'begin_date': request.GET['begin-date'],
        'end_date': request.GET['end-date'],
    }

    res = get_manager_response({'type': 13, 'content': {
        'userId': request.session['user_id'],
        'agentId': request.GET['agent'],
        'beginDate': request.GET['begin-date'],
        'endDate': request.GET['end-date'],
    }})

    if res['type'] == 0:
        context['agent_name'] = res['content']['agentName']

    res = get_manager_response({'type': 11, 'content': {'userId': request.session['user_id']}})

    if res['type'] == 0:
        context['name'] = res['content']['user']['name']
    else:
        context['name'] = 'usuário'

    return render(request, 'core/results.html', context)
def test(request):
    res = get_manager_response({'type': 5, 'content': {'agentId': 20, 'userId': 2}})

    context = {'agent': res['content']['agent']}

    res = get_manager_response({'type': 11, 'content': {'userId': 2}})

    if res['type'] == 0:
        context['name'] = res['content']['user']['name']
    else:
        context['name'] = 'usuário'

    return render(request, 'core/agent_settings.html', context)
Exemple #4
0
def sign_up(request):
    if request.method != 'POST':
        return HttpResponse('Wrong request method. Use POST.',
                            content_type='text/plain')

    name = request.POST['name']
    username = request.POST['username']
    password = request.POST['password']
    confirm = request.POST['confirm']

    # field validations go here
    if password != confirm:
        return HttpResponse(json.dumps({'status': 1}),
                            content_type='text/JSON')

    res = get_manager_response({
        'type': 1,
        'content': {
            'name': name,
            'username': username,
            'password': password,
        }
    })

    if res['type'] == 0:
        obj = {'status': 0}
    else:
        obj = {'status': 2}

    return HttpResponse(json.dumps(obj), content_type='text/JSON')
def records(request):
    if 'user_id' not in request.session.keys():
        return redirect('/')

    res = get_manager_response({'type': 4, 'content': {'userId': request.session['user_id']}})
    agents = res['content']['agents']

    context = {
        'agents': agents,
    }

    res = get_manager_response({'type': 11, 'content': {'userId': request.session['user_id']}})

    if res['type'] == 0:
        context['name'] = res['content']['user']['name']
    else:
        context['name'] = 'usuário'

    return render(request, 'core/records.html', context)
def details(request, agent_id):
    if 'user_id' not in request.session.keys():
        return redirect('/')

    res = get_manager_response({'type': 5, 'content': {'agentId': agent_id, 'userId': request.session['user_id']}})

    if res['type'] == 0:
        return render(request, 'core/old/details.html', {'agent': res['content']['agent']})
    else:
        return render(request, 'core/old/details.html')
def agent_settings(request, agent_id):
    if 'user_id' not in request.session.keys():
        return redirect('/')

    res = get_manager_response({'type': 5, 'content': {'agentId': agent_id, 'userId': request.session['user_id']}})

    context = {'agent': res['content']['agent']}

    res = get_manager_response({'type': 11, 'content': {'userId': request.session['user_id']}})

    if res['type'] == 0:
        context['name'] = res['content']['user']['name']
    else:
        context['name'] = 'usuário'

    if res['type'] == 0:
        return render(request, 'core/agent_settings.html', context)
    else:
        return redirect('/settings')
def main_page(request):
    if 'user_id' not in request.session.keys():
        return redirect('/')

    res = get_manager_response({'type': 4, 'content': {'userId': request.session['user_id']}})

    context = {
        'agents': res['content']['agents'],
    }

    return render(request, 'core/old/main_page.html', context)
def remove_discord(request):
    if request.method != 'POST':
        return HttpResponse('Wrong request method. Use POST.', content_type='text/plain')

    res = get_manager_response({'type': 10, 'content': {
        'userId': request.session['user_id'],
    }})

    if res['type'] == 0:
        obj = {'status': 0}
    else:
        obj = {'status': 1}
        print(res['content']['message'])

    return HttpResponse(json.dumps(obj), content_type='text/JSON')
def query_last_alerts(request):
    if request.method != 'GET':
        return HttpResponse('Wrong request method. Use GET.',
                            content_type='text/plain')

    obj = get_manager_response({
        'type': 14,
        'content': {
            'agentId': int(request.GET['id']),
            'userId': request.session['user_id'],
        },
    })

    if obj['type'] == 0:
        alerts = []
        for alert in obj['content']['alerts']:
            alerts.append({
                'resource': alert['type'],
                'threshold': {
                    'value':
                    alert['threshold']
                    if alert['threshold'] >= 0 else -alert['threshold'],
                    'type':
                    0 if alert['threshold'] >= 0 else 1,
                },
                'begin': alert['beginTime'],
                'end': alert['endTime'],
            })

        response = {
            'status': 0,
            'content': {
                'alerts': alerts,
            },
        }
    else:
        response = {
            'status': 1,
            'content': {
                'type': obj['type'],
                'message': obj['content']['message'],
            },
        }

    return HttpResponse(json.dumps(response), content_type='text/JSON')
Exemple #11
0
def ping(request):
    if request.method != 'GET':
        return HttpResponse('Wrong request method. Use GET.',
                            content_type='text/plain')

    res = get_manager_response({
        'type': 6,
        'content': {
            'userId': request.session['user_id']
        }
    })

    if res['type'] == 0:
        obj = {'status': 0}
    else:
        obj = {'status': 1}

    return HttpResponse(json.dumps(obj), content_type='text/JSON')
def setup_discord_dm(request):
    if request.method != 'POST':
        return HttpResponse('Wrong request method. Use POST.',
                            content_type='text/plain')

    user_tag = request.POST['discord-tag']

    res = get_manager_response({
        'type': 3,
        'content': {
            'userId': request.session['user_id'],
            'userTag': user_tag,
        }
    })

    if res['type'] == 0:
        obj = {'status': 0}
    else:
        obj = {'status': 1}
        print(res['content']['message'])

    return HttpResponse(json.dumps(obj), content_type='text/JSON')
def remove_agent(request):
    if request.method != 'POST':
        return HttpResponse('Wrong request method. Use POST.',
                            content_type='text/plain')

    agent_id = int(request.POST['agent-id'])

    # validate fields

    res = get_manager_response({
        'type': 8,
        'content': {
            'userId': request.session['user_id'],
            'agentId': agent_id,
        },
    })

    if res['type'] == 0:
        obj = {'status': 0}
    else:
        obj = {'status': 1, 'message': res['content']['message']}

    return HttpResponse(json.dumps(obj), content_type='text/JSON')
def sign_in(request):
    if request.method != 'POST':
        return HttpResponse('Wrong request method. Use POST.',
                            content_type='text/plain')

    u = request.POST['username']
    p = request.POST['password']

    res = get_manager_response({
        'type': 0,
        'content': {
            'username': u,
            'password': p
        }
    })

    if res['type'] == 0:
        request.session['user_id'] = res['content']['id']
        obj = {'status': 0}
    else:
        obj = {'status': 1}

    return HttpResponse(json.dumps(obj), content_type='text/JSON')
def update_user(request):
    if request.method != 'POST':
        return HttpResponse('Wrong request method. Use POST.', content_type='text/plain')

    name = request.POST['name']
    username = request.POST['username']
    password = request.POST['password']
    repeat = request.POST['confirm-password']
    current = request.POST['current-password']

    # field validations go here
    if password != repeat:
        return HttpResponse(json.dumps({'status': 1}), content_type='text/JSON')

    params = {}

    if name:
        params['name'] = name
    if username:
        params['username'] = username
    if password:
        params['password'] = password

    res = get_manager_response({'type': 12, 'content': {
        'userId': request.session['user_id'],
        'currentPassword': current,
        'params': params,
    }})

    if res['type'] == 0:
        obj = {'status': 0}
    else:
        obj = {'status': 2}
        print(res['content']['message'])

    return HttpResponse(json.dumps(obj), content_type='text/JSON')
def query_last_data(request):
    if request.method != 'GET':
        return HttpResponse('Wrong request method. Use GET.',
                            content_type='text/plain')

    obj = get_manager_response({
        'type': 2,
        'content': {
            'agentId': int(request.GET['id']),
            'userId': request.session['user_id'],
        },
    })

    if obj['type'] == 0:
        response = {
            'status': 0,
            'content': obj['content'],
        }
    else:
        response = {
            'status': 1,
        }

    return HttpResponse(json.dumps(response), content_type='text/JSON')
def change_agent(request):
    if request.method != 'POST':
        return HttpResponse('Wrong request method. Use POST.',
                            content_type='text/plain')

    # name, interval, interval-unit-select
    # monitor-processor, alert-processor, processor
    # monitor-memory, alert-memory, memory-select, memory, memory-unit-select
    # monitor-disk, alert-disk, memory-disk, disk, disk-unit-select
    # agent-id

    name = request.POST['name']

    temp = request.POST['interval']
    if temp:
        interval = int(temp)
        unit = int(request.POST['interval-unit-select'])
        if unit == 1:
            interval *= 1000
        elif unit == 2:
            interval *= 60000
        elif unit == 3:
            interval *= 3600000
        else:
            interval = None
    else:
        interval = None

    if 'monitor-processor' in request.POST.keys():
        if 'alert-processor' in request.POST.keys():
            temp = request.POST['processor']
            if temp:
                cpu = -int(temp)
            else:
                cpu = None
        else:
            cpu = -101
    else:
        cpu = -102

    if 'monitor-memory' in request.POST.keys():
        if 'alert-memory' in request.POST.keys():
            temp = request.POST['memory']
            if temp:
                memory = int(temp)
                alert_type = int(request.POST['memory-select'])
                if alert_type == 1:
                    memory = -memory
                elif alert_type == 2:
                    unit = int(request.POST['memory-unit-select'])
                    if unit == 1:
                        memory *= 1024
                    elif unit == 2:
                        memory *= 1024**2
                    elif unit == 3:
                        memory *= 1024**3
                    else:
                        memory = None
                else:
                    memory = None
            else:
                memory = None
        else:
            memory = -101
    else:
        memory = -102

    if 'monitor-disk' in request.POST.keys():
        if 'alert-disk' in request.POST.keys():
            temp = request.POST['disk']
            if temp:
                disk = int(temp)
                alert_type = int(request.POST['disk-select'])
                if alert_type == 1:
                    disk = -disk
                elif alert_type == 2:
                    unit = int(request.POST['disk-unit-select'])
                    if unit == 1:
                        disk *= 1024
                    elif unit == 2:
                        disk *= 1024**2
                    elif unit == 3:
                        disk *= 1024**3
                    else:
                        disk = None
                else:
                    disk = None
            else:
                disk = None
        else:
            disk = -101
    else:
        disk = -102

    agent_id = int(request.POST['agent-id'])

    # validate fields

    res = get_manager_response({
        'type': 7,
        'content': {
            'agentParams': {
                'id': agent_id,
                'name': name,
                'interval': interval,
                'cpu': cpu,
                'memory': memory,
                'disk': disk,
            },
            'userId': request.session['user_id'],
        },
    })

    if res['type'] == 0:
        obj = {'status': 0}
    else:
        obj = {'status': 1, 'message': res['content']['message']}

    return HttpResponse(json.dumps(obj), content_type='text/JSON')