Commits

Fabrice Gabolde committed 36550a3

Fix #8 (untested!)

Comments (0)

Files changed (2)

lib/Zabbix2/API/Item.pm

 use Carp;
 
 use Params::Validate qw/validate validate_with :types/;
+use Text::ParseWords;
 
 use Moo::Lax;
 extends qw/Exporter Zabbix2::API::CRUDE/;
     return $self->data->{name};
 }
 
+sub expanded_name {
+
+    my $self = shift;
+
+    # we are not expanding hostmacros or globalmacros, those are problematic
+    my $key = $self->data->{key_};
+    my $token_re = qr/[0-9a-zA-Z_.-]+/;
+    $key =~ m/^$token_re\[(.*)\]$/;
+    my $arg_list = $1;
+
+    return $self->name unless $arg_list;
+
+    my @args = parse_line(',', 0, $arg_list);
+
+    my $name = $self->data->{name};
+    $name =~ s/\$(\d)/$args[$1 -1]/e;
+
+    # if ($name =~ m/\{\$[A-Z0-9_.]+\}/) {
+    #     die "there are macro names in this name, can't expand them";
+    # }
+
+    return $name;
+
+}
+
 sub _fetch_graphs {
     my $self = shift;
     my $graphs = $self->{root}->fetch('Graph', params => { itemids => [ $self->id ] });
 
 =head1 METHODS
 
+=head2 expanded_name
+
+  my $name = $item->expanded_name;
+
+Returns the item's name (its "name" property) with the macros
+expanded.
+
+Currently this only supports parameter replacement, so if the name is
+
+  CPU $2 time
+
+and the key is
+
+  system.cpu.util[,idle]
+
+the value returned should be "CPU idle time".
+
+Host macros and global macros are not replaced because this feature is
+not implemented directly in the API as far as I can tell, and a manual
+implementation in this wrapper would require many calls to the API
+macro endpoints.
+
 =head2 history
 
   my $historical_data = $item->history(time_from => ...,
 
 my $new_item = Zabbix2::API::Item->new(root => $zabber,
                                       data => { key_ => 'system.uptime[minutes]',
-                                                name => 'Uptime in minutes',
+                                                name => 'Uptime in $1',
                                                 delay => 300,
                                                 type => ITEM_TYPE_ZABBIX_ACTIVE,
                                                 value_type => ITEM_VALUE_TYPE_UINT64,
 ok($new_item->exists,
    '... and pushing it to the server creates a new item');
 
+is($new_item->name, 'Uptime in minutes',
+   q{... and its name is correctly expanded});
+
 eval { $new_item->delete };
 
 if ($@) { diag "Caught exception: $@" };