This file is indexed.

/usr/share/perl5/Zabbix/API/Macro.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
package Zabbix::API::Macro;

use strict;
use warnings;
use 5.010;
use Carp;

use parent qw/Zabbix::API::CRUDE/;

use Scalar::Util qw/reftype/;
use Zabbix::API::Host;

sub id {

    ## mutator for id

    my ($self, $value) = @_;

    if (defined $value) {

        if ($self->globalp) {

            $self->data->{globalmacroid} = $value;

            delete $self->data->{hostmacroid};

            return $self->data->{globalmacroid};

        } else {

            $self->data->{hostmacroid} = $value;

            delete $self->data->{globalmacroid};

            return $self->data->{hostmacroid};

        }

    } else {

        if ($self->globalp) {

            return $self->data->{globalmacroid};

        } else {

            return $self->data->{hostmacroid};

        }

    }

}

sub prefix {

    my ($self, $suffix) = @_;

    if ($suffix) {

        if ($suffix =~ m/ids?/) {

            return ($self->globalp?'globalmacro':'hostmacro').$suffix;

        } elsif ($suffix eq '.delete') {

            return 'usermacro.'.($self->globalp?'deleteGlobal':'deleteHostMacro');

        }

        return 'usermacro'.$suffix;

    } else {

        return 'usermacro';

    }

}

sub extension {

    return ( output => 'extend',
             select_hosts => 'shorten' );

}

sub collides {

    my $self = shift;

    if ($self->globalp) {

        return @{$self->{root}->query(method => $self->prefix('.get'),
                                      params => { filter => { macro => $self->data->{macro},
                                                              globalmacro => 1 } })};

    } else {

        return @{$self->{root}->query(method => $self->prefix('.get'),
                                      params => { filter => { macro => $self->data->{macro} },
                                                  hostids => [ $self->host->id ] })};
    }

}

sub name {

    my $self = shift;
    return $self->data->{macro};

}

sub globalp {

    my $self = shift;

    return (!defined $self->host);

}

sub host {

    ## mutator for host

    my ($self, $value) = @_;

    if (defined $value) {

        my $type = reftype $value;

        if (eval { $value->isa('Zabbix::API::Host') }) {

            if ($value->created) {

                $value->pull;

            } else {

                $value->push;

            }

        } elsif (defined $type and $type eq 'HASH') {

            $value = $self->{root}->fetch('Host', params => { filter => { hostid => $value->{hostid} } })->[0];

        } else {

            croak 'Type mismatch: Expected hashref or Zabbix::API::Host instance';

        }

        $self->data->{hosts} = [ $value ];
        return $self->data->{hosts}[0];

    } else {

        return $self->data->{hosts}[0];

    }

}

sub pull {

    # override CRUDE's pull()

    my ($self, $data) = @_;

    if (defined $data) {

        $self->{data} = $data;

    } else {

        # this happens to work because usermacro.get works for both types of
        # macros...
        $self->SUPER::pull;

        $self->host($self->data->{hosts}->[0]);

    }

    return $self;

}

sub push {

    my ($self, $data) = @_;

    $data //= $self->data;

    my $method;
    my $parameters;

    my @colliders;

    if ($self->id
        and $self->created) {

        say sprintf('Updating %s %s', $self->prefix, $self->id)
            if $self->{root}->{verbosity};

        if ($self->globalp) {

            $method = '.updateGlobal';

            $parameters = { macro => $self->data->{macro},
                            value => $self->data->{value} };

        } else {

            $method = '.massUpdate';

            $parameters = { macros => [
                                { macro => $self->data->{macro},
                                  value => $self->data->{value} }
                                ],
                            hosts => [ { hostid => $self->host->id } ]};

        }

        $self->{root}->query(method => $self->prefix($method),
                             params => $parameters);

        $self->pull;

    } elsif ($self->id) {

        croak sprintf('%s has a %s but does not exist on server', $self->id, $self->prefix('id'));

    } elsif (@colliders = $self->collides and $colliders[0]) {

        say sprintf('Updating %s (match by collisions)', $self->prefix)
            if $self->{root}->{verbosity};

        if (@colliders > 1) {

            croak sprintf('Cannot push %s: too many possible targets', $self->prefix);

        }

        my $class = ref $self;

        # not referenced! and that's the way we want it
        my $collider = $class->new(root => $self->{root}, data => $colliders[0]);
        $collider->host($self->data->{hosts}->[0]);

        $self->id($collider->id);

        $self->host->push if $self->host;

        if ($self->globalp) {

            $method = '.updateGlobal';

            $parameters = { macro => $self->data->{macro},
                            value => $self->data->{value} };

        } else {

            $method = '.massUpdate';

            $parameters = { macros => [
                                { macro => $self->data->{macro},
                                  value => $self->data->{value} }
                                ],
                            hosts => [ { hostid => $self->host->id } ] };

        }

        $self->{root}->query(method => $self->prefix($method),
                             params => $parameters);

        $self->pull;

    } else {

        say 'Creating '.$self->prefix
            if $self->{root}->{verbosity};

        if ($self->globalp) {

            $method = '.createGlobal';

            $parameters = { macro => $self->data->{macro},
                            value => $self->data->{value} };

        } else {

            $method = '.massAdd';

            $parameters = { macros => [
                                { macro => $self->data->{macro},
                                  value => $self->data->{value} }
                                ],
                            hosts => [ { hostid => $self->host->id } ] };

        }

        my $id = $self->{root}->query(method => $self->prefix($method),
                                      params => $parameters)->{$self->prefix('ids')}->[0];

        $self->id($id);

        $self->pull;

    }

    return $self;

}

1;
__END__
=pod

=head1 NAME

Zabbix::API::Macro -- Zabbix usermacro objects

=head1 SYNOPSIS

  use Zabbix::API::Macro;

  # TODO write the rest

=head1 DESCRIPTION

Handles CRUD for Zabbix usermacro objects.

Both global and host macro types are represented by this class.  If the C<hosts>
arrayref attribute is undef or empty, then we assume it's a global macro.

This class' methods work transparently around the weird Zabbix macro API, which
uses different methods on the same object depending on whether it's a global or
host macro... except sometimes, for instance the C<usermacro.get> method which
can be called on both and will return different keys...  And macros don't seem
to have an C<exists> method.  It's kind of a mess.

The various massFoo methods have not been implemented at all because I did not
have a use for their "mass" functionality, despite their being the only way to
CRUD host macros.

=head1 METHODS

=over 4

=item prefix([SUFFIX])

This class' C<prefix> method is B<not> a class method.  The prefix returned
depends on the type of macro (global or host) which is a characteristic of an
instance.

=item host([HOST])

Mutator for the macro's host.  C<HOST> should be a C<Host> instance or a hashref
with a C<hostid> key.

=item globalp()

Return a true value when the macro is global, a false value otherwise.

=back

=head1 SEE ALSO

TODO links to other pods and documentation

=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