| 1 | package Device::ZyXEL::IES; | ||
| 2 | 17 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 17 | ▶ | use Net::SNMP::Util qw/snmpwalk snmpget/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 0 | ▶ | use Device::ZyXEL::IES::Slot; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 5 | use Device::ZyXEL::IES::OID; | ||
| 6 | |||
| 7 | # ABSTRACT: SNMP with a ZyXEL IES device | ||
| 8 | |||
| 9 | =pod | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | Device::ZyXEL::IES - A module for getting and setting values on a ZyXEL IES5xxx device | ||
| 14 | using SNMP. | ||
| 15 | |||
| 16 | =head1 VERSION | ||
| 17 | |||
| 18 | Version 0.11 | ||
| 19 | |||
| 20 | =cut | ||
| 21 | |||
| 22 | our $VERSION = '0.11'; | ||
| 23 | |||
| 24 | =head1 SYNOPSIS | ||
| 25 | |||
| 26 | Quick summary of what the module does. | ||
| 27 | |||
| 28 | use Device::ZyXEL::IES; | ||
| 29 | |||
| 30 | my $device = Device::ZyXEL::IES->new( { | ||
| 31 | hostname => 'some_ies.example.com', | ||
| 32 | get_community => 'public', | ||
| 33 | set_community => 'something', | ||
| 34 | }); | ||
| 35 | |||
| 36 | # retrieve slot inventory (slot number and firmwares) | ||
| 37 | # for an ies | ||
| 38 | my $result = $device->slotInventory; | ||
| 39 | |||
| 40 | # Data for the slots are now in the slots attribute | ||
| 41 | # returning a list of Device::ZyXEL::IES::Slot objects | ||
| 42 | my $slotlist = $device->slots; | ||
| 43 | |||
| 44 | =cut | ||
| 45 | |||
| 46 | has 'slots' => ( | ||
| 47 | traits => ['Hash'], | ||
| 48 | isa => 'HashRef[Device::ZyXEL::IES::Slot]', | ||
| 49 | is => 'ro', | ||
| 50 | default => sub { {} }, | ||
| 51 | required => 0, | ||
| 52 | handles => { | ||
| 53 | slot_exists => 'exists', | ||
| 54 | get_slot => 'get', | ||
| 55 | add_slot => 'set', | ||
| 56 | delete_slot => 'delete', | ||
| 57 | num_slots => 'count' | ||
| 58 | }, | ||
| 59 | writer => '_set_slots' | ||
| 60 | ); | ||
| 61 | |||
| 62 | our $oid_tr; | ||
| 63 | sub BUILD { | ||
| 64 | my ( $self, $params ) = @_; | ||
| 65 | my $slots = $self->slots; | ||
| 66 | foreach my $slot ( keys %{$slots} ) { | ||
| 67 | $slots->{$slot}->ies( $self ) | ||
| 68 | } | ||
| 69 | $oid_tr = Device::ZyXEL::IES::OID->new; | ||
| 70 | } | ||
| 71 | |||
| 72 | after 'slots' => sub { | ||
| 73 | my ( $self, $slots ) = @_; | ||
| 74 | return unless $slots; | ||
| 75 | foreach my $slot ( keys %{$slots} ) { | ||
| 76 | $slots->{$slot}->ies( $self ) | ||
| 77 | } | ||
| 78 | }; | ||
| 79 | |||
| 80 | has 'uptime' => ( | ||
| 81 | isa => 'Str', | ||
| 82 | is => 'ro', | ||
| 83 | default => sub { 'unknown' }, | ||
| 84 | writer => '_set_uptime' | ||
| 85 | ); | ||
| 86 | |||
| 87 | has 'sysdescr' => ( | ||
| 88 | isa => 'Str', | ||
| 89 | is => 'ro', | ||
| 90 | writer => '_set_sysdescr' | ||
| 91 | ); | ||
| 92 | |||
| 93 | has 'get_community' => ( | ||
| 94 | isa => 'Str', | ||
| 95 | is => 'ro', | ||
| 96 | required => 1 | ||
| 97 | ); | ||
| 98 | |||
| 99 | has 'set_community' => ( | ||
| 100 | isa => 'Str', | ||
| 101 | is => 'ro', | ||
| 102 | ); | ||
| 103 | |||
| 104 | # The hostname to connect to | ||
| 105 | has 'hostname' => ( | ||
| 106 | isa => 'Str', | ||
| 107 | is => 'ro', | ||
| 108 | required => 1 | ||
| 109 | ); | ||
| 110 | |||
| 111 | =head1 FUNCTIONS | ||
| 112 | |||
| 113 | =head2 read_oid | ||
| 114 | |||
| 115 | Uses Net::SNMP::Util to read the value of an oid | ||
| 116 | |||
| 117 | The oid passed here is a real one, not a name. | ||
| 118 | |||
| 119 | =cut | ||
| 120 | sub read_oid { | ||
| 121 | my ($self, $oid, $translate) = @_; | ||
| 122 | |||
| 123 | $translate = {} unless defined $translate; | ||
| 124 | $translate->{'-timeticks'} = 0x0; # Turn off so sysUpTime is numeric | ||
| 125 | my @translatelist = (); | ||
| 126 | for my $k ( keys %{$translate} ) { | ||
| 127 | push @translatelist,$k,$translate->{$k}; | ||
| 128 | } | ||
| 129 | my %snmpparams = ( | ||
| 130 | -version => 2, | ||
| 131 | -community => $self->get_community(), | ||
| 132 | -translate => \@translatelist | ||
| 133 | ); | ||
| 134 | |||
| 135 | my $r = snmpget( | ||
| 136 | hosts => [ $self->hostname ], | ||
| 137 | oids => { | ||
| 138 | o => $oid | ||
| 139 | }, | ||
| 140 | snmp => \%snmpparams | ||
| 141 | ); | ||
| 142 | |||
| 143 | return "[ERROR] Nothing returned" unless defined $r->{$self->hostname}; | ||
| 144 | |||
| 145 | my $vals = $r->{$self->hostname}; | ||
| 146 | |||
| 147 | return "[ERROR] Wrong key returned" unless defined( $vals->{o} ); | ||
| 148 | |||
| 149 | return $vals->{o}; | ||
| 150 | } | ||
| 151 | |||
| 152 | =head2 walk_oid | ||
| 153 | |||
| 154 | Does a snmpwalk of the given oid, and returns the result. | ||
| 155 | |||
| 156 | scalar containing "ERROR: <something>" is returned upon error. | ||
| 157 | |||
| 158 | =cut | ||
| 159 | sub walk_oid { | ||
| 160 | my ($self, $oid, $translate) = @_; | ||
| 161 | $translate = {} unless defined $translate; | ||
| 162 | my @translatelist = (); | ||
| 163 | for my $k ( keys %{$translate} ) { | ||
| 164 | push @translatelist,$k,$translate->{$k}; | ||
| 165 | } | ||
| 166 | |||
| 167 | my %snmpparams = ( | ||
| 168 | -version => 1, | ||
| 169 | -community => $self->get_community(), | ||
| 170 | -translate => \@translatelist | ||
| 171 | ); | ||
| 172 | |||
| 173 | # use Net::SNMP::Util to do the walk | ||
| 174 | my ($result, $error) = snmpwalk( | ||
| 175 | hosts => [$self->hostname()], | ||
| 176 | snmp => \%snmpparams, | ||
| 177 | oids => [$oid] ); | ||
| 178 | |||
| 179 | return "ERROR: $error" if defined $error && $error ne ''; | ||
| 180 | return $result->{$self->hostname()}->[0]; | ||
| 181 | } | ||
| 182 | |||
| 183 | =head2 read_oids | ||
| 184 | |||
| 185 | Uses Net::SNMP::Util to read the value of a set of oids | ||
| 186 | |||
| 187 | The oids passed here is a real ones, not a name. | ||
| 188 | |||
| 189 | =cut | ||
| 190 | sub read_oids { | ||
| 191 | my ($self, $oids ) = @_; | ||
| 192 | |||
| 193 | my %snmpparams = ( | ||
| 194 | -version => 2, | ||
| 195 | -community => $self->get_community(), | ||
| 196 | -translate => [ -timeticks => 0x0 ] | ||
| 197 | ); | ||
| 198 | |||
| 199 | my $r = snmpget( | ||
| 200 | hosts => [ $self->hostname ], | ||
| 201 | oids => $oids, | ||
| 202 | snmp => \%snmpparams | ||
| 203 | ); | ||
| 204 | |||
| 205 | return "[ERROR] Nothing returned" unless defined $r->{$self->hostname}; | ||
| 206 | |||
| 207 | my $vals = $r->{$self->hostname}; | ||
| 208 | |||
| 209 | return $vals; | ||
| 210 | } | ||
| 211 | |||
| 212 | =head2 has_slot | ||
| 213 | |||
| 214 | Checks whether or not a specific slot id is present in the slot list. | ||
| 215 | |||
| 216 | =cut | ||
| 217 | sub has_slot { | ||
| 218 | my ($self,$slotid) = @_; | ||
| 219 | |||
| 220 | return ( defined $self->slots()->{$slotid} ); | ||
| 221 | } | ||
| 222 | |||
| 223 | |||
| 224 | =head2 read_uptime | ||
| 225 | |||
| 226 | Reads the system uptime from the IES. | ||
| 227 | |||
| 228 | =cut | ||
| 229 | sub read_uptime { | ||
| 230 | my ($self) = @_; | ||
| 231 | my $oid = $oid_tr->translate('DISMAN-EVENT-MIB::sysUpTimeInstance'); | ||
| 232 | my $uptime = $self->read_oid($oid); | ||
| 233 | if ( $uptime !~ /ERROR/ ) { | ||
| 234 | $self->_set_uptime( $uptime ); | ||
| 235 | } | ||
| 236 | return $uptime; | ||
| 237 | } | ||
| 238 | |||
| 239 | =head2 read_sysdescr | ||
| 240 | |||
| 241 | Reads the system description from the IES. | ||
| 242 | |||
| 243 | =cut | ||
| 244 | sub read_sysdescr { | ||
| 245 | my ($self) = @_; | ||
| 246 | my $oid = $oid_tr->translate('SNMPv2-MIB::sysDescr.0'); | ||
| 247 | my $sysdescr = $self->read_oid($oid); | ||
| 248 | if ( $sysdescr !~ /ERROR/ ) { | ||
| 249 | $self->_set_sysdescr( $sysdescr ); | ||
| 250 | } | ||
| 251 | return $sysdescr; | ||
| 252 | } | ||
| 253 | |||
| 254 | =head2 slotInventory | ||
| 255 | |||
| 256 | Sweeps slots on the IES, creating all the required | ||
| 257 | Device::ZyXEL::IES::Slot objects along the way. | ||
| 258 | |||
| 259 | It only performs one walk for the firmware version | ||
| 260 | to get the Slot objects started. Further Slot info | ||
| 261 | will require interaction with the Slot objects | ||
| 262 | themselves. | ||
| 263 | |||
| 264 | =cut | ||
| 265 | |||
| 266 | sub slotInventory { | ||
| 267 | my $self = shift; | ||
| 268 | |||
| 269 | # Start by clearing the slotlist. | ||
| 270 | $self->_set_slots({}); | ||
| 271 | |||
| 272 | my %snmpparams = ( | ||
| 273 | -version => 1, | ||
| 274 | -community => $self->get_community() | ||
| 275 | ); | ||
| 276 | |||
| 277 | my $oid = $oid_tr->translate('ZYXEL-IES5000-MIB::slotModuleDescr.0'); | ||
| 278 | # use Net::SNMP::Util to do the walk | ||
| 279 | my ($result, $error) = snmpwalk( | ||
| 280 | hosts => [$self->hostname()], | ||
| 281 | snmp => \%snmpparams, | ||
| 282 | oids => [$oid] ); | ||
| 283 | |||
| 284 | return "[ERROR] $error" unless defined $result; | ||
| 285 | |||
| 286 | # we have some slot cardtype. | ||
| 287 | if ( defined $result->{$self->hostname()}->[0] ) { | ||
| 288 | foreach my $o ( keys %{$result->{$self->hostname()}->[0]} ) { | ||
| 289 | if ( $result->{$self->hostname()}->[0]->{$o} ne '' ) { | ||
| 290 | my $s = Device::ZyXEL::IES::Slot->new( | ||
| 291 | id => $o, | ||
| 292 | cardtype => $result->{$self->hostname()}->[0]->{$o}, | ||
| 293 | ies => $self | ||
| 294 | ); | ||
| 295 | $self->add_slot( $o, $s ); | ||
| 296 | } | ||
| 297 | } | ||
| 298 | } | ||
| 299 | else { | ||
| 300 | return "[ERROR] Nothing returned"; | ||
| 301 | } | ||
| 302 | |||
| 303 | return 'OK'; | ||
| 304 | } | ||
| 305 | |||
| 306 | =head2 fetchDetails | ||
| 307 | |||
| 308 | Uses Net::SNMP::Util to fetch enough items from the ies to fill in the | ||
| 309 | attributes of the object. | ||
| 310 | |||
| 311 | Fetches uptime, sysdescr, ... | ||
| 312 | =cut | ||
| 313 | |||
| 314 | sub fetchDetails { | ||
| 315 | my $self = shift; | ||
| 316 | my $meta = $self->meta(); | ||
| 317 | |||
| 318 | foreach my $method ( $meta->get_method_list ) { | ||
| 319 | if ( $method =~ /^read_/ && $method !~ /^read_oid.*$/ ) { | ||
| 320 | my $res = $self->$method; | ||
| 321 | return $res if $res =~ /ERROR/i; | ||
| 322 | } | ||
| 323 | } | ||
| 324 | return 'OK'; | ||
| 325 | } | ||
| 326 | |||
| 327 | =head1 AUTHOR | ||
| 328 | |||
| 329 | Jesper Dalberg, C<< <jdalberg at gmail.com> >> | ||
| 330 | |||
| 331 | =head1 BUGS | ||
| 332 | |||
| 333 | Please report any bugs or feature requests to C<bug-device-zyxel-ies at rt.cpan.org>, or through | ||
| 334 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Device-ZyXEL-IES>. I will be notified, and then you'll | ||
| 335 | automatically be notified of progress on your bug as I make changes. | ||
| 336 | |||
| 337 | :w | ||
| 338 | |||
| 339 | |||
| 340 | =head1 SUPPORT | ||
| 341 | |||
| 342 | You can find documentation for this module with the perldoc command. | ||
| 343 | |||
| 344 | perldoc Device::ZyXEL::IES | ||
| 345 | |||
| 346 | |||
| 347 | You can also look for information at: | ||
| 348 | |||
| 349 | =over 4 | ||
| 350 | |||
| 351 | =item * RT: CPAN's request tracker | ||
| 352 | |||
| 353 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Device-ZyXEL-IES> | ||
| 354 | |||
| 355 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 356 | |||
| 357 | L<http://annocpan.org/dist/Device-ZyXEL-IES> | ||
| 358 | |||
| 359 | =item * CPAN Ratings | ||
| 360 | |||
| 361 | L<http://cpanratings.perl.org/d/Device-ZyXEL-IES> | ||
| 362 | |||
| 363 | =item * Search CPAN | ||
| 364 | |||
| 365 | L<http://search.cpan.org/dist/Device-ZyXEL-IES/> | ||
| 366 | |||
| 367 | =back | ||
| 368 | |||
| 369 | |||
| 370 | =head1 ACKNOWLEDGEMENTS | ||
| 371 | |||
| 372 | Fullrate (http://www.fullrate.dk) | ||
| 373 | Thanks for allowing me to be introduced to the "wonderful" device ;) | ||
| 374 | And thanks for donating some of my work time to create this module and | ||
| 375 | sharing it with the world. | ||
| 376 | |||
| 377 | =head1 COPYRIGHT & LICENSE | ||
| 378 | |||
| 379 | Copyright 2012 Jesper Dalberg, all rights reserved. | ||
| 380 | |||
| 381 | This program is free software; you can redistribute it and/or modify it | ||
| 382 | under the same terms as Perl itself. | ||
| 383 | |||
| 384 | |||
| 385 | =cut | ||
| 386 | |||
| 387 | __PACKAGE__->meta->make_immutable; | ||
| 388 | |||
| 389 | 1; | ||
| 390 | |||