Пример #1
0
 def set(self, key, value, time=0, min_compress_len=0):
     """Sets the value for a key."""
     timeout = 0
     if time != 0:
         timeout = utils.utcnow_ts() + time
     self.cache[key] = (timeout, value)
     return True
Пример #2
0
 def get_data(self, row_id, cf_str, scf_str, time_from=0, time_to=0):
     """
     param type: UnicodeType and IntType
     return: recordset, count, bool(count > limit?)
     """
     if not isinstance(row_id, unicode) \
         or not isinstance(cf_str, unicode) \
         or not isinstance(scf_str, unicode) \
         or not isinstance(time_from, int) \
         or not isinstance(time_to, int):
         return None, 0, True
         
     if time_to == 0:
         time_to = int(utils.utcnow_ts())
     print "get_data:", row_id, cf_str, scf_str, time_from, time_to
     
     bufkey = str([row_id, cf_str, scf_str, time_from, time_to])
     if self.buf.hit_test(bufkey):
         return self.buf.get_buf(bufkey)
     
     db = self.get_db()
     rs = db.get(cf_str, row_id, super_column=scf_str, 
             column_start=time_from, column_finish=time_to, column_count=20000)
     count = 0 if rs is None else len(rs)
     
     ret = rs, count, False if (count == 20000) else True
     if not rs is None:
         self.buf.save(bufkey, ret)
     
     return ret
Пример #3
0
 def set(self, key, value, time=0, min_compress_len=0):
     """Sets the value for a key."""
     timeout = 0
     if time != 0:
         timeout = utils.utcnow_ts() + time
     self.cache[key] = (timeout, value)
     return True
Пример #4
0
    def get(self, key):
        """Retrieves the value for a key or None.

        this expunges expired keys during each get"""

        for k in self.cache.keys():
            (timeout, _value) = self.cache[k]
            if timeout and utils.utcnow_ts() >= timeout:
                del self.cache[k]

        return self.cache.get(key, (0, None))[1]
Пример #5
0
def get_traffic_accounting_info():
    """
    return value format example:{'key': ('ip', time, bytes)}
    {'116@swsdevp': ('10.0.0.95', 1334555143, '0')}
    """
    global _ip_bytes
    
    records = subprocess.check_output(shlex.split(CMD),
                                      stderr=subprocess.STDOUT)
    lines = records.splitlines()[2:]
    acct_records = [line for line in lines if "accounting rule" in line]
    
    # ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            /*  240 10.42.1.0 accounting rule  */
    ret = dict()

# acct_records:
# ['[161:10604] -A nova-compute-f-inst-240 -o eth0 -m comment --comment " 240 10.42.1.0 accounting rule " -j ACCEPT']

# rawdata:
#['[0:0]', '-A', 'nova-compute-f-inst-95', '-o', 'br100', '-m', 'comment', '--comment', '"', '95', '10.30.0.15', 'accounting', 'rule', '"']

#------------------------------ traffic ------------------------------
#{'95@sws-qd-10': ('10.30.0.15', 1338449763, '0.0'), '10.30.0.15@sws-qd-10': ('accounting', 1338449763, '0.0')}


    for record in acct_records:
        rawdata = record.split()
        out_bytes = rawdata[0].partition(':')[2][0:-1]
        instance_id = rawdata[9] + hostname
        instance_ip = rawdata[10]

        # Is total out bytes(sum of history)
        val = float(out_bytes)

        if instance_id in _ip_bytes:
            prev_out_bytes = _ip_bytes[instance_id]
            val = float(out_bytes) - prev_out_bytes

            if val < 0:
                # error
                print "get_traffic_accounting_info error", float(out_bytes), prev_out_bytes
                val = float(out_bytes)
        else:
            # discard the first value
            val = 0

        # save current value
        _ip_bytes[instance_id] = float(out_bytes)

        ret[instance_id] = (instance_ip, int(utils.utcnow_ts()), str(val))

    return ret
