line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Zabbix2::API::Graph;
|
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
509
|
use strict;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
31
|
|
4
|
1
|
|
|
1
|
|
6
|
use warnings;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
38
|
|
5
|
1
|
|
|
1
|
|
19
|
use 5.010;
|
|
1
|
|
|
|
|
4
|
|
6
|
1
|
|
|
1
|
|
5
|
use Carp;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
65
|
|
7
|
1
|
|
|
1
|
|
7
|
use autodie;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
6
|
|
8
|
1
|
|
|
1
|
|
5605
|
use utf8;
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
17
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
33
|
use URI;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
49
|
|
11
|
1
|
|
|
1
|
|
23
|
use Params::Validate qw/validate :types/;
|
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
210
|
|
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
8
|
use Moo;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
6
|
|
14
|
|
|
|
|
|
|
extends qw/Exporter Zabbix2::API::CRUDE/;
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# extracted from frontends/php/include/defines.inc.php
|
17
|
|
|
|
|
|
|
use constant {
|
18
|
1
|
|
|
|
|
149
|
GRAPH_TYPE_NORMAL => 0,
|
19
|
|
|
|
|
|
|
GRAPH_TYPE_STACKED => 1,
|
20
|
|
|
|
|
|
|
GRAPH_TYPE_PIE => 2,
|
21
|
|
|
|
|
|
|
GRAPH_TYPE_EXPLODED => 3,
|
22
|
1
|
|
|
1
|
|
632
|
};
|
|
1
|
|
|
|
|
4
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
our @EXPORT_OK = qw/GRAPH_TYPE_NORMAL GRAPH_TYPE_STACKED GRAPH_TYPE_PIE GRAPH_TYPE_EXPLODED/;
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
our %EXPORT_TAGS = (
|
27
|
|
|
|
|
|
|
graphtypes => [ qw/GRAPH_TYPE_NORMAL GRAPH_TYPE_STACKED GRAPH_TYPE_PIE GRAPH_TYPE_EXPLODED/ ]
|
28
|
|
|
|
|
|
|
);
|
29
|
|
|
|
|
|
|
|
30
|
1
|
|
|
1
|
|
505
|
use Zabbix2::API::GraphItem;
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
1295
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
has 'graphitems' => (is => 'rw');
|
33
|
|
|
|
|
|
|
has 'color_wheel' => (is => 'ro',
|
34
|
|
|
|
|
|
|
clearer => 1,
|
35
|
|
|
|
|
|
|
lazy => 1,
|
36
|
|
|
|
|
|
|
builder => '_build_color_wheel');
|
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub make_color_wheel {
|
39
|
0
|
|
|
0
|
1
|
|
my ($class, $colors) = @_;
|
40
|
|
|
|
|
|
|
return sub {
|
41
|
0
|
|
|
0
|
|
|
state $iterator = 0;
|
42
|
0
|
|
|
|
|
|
my $color = $colors->[$iterator];
|
43
|
0
|
|
|
|
|
|
$iterator++;
|
44
|
0
|
|
|
|
|
|
$iterator = $iterator % scalar(@{$colors});
|
|
0
|
|
|
|
|
|
|
45
|
0
|
|
|
|
|
|
return $color;
|
46
|
0
|
|
|
|
|
|
};
|
47
|
|
|
|
|
|
|
}
|
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub _build_color_wheel {
|
50
|
0
|
|
|
0
|
|
|
my $self = shift;
|
51
|
|
|
|
|
|
|
# TODO pick nicer colors
|
52
|
0
|
|
|
|
|
|
return $self->make_color_wheel([qw/1C1CCC 1CCC1C CC1C1C FDFD49 9A1C9A 1CCCCC FD8C1C/]);
|
53
|
|
|
|
|
|
|
}
|
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub add_items {
|
56
|
0
|
|
|
0
|
1
|
|
my ($self, @items) = @_;
|
57
|
0
|
|
|
|
|
|
my @graphitems = map { Zabbix2::API::GraphItem->new(
|
|
0
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
root => $self->root,
|
59
|
|
|
|
|
|
|
data => { itemid => $_->id,
|
60
|
|
|
|
|
|
|
color => $self->color_wheel->() }) } @items;
|
61
|
0
|
0
|
|
|
|
|
if ($self->id) {
|
62
|
|
|
|
|
|
|
# has been sync'd with server at least once.
|
63
|
0
|
|
|
|
|
|
push @{$self->graphitems}, @graphitems;
|
|
0
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
} else {
|
65
|
|
|
|
|
|
|
# assume graphitems is empty, avoids fetching the graphitems
|
66
|
|
|
|
|
|
|
# list which we cannot do without a graphid
|
67
|
0
|
|
|
|
|
|
$self->graphitems(\@graphitems);
|
68
|
|
|
|
|
|
|
}
|
69
|
0
|
|
|
|
|
|
return @graphitems;
|
70
|
|
|
|
|
|
|
}
|
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub id {
|
73
|
|
|
|
|
|
|
## mutator for id
|
74
|
0
|
|
|
0
|
1
|
|
my ($self, $value) = @_;
|
75
|
0
|
0
|
|
|
|
|
if (defined $value) {
|
76
|
0
|
|
|
|
|
|
$self->data->{graphid} = $value;
|
77
|
0
|
|
|
|
|
|
return $self->data->{graphid};
|
78
|
|
|
|
|
|
|
} else {
|
79
|
0
|
|
|
|
|
|
return $self->data->{graphid};
|
80
|
|
|
|
|
|
|
}
|
81
|
|
|
|
|
|
|
}
|
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub _readonly_properties {
|
84
|
|
|
|
|
|
|
## hash of item properties that cannot be updated; they will be
|
85
|
|
|
|
|
|
|
## removed before pushing the item to the server
|
86
|
|
|
|
|
|
|
return {
|
87
|
0
|
|
|
0
|
|
|
graphid => 1,
|
88
|
|
|
|
|
|
|
flags => 1,
|
89
|
|
|
|
|
|
|
templateid => 1,
|
90
|
|
|
|
|
|
|
};
|
91
|
|
|
|
|
|
|
}
|
92
|
|
|
|
|
|
|
sub _prefix {
|
93
|
0
|
|
|
0
|
|
|
my ($class, $suffix) = @_;
|
94
|
0
|
0
|
|
|
|
|
if ($suffix) {
|
95
|
0
|
|
|
|
|
|
return 'graph'.$suffix;
|
96
|
|
|
|
|
|
|
} else {
|
97
|
0
|
|
|
|
|
|
return 'graph';
|
98
|
|
|
|
|
|
|
}
|
99
|
|
|
|
|
|
|
}
|
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub _extension {
|
102
|
0
|
|
|
0
|
|
|
return (output => 'extend',
|
103
|
|
|
|
|
|
|
selectHosts => ['hostid'],
|
104
|
|
|
|
|
|
|
selectGraphItems => 'extend');
|
105
|
|
|
|
|
|
|
}
|
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub name {
|
108
|
0
|
|
0
|
0
|
1
|
|
return shift->data->{name} || '???';
|
109
|
|
|
|
|
|
|
}
|
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub url {
|
112
|
0
|
|
|
0
|
1
|
|
my $self = shift;
|
113
|
0
|
|
|
|
|
|
my $base_url = $self->{root}->{server};
|
114
|
0
|
|
|
|
|
|
my %args = validate(@_, { width => { type => SCALAR, optional => 1, regex => qr/^\d+$/ },
|
115
|
|
|
|
|
|
|
height => { type => SCALAR, optional => 1, regex => qr/^\d+$/ },
|
116
|
|
|
|
|
|
|
period => { type => SCALAR, optional => 1, regex => qr/^\d+$/ },
|
117
|
|
|
|
|
|
|
start_time => { type => SCALAR, optional => 1, regex => qr/^\d{14}$/ } });
|
118
|
|
|
|
|
|
|
|
119
|
0
|
|
|
|
|
|
my $url = URI->new($base_url);
|
120
|
0
|
|
|
|
|
|
my @path_segments = $url->path_segments;
|
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
# replace api_jsonrpc.php with the chart generation page
|
123
|
0
|
0
|
0
|
|
|
|
if ($self->data->{graphtype} == GRAPH_TYPE_NORMAL
|
|
|
0
|
0
|
|
|
|
|
124
|
|
|
|
|
|
|
or $self->data->{graphtype} == GRAPH_TYPE_STACKED) {
|
125
|
0
|
|
|
|
|
|
$path_segments[-1] = 'chart2.php';
|
126
|
|
|
|
|
|
|
} elsif ($self->data->{graphtype} == GRAPH_TYPE_PIE
|
127
|
|
|
|
|
|
|
or $self->data->{graphtype} == GRAPH_TYPE_EXPLODED) {
|
128
|
0
|
|
|
|
|
|
$path_segments[-1] = 'chart6.php';
|
129
|
|
|
|
|
|
|
} else {
|
130
|
|
|
|
|
|
|
croak(sprintf(q{Unknown graph type %d, cannot guess URL},
|
131
|
0
|
|
|
|
|
|
$self->data->{graphtype}));
|
132
|
|
|
|
|
|
|
}
|
133
|
|
|
|
|
|
|
|
134
|
0
|
|
|
|
|
|
$url->path_segments(@path_segments);
|
135
|
|
|
|
|
|
|
|
136
|
0
|
|
|
|
|
|
$url->query_form(graphid => $self->id, %args);
|
137
|
|
|
|
|
|
|
|
138
|
0
|
|
|
|
|
|
return $url;
|
139
|
|
|
|
|
|
|
}
|
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub _map_graphitems_to_property {
|
142
|
0
|
|
|
0
|
|
|
my ($self) = @_;
|
143
|
0
|
|
|
|
|
|
$self->data->{gitems} = [ map { $_->data } @{$self->graphitems} ];
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
144
|
0
|
|
|
|
|
|
return;
|
145
|
|
|
|
|
|
|
}
|
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub _map_property_to_graphitems {
|
148
|
0
|
|
|
0
|
|
|
my ($self) = @_;
|
149
|
0
|
|
|
|
|
|
my @graphitems = map { Zabbix2::API::GraphItem->new(root => $self->root,
|
150
|
0
|
|
|
|
|
|
data => $_) } @{$self->data->{gitems}};
|
|
0
|
|
|
|
|
|
|
151
|
0
|
|
|
|
|
|
$self->graphitems(\@graphitems);
|
152
|
0
|
|
|
|
|
|
return;
|
153
|
|
|
|
|
|
|
}
|
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
before 'create' => \&_map_graphitems_to_property;
|
156
|
|
|
|
|
|
|
before 'update' => \&_map_graphitems_to_property;
|
157
|
|
|
|
|
|
|
after 'pull' => \&_map_property_to_graphitems;
|
158
|
|
|
|
|
|
|
around 'new' => sub {
|
159
|
|
|
|
|
|
|
my ($orig, @rest) = @_;
|
160
|
|
|
|
|
|
|
my $graph = $orig->(@rest);
|
161
|
|
|
|
|
|
|
$graph->_map_property_to_graphitems;
|
162
|
|
|
|
|
|
|
return $graph;
|
163
|
|
|
|
|
|
|
};
|
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
1;
|
166
|
|
|
|
|
|
|
__END__
|