예제 #1
0
def delHost(aliyunids, ecsapi):
    idlist = []
    status, result = ecsapi.getECSHostListByID(aliyunids)
    if not status:
        return
    for h in result:
        idlist.append(h['aliyun_id'])
    delid = list(set(aliyunids) ^ set(idlist))
    if len(delid) > 0:
        delHostList = Hosts.objects.filter(aliyun_id__in=delid)
        hostids = []
        iplist = []
        for delhost in delHostList:
            ###################### 删除主机的时候删除对应的监控 ######################
            try:
                DelMonitorTask().addTask('script', ip=delhost.ip)
            except Exception, ex:
                logger.error(str(ex))
            ###################### 删除主机的时候删除对应监控 ######################
            iplist.append(delhost.ip)
            HostDeleted.objects.create(**model_to_dict(delhost))
            hostids.append(delhost.id)
        ServiceHost.objects.filter(host_id__in=hostids).delete()
        ################### 将ip设置为空闲 ###################
        if len(iplist) > 0:
            IpAddress.objects.filter(ip__in=iplist).update(state='free')
        ################### 将ip设置为空闲 ###################
        delHostList.delete()
예제 #2
0
 def excute(self, **kwargs):
     ip = kwargs.get('ip', '')
     hostname = kwargs.get('hostname', '')
     if not ip or not hostname:
         return False, u'ip和hostname不能为空'
     command = 'ssh root@%s -o ConnectTimeout=2 "/usr/bin/python /opt/cmdb-script/change_hostname.py %s %s"' % (
         configs.FORTRESS_HOST, hostname, ip)
     logger.info(command)
     try:
         status, output = commands.getstatusoutput(command)
         if status != 0:
             logger.error(output)
             return False, output
         logger.info(output)
         result_dict = json.loads(output)
         if result_dict['success']:
             msg = "change %s hostname to %s success" % (ip, hostname)
             logger.info(msg)
         else:
             msg = "change %s hostname to %s error: %s" % (
                 ip, hostname, result_dict['msg'])
             logger.info(msg)
         return True, output
     except Exception, e:
         logger.error(str(e))
         return False, str(e)
예제 #3
0
def run_task_job(queue_key):
    q = Q(queue_key)
    while True:
        ############### 解决mysql连接关闭的问题 ###############
        try:
            connection.close()
            print True
        except Exception, ex:
            print str(ex)
            logger.error(str(ex))
        ############### 解决mysql连接关闭的问题 ###############

        msg = q.pop()
        try:
            task_data = json.loads(msg[1])
            print task_data
            task = task_data['task']
            task_list = task.split(".")
            module = importlib.import_module(".".join(task_list[:-1]))
            reload(module)
            TaskClass = getattr(module, task_list[-1])
            obj = new.instance(TaskClass)
            obj.run(task_data['id'], **json.loads(task_data['params']))
        except Exception, ex:
            print str(ex)
            logger.error(str(ex))
예제 #4
0
 def updateTemplate(self,
                    name,
                    projectName,
                    cmdbServiceName,
                    cmdbServiceId,
                    vcsRep,
                    vcsType='git',
                    review=0):
     params = {}
     params['token'] = configs.AUTO_TOKEN
     params['name'] = name
     params['projectName'] = projectName
     params['cmdbServiceName'] = cmdbServiceName
     params['cmdbServiceId'] = cmdbServiceId
     if vcsRep:
         params['vcsRep'] = vcsRep
     params['vcsType'] = vcsType
     params['review'] = review
     try:
         response = requests.post(
             configs.AUTO_DOMAIN + '/gateway/template/modifyTemplate',
             data=json.dumps(params),
             headers={"Content-Type": "application/json"})
         data = json.loads(response.text)
         if data['statusCode'] == '200':
             return True, 'succ'
         logger.error(response.text)
         return False, data['msg']
     except Exception, ex:
         logger.error(str(ex))
         return False, str(ex)
예제 #5
0
 def addSSHKey(self, username, ssh_key):
     command = 'ssh root@%s -o ConnectTimeout=2 "echo %s > /home/%s/.ssh/authorized_keys"' % (
         configs.FORTRESS_HOST, ssh_key, username)
     try:
         status, output = commands.getstatusoutput(command)
         if status != 0:
             logger.error(output)
             return False, output
         return True, output
     except Exception, e:
         logger.error(str(e))
         return False, str(e)
예제 #6
0
 def getGoogleKey(self, username):
     command = 'ssh root@%s -o ConnectTimeout=2 "head -n 1 /home/%s/.google_authenticator"' % (
         configs.FORTRESS_HOST, username)
     try:
         status, output = commands.getstatusoutput(command)
         if status != 0:
             logger.error(output)
             return False, output
         return True, output
     except Exception, e:
         logger.error(str(e))
         return False, str(e)
