Exemple #1
0
def alert_task(alert_data, users):
    res = GetRedis.connect()
    res_key = str(hash(str(users)))  # to be unique ?
    if res.get("alert_{0}".format(res_key)) == 1:
        return True

    res.set("alert_{0}".format(res_key), 1)  # set status running
    try:
        msg = render_to_string(
            "monitor/wx2.html.j2", {
                "data": alert_data,
                "users": ', '.join([str(u) for u in list(users)[:10]])
            })
        # labels = ["zabbix", "prometheus", "elasticsearch", "xxl-job", "else"]
        # subject = "`{}` `{}    `".format(labels[alert_data.get("source")],
        #                                  str(alert_data.get("ip"))[0:15])
        status_code = alert_data.get("status_code")
        if 0 == status_code:
            subject = "> `{}` `{}    `".format(alert_data.get("status"),
                                               str(alert_data.get("ip"))[0:15])
        else:
            subject = '> <font color="info">{}  {}</font>'.format(
                alert_data.get("status"),
                str(alert_data.get("ip"))[0:15])
        to_list = [u.wid for u in users if u.wid]
        for receiver in to_list:
            wx(receiver, subject, msg)
    finally:
        res.set("alert_{0}".format(res_key), 0)
        res.set("alert_data_{0}".format(res_key),
                json.dumps(alert_data, indent=4))
        res.set("alert_users_{0}".format(res_key),
                ','.join([str(u) for u in users]))

    return True
Exemple #2
0
def exec_status(request, exec_type):
    r = GetRedis.connect()
    if exec_type == "1":
        data = r.get("ansible_{0}".format(request.user.username))
    elif exec_type == "2":
        data = r.get("shell_{0}".format(request.user.username))
    else:
        data = None
    if not data:
        data = 0
    return HttpResponse(data)
Exemple #3
0
def exec_scripts(request):
    if os.path.exists(log_path + "/execlog/shell_{0}.log".format(request.user.username)):
        os.remove(log_path + "/execlog/shell_{0}.log".format(request.user.username))
    if request.method == 'POST':
        server = request.POST.getlist('mserver', [])
        group = request.POST.getlist('mgroup', [])
        scripts = request.POST.getlist('mscripts', [])
        args = request.POST.getlist('margs')
        shell_command = request.POST.get('mcommand')

        #connect redis for record shell running status
        res = GetRedis.connect()
        res.set("shell_{0}".format(request.user.username), 1)
        # run async shell tasks
        shell_task(request, server, group, scripts, args, shell_command)

        return HttpResponse("ok")
Exemple #4
0
def playbook(request):
    if os.path.exists(ansible_dir + '/gexec.yml'):
        os.remove(ansible_dir + '/gexec.yml')
    if os.path.exists(log_path + "/execlog/ansible_{0}.log".format(request.user.username)):
        os.remove(log_path + "/execlog/ansible_{0}.log".format(request.user.username))
    else:
        pass
    if request.method == 'POST':
        host = request.POST.getlist('mserver', [])
        group = request.POST.getlist('mgroup', [])
        pbook = request.POST.getlist('splaybook', [])
        roles = request.POST.getlist('mroles', [])
        role_vars = request.POST.get('mvars')
        res = GetRedis.connect()
        res.set("ansible_{0}".format(request.user.username), 1)
        ansible_task(request, host, group, pbook, roles, role_vars, write_role_vars)

    return HttpResponse("ok")
