API

The goal of the circonus package is to combine the ease of use of the requests.Response API with the robust Circonus REST API.

Client Interface

The majority of functionality is provided by methods belonging to the CirconusClient. Most methods return a requests.Response object directly. This gives access to most of the information that requests.request() had when the API call was made, along with the response details.

The Circonus REST API returns a JSON encoded response that is available via the requests.Response.json() method on the returned object. In the event of an error the details will available there as well.

class circonus.CirconusClient(api_app_name, api_token, common_tags=None)

Construct a CirconusClient.

Parameters:
  • api_app_name (str) – The Circonus API application name.
  • api_token (str) – The Circonus API token.
  • common_tags (list) – (optional) The str tags to apply to all resources.
Return type:

CirconusClient

Usage:

>>> from circonus import CirconusClient
>>> circonus = CirconusClient("my-circonus-app", "generated-by-circonus-ui")
>>> response = circonus.get("/user/current")
>>> response.json()
{u'_cid': u'/user/1234',
 u'contact_info': {u'sms': u'', u'xmpp': u''},
 u'email': u'user@example.com',
 u'firstname': u'Ewe',
 u'lastname': u'Sure'}
get(resource_type_or_cid, params=None)

Get the resource at resource type or cid via requests.get().

Parameters:
  • resource_type_or_cid (str) – The resource type or cid representing a specific resource.
  • params (dict) – (optional) The parameters to pass to requests.get().
Return type:

requests.Response

If a resource type is given, e.g., /user, a list of resources will exist in the Response JSON.

If a cid is given, e.g., /check_bundle/123456, a single resource will exist in the Response JSON.

create(resource_type, data)

Create the resource type with data via requests.post().

Parameters:
  • resource_type (str) – The resource type to create.
  • data (dict) – The data used to create the resource.
Return type:

requests.Response

update(cid, data)

Update the resource at cid with data via requests.put().

Parameters:
  • cid (str) – The resource to update.
  • data (dict) – The data used to update the resource.
Return type:

requests.Response

delete(cid, params=None)

Delete the resource at cid via requests.delete().

Parameters:
Return type:

requests.Response

annotation(title, category, description='', rel_metrics=None)

Context manager and decorator for creating Annotation instances.

Parameters:
  • title (str) – The title.
  • category (str) – The category.
  • description (str) – (optional) The description.
  • rel_metrics (list) – (optional) The str names of metrics related to this annotation.
Return type:

Annotation

If rel_metrics is given, the metric names should be specified in the fully qualified format <digits>_<string> as required by the Circonus API documentation for annotation.

create_annotation(title, category, start=None, stop=None, description='', rel_metrics=None)

Create an Annotation instance immediately.

Parameters:
  • title (str) – The title.
  • category (str) – The category.
  • start (datetime.datetime) – (optional) The start time.
  • stop (datetime.datetime) – (optional) The stop time.
  • description (str) – (optional) The description.
  • rel_metrics (list) – (optional) The str names of metrics related to this annotation.
Return type:

Annotation

Note: sub-second annotation time resolution is not supported by Circonus.

stop defaults to the value of start if it is not given.

If rel_metrics is given, the metric names should be specified in the fully qualified format <digits>_<string> as required by the Circonus API documentation for annotation.

create_collectd_cpu_graph(check_bundle, title=None)

Create a CPU graph from the collectd check_bundle.

Parameters:
  • check_bundle (dict) – The check bundle to create a graph for.
  • title (str) – (optional) The title to use for the graph.
Return type:

requests.Response or None

None is returned if no data to create the graph could be found in check_bundle.

create_collectd_df_graph(check_bundle, mount_dir, title=None)

Create a disk free graph from the collectd check_bundle for mount_dir on target.

Parameters:
  • check_bundle (dict) – The check bundle to create a graph for.
  • mount_dir (str) – The mount directory to create the graph for.
  • title (str) – (optional) The title to use for the graph.
Return type:

requests.Response or None

None is returned if no data to create the graph could be found in check_bundle.

create_collectd_graphs(check_bundle, interface_names=None, mount_dirs=None, titles=None)

Create several graphs from the collectd check_bundle.

