예제 #1
0
    def getStatusJSON(self):
        # Sys Info
        statusJSON = {}
        statusJSON['status'] = 'success'
        statusJSON['sys_info'] = {
            'os_platform': self.config.osplatform,
            'cpu_nums': psutil.cpu_count(),
            'cpu_percent': psutil.cpu_percent(interval=0, percpu=True),
            'cpu_freq': {
                k: v
                for k, v in psutil.cpu_freq()._asdict().items()
                if k in ('current', 'min', 'max')
            } if psutil.cpu_freq() else None,
            'mem_info': {
                k: v
                for k, v in psutil.virtual_memory()._asdict().items()
                if k in ('total', 'used', 'available')
            },
            'disk_info': {
                k: v
                for k, v in psutil.disk_usage(getcwdb())._asdict().items()
                if k in ('total', 'used', 'free')
            }
        }

        statusJSON['connection_info'] = {
            'max_clients': self.config.maxconns,
            'total_clients': self.stuff.clientcounter.totalClients(),
        }

        statusJSON['clients_data'] = []
        # Dict {'CID': [client1, client2,....]} to list of values
        clients = [
            item for sublist in self.stuff.clientcounter.streams.values()
            for item in sublist
        ]
        for c in clients:
            if not c.clientInfo:
                if any([
                        requests.utils.address_in_network(c.clientip, i)
                        for i in localnetranges
                ]):
                    c.clientInfo = {
                        'vendor': self.get_vendor_Info(c.clientip),
                        'country_code': '',
                        'country_name': '',
                        'city': ''
                    }
                else:
                    try:
                        headers = {'User-Agent': 'API Browser'}
                        with requests.get('https://geoip-db.com/jsonp/%s' %
                                          c.clientip,
                                          headers=headers,
                                          stream=False,
                                          timeout=5) as r:
                            if r.encoding is None: r.encoding = 'utf-8'
                            c.clientInfo = json.loads(
                                r.text.split('(', 1)[1].strip(')'))
                            c.clientInfo['vendor'] = ''
                    except:
                        c.clientInfo = {
                            'vendor': '',
                            'country_code': '',
                            'country_name': '',
                            'city': ''
                        }

            statusJSON['clients_data'].append({
                'channelIcon':
                c.channelIcon,
                'channelName':
                c.channelName,
                'clientIP':
                c.clientip,
                'clientInfo':
                c.clientInfo,
                'startTime':
                time.strftime('%d/%m/%Y %H:%M:%S',
                              time.localtime(c.connectionTime)),
                'durationTime':
                time.strftime('%H:%M:%S',
                              time.gmtime(time.time() - c.connectionTime)),
                'stat':
                requests.get(c.cmd['stat_url'], timeout=2,
                             stream=False).json()['response']
                if self.config.new_api else c.ace._status.get(timeout=2)
            })
        return statusJSON
예제 #2
0
    def getStatusJSON(self):
        # Sys Info
        clients = self.stuff.clientcounter.getAllClientsList(
        )  # Get connected clients list
        statusJSON = {}
        statusJSON['status'] = 'success'
        statusJSON['sys_info'] = {
            'os_platform': self.config.osplatform,
            'cpu_nums': psutil.cpu_count(),
            'cpu_percent': psutil.cpu_percent(interval=0, percpu=True),
            'cpu_freq': {
                k: v
                for k, v in psutil.cpu_freq()._asdict().items()
                if k in ('current', 'min', 'max')
            } if psutil.cpu_freq() else {},
            'mem_info': {
                k: v
                for k, v in psutil.virtual_memory()._asdict().items()
                if k in ('total', 'used', 'available')
            },
            'disk_info': {
                k: v
                for k, v in psutil.disk_usage(getcwdb())._asdict().items()
                if k in ('total', 'used', 'free')
            }
        }

        statusJSON['connection_info'] = {
            'max_clients': self.config.maxconns,
            'total_clients': len(clients),
        }

        def _add_client_data(c):
            if not c.clientInfo:
                if self.ip_is_local(c.clientip):
                    c.clientInfo = {
                        'vendor': self.get_vendor_Info(c.clientip),
                        'country_code': '',
                        'country_name': '',
                        'city': ''
                    }
                else:
                    try:
                        headers = {'User-Agent': 'API Browser'}
                        with requests.get('https://geoip-db.com/jsonp/%s' %
                                          c.clientip,
                                          headers=headers,
                                          stream=False,
                                          timeout=5) as r:
                            if r.encoding is None: r.encoding = 'utf-8'
                            c.clientInfo = json.loads(
                                r.text.split('(', 1)[1].strip(')'))
                            c.clientInfo['vendor'] = ''
                    except:
                        c.clientInfo = {
                            'vendor': '',
                            'country_code': '',
                            'country_name': '',
                            'city': ''
                        }

            return {
                'sessionID':
                c.sessionID,
                'channelIcon':
                c.channelIcon,
                'channelName':
                ensure_text(c.channelName),
                'clientIP':
                c.clientip,
                'clientInfo':
                c.clientInfo,
                #'clientBuff': c.q.qsize()*100/self.config.videotimeout,
                'startTime':
                time.strftime('%d/%m/%Y %H:%M:%S',
                              time.localtime(c.connectionTime)),
                'durationTime':
                time.strftime('%H:%M:%S',
                              time.gmtime(time.time() - c.connectionTime)),
                'stat':
                c.ace.GetSTATUS(),
            }

        statusJSON['clients_data'] = Group().map(_add_client_data, clients)
        return statusJSON