예제 #1
0
    def test_context_manager(self):
        fake_socket = FakeSocket()
        with DogStatsd() as statsd:
            statsd.socket = fake_socket
            statsd.gauge('page.views', 123)
            statsd.timing('timer', 123)

        t.assert_equal('page.views:123|g\ntimer:123|ms', fake_socket.recv())
예제 #2
0
파일: graphite.py 프로젝트: orviz/z2g
def send_to_statsd(key, value, domain=None):
    message = "%s %s %s\n" % (key, value, time.time())
    message = remove_domain(message, domain)
    logger.debug("Sending message to Statsd: %s" % message)
    statsd.init_statsd({
        "STATSD_HOST": CONF.graphite.carbon_server,
        "STATSD_BUCKET_PREFIX": CONF.graphite.prefix or ''})
    statsd.gauge(key, value)
예제 #3
0
    def test_context_manager(self):
        fake_socket = FakeSocket()
        with DogStatsd() as statsd:
            statsd.socket = fake_socket
            statsd.gauge('page.views',123)
            statsd.timing('timer',123)

        t.assert_equal('page.views:123|g\ntimer:123|ms', fake_socket.recv())
예제 #4
0
파일: graphite.py 프로젝트: orviz/z2g
def send_to_statsd(key, value, domain=None):
    message = "%s %s %s\n" % (key, value, time.time())
    message = remove_domain(message, domain)
    logger.debug("Sending message to Statsd: %s" % message)
    statsd.init_statsd({
        "STATSD_HOST": CONF.graphite.carbon_server,
        "STATSD_BUCKET_PREFIX": CONF.graphite.prefix or ''
    })
    statsd.gauge(key, value)
예제 #5
0
def main():
    last_bandwidths = get_bandwidth()

    if last_bandwidths == None:
        logger.error("Can not find interface " + interface)
        exit(1)

    while True:
        time.sleep(1)
        current_bandwidths = get_bandwidth()
        rx = round(current_bandwidths[0] - last_bandwidths[0], 2)
        tx = round(current_bandwidths[1] - last_bandwidths[1], 2)
        logger.info(interface + ": RX: {rx}MBit/s TX: {tx}MBit/s".format(rx=rx, tx=tx))
        statsd.gauge("network." + hostname + "." + interface + ".rx", rx)
        statsd.gauge("network." + hostname + "." + interface + ".tx", tx)
        last_bandwidths = get_bandwidth()
예제 #6
0
def log_metric(statsd, car, metric, value):
    """
    Logs the metrics to statsd (influxdb)

    Inputs: statsd - a Statsd connection
            car - Care name
            metric - The name of the metric to be logged
            value - value to be logged for the metric
    """
    if metric == "timestamp":
        return None
    if is_metric(value):
        if value is True:
            value = 1
        if value is False:
            value = 0

        influx_measure = metric + ",device=tesla,car=" + car
        statsd.gauge(influx_measure, value)
        print(influx_measure, ": ", str(value))
예제 #7
0
 def test_gauge(self):
     statsd.gauge('gauged', 1)
     self.assertEqual(statsd._statsd._socket.data, b'gauged:1|g')
     statsd.gauge('gauged', 5)
     self.assertEqual(statsd._statsd._socket.data, b'gauged:5|g')
     statsd.gauge('gauged', -5, 0.99)
     self.assertTrue(statsd._statsd._socket.data.startswith(b'gauged:-5|g'))
     if statsd._statsd._socket.data != b'gauged:-5|g':
         self.assertTrue(statsd._statsd._socket.data.endswith(b'|@0.99'))
예제 #8
0
def cache_metrics(cache, lookup_type, statsd, hostname):
    if hasattr(cache, 'hit_miss_ratio'):
        hitratio = cache.hit_miss_ratio
        for metric in hitratio:
            statsd.gauge('%s.%i.hitratio.%s' % (hostname, os.getpid(), metric),
                         hitratio[metric])
    if hasattr(cache, 'profiling'):
        profiling = cache.profiling
        for metric in profiling:
            statsd.gauge(
                '%s.%i.profiling.%s' % (hostname, os.getpid(), metric),
                profiling[metric])

    if 'items' in lookup_type:
        for ltype in lookup_type['items']:
            if 'nb' in lookup_type['items'][ltype]:
                tvalue = lookup_type['items'][ltype]['nb']
                lookup_type['items'][ltype]['nb'] -= tvalue
            else:
                tvalue = 0
            if 'size' in lookup_type['items'][ltype]:
                tsize = lookup_type['items'][ltype]['size']
            else:
                tsize = 0

            statsd.gauge('%s.%i.lookuptype.items.%s.request_nb' %
                         (hostname, os.getpid(), ltype),
                         tvalue,
                         delta=True)
            #statsd.incr('%s.%i.lookuptype.items.%s.request_nb'%(hostname, os.getpid(),ltype), tvalue)

            statsd.gauge(
                '%s.%i.lookuptype.items.%s.size' %
                (hostname, os.getpid(), ltype), tsize)

    if 'not_allowed' in lookup_type:
        for naltype in lookup_type['not_allowed']:
            natvalue = lookup_type['not_allowed'][naltype]
            lookup_type['not_allowed'][naltype] -= natvalue
            statsd.gauge('%s.%i.lookuptype.not_allowed.%s.request_nb' %
                         (hostname, os.getpid(), naltype),
                         natvalue,
                         delta=True)

    if 'env' in lookup_type:
        for env in lookup_type['env']:
            if 'items' in lookup_type['env'][env]:
                for ltype in lookup_type['env'][env]['items']:
                    if 'nb' in lookup_type['env'][env]['items'][ltype]:
                        tvalue = lookup_type['env'][env]['items'][ltype]['nb']
                        lookup_type['env'][env]['items'][ltype]['nb'] -= tvalue

                        statsd.gauge(
                            '%s.%i.lookuptype.env.%s.items.%s.request_nb' %
                            (hostname, os.getpid(), env, ltype),
                            tvalue,
                            delta=True)

            if 'pmodules' in lookup_type['env'][env]:
                for pmodule in lookup_type['env'][env]['pmodules']:
                    if 'items' in lookup_type['env'][env]['pmodules'][pmodule]:
                        for ltype in lookup_type['env'][env]['pmodules'][
                                pmodule]['items']:
                            if 'nb' in lookup_type['env'][env]['pmodules'][
                                    pmodule]['items'][ltype]:
                                tvalue = lookup_type['env'][env]['pmodules'][
                                    pmodule]['items'][ltype]['nb']
                                lookup_type['env'][env]['pmodules'][pmodule][
                                    'items'][ltype]['nb'] -= tvalue
                                statsd.gauge(
                                    '%s.%i.lookuptype.env.%s.pmodules.%s.items.%s.request_nb'
                                    % (hostname, os.getpid(), env, pmodule,
                                       ltype),
                                    tvalue,
                                    delta=True)

    statsd.gauge('%s.%i.profiling.memory.used' % (hostname, os.getpid()),
                 memory_usage())