Parameters:
  • check_bundle (dict) – The check bundle to create graphs for.
  • interface_name (list) – (optional) The interface names to create interface graphs for, e.g., ["eth0"].
  • mount_dirs (list) – (optional) The mount directories to create df graphs for, e.g., ["/root", "/mnt"].
  • titles (dict) – (optional) The titles to use for each graph.
Return type:

(bool, list)

mount_dirs should be a list of directories where devices are mounted. df graphs will be created for each.

interface_names should be a list of network interface device names. interface graphs will be created for each.

titles should be a dict instance mapping a key representing the collectd plugin name to a title for the graph representing it. For example:

>>> {"cpu": "test.example.com CPU", "df": "test.example.com Disk"}

Return True if all collectd graphs were created successfully, False otherwise, along with a list of requests.Response instances for requests made.

create_collectd_interface_graph(check_bundle, interface_name='eth0', title=None)

Create an interface graph from the collectd check_bundle.

Parameters:
  • check_bundle (dict) – The check bundle to create a graph for.
  • interface_name (str) – (optional) The interface name, e.g., “eth0”.
  • title (str) – (optional) The title to use for the graph.
Return type:

requests.Response or None

None is returned if no data to create the graph could be found in check_bundle.

create_collectd_memory_graph(check_bundle, title=None)

Create a memory graph from the collectd check_bundle.

Parameters:
  • check_bundle (dict) – The check bundle to create a graph for.
  • title (str) – (optional) The title to use for the graph.
Return type:

requests.Response or None

None is returned if no data to create the graph could be found in check_bundle.

update_with_tags(cid, new_tags)

Update the resource at cid to have new_tags added to it via update().

Parameters:
  • cid (str) – The resource to update.
  • new_tags (list) – The str tags to add to the resource.
Return type:

requests.Response or False

False is returned in instances where a request to update the resource was not necessary because the resource is already tagged with new_tags.

Client Functions

Several functions exist that CirconusClient depends on and may be useful elsewhere:

circonus.client.get_api_url(resource_type_or_cid)

Get a valid fully qualified Circonus API URL for the given resource type or cid.

Parameters:resource_type_or_cid (str) – The resource type or cid representing a specific resource.
Returns:The API URL.
Return type:str
circonus.client.with_common_tags(f)

Decorator to ensure that common tags exist on resources.

Only resources which support tagging via the Circonus API will be affected.

circonus.client.log_http_error(f)

Decorator for logging any HTTPError raised by a request.

Raises:requests.exceptions.HTTPError

Classes

class circonus.client.Annotation(client, title, category, description='', rel_metrics=None)

Construct an Annotation.

Parameters:
  • client (CirconusClient) – The client to create the annotation with.
  • title (str) – The title.
  • category (str) – The category.
  • description (str) – (optional) The description.
  • rel_metrics (list) – (optional) The str names of metrics related to this annotation.

If rel_metrics is given, the metric names should be specified in the fully qualified format <digits>_<string> as required by the Circonus API documentation for annotation.

create()

Create an annotation from the current state.

Modules

circonus.collectd

Turn collectd check bundles into graphs and worksheets.

circonus.collectd.cpu

Create graph data from a collectd check bundle containing cpu metrics.

circonus.collectd.cpu.CPU_METRIC_RE = <_sre.SRE_Pattern object at 0x7f36f9a1cbb0>

A compiled regular expression which matches collectd metrics.

circonus.collectd.cpu.CPU_METRIC_SUFFIXES = ['steal', 'interrupt', 'softirq', 'system', 'wait', 'user', 'nice', 'idle']

Ordered list of metric suffixes.

This list is used to filter and sort metrics in preparation for creating a graph.

circonus.collectd.cpu.CPU_NUMBER_RE = <_sre.SRE_Pattern object at 0x7f36f99fa978>

A compiled regular expression which captures CPU number from the CPU metric.

circonus.collectd.cpu.get_cpu_datapoints(check_bundle, metrics)

Get a list of datapoints from sorted metrics.

Parameters:
  • check_bundle (dict) – The check bundle.
  • metrics (list) – Sorted CPU metrics.
Return type:

:py:class`list`

circonus.collectd.cpu.get_cpu_graph_data(check_bundle, title=None)

