faa51ed0684ad0689bfa14a918c00d12b03c2297
[freeside.git] / FS / FS / part_export / broadband_snmp_get.pm
1 package FS::part_export::broadband_snmp_get;
2
3 use strict;
4 use vars qw(%info $DEBUG);
5 use base 'FS::part_export';
6 use SNMP;
7 use Tie::IxHash;
8
9 tie my %snmp_version, 'Tie::IxHash',
10   v1  => '1',
11   v2c => '2c'
12   # v3 unimplemented
13 ;
14
15 tie my %options, 'Tie::IxHash',
16   'snmp_version' => {
17     label=>'SNMP version', 
18     type => 'select',
19     options => [ keys %snmp_version ],
20    },
21   'snmp_community' => { 'label'=>'Community', 'default'=>'public' },
22   'snmp_timeout' => { label=>'Timeout (seconds)', 'default'=>1 },
23   'snmp_oid' => { label=>'Object ID', multiple=>1 },
24 ;
25
26 %info = (
27   'svc'     => 'svc_broadband',
28   'desc'    => 'Enable interface display of realtime SNMP get requests to service IP address',
29   'config_element' => '/edit/elements/part_export/broadband_snmp_get.html',
30   'options' => \%options,
31   'no_machine' => 1,
32   'notes'   => <<'END',
33 Use this export to configure the community and object ids for displaying realtime 
34 SNMP data from the service IP address when viewing a provisioned service.  Timeout is
35 per object, and should be small enough for realtime use.  This export takes no action 
36 during provisioning itself;  it is expected that snmp will be separately
37 configured on the service machine.
38 END
39 );
40
41 sub export_insert { ''; }
42 sub export_replace { ''; }
43 sub export_delete { ''; }
44 sub export_suspend { ''; }
45 sub export_unsuspend { ''; }
46
47 =pod
48
49 =head1 NAME
50
51 FS::part_export::broadband_snmp_get
52
53 =head1 SYNOPSIS
54
55 Configuration for realtime snmp requests to svc_broadband IP address
56
57 =head1 METHODS
58
59 =cut
60
61 =over 4
62
63 =item snmp_results SVC
64
65 Request statistics from SVC ip address.  Returns an array of hashes with keys 
66
67 objectID
68
69 label
70
71 value
72
73 error - error when attempting to load this object
74
75 =cut
76
77 sub snmp_results {
78   my ($self, $svc) = @_;
79   my $host = $svc->ip_addr;
80   my $comm = $self->option('snmp_community');
81   my $vers = $self->option('snmp_version');
82   my $time = ($self->option('snmp_timeout') || 1) * 1000;
83   my @oids = split("\n", $self->option('snmp_oid'));
84   my %connect = (
85     'DestHost'  => $host,
86     'Community' => $comm,
87     'Version'   => $vers,
88     'Timeout'   => $time,
89   );
90   my $snmp = new SNMP::Session(%connect);
91   return { 'error' => 'Error creating SNMP session' } unless $snmp;
92   return { 'error' => $snmp->{'ErrorStr'} } if $snmp->{'ErrorStr'};
93   my @out;
94   foreach my $oid (@oids) {
95     $oid = $SNMP::MIB{$oid}->{'objectID'} if $SNMP::MIB{$oid};
96     my $value = $snmp->get($oid.'.0');
97     if ($snmp->{'ErrorStr'}) {
98       push @out, { 'error' => $snmp->{'ErrorStr'} };
99       next;
100     }
101     my %result = map { $_ => $SNMP::MIB{$oid}{$_} } qw( objectID label value );
102     $result{'value'} = $value;
103     push @out, \%result;
104   }
105   return @out;      
106 }
107
108 =back
109
110 =cut
111
112 1;
113