/usr/share/perl5/Zabbix/API/Item.pm is in libzabbix-api-perl 0.009-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 | package Zabbix::API::Item;
use strict;
use warnings;
use 5.010;
use Carp;
use Params::Validate qw/validate validate_with :types/;
use parent qw/Exporter Zabbix::API::CRUDE/;
use constant {
ITEM_TYPE_ZABBIX => 0,
ITEM_TYPE_SNMPV1 => 1,
ITEM_TYPE_TRAPPER => 2,
ITEM_TYPE_SIMPLE => 3,
ITEM_TYPE_SNMPV2C => 4,
ITEM_TYPE_INTERNAL => 5,
ITEM_TYPE_SNMPV3 => 6,
ITEM_TYPE_ZABBIX_ACTIVE => 7,
ITEM_TYPE_AGGREGATE => 8,
ITEM_TYPE_HTTPTEST => 9,
ITEM_TYPE_EXTERNAL => 10,
ITEM_TYPE_DB_MONITOR => 11,
ITEM_TYPE_IPMI => 12,
ITEM_TYPE_SSH => 13,
ITEM_TYPE_TELNET => 14,
ITEM_TYPE_CALCULATED => 15,
ITEM_VALUE_TYPE_FLOAT => 0,
ITEM_VALUE_TYPE_STR => 1,
ITEM_VALUE_TYPE_LOG => 2,
ITEM_VALUE_TYPE_UINT64 => 3,
ITEM_VALUE_TYPE_TEXT => 4,
ITEM_DATA_TYPE_DECIMAL => 0,
ITEM_DATA_TYPE_OCTAL => 1,
ITEM_DATA_TYPE_HEXADECIMAL => 2,
ITEM_STATUS_ACTIVE => 0,
ITEM_STATUS_DISABLED => 1,
ITEM_STATUS_NOTSUPPORTED => 3
};
our @EXPORT_OK = qw/
ITEM_TYPE_ZABBIX
ITEM_TYPE_SNMPV1
ITEM_TYPE_TRAPPER
ITEM_TYPE_SIMPLE
ITEM_TYPE_SNMPV2C
ITEM_TYPE_INTERNAL
ITEM_TYPE_SNMPV3
ITEM_TYPE_ZABBIX_ACTIVE
ITEM_TYPE_AGGREGATE
ITEM_TYPE_HTTPTEST
ITEM_TYPE_EXTERNAL
ITEM_TYPE_DB_MONITOR
ITEM_TYPE_IPMI
ITEM_TYPE_SSH
ITEM_TYPE_TELNET
ITEM_TYPE_CALCULATED
ITEM_VALUE_TYPE_FLOAT
ITEM_VALUE_TYPE_STR
ITEM_VALUE_TYPE_LOG
ITEM_VALUE_TYPE_UINT64
ITEM_VALUE_TYPE_TEXT
ITEM_DATA_TYPE_DECIMAL
ITEM_DATA_TYPE_OCTAL
ITEM_DATA_TYPE_HEXADECIMAL
ITEM_STATUS_ACTIVE
ITEM_STATUS_DISABLED
ITEM_STATUS_NOTSUPPORTED/;
our %EXPORT_TAGS = (
item_types => [
qw/ITEM_TYPE_ZABBIX
ITEM_TYPE_SNMPV1
ITEM_TYPE_TRAPPER
ITEM_TYPE_SIMPLE
ITEM_TYPE_SNMPV2C
ITEM_TYPE_INTERNAL
ITEM_TYPE_SNMPV3
ITEM_TYPE_ZABBIX_ACTIVE
ITEM_TYPE_AGGREGATE
ITEM_TYPE_HTTPTEST
ITEM_TYPE_EXTERNAL
ITEM_TYPE_DB_MONITOR
ITEM_TYPE_IPMI
ITEM_TYPE_SSH
ITEM_TYPE_TELNET
ITEM_TYPE_CALCULATED/
],
value_types => [
qw/ITEM_VALUE_TYPE_FLOAT
ITEM_VALUE_TYPE_STR
ITEM_VALUE_TYPE_LOG
ITEM_VALUE_TYPE_UINT64
ITEM_VALUE_TYPE_TEXT/
],
data_types => [
qw/ITEM_DATA_TYPE_DECIMAL
ITEM_DATA_TYPE_OCTAL
ITEM_DATA_TYPE_HEXADECIMAL/
],
status_types => [
qw/ITEM_STATUS_ACTIVE
ITEM_STATUS_DISABLED
ITEM_STATUS_NOTSUPPORTED/
]
);
sub id {
## mutator for id
my ($self, $value) = @_;
if (defined $value) {
$self->data->{itemid} = $value;
return $self->data->{itemid};
} else {
return $self->data->{itemid};
}
}
sub prefix {
my (undef, $suffix) = @_;
if ($suffix) {
return 'item'.$suffix;
} else {
return 'item';
}
}
sub extension {
return ( output => 'extend' );
}
sub collides {
my $self = shift;
return @{$self->{root}->query(method => $self->prefix('.get'),
params => { filter => { key_ => $self->data->{key_} },
hostids => [ $self->host->id ]})};
}
sub name {
my $self = shift;
return sprintf('%s/%s',
eval { $self->host->name } || '???',
$self->data->{key_} || '???');
}
sub graphs {
## accessor for this item's graphs
my ($self, $value) = @_;
if (defined $value) {
croak 'Accessor graphs called as mutator';
} else {
unless (exists ($self->{graphs})) {
my $graphs = $self->{root}->fetch('Graph', params => { itemids => [ $self->id ] });
$self->{graphs} = $graphs;
}
}
return $self->{graphs};
}
sub host {
## accessor for host
my ($self, $value) = @_;
if (defined $value) {
croak 'Accessor host called as mutator';
} else {
unless (exists $self->{host}) {
my $hosts = $self->{root}->fetch('Host', params => { hostids => [ $self->data->{hostid} ] });
croak 'Unexpectedly found more than one host for a given item'
if @{$hosts} > 1;
$self->{host} = $hosts->[0];
}
return $self->{host};
}
}
sub history {
## accessor for history
## DOES NOT CACHE!
my $self = shift;
my %extra_parameters = validate_with(params => \@_,
spec => { time_from => { type => SCALAR, optional => 1 },
time_till => { type => SCALAR, optional => 1 } },
allow_extra => 1);
my $history = $self->{root}->query(method => 'history.get',
params => { %extra_parameters,
itemids => [ $self->id ],
output => 'extend' });
return $history;
}
sub delay {
## mutator for the item's polling period
my ($self, $value) = @_;
if ($value) {
$self->data->{delay} = $value;
}
return $self->data->{delay};
}
1;
__END__
=pod
=head1 NAME
Zabbix::API::Item -- Zabbix item objects
=head1 SYNOPSIS
use Zabbix::API::Item qw/:item_types/;
# fetch a single item...
my $item = $zabbix->fetch('Item', params => { filter => { itemid => 22379 } })->[0];
# manipulate its properties...
$item->data->{multiplier} = 3;
# and update the properties on the server.
$item->push;
# fetch all items from a host
my $host = $zabbix->fetch('Host', params => { filter => { hostid => 10105 } })->[0];
my $items = $host->items;
# create a new item
my $new_item = Zabbix::API::Item->new(
root => $zabbix,
data => { type => ITEM_TYPE_SNMPV2C,
value_type => ITEM_VALUE_TYPE_UINT64,
snmp_oid => ...,
snmp_community => ...,
# that's right, key_
key_ => 'mynewitem',
# so far the following is the only way to create a "item belongs
# to host" relationship
hostid => $host->id,
});
# an itemid will be generated if the item does not already exist
$new_item->push;
=head1 DESCRIPTION
Handles CRUD for Zabbix item objects.
This is a subclass of C<Zabbix::API::CRUDE>; see there for inherited methods.
=head1 METHODS
=over 4
=item collides()
Returns true if the item exists with this key on this hostid, false otherwise.
=item host()
Accessor for a local C<host> attribute, which it also happens to set from the
server data if it isn't set already. The host is an instance of
C<Zabbix::API::Host>.
=item graphs()
Like C<host()>, returning an arrayref of C<Zabbix::API::Graph>
instances in which this item is involved.
=item history(PARAMS)
Accessor for the item's history data. Calling this method does not store the
history data into the object, unlike other accessors. History data is an AoH:
[ { itemid => ITEMID,
clock => UNIX_TIMESTAMP,
value => VALUE }, ... ]
C<PARAMS> should be a hash containing arguments for the C<history.get> method
(see here: L<http://www.zabbix.com/documentation/1.8/api/history/get>). The
time_from and time_till keys (with UNIX timestamps as values) are mandatory.
The C<itemids> and C<output> parameters are already set and cannot be
overwritten by the contents of C<PARAMS>.
=item delay(NEW_DELAY)
Mutator for the item's C<delay> value; that is, the polling period in
seconds. This is just a shortcut to set C<delay> in the C<data>
hashref. The method doesn't call C<pull()> or C<push()>, you need to
do it manually.
Returns the newly-set value.
=back
=head1 EXPORTS
Way too many constants, but for once they're documented (here:
L<http://www.zabbix.com/documentation/1.8/api/item/constants>).
Nothing is exported by default; you can use the tags C<:item_types>,
C<:value_types>, C<:data_types> and C<:status_types> (or import by name).
=head1 BUGS AND ODDITIES
This is probably because of the extremely smart way the Zabbix team has set up
their database schema, but what you'd expect to be C<"key"> in an item's data is
actually C<"key_">.
=head1 SEE ALSO
L<Zabbix::API::CRUDE>.
=head1 AUTHOR
Fabrice Gabolde <fabrice.gabolde@uperto.com>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2011 SFR
This library is free software; you can redistribute it and/or modify it under
the terms of the GPLv3.
=cut
|