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