예제 #7
0
def rsync_fortress():
    command = 'ssh root@%s -o ConnectTimeout=5 "python /opt/cmdb-script/rsync_backup.py"' % (
        configs.FORTRESS_HOST, )
    try:
        status, output = commands.getstatusoutput(command)
        if status != 0:
            logger.error(output)
            return False, output
        logger.info(output)
        return True, output
    except Exception, e:
        logger.error(str(e))
        return False, str(e)
예제 #8
0
 def getProjects(self, page, per_page):
     self.params['page'] = page
     self.params['per_page'] = per_page
     try:
         response = requests.get(configs.GITLAB_DOMAIN + '/api/v3/projects',
                                 params=self.params,
                                 headers=self.headers)
         data = response.json()
         # data = json.loads(response.text)
         return True, data
     except Exception, ex:
         logger.error(str(ex))
         return False, str(ex)
예제 #9
0
def sshGetHostName(ip):
    if not ip:
        return False, u'ip不能为空'
    command = 'ssh root@%s -o ConnectTimeout=5 "/usr/bin/python /opt/cmdb-script/get_hostname.py %s"' % (
        configs.FORTRESS_HOST, ip)
    try:
        status, output = commands.getstatusoutput(command)
        if status != 0:
            logger.error(output)
            return False, output
        return True, output
    except Exception, e:
        logger.error(str(e))
        return False, str(e)
예제 #10
0
 def excute(self, **kwargs):
     ip = kwargs.get('ip', '')
     if not ip:
         return False, u'ip不能为空'
     try:
         if type == 'aliyun':
             zabbixrestapi = ZabbixAPi()
         else:
             zabbixrestapi = ZabbixAPi(url=configs.ZABBIX_IDC_API_URL)
         res, output = zabbixrestapi.deleteHost(ip)
         return True, output
     except Exception, e:
         logger.error(str(e))
         return False, str(e)
예제 #11
0
 def delUserKey(self, ip, role, username):
     command = 'ssh root@%s -o ConnectTimeout=2 "/usr/bin/python %s/auth.ssh_key.py del %s %s %s"' % (
         configs.FORTRESS_HOST, configs.REMOTE_CMDB_DIR, ip, role, username)
     logger.info(command)
     try:
         status, output = commands.getstatusoutput(command)
         if status != 0:
             logger.error(output)
             return False, output
         logger.info(output)
         result_dict = json.loads(output)[0]
         if result_dict['success'] == "true":
             return True, result_dict['msg']
         return False, result_dict['msg']
     except Exception, e:
         logger.error("add user key:" + str(e))
         return False, str(e)
예제 #12
0
 def run(self, task_id=0, **kwargs):
     AsyncTask.objects.filter(id=task_id).update(start_time=int(
         time.time()),
                                                 state='running')
     try:
         res, data = self.excute(**kwargs)
         state = 'failure'
         if res:
             state = 'success'
         AsyncTask.objects.filter(id=task_id).update(state=state,
                                                     result=data,
                                                     finish_time=int(
                                                         time.time()))
     except Exception, ex:
         AsyncTask.objects.filter(id=task_id).update(state='failure',
                                                     result=str(ex))
         logger.error("task " + str(task_id) + ":" + str(ex))
예제 #13
0
 def excute(self, **kwargs):
     ip = kwargs.get('ip', '')
     state = kwargs.get('state', '')
     if not ip or not state:
         return False, u'ip和state不能为空'
     try:
         # 只监控online状态的主机
         if state in ('online', ):
             status = 0
         else:
             status = 1
         if type == 'aliyun':
             zabbixrestapi = ZabbixAPi()
         else:
             zabbixrestapi = ZabbixAPi(url=configs.ZABBIX_IDC_API_URL)
         res, output = zabbixrestapi.changeStatus(ip, status)
         return True, output
     except Exception, e:
         logger.error(str(e))
         return False, str(e)
예제 #14
0
 def addProject(self, projectName, code, description):
     params = {}
     params['token'] = configs.AUTO_TOKEN
     params['code'] = code
     params['projectName'] = projectName
     if description:
         params['description'] = description
     try:
         response = requests.post(
             configs.AUTO_DOMAIN + '/gateway/project/addProject',
             data=json.dumps(params),
             headers={"Content-Type": "application/json"})
         data = json.loads(response.text)
         if data['statusCode'] == '200':
             return True, 'succ'
         logger.error(response.text)
         return False, data['msg']
     except Exception, ex:
         logger.error(str(ex))
         return False, str(ex)
예제 #15
0
 def addFortressUser(self, username, email):
     m = md5()
     m.update(username + str(time.time()))
     password = m.hexdigest()[0:20]
     command = 'ssh root@%s -o ConnectTimeout=2 "/bin/sh %s/fortress/create.sh %s %s"' % (
         configs.FORTRESS_HOST, configs.REMOTE_CMDB_DIR, username, password)
     logger.info(command)
     try:
         status, output = commands.getstatusoutput(command)
         if status != 0:
             logger.error(output)
             return False, output
         logger.info(output)
         content = u'已为你开通堡垒机账号<br/>用户名为:' + username + u'<br/>密码为:' + password + u"<br/>堡垒机使用方式见:http://wiki.mwbyd.cn/pages/viewpage.action?pageId=6334750<br/>" \
                   + u"如有疑问请联系陈绍东,邮箱:[email protected],手机号:15921777942"
         send_mail(u'堡垒机开通通知',
                   '',
                   configs.EMAIL_USER, [email],
                   html_message=content)
         return True, output
     except Exception, e:
         logger.error(str(e))
         return False, str(e)