예제 #9
0
            statsd.set(environment + '.' + agent.name + '.' + 'success_rate', 1)
        else:
            statsd.set(environment + '.' + agent.name + '.' + 'episode', 1)
        found = False
        for t in range(max_attempts):
            action = agent.act(observation)
            new_observation, reward, done, info = env.step(action)
            if episodes % render_each == 0:
                env.render()
                time.sleep(sleep)

            agent.add_reward(observation, reward)
            observation = new_observation

            statsd.set(environment + '.' + agent.name, agent.experience.total_reward)
            statsd.gauge(environment + '.' + agent.name + '.' + 'avg_success_rate',
                         agent.experience.get_avg_success_rate())

            if done:
                found = True
                print("Episode finished after {} timesteps".format(t + 1))
                break
        if episodes % render_each == 0:
            information = agent.knowledge.get_information(observation)
            print(str(episodes) + "-" + str(agent.experience.get_avg_success_rate()) + "/" + str(
                len(information.behaviour)))
            information.show_top_behaviour()
        if not found:
            break
        observation = env.reset()

agent.knowledge.show_summary()
예제 #10
0
        def func(a, b, c=1, d=1):
            """docstring"""
            time.sleep(0.5)
            return (a, b, c, d)

        t.assert_equal('func', func.__name__)
        t.assert_equal('docstring', func.__doc__)

        result = func(1, 2, d=3)
        # Assert it handles args and kwargs correctly.
        t.assert_equal(result, (1, 2, 1, 3))

        packet = self.recv()
        name_value, type_ = packet.split('|')
        name, value = name_value.split(':')

        t.assert_equal('ms', type_)
        t.assert_equal('timed.test', name)
        self.assert_almost_equal(0.5, float(value), 0.1)

    def test_module_level_instance(self):
        t.assert_true(isinstance(statsd.statsd, statsd.DogStatsd))


if __name__ == '__main__':
    statsd = statsd
    while True:
        statsd.gauge('test.gauge', 1)
        statsd.increment('test.count', 2)
        time.sleep(0.05)
예제 #11
0
    def test_context_manager(self):
        fake_socket = FakeSocket()
        with DogStatsd() as statsd:
            statsd.socket = fake_socket
            statsd.gauge('page.views', 123)
            statsd.timing('timer', 123)

        t.assert_equal('page.views:123|g\ntimer:123|ms', fake_socket.recv())

    def test_batched_buffer_autoflush(self):
        fake_socket = FakeSocket()
        with DogStatsd() as statsd:
            statsd.socket = fake_socket
            for i in range(51):
                statsd.increment('mycounter')
            t.assert_equal('\n'.join(['mycounter:1|c' for i in range(50)]),
                           fake_socket.recv())

        t.assert_equal('mycounter:1|c', fake_socket.recv())

    def test_module_level_instance(self):
        t.assert_true(isinstance(statsd.statsd, statsd.DogStatsd))


if __name__ == '__main__':
    statsd = statsd
    while True:
        statsd.gauge('test.gauge', 1)
        statsd.increment('test.count', 2)
        time.sleep(0.05)
예제 #12
0
# Services to check
services = himutils.load_region_config('config/checks',
                                       region=region,
                                       log=logger)

for name, check in sorted(services['checks'].iteritems()):
    if 'timeout' in check:
        timeout = check['timeout']
    else:
        timeout = 10
    if check['ssl']:
        c = httplib.HTTPSConnection(check['host'], timeout=timeout)
    else:
        c = httplib.HTTPConnection(check['host'], timeout=timeout)
    try:
        c.request("HEAD", check['url'])
        response = c.getresponse()
        status = response.status
    except socket.error as e:
        logger.debug("=> error on connect: %s" % e)
        status = 0
    statsd.gauge(name, status)
    if status == check['code']:
        statsd.gauge(name, 1)
        print '%s -> ok' % check['host']
    else:
        statsd.gauge(name, 0)
        logger.debug('=> %s = %s' % (name, status))
        print '%s -> failed' % check['host']