The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Zabbix2::API::Graph -- Zabbix graph objects

SYNOPSIS

  use Zabbix2::API::Graph;
  
  my $graph = $zabbix->fetch_single('Graph', params => { ... });
  
  my $items = $zabbix->fetch('Item', params => { ... });
  $graph->add_items(@{$items});
  $graph->update;
  
  $graph->delete;

DESCRIPTION

Handles CRUD for Zabbix graph objects.

This is a subclass of Zabbix2::API::CRUDE.

ATTRIBUTES

color_wheel

(read-only coderef, with predicate and clearer)

This attribute is used to generate hex triplets representing colors. See make_color_wheel for a complete usage explanation.

graphitems

(read-write arrayref of Zabbix2::API::GraphItem instances)

This attribute is populated automatically when the Perl object is updated from the "gitems" server property (i.e. when the pull method is called).

Likewise, it is automatically used to populate the "gitems" property before either create or update are called.

METHODS

add_items

  my $items = $zabbix->fetch('Item', ...);
  $graph->add_items(@{$items});

This method is a shortcut to create graphs with the least hassle. It pushes new graph items in the graph's graphitems attribute, providing only the corresponding item ID and a color generated from the graph's color_wheel.

Returns the Zabbix2::API::GraphItem objects created.

make_color_wheel

  my $color_wheel = Zabbix2::API::Graph->make_color_wheel([ qw/1C1CCC 1CCC1C CC1C1C/ ... ]);

This class method returns an iterator over its argument. This makes it easy to repeatedly generate colors for graph items. Once the last element in the color array is reached, the iterator wraps around.

The default color wheel in the color_wheel attribute is

  [qw/1C1CCC 1CCC1C CC1C1C FDFD49 9A1C9A 1CCCCC FD8C1C/]
  # royal blue, green, dark red, yellow, purple, turquoise, orange

which is a decently contrasted set of not-too-flashy colors, and happens to be the set of colors used in a CPU utilization graph at work.

url

  my $url = $graph->url(width => $width,
                        height => $height,
                        period => $period,
                        start_time => $start_time);

This method returns a URL to an image on the Zabbix server. The image of width width and height height will represent the current graph, plotted for data starting at start_time (a UNIX timestamp) over period seconds. (Note that the height and width parameters describe the plotting area; Zabbix will then make the final image bigger so that the legend and title can fit.) It uses the current connection's host name to guess what path to base the URL on.

All parameters are optional.

If the current user agent has cookies enabled, you can even fetch the image directly, since your API session is completely valid for all regular requests:

  my $zabbix = Zabbix2::API->new(server => ...,
                                 ua => LWP::UserAgent->new(cookie_jar => { file => 'cookie.jar' }),
                                 ...);
  my $graph = $zabbix->fetch_single('Graph', ...);
  my $response = $zabbix->useragent->get($graph->url);
  open my $image, '>', 'graph.png' or die $!;
  $image->print($response->decoded_content);
  $image->close;

EXPORTS

Some constants:

  GRAPH_TYPE_NORMAL
  GRAPH_TYPE_STACKED
  GRAPH_TYPE_PIE
  GRAPH_TYPE_EXPLODED

They are not exported by default, only on request; or you could import the :graphtypes tag.

A bunch of constants (graphitem types, axis stuff, ...) are not defined. If you need them, send me a feature request (or better, a pull request).

SEE ALSO

Zabbix2::API::CRUDE.

AUTHOR

Fabrice Gabolde <fga@cpan.org>

COPYRIGHT AND LICENSE

Copyright (C) 2011, 2014 SFR

This library is free software; you can redistribute it and/or modify it under the terms of the GPLv3.