示例#1
0
    def test_cgroup(self):
        if self.con is None:
            return
            
        print self.conts
        cids = [c['Id'] for c in self.conts]
        print 'CIDS', cids

        metrics = cgroupmgr.get_containers_metrics(cids)
        print "METRICS", metrics
示例#2
0
    def compute_stats(self):
        cids = self.containers.keys()
        stats = cgroupmgr.get_containers_metrics(cids)

        now = time.time()
        for (cid, nst) in stats.iteritems():
            c_stats = self.stats.get(cid, {})
            if self.last_stats != 0:
                diff = now - self.last_stats
            else:
                diff = 0
            
            for d in nst:
                k = '%s.%s' % (d['scope'], d['mname'])
                _type = d['type']
                scope = d['scope']
                if _type == 'gauge':
                    c_stats[k] = {'type':_type, 'key':k, 'value':d['value'], 'scope':scope}
                elif _type == 'rate':
                    o = c_stats.get(k, {})
                    rate_f = d['rate_f']
                    if o == {}:
                        # If there is no old value, don't need to compare rate as
                        # there is no older value
                        c_stats[k] = {'type':_type, 'key':k, 'value':None, 'raw_value':d['value'], 'scope':scope}
                        continue
                    
                    if rate_f is None:
                        rate_v = (d['value'] - o['raw_value']) / diff
                    else:
                        rate_v = rate_f(o['raw_value'], d['value'], diff)
                    c_stats[k] = {'type':_type, 'key':k, 'value':rate_v, 'raw_value':d['value'], 'scope':scope}
                    
            self.stats[cid] = c_stats

        # Keep stats only for the known containers
        to_del = []
        for cid in self.stats:
            if cid not in self.containers:
                to_del.append(cid)
        for cid in to_del:
            if cid in self.containers:
                del self.containers[cid]
            
        # tag the current time so we can diff rate in the future
        self.last_stats = now

        # Now pack the value based on the images if need
        self.aggregate_stats()