Exemple #5
0
def ansible_task(request, host, group, pbook, roles, role_vars, write_role_vars):
    ret = []
    res = GetRedis.connect()
    #write real time ansible display log
    logging.info("==========ansible tasks start==========\n")
    logging.info("User:"******"/ansible.log", 'wb+') as f:
        f.writelines("==========ansible tasks start==========\n")
    if host:
        if roles:
            if role_vars:
                write_role_vars(roles, role_vars)
            for h in host:
                # wirte ansible-play yaml file
                with open(ansible_dir + '/gexec.yml', 'w+') as f:
                    flist = ['- hosts: '+h+'\n', '  remote_user: root\n', '  gather_facts: true\n', '  roles:\n']
                    for r in roles:
                        rs = '    - ' + r + '\n'
                        flist.append(rs)
                        logging.info("Role:"+r)
                    f.writelines(flist)
                cmd = "ansible-playbook"+" " + ansible_dir+'/gexec.yml'
                p = Popen(cmd, stderr=PIPE, stdout=PIPE, shell=True)
                data = p.communicate()
                ret.append(data)
                for d in data:
                    logging.info(d)
                with open(log_path + "/execlog/ansible_{0}.log".format(request.user.username), 'ab+') as f1:
                    f1.writelines("***Host: {0} ***\n".format(h))
                    f1.writelines("=================================\n")
                    f1.writelines(data)
        else:
            for h in host:
                for p in pbook:
                    f = open(playbook_dir + p, 'r+')
                    flist = f.readlines()
                    flist[0] = '- hosts: '+h+'\n'
                    f = open(playbook_dir + p, 'w+')
                    f.writelines(flist)
                    f.close()
                    cmd = "ansible-playbook"+" " + playbook_dir + p
                    pcmd = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True)
                    data = pcmd.communicate()
                    ret.append(data)
                    with open(log_path + "/execlog/ansible_{0}.log".format(request.user.username), 'ab+') as f2:
                        f2.writelines("*** Host: {0} ***\n".format(h))
                        f2.writelines("=================================\n")
                        f2.writelines(data)
                    for d in data:
                        logging.info(d)
        with open(log_path + "/execlog/ansible_{0}.log".format(request.user.username), 'ab+') as f3:
            f3.writelines("==========ansible tasks end============")
        logging.info("==========ansible tasks end============")
        res.set("ansible_{0}".format(request.user.username), 0)
        return True

    if group:
        if roles:
            if role_vars:
                write_role_vars(roles, role_vars)
            for g in group:
                f = open(ansible_dir + '/gexec.yml', 'w+')
                flist = ['- hosts: '+g+'\n', '  remote_user: root\n', '  gather_facts: true\n', '  roles:\n']
                for r in roles:
                    rs = '    - ' + r + '\n'
                    flist.append(rs)
                    logging.info("Role:"+r)
                f.writelines(flist)
                f.close()
                cmd = "ansible-playbook"+" " + ansible_dir+'/gexec.yml'
                p = Popen(cmd, stderr=PIPE, stdout=PIPE, shell=True)
                data = p.communicate()
                ret.append(data)
                with open(log_path + "/execlog/ansible_{0}.log".format(request.user.username), 'ab+') as f4:
                    f4.writelines("*** Group: {0} ***\n".format(g))
                    f4.writelines("=================================\n")
                    f4.writelines(data)
                for d in data:
                    logging.info(d)
        else:
            for g in group:
                for p in pbook:
                    f = open(playbook_dir + p, 'r+')
                    flist = f.readlines()
                    flist[0] = '- hosts: '+g+'\n'
                    f = open(playbook_dir + p, 'w+')
                    f.writelines(flist)
                    f.close()
                    cmd = "ansible-playbook"+" " + playbook_dir + p
                    pcmd = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True)
                    data = pcmd.communicate()
                    ret.append(data)
                    with open(log_path + "/execlog/ansible_{0}.log".format(request.user.username), 'ab+') as f5:
                        f5.writelines("*** Group: {0} ***\n".format(g))
                        f5.writelines("=================================\n")
                        f5.writelines(data)
                    for d in data:
                        logging.info(d)
        with open(log_path + "/execlog/ansible_{0}.log".format(request.user.username), 'ab+') as f6:
            f6.writelines("==========ansible tasks end============")
        logging.info("==========ansible tasks end============")
        res.set("ansible_{0}".format(request.user.username), 0)
        return True
