| 1 | package SNMP::Effective::Host; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | SNMP::Effective::Host - A SNMP::Effective host class | ||
| 6 | |||
| 7 | =head1 DESCRIPTION | ||
| 8 | |||
| 9 | A host object holds all the information pr. host | ||
| 10 | L<SNMP::Effective> requires. This C<$host> object | ||
| 11 | is available in L<SNMP::Effective/THE CALLBACK METHOD>. | ||
| 12 | |||
| 13 | =cut | ||
| 14 | |||
| 15 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 17 | 1 | ▶ | use SNMP::Effective::VarList; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 18 | use Carp qw/ cluck confess /; | ||
| 19 | |||
| 20 | use overload '""' => sub { shift->{'_address'} }; | ||
| 21 | use overload '${}' => sub { shift->{'_session'} }; | ||
| 22 | use overload '@{}' => sub { shift->{'_varlist'} }; | ||
| 23 | |||
| 24 | =head1 ATTRIBUTES | ||
| 25 | |||
| 26 | =head2 address | ||
| 27 | |||
| 28 | Get host address, also overloaded by "$self". | ||
| 29 | |||
| 30 | =head2 session | ||
| 31 | |||
| 32 | Get L<SNMP::Session>, also overloaded by $$self. | ||
| 33 | |||
| 34 | =head2 sesssion | ||
| 35 | |||
| 36 | Alias for L</session> (because of previous typo). Will be deprecated. | ||
| 37 | |||
| 38 | =head2 varlist | ||
| 39 | |||
| 40 | The remaining OIDs to get/set, also overloaded by @$self. | ||
| 41 | |||
| 42 | =head2 callback | ||
| 43 | |||
| 44 | Get a ref to the callback method. | ||
| 45 | |||
| 46 | =head2 heap | ||
| 47 | |||
| 48 | Get/set any data you like. By default, it returns a hash-ref, so you can do: | ||
| 49 | |||
| 50 | $host->heap->{'mykey'} = "remember this"; | ||
| 51 | |||
| 52 | =head2 pre_collect_callback | ||
| 53 | |||
| 54 | Holds a callback which will be called right before the first request is sent | ||
| 55 | to the target host. The callback recevies L<$self|SNMP::Effective::Host> as | ||
| 56 | the first argument and the L<SNMP::Effective> object as the second. | ||
| 57 | |||
| 58 | =head2 post_collect_callback | ||
| 59 | |||
| 60 | Holds a callback which will be called after L<SNMP::Effective> is done with | ||
| 61 | the C<$host> object. The callback recevies L<$self|SNMP::Effective::Host> as | ||
| 62 | the first argument and the L<SNMP::Effective> object as the second. | ||
| 63 | |||
| 64 | =cut | ||
| 65 | |||
| 66 | BEGIN { | ||
| 67 | no strict 'refs'; | ||
| 68 | my %sub2key = qw/ | ||
| 69 | address _address | ||
| 70 | session _session | ||
| 71 | varlist _varlist | ||
| 72 | callback _callback | ||
| 73 | heap _heap | ||
| 74 | pre_collect_callback _pre_collect_callback | ||
| 75 | post_collect_callback _post_collect_callback | ||
| 76 | /; | ||
| 77 | |||
| 78 | for my $subname (keys %sub2key) { | ||
| 79 | *$subname = sub { | ||
| 80 | my($self, $set) = @_; | ||
| 81 | $self->{ $sub2key{$subname} } = $set if(defined $set); | ||
| 82 | $self->{ $sub2key{$subname} }; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | *sesssion = sub { | ||
| 87 | cluck "->sesssion() will be deprecated. Use ->session()> instead"; | ||
| 88 | return shift->session(@_); | ||
| 89 | }; | ||
| 90 | } | ||
| 91 | |||
| 92 | =head2 arg | ||
| 93 | |||
| 94 | Get/set L<SNMP::Session> args. | ||
| 95 | |||
| 96 | =cut | ||
| 97 | |||
| 98 | sub arg { | ||
| 99 | my $self = shift; | ||
| 100 | my $arg = shift; | ||
| 101 | |||
| 102 | if(ref $arg eq 'HASH') { | ||
| 103 | $self->{'_arg'}{$_} = $arg->{$_} for(keys %$arg); | ||
| 104 | } | ||
| 105 | |||
| 106 | return %{$self->{'_arg'}}, DestHost => "$self" if(wantarray); | ||
| 107 | return $self->{'_arg'}; | ||
| 108 | } | ||
| 109 | |||
| 110 | =head2 data | ||
| 111 | |||
| 112 | $hash_ref = $self->data; | ||
| 113 | $hash_ref = $self->data(\@data0, $ref_oid0, ...); | ||
| 114 | |||
| 115 | Get the retrieved data or add more data to the host cache. | ||
| 116 | |||
| 117 | C<@data0> looks like: C<[ $oid0, $iid0, $value0, $type0 ]>, where | ||
| 118 | C<$ref_oid0> is used to figure out the C<$iid> unless specified | ||
| 119 | in C<@data0>. C<$iid0> will fallback to "1", if everything fails. | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | sub data { | ||
| 124 | my $self = shift; | ||
| 125 | |||
| 126 | if(@_) { | ||
| 127 | my $r = shift; | ||
| 128 | my $ref_oid = shift || ''; | ||
| 129 | my $iid = $r->[1] | ||
| 130 | || SNMP::Effective::match_oid($r->[0], $ref_oid) | ||
| 131 | || 1; | ||
| 132 | |||
| 133 | $ref_oid =~ s/^\.//mx; | ||
| 134 | |||
| 135 | $self->{'_data'}{$ref_oid}{$iid} = $r->[2]; | ||
| 136 | $self->{'_type'}{$ref_oid}{$iid} = $r->[3]; | ||
| 137 | } | ||
| 138 | |||
| 139 | return $self->{'_data'}; | ||
| 140 | } | ||
| 141 | |||
| 142 | =head1 METHODS | ||
| 143 | |||
| 144 | =head2 new | ||
| 145 | |||
| 146 | $self = $class->new($address); | ||
| 147 | |||
| 148 | Object constructor. C<$address> can also be an ip-address. | ||
| 149 | |||
| 150 | =cut | ||
| 151 | |||
| 152 | sub new { | ||
| 153 | my $class = shift; | ||
| 154 | my $args = shift; | ||
| 155 | my $log = shift; | ||
| 156 | my($session, @varlist); | ||
| 157 | |||
| 158 | tie @varlist, "SNMP::Effective::VarList"; | ||
| 159 | |||
| 160 | $args = { address => $args } unless(ref $args eq 'HASH'); | ||
| 161 | $args->{'address'} or confess 'Usage: $class->new(\%args)'; | ||
| 162 | |||
| 163 | return bless { | ||
| 164 | _address => $args->{'address'}, | ||
| 165 | _arg => $args->{'arg'} || {}, | ||
| 166 | _callback => $args->{'callback'} || sub {}, | ||
| 167 | _data => {}, | ||
| 168 | _heap => $args->{'heap'}, | ||
| 169 | _session => \$session, | ||
| 170 | _varlist => \@varlist, | ||
| 171 | _pre_collect_callback => $args->{'pre_collect_callback'} || sub {}, | ||
| 172 | _post_collect_callback => $args->{'post_collect_callback'} || sub {}, | ||
| 173 | }, $class; | ||
| 174 | } | ||
| 175 | |||
| 176 | =head2 clear_data | ||
| 177 | |||
| 178 | Remove data from the host cache. Will make C</data> return an | ||
| 179 | empty hash-ref. | ||
| 180 | |||
| 181 | =cut | ||
| 182 | |||
| 183 | sub clear_data { | ||
| 184 | my $self = shift; | ||
| 185 | |||
| 186 | $self->{'_data'} = {}; | ||
| 187 | $self->{'_type'} = {}; | ||
| 188 | |||
| 189 | return; | ||
| 190 | } | ||
| 191 | |||
| 192 | =head1 AUTHOR | ||
| 193 | |||
| 194 | =head1 ACKNOWLEDGEMENTS | ||
| 195 | |||
| 196 | =head1 COPYRIGHT & LICENSE | ||
| 197 | |||
| 198 | See L<SNMP::Effective> | ||
| 199 | |||
| 200 | =cut | ||
| 201 | |||
| 202 | 1; | ||