Пример #6
0
def get_traffic_accounting_info():
    """
    return value format example:{'key': ('ip', time, bytes)}
    {'116@swsdevp': ('10.0.0.95', 1334555143, '0')}
    """
    global _ip_bytes

    records = subprocess.check_output(shlex.split(CMD),
                                      stderr=subprocess.STDOUT)
    lines = records.splitlines()[2:]
    acct_records = [line for line in lines if "accounting rule" in line]

    # ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            /*  240 10.42.1.0 accounting rule  */
    ret = dict()

    # acct_records:
    # ['[161:10604] -A nova-compute-f-inst-240 -o eth0 -m comment --comment " 240 10.42.1.0 accounting rule " -j ACCEPT']

    # rawdata:
    #['[0:0]', '-A', 'nova-compute-f-inst-95', '-o', 'br100', '-m', 'comment', '--comment', '"', '95', '10.30.0.15', 'accounting', 'rule', '"']

    #------------------------------ traffic ------------------------------
    #{'95@sws-qd-10': ('10.30.0.15', 1338449763, '0.0'), '10.30.0.15@sws-qd-10': ('accounting', 1338449763, '0.0')}

    for record in acct_records:
        rawdata = record.split()
        out_bytes = rawdata[0].partition(':')[2][0:-1]
        instance_id = rawdata[9] + hostname
        instance_ip = rawdata[10]

        # Is total out bytes(sum of history)
        val = float(out_bytes)

        if instance_id in _ip_bytes:
            prev_out_bytes = _ip_bytes[instance_id]
            val = float(out_bytes) - prev_out_bytes

            if val < 0:
                # error
                print "get_traffic_accounting_info error", float(
                    out_bytes), prev_out_bytes
                val = float(out_bytes)
        else:
            # discard the first value
            val = 0

        # save current value
        _ip_bytes[instance_id] = float(out_bytes)

        ret[instance_id] = (instance_ip, int(utils.utcnow_ts()), str(val))

    return ret
Пример #7
0
 def get_instances_list(self, cf_str):
     if not isinstance(cf_str, unicode):
         print 'param types error'
         return None
     ret = list()
     limit = 20000
     time_to = int(utils.utcnow_ts())
     time_from = time_to - 24 * 60 * 60
     db = self.get_db()
     
     rs = db.get_range2(cf_str, row_count=20)
     return list(rs)
     if not rs is None:
         for i in rs:
             ret.append(i[0])
     
     return ret
Пример #8
0
    def get_context_data(self, request):
        instance = self.tab_group.kwargs['instance']
        scf = 'total'
        statistic = 'avg'
        period = 10
        time_to = utils.utcnow().isoformat()
        time_from = datetime.fromtimestamp(utils.utcnow_ts() - 24 * 60 * 60).isoformat()
        cpu_data = api.get_instance_data(instance.id, 'cpu', scf, statistic, period, time_to, time_from)
        mem_max_data = api.get_instance_data(instance.id, 'mem_max', scf, statistic, period, time_to, time_from)
        mem_free_data = api.get_instance_data(instance.id, 'mem_free', scf, statistic, period, time_to, time_from)

        host = getattr(instance, "OS-EXT-SRV-ATTR:host", None)
        instance_name = getattr(instance, "OS-EXT-SRV-ATTR:instance_name", None)
        if(host):
            detail = api.get_instance_detail(instance_name, host)
            blk_read, blk_write, blk_name = list(), list(), list()
            nic_incoming, nic_outgoing, nic_name = list(), list(), list()
            for blk in detail['blk_devs']:
                if(blk):
                    blk_read.append(api.get_instance_data(instance.id, 'blk_read', blk, statistic, period, time_to, time_from))
                    blk_write.append(api.get_instance_data(instance.id, 'blk_write', blk, statistic, period, time_to, time_from))
                    blk_name.append(blk)
            for nic in detail['nic_devs']:
                if(nic):
                    nic_incoming.append(api.get_instance_data(instance.id, 'nic_incoming', nic, statistic, period, time_to, time_from))
                    nic_outgoing.append(api.get_instance_data(instance.id, 'nic_outgoing', nic, statistic, period, time_to, time_from))
                    nic_name.append(nic)

        return {
                "instance": instance,
                'cpu_data': cpu_data,
                'mem_max_data': mem_max_data,
                'mem_free_data': mem_free_data,
                'blk_read': blk_read,
                'blk_write': blk_write,
                'blk_name': blk_name,
                'nic_outgoing': nic_outgoing,
                'nic_incoming': nic_incoming,
                'nic_name': nic_name
                }
