xtd.core.stat.handler module

class xtd.core.stat.handler.BaseHandler(p_name, p_interval=50, p_fetcher=None)[source]

Bases: xtd.core.tools.thread.SafeThread

Abstract statistic handler

Spawns a dedicated thread that outputs a set counters every p_interval seconds.

User can provide a custom function to get the counters to output. By default the object gets all counters registered in the StatManager singleton.

User should inherit this class and define write() method.

Note

The functor must take no parameter and return something suitable for write()

Parameters:
  • p_name (str) – handler name
  • p_interval (int) – interval, in second, between two outputs
  • p_fetcher (function) – functor that retrieves data counters
__init__(p_name, p_interval=50, p_fetcher=None)[source]
write(p_counters)[source]

Ouput available counter data (abstract)

Parameters:p_counters (dict) – dictionary holding all available counters assiciated with their namespace. { "<namespace>" : [ <counter1>, <counter2>, ... ] }
work()[source]

Output available counter data

Implementation of xtd.core.tools.thread.SafeThread.work()

__module__ = 'xtd.core.stat.handler'
class xtd.core.stat.handler.DiskHandler(p_directory, p_interval=50, p_fetcher=None)[source]

Bases: xtd.core.stat.handler.BaseHandler

Output counters to filesystem

Given :

This will create a file /var/snmp/a/b/c/counter.name containing the string 55

Parameters:
  • p_directory (str) – target output directory path. If given directory doesn’t exist, the object will attempt to create it (and all necessary parent directories).
  • p_interval (int) – interval between two outputs (in seconds)
Raises:

XtdErrorp_directory isn’t writable or could ne be created

__init__(p_directory, p_interval=50, p_fetcher=None)[source]
write(p_counters)[source]

Write all available counters to filesystem

When object fails to write a counte file, an error log is triggered. Raising an exception woudn’t be helpfull since this part of the code runs in a dedicated thread.

Parameters:p_counters (dict) – see BaseHandler.write()
__module__ = 'xtd.core.stat.handler'
class xtd.core.stat.handler.HttpHandler(p_url, p_interval=50, p_fetcher=None)[source]

Bases: xtd.core.stat.handler.BaseHandler

Send counter values to a http server

Counter values are gathered in a single json and sent to the target url as a POST request.

HTTP Request details :

  • Method : POST

  • Content-Type : application/json

  • Body :

    {
      "<namespace>" : {
            "<name-of-counter-1>" : value-of-counter-1,
            "<name-of-counter-2>" : value-of-counter-2,
            ...
      },
      ...
      "<namespace>" : {
        <name-of-counter-N>" : value-of-counter-N
        ...
      }
    }
    

Note

Keep in mind that counter values can be undefined.

In such case, the json value is a string equals to "NaN".

Parameters:
  • p_url – target url for post request
  • p_interval (int) – interval, in second, between two outputs
  • p_fetcher (function) – functor that retrieves data counters
__init__(p_url, p_interval=50, p_fetcher=None)[source]
write(p_counters)[source]

Write all available counters to filesystem

When object fails to send HTTP request, an error log is triggered. Raising an exception woudn’t be helpfull since this part of the code runs in a dedicated thread.

Parameters:p_counters (dict) – see BaseHandler.write()
__module__ = 'xtd.core.stat.handler'
class xtd.core.stat.handler.LoggingHandler(p_name, p_interval=50, p_fetcher=None)[source]

Bases: xtd.core.stat.handler.BaseHandler

Output counter to application logs

Parameters:
  • p_name (str) – logger module name
  • p_interval (int) – interval, in second, between two outputs
  • p_fetcher (function) – functor that retrieves data counters
__init__(p_name, p_interval=50, p_fetcher=None)[source]
__module__ = 'xtd.core.stat.handler'
write(p_counters)[source]

Output all available counters to logging facility :param p_counters: see BaseHandler.write()