Get graph data for check_bundle.

Parameters:
  • check_bundle (dict) – The check bundle to create graph data with.
  • title (str) – (optional) The title to use for the graph.
Return type:

dict

title defaults to using check_bundle["target"].

The returned data dict can be used to create() a graph.

circonus.collectd.cpu.get_cpu_metrics(metrics)

Get a sorted list of metrics from metrics.

Parameters:metrics (list) – The metrics to sort.

The CPU metrics are sorted by:

  1. Name, ascending
  2. Explicit suffix, i.e., CPU_METRIC_SUFFIXES
circonus.collectd.cpu.get_stacked_cpu_metrics(metrics, hide_idle=True)

Get metrics with the stack attribute added.

Parameters:
  • metrics (list) – The metrics to stack.
  • hide_idle (bool) – (optional) Hide CPU idle.
Return type:

list

Each CPU will be added to a stack group equal to that CPU’s number. CPU idle metrics are hidden by default. metrics is not modified by this function.

circonus.collectd.df

Create graph data from a collectd check bundle containing df metrics.

circonus.collectd.df.DF_METRIC_RE = <_sre.SRE_Pattern object at 0x7f36f9370b10>

A compiled regular expression which matches collectd metrics.

circonus.collectd.df.DF_METRIC_SUFFIXES = ['reserved', 'used', 'free']

Ordered list of metric suffixes.

This list is used to filter and sort metrics in preparation for creating a graph.

circonus.collectd.df.get_df_datapoints(check_bundle, metrics)

Get a list of datapoints from sorted metrics.

Parameters:
  • check_bundle (dict) – The check bundle.
  • metrics (list) – The sorted metrics to cerate datapoints with.
Return type:

list

circonus.collectd.df.get_df_graph_data(check_bundle, mount_dir, title=None)

Get graph data for check_bundle.

Parameters:
  • check_bundle (dict) – The check bundle to create graph data with.
  • title (str) – (optional) The title to use for the graph.
  • mount_dir (str) – The mount directory to create graph data for.
Return type:

dict

title defaults to using check_bundle["target"]. df and mount_dir will be appended to title.

The returned data dict can be used to create() a graph.

circonus.collectd.df.get_df_metrics(metrics, mount_dir)

Get disk free metrics from metrics for mount_dir.

Parameters:
  • metrics (list) – The metrics.
  • mount_dir (str) – The mount directory to get metrics for.
Return type:

list

circonus.collectd.df.get_sorted_df_metrics(metrics)

Get sorted disk free metrics from metrics.

Parameters:metrics (list) – The metrics to sort.
Return type:list
circonus.collectd.df.is_mount_dir(metric_name, mount_dir)

Is metric_name the collectd representation of mount_dir?