Exemple #6
0
def shell_task(request, server, group, scripts, args, shell_command):
    ret = []
    res = GetRedis.connect()
    #write real time ansible display log
    logging.info("==========Shell Tasks Start==========\n")
    logging.info("User:"******"/execlog/shell_{0}.log".format(request.user.username), 'wb+') as f:
        f.writelines("==========Shell Tasks Start==========\n")
    if server:
        if scripts:
            for name in server:
                host = Host.objects.get(hostname=name)
                ret.append(host.hostname)
                logging.info("Host:"+host.hostname)
                with open(log_path + "/execlog/shell_{0}.log".format(request.user.username), 'ab+') as f7:
                    f7.writelines("*** Host: {0} ***\n".format(host.hostname))
                    f7.writelines("=================================\n")

                for s in scripts:
                    try:
                        sh.scp(scripts_dir+s, "root@{}:/tmp/".format(host.ip)+s)
                    except:
                        pass
                    cmd = "ssh root@"+host.ip+" "+'"sh /tmp/{} {}"'.format(s, args)
                    p = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True)
                    data = p.communicate()
                    ret.append(data)
                    logging.info("Scripts:"+s)
                    for d in data:
                        logging.info(d)
                    with open(log_path + "/execlog/shell_{0}.log".format(request.user.username), 'ab+') as f7:
                        f7.writelines(data)
        else:
            for name in server:
                host = Host.objects.get(hostname=name)
                ret.append(host.hostname)
                command_list = shell_command.split('\n')
                with open(log_path + "/execlog/shell_{0}.log".format(request.user.username), 'ab+') as f8:
                    f8.writelines("*** Host: {0} ***\n".format(host.hostname))
                    f8.writelines("=================================\n")
                for cmd in command_list:
                    dcmd = "ssh root@"+host.ip+" "+'"{}"'.format(cmd.strip())
                    p = Popen(dcmd, stdout=PIPE, stderr=PIPE, shell=True)
                    data = p.communicate()
                    ret.append(data)
                    logging.info("command:"+cmd)
                    for d in data:
                        logging.info(d)
                    with open(log_path + "/execlog/shell_{0}.log".format(request.user.username), 'ab+') as f10:
                        f10.writelines(data)
    if group:
        if scripts:
            for g in group:
                get_group = HostGroup.objects.get(name=g)
                hosts = get_group.serverList.all()
                ret.append(g)
                for host in hosts:
                    ret.append(host.hostname)
                    for s in scripts:
                        try:
                            sh.scp(scripts_dir+s, "root@{}:/tmp/".format(host.ip)+s)
                        except:
                            pass
                        cmd = "ssh root@"+host.ip+" "+'"sh /tmp/{} {}"'.format(s, args)
                        p = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True)
                        data = p.communicate()
                        ret.append(data)
                        with open(log_path + "/execlog/shell_{0}.log".format(request.user.username), 'ab+') as f11:
                            f11.writelines(data)
                        logging.info("command:"+cmd)
                        for d in data:
                            logging.info(d)
        else:
            command_list = []
            command_list = shell_command.split('\n')
            for g in group:
                logging.info("==========Shell Start==========")
                logging.info("User:"******"Group:"+g)
                get_group = HostGroup.objects.get(name=g)
                hosts = get_group.serverList.all()
                ret.append(g)
                for host in hosts:
                    ret.append(host.hostname)
                    for cmd in command_list:
                        cmd = "ssh root@"+host.ip+" "+'"{}"'.format(cmd)
                        p = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True)
                        data = p.communicate()
                        ret.append(data)
                        with open(log_path + "/execlog/shell_{0}.log".format(request.user.username), 'ab+') as f12:
                            f12.writelines(data)
                        logging.info("command:"+cmd)
                        for d in data:
                            logging.info(d)
    with open(log_path + "/execlog/shell_{0}.log".format(request.user.username), 'ab+') as f6:
        f6.writelines("==========Shell Tasks Finished========")
    logging.info("==========Shell Tasks Finished========")
    res.set("shell_{0}".format(request.user.username), 0)
    return True