Пример #9
0
 def get_utc_sec():
     return utils.utcnow_ts()
Пример #10
0
 def get(self, key):
     """Retrieves the value for a key or None."""
     (timeout, value) = self.cache.get(key, (0, None))
     if timeout == 0 or utils.utcnow_ts() < timeout:
         return value
     return None
Пример #11
0
    def query_usage_report(self, args, **kwargs):
# TODO: how to use kwargs?
#    def query_usage_report(self, arg, id=None, metric='cpu', 
#                           metric_param='total',
#                           statistic='avg', period=5,
#                           timestamp_from=None, timestamp_to=None,
#                           **kwargs):
        """statistic is STATISTIC enum
        period default=5 minutes
        time_to default=0(now)"""
        """
        {
            'id': 'instance00001',
            'metric': 'network',
            'metric_param': 'vnet0',
            'statistic': 'sum',
            'period': 5,
            'timestamp_from': '2012-02-20T12:12:12',
            'timestamp_to': '2012-02-22T12:12:12',
        }
        """
#        usage_report = dict()
#        datetime_from = iso8601.parse_date(timestamp_from)
#        datetime_to = iso8601.parse_date(timestamp_to)
#        # TODO: implement
#        return {'data': usage_report}

        row_id = args['id']
        cf_str = args['metric']
        scf_str = args.setdefault("metric_param", "total")
        statistic = args['statistic']
        period = int(args['period'])
        timestamp_from = args['timestamp_from']
        timestamp_to = args['timestamp_to']
#        time_from = iso8601.parse_date(timestamp_from)
#        time_from = int(time.mktime(time_from.timetuple()))
        time_from = parse(timestamp_from)
        time_from = int(time.mktime(time_from.timetuple()))
        time_to = int(utils.utcnow_ts())
        if not timestamp_to is None:
#            time_to = iso8601.parse_date(timestamp_to)
#            time_to = int(time.mktime(time_to.timetuple()))
            time_to = parse(timestamp_to)
            time_to = int(time.mktime(time_to.timetuple()))
            
        bufkey = str([row_id, cf_str, scf_str, 
                      statistic, period, time_from, time_to])
        print "query_usage_report time range:", time_from, time_to
        if self.buf.hit_test(bufkey):
            print "buffer hit:", bufkey
            return self.buf.get_buf(bufkey)
            
        ret_len = 0
        (rs, count, all_data) = self.get_data(row_id, cf_str, scf_str, 
                                          time_from, time_to)
        if not rs is None and count > 0:
            buf = self.analyize_data(rs, 1, statistic)
            ret = self.analyize_data(buf, period, statistic, show=True)
            if ret is None:
                ret_len = 0
            else:
                ret = OrderedDict(sorted(ret.items(), key=lambda t: t[0]))
                ret_len = len(ret)
            print ret_len, "result."
        else:
            print "no result."
            ret = None
            ret_len = 0
            
        result = ret, ret_len, all_data
        if (not result is None and utils.utcnow_ts() - time_to > 120):
            self.buf.cleanup()
            self.buf.save(bufkey, result)
        return result
Пример #12
0
 def get(self, key):
     """Retrieves the value for a key or None."""
     (timeout, value) = self.cache.get(key, (0, None))
     if timeout == 0 or utils.utcnow_ts() < timeout:
         return value
     return None
Пример #13
0
def plugin_heartbeat(worker_id, status=1):
    """status: 0:I will exit; 1:working"""
    info = [worker_id, utils.utcnow_ts(), status]
    return MSG_TYPE.HEART_BEAT, info
Пример #14
0
 def end(self):
     info = [self.worker_id, utils.utcnow_ts(), 0]
     self.send(MSG_TYPE.HEART_BEAT, 0, json.dumps(info))
Пример #15
0
 def get_utc_sec():
     return utils.utcnow_ts()