collectd represents mount directories with / as -. This makes it impossible to know if a - in a collectd mount directory was a / or a - on the host, e.g., a mount directory may be /mnt/solr-home and the metric name representing it may be df`mnt-solr-home`df_complex`free.

This function takes a naïve approach and removes all punctuation from both directory names before comparing them.

Both directory names are coerced with unicode() before translation because requests encodes responses in UTF-8 by default and Python has different signatures for the function string.translate() and the method str.translate() method.

circonus.collectd.graph

Create graph data for several collectd graphs.

circonus.collectd.graph.get_collectd_graph_data(check_bundle, interface_names, mount_dirs, titles=None)

Get collectd graph data for check_bundle.

Parameters:
  • check_bundle (dict) – The check bundle to get graph data from.
  • interface_names (list) – The interface names to get data for.
  • mount_dirs (list) – The mount directories to get data for.
  • titles (dict) – (optional) The titles to use for each graph.
Return type:

list

titles should be a dict instance mapping a key representing the collectd plugin name to a title for the graph representing it. For example:

>>> {"cpu": "test.example.com CPU", "df": "test.example.com Disk"}

The returned list will only contain valid graph data.

circonus.collectd.memory

Create graph data from a collectd check bundle.

circonus.collectd.memory.MEMORY_METRIC_RE = <_sre.SRE_Pattern object at 0x7f36f9370ce0>

A compiled regular expression which matches collectd metrics.

circonus.collectd.memory.MEMORY_METRIC_SUFFIXES = ['used', 'buffered', 'cached', 'free']

Ordered list of metric suffixes.

This list is used to filter and sort metrics in preparation for creating a graph.

circonus.collectd.memory.get_memory_datapoints(check_bundle, metrics)

Get a list of datapoints from sorted metrics.

Parameters:
  • check_bundle (dict) – The check bundle.
  • metrics (list) – The sorted metrics to cerate datapoints with.
Return type:

list

circonus.collectd.memory.get_memory_graph_data(check_bundle, title=None)

Get graph data for check_bundle.

Parameters:
  • check_bundle (dict) – The check bundle to create graph data with.
  • title (str) – (optional) The title to use for the graph.
Return type:

dict

title defaults to using check_bundle["target"].

The returned data dict can be used to create() a graph.

circonus.collectd.memory.get_sorted_memory_metrics(metrics)

Get a sorted list of metrics from metrics.

Parameters:metrics (list) – The metrics to sort.

The metrics are sorted by explicit suffix, i.e., MEMORY_METRIC_SUFFIXES.

circonus.collectd.interface

Create graph data from a collectd check bundle containing interface metrics.

Transmitted metrics will be on the top of the graph and received metrics will be on the bottom.

circonus.collectd.interface.DATA_FORMULA_RECEIVER = '=-8*VAL'

The data formula to apply to received interface metrics.

circonus.collectd.interface.DATA_FORMULA_TRANSMITTER = '=8*VAL'

The data formula to apply to transmitted interface metrics.

circonus.collectd.interface.get_interface_datapoints(check_bundle, interface_name='eth0')

Get a list of datapoints for check_bundle and interface_name.

Parameters:
  • check_bundle (list) – The check bundle.
  • interface_name (str) – (optional) The interface name, e.g., “eth0”.
Return type:

list

octets and errors will be returned. octets datapoints have data formulas added to them which makes them render as bits per second. Transmitted octets will be on the top and received octets will be on the bottom of the graph due to the data formulas.

circonus.collectd.interface.get_interface_graph_data(check_bundle, interface_name='eth0', title=None)

Get graph data for check_bundle.

Parameters:
  • check_bundle (dict) – The check bundle to create graph data with.
  • title (str) – (optional) The title to use for the graph.
  • interface_name (str) – (optional) The interface name, e.g., “eth0”.
Return type:

dict

title defaults to using check_bundle["target"]. interface_name and bit/s will be appended to title.

The returned data dict can be used to create() a graph.

circonus.collectd.interface.get_interface_metrics(metrics, interface_name='eth0', kind=None)

Get metrics for interface interface_name and kind.

Parameters:
  • metrics (list) – The metrics.
  • interface_name (str) – (optional) The interface name, e.g., “eth0”.
  • kind (str) – (optional) The kind of interface metrics to get, e.g., “octets”.
Return type:

list

circonus.collectd.interface.is_receiver(metric)

Is interface metric a receiver?

Parameters:metric (dict) – The metric.
Return type:bool
circonus.collectd.interface.is_transmitter(metric)

Is interface metric a transmitter?

Parameters:metric (dict) – The metric.
Return type:bool

circonus.graph

Create graph data from a check bundle.

circonus.graph.get_graph_data(check_bundle, datapoints, custom_data=None)

Get graph data for check_bundle and datapoints.

Parameters:
  • check_bundle (dict) – The check bundle to create graph data for.
  • datapoints (list) – The datapoints to include in the graph data.
  • custom_data (dict) – (optional) The custom data to include in the graph data.
Return type:

dict

Merge common graph data with custom_data. Add the telemetry tag based on the type attribute of check_bundle.

circonus.metric

Manipulate check metrics.

circonus.metric.get_datapoints(check_id, metrics, custom=None)

Get a list of datapoints for check_id from metrics.

Parameters:
  • check_id (str) – The check id.
  • metrics (list) – The metrics.
  • custom (dict) – (optional) The custom datapoint attributes used to update each datapoint.
Return type:

list

Datapoints determine how metrics are rendered on a graph. This function merges values from metrics with a few default values of required datapoint attributes. Datapoint attributes can be overridden with the custom parameter. The custom dict is used to update() each datapoint as it is created.

circonus.metric.get_metrics(check_bundle, metric_re)

Get a list of metrics from check_bundle.

Parameters:
  • check_bundle (dict) – A check bundle with metrics.
  • metric_re (re) – Regular expression matching metrics to return.
Return type:

list

circonus.metric.get_metrics_sorted_by_suffix(metrics, suffixes)

Get a list of metrics sorted by suffix from the list of metrics.

Parameters:
  • metrics (list) – Metrics to sort.
  • suffixes (list) – Sorted list of suffixes used to sort the return metrics list.
Return type:

list

Sort the metrics list by metric names ending with values in the suffixes list. When creating graphs with stacked metrics the order in which metrics are stacked affects the manner in which they are displayed, e.g., perhaps “free” memory makes the most sense when it is at the top of a memory graph.

If there are not enough metrics to sort for suffixes an empty list is returned.

circonus.metric.get_metrics_with_status(metrics, status, metric_re=None)

Get a copy of metrics with each status attribute value set to status.

Parameters:
  • metrics (list) – The metrics to set statuses for.
  • status (str) – The status to set on each metric.
  • metric_re (re) – (optional) The compiled regular expression used to match metrics to update.
Return type:

list

metrics is not modified by this function.

circonus.tag

Manipulate tags on resources that support them.

circonus.tag.TAGGABLE_RESOURCES = ['check_bundle', 'contact_group', 'graph', 'maintenance', 'metric_cluster', 'template', 'worksheet']

Circonus API resources for which tags can be modified.

circonus.tag.get_tag_string(tag, category=None)

Get a string representing tag.

Parameters:
  • tag (str) – The tag.
  • category (str) – (optional) The category.
Return type:

str

Circonus requires categorized tags to be a string of the form, “category:tag”. Uncategorized tags are simply, “tag”.

circonus.tag.get_tags_with(resource, tags)

Get the list of tags for resource with tags added to it.

Parameters:
  • resource (dict) – The resource with a tags key.
  • tags (list) – The tags to add to resource.
Return type:

list or None

If tags changes the existing tags on the resource by adding new tags then that new list of tags is returned.

If tags does not change the existing tags on the resource then None is returned.

All other failure states resulting from trying to add the list of tags to resource will return None.

circonus.tag.get_tags_without(resource, tags)

Get the list of tags for resource with tags removed from it.

Parameters:
  • resource (dict) – The resource with a tags key.
  • tags (list) – The tags to remove from resource.
Return type:

list or None

If tags changes the existing tags on the resource by removing new tags then that new list of tags is returned.

If tags does not change the existing tags on the resource then None is returned.

All other failure states resulting from trying to remove the list of tags to resource will return None.

circonus.tag.get_telemetry_tag(check_bundle)

Get a telemetry tag string for check_bundle.

Parameters:check_bundle (dict) – The check bundle to get a telemetry tag from.
Return type:str

If check_bundle has a type attribute, a tag of the form “telemetry:type” will be returned. This makes filtering check bundles by the source of telemetry data easier in the Circonus UI.

circonus.tag.is_taggable(cid)

Is the resource represented by the given cid taggable?

Parameters:cid (str) – The cid of a resource that may support tags.
Return type:bool

Only resources which support tagging via the Circonus API are considered taggable. Resources which have a _tags attribute are not considered taggable since the _tags list cannot be updated via the API – it is read-only.

circonus.util

Utility functions that other modules depend upon.

circonus.util.colors(items)

Create a generator which returns colors for each item in items.

Parameters:items (list) – The list to generate colors for.
Return type:generator(colour.Color)
circonus.util.datetime_to_int(dt)

Convert date and time to seconds since the epoch.

Parameters:dt (datetime.datetime) – The date and time to convert.
Return type:int

dt is expected to have been created for the UTC date and time, e.g., with datetime.datetime.utcnow(). It is converted to seconds since the epoch with calendar.timegm() to respect UTC.

circonus.util.get_check_id_from_cid(cid)

Get a check id integer from cid.

Parameters:cid (str) – The check id.
Return type:int
circonus.util.get_resource_from_cid(cid)

Get the resource name from cid.

Parameters:cid (str) – The check id.
Return type:str