Ejemplo n.º 1
0
    def get_brief_list(self, **cond):
        ''' 集群详情中获取主机列表
        '''
        arg = []
        extra = ''

        if len(cond) > 1: # 暂时保留cluster_id,但实际没用到
            extra = 'WHERE '
            e = []

            if cond.get('provider'):
                e.append(get_in_formats(field='i.provider', contents=cond['provider']))
                if isinstance(cond['provider'], list):
                    arg.extend(cond['provider'])
                else:
                    arg.append(cond['provider'])

            if cond.get('region'):
                e.append(get_in_formats(field='i.region_name', contents=cond['region']))
                if isinstance(cond['region'], list):
                    arg.extend(cond['region'])
                else:
                    arg.append(cond['region'])

            if cond.get('cluster_id'):
                e.append('s.cluster_id=%s')
                arg.append(cond['cluster_id'])

            if cond.get('lord'):
                e.append('s.lord=%s')
                arg.append(cond['lord'])

            if cond.get('form'):
                e.append('s.form=%s')
                arg.append(cond['form'])

            extra += ' AND '.join(e)

        sql = """
            SELECT s.id, s.name, s.public_ip, s.cluster_id, i.instance_id, i.provider, i.instance_name, i.region_name AS address, i.status AS machine_status
            FROM server s
            JOIN instance i USING(instance_id)
            """+"""
            {where}
            ORDER BY i.provider
        """.format(where=extra)

        cur = yield self.db.execute(sql, arg)
        data = cur.fetchall()

        # 添加最新上报信息
        report_info = self.redis.hgetall(SERVERS_REPORT_INFO)
        for d in data:
            info = fake_report_info() if is_faker(d['instance_id']) else json_loads(report_info.get(d['public_ip']))
            d.update(info)

        data = sorted(data, key=lambda x: x['name'], reverse=True)
        return data
Ejemplo n.º 2
0
    def get_performance(self, params):
        params['public_ip'] = yield self.fetch_public_ip(params['id'])

        info = yield self.fetch_instance_info(params['id'])
        if info and is_faker(info['instance_id']):
            return fake_performance(params)

        data = {}
        if params['type'] == 0:
            data['cpu'] = yield self._get_performance('cpu', params)
            data['memory'] = yield self._get_performance('memory', params)
            data['disk'] = yield self._get_performance('disk', params)
            data['net'] = yield self._get_performance('net', params)
        elif params['type'] == 1:
            data = yield self._get_performance_page(params)
        elif params['type'] == 2:
            data = yield self._get_performance_avg('server_log_hour', params)
        elif params['type'] == 3:
            data = yield self._get_performance_avg('server_log_day', params)
        return data
Ejemplo n.º 3
0
    def get(self, sid):
        """
        @api {get} /api/server/(\d+)/systemload 服务器负载
        @apiName SystemLoadHandler
        @apiGroup Server

        @apiParam {number} sid 服务器id

        @apiSuccessExample {json} Success-Response:
        HTTP/1.1 200 OK
        {
            "status": 0,
            "msg": "success",
            "data": {
                "date": "2018-03-08 14:23:30"
                "run_time": "6天6小时30分钟"
                "login_users": 1,
                "one_minute_load": 0.14
                "five_minute_load": 0.34
                "fifteen_minute_load:" 0.24
                "monitor": object
            }
        }
        """
        with catch(self):
            sid = int(sid)
            instance_info = yield self.server_service.fetch_instance_info(sid)
            server_info = yield self.server_service.select(conds={'id': sid},
                                                           fields='name',
                                                           one=True)
            if instance_info and is_faker(instance_info['instance_id']):
                self.success(
                    fake_systemload({
                        'sid': int(sid),
                        'name': server_info['name']
                    }))
                return

            ip = yield self.server_service.fetch_public_ip(int(sid))
            info = json_loads(self.redis.hget(SERVERS_REPORT_INFO,
                                              ip))['system_load']

            data = yield self.server_service.get_monitor_data([sid])
            resp = {
                'serverID': '',
                'name': '',
                'colorType': '',
                'cpuUsageRate': '',
                'memUsageRate': '',
                'diskUsageRate': '',
                'diskUtilize': '',
                'netUsageRate': '',
                'netDownload': '',
                'netUpload': '',
                "netInputMax": '',
                "netOutputMax": ''
            }
            monitor_data = data[0] if data else resp

            info.update({'monitor': monitor_data})
            self.success(info)
Ejemplo n.º 4
0
    def get(self):
        """
        @api {get} /api/server/monitor 服务器热图数据
        @apiName ServerMonitorHandler
        @apiGroup Server

        @apiSuccessExample {json} Success-Response:
        HTTP/1.1 200 OK
        {
            "status": 0,
            "msg": "success",
            "data": [
                {
                    "serverID": int,
                    "name": string,
                    "colorType": int,
                    "cpuUsageRate": float,
                    "memUsageRate": float,
                    "diskUsageRate": float,
                    "diskUtilize": string,
                    "netUsageRate": float,
                    "netDownload": str,
                    "netUpload": str,
                    "netInputMax":str,
                    "netOutputMax": str
                }
            ]
        }
        """
        with catch(self):
            cid, uid = self.params.get('cid'), self.current_user['id']
            if not cid:
                sid = yield self.server_service.select(fields='id',
                                                       conds={
                                                           'lord': uid,
                                                           'form': FORM_PERSON
                                                       },
                                                       ct=False,
                                                       ut=False)
            else:
                is_admin = yield self.company_employee_service.check_admin_bool(
                    uid=uid, cid=cid)
                if is_admin:
                    sid = yield self.server_service.select(
                        fields='id as sid',
                        conds={
                            'lord': cid,
                            'form': RESOURCE_TYPE['firm']
                        },
                        ct=False,
                        ut=False)
                else:
                    sid = yield self.user_access_server_service.select(
                        fields='sid',
                        conds={
                            'cid': cid,
                            'uid': uid
                        },
                        ct=False,
                        ut=False)
            sids = [i['sid'] for i in sid]

            faker_sids, real_sids = [], []
            for sid in sids:
                data = yield self.server_service.fetch_instance_info(sid)
                if is_faker(data['instance_id']):
                    faker_sids.append(sid)
                else:
                    real_sids.append(sid)

            if faker_sids:
                data = []
                for sid in faker_sids:
                    instance_info = yield self.server_service.fetch_instance_info(
                        sid)
                    server_info = yield self.server_service.select(
                        conds={'id': sid}, fields='name', one=True)
                    if instance_info and is_faker(
                            instance_info['instance_id']):
                        data.append(
                            fake_systemload({
                                'sid': int(sid),
                                'name': server_info['name'],
                                'monitor': True
                            }))

                real_data = yield self.server_service.get_monitor_data(
                    real_sids)
                data.extend(real_data)

                self.success(data)
                return

            data = yield self.server_service.get_monitor_data(sids)
            self.success(data)