예제 #16
0
    def excute(self, **kwargs):
        ip = kwargs.get('ip', '')
        state = kwargs.get('state', '')
        type = kwargs.get('type', '')
        attribute = kwargs.get('attribute', '')
        if not ip or not state or not type:
            return False, u'ip, type和state不能为空'

        ip_arr = ip.split(".")

        # 只监控online状态的主机
        if state in ('online', ):
            monitor_status = 0
        else:
            monitor_status = 1
        try:
            hosts = Hosts.objects.filter(ip=ip)
            if len(hosts) <= 0:
                return False, u'主机不存在'
            host = hosts[0]
            if type == 'aliyun':
                zabbixrestapi = ZabbixAPi()
            else:
                zabbixrestapi = ZabbixAPi(url=configs.ZABBIX_IDC_API_URL)

            ######################## 根据应用名称获取或创建组 ########################
            serviceids = []
            for s in ServiceHost.objects.filter(host_id=host.id):
                serviceids.append(s.service_id)
            appnames = []
            service_types = []
            appids = []
            for s in AppService.objects.filter(id__in=serviceids):
                appids.append(s.app_id)
                service_types.append(s.type)
            for a in App.objects.filter(id__in=appids):
                appnames.append(a.name)
            appnames = list(set(appnames))
            res, result = zabbixrestapi.getHostGroupsByName(*appnames)
            if not res:
                return False, result
            groupids = []
            groupnames = []
            for r in result['result']:
                groupnames.append(r['name'])
                groupids.append(r['groupid'])

            # 求服务名和查询出来的zabbix分组的差集,如果存在差集就新建组
            nocreate_groups = list(set(appnames).difference(set(groupnames)))
            if len(nocreate_groups) > 0:
                for g in nocreate_groups:
                    status, result = zabbixrestapi.createHostGroup(g)
                    if not status:
                        return False, result
                    groupids += result['result']['groupids']
            res, result = zabbixrestapi.getGroupByHost(*[ip])
            if not res:
                return False, result
            for allgroups in result['result']:
                for g in allgroups['groups']:
                    # 如果组名在cmdb的应用中找不到则说明是自己分配的组,需要保留
                    if len(App.objects.filter(name=g['name'])) <= 0:
                        groupids.append(g['groupid'])

            ######################## 根据应用名称获取或创建组 ########################

            ######################## 根据service类型获取监控模板id ########################
            tamplate_dict = IDC_TEMPLATE
            if type == 'aliyun':
                tamplate_dict = ALIYUN_TEMPLATE
            if str(ip_arr[2]) == "146":
                tamplateids = [tamplate_dict['default146']]
            else:
                tamplateids = [tamplate_dict['default']]
            for s in list(set(service_types)):
                if tamplate_dict.has_key(s):
                    tamplateids.append(tamplate_dict[s])
            tamplateids = list(set(tamplateids))
            ######################## 根据service类型获取监控模板id ########################

            ######################## 根据ip查询主机,如果不存在则创建主机,如果存在则修改主机组、模板和状态 ########################
            status, result = zabbixrestapi.getHostsByName(ip)
            if not status:
                return False, result
            if len(result['result']) <= 0:
                interface = ip
                if attribute == 'RDS':
                    interface = '127.0.0.1'
                    tamplateids = [12429]
                    proxyid = 0
                else:
                    if type == 'aliyun':
                        proxy_name = "zabbix_proxy_ali" + ip_arr[2]
                    else:
                        proxy_name = "zabbix_proxy" + ip_arr[2]
                    status, result = zabbixrestapi.getProxy(proxy_name)
                    if not status:
                        return False, result
                    proxyid = ''
                    if len(result['result']) > 0:
                        proxyid = result['result'][0]['proxyid']
                    if str(ip_arr[2]) == "146":
                        proxyid = 0

                status, result = zabbixrestapi.addHost(ip,
                                                       interface,
                                                       proxyid,
                                                       groups=groupids,
                                                       templates=tamplateids,
                                                       status=monitor_status)
            else:
                if attribute == 'RDS':
                    tamplateids = [12429]
                status, result = zabbixrestapi.changeHost(
                    ip, monitor_status, groups=groupids, templates=tamplateids)
            ######################## 根据ip查询主机,如果不存在则创建主机,如果存在则修改主机组、模板和状态 ########################

            if not status:
                return False, result
            return True, result
        except Exception, e:
            logger.error(str(e))
            return False, str(e)