broadband_snmp export: better MIB selection, #16588
[freeside.git] / FS / FS / part_export / broadband_snmp.pm
1 package FS::part_export::broadband_snmp;
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 $DEBUG = 0;
10
11 my $me = '['.__PACKAGE__.']';
12
13 tie my %snmp_version, 'Tie::IxHash',
14   v1  => '1',
15   v2c => '2c',
16   # v3 unimplemented
17 ;
18
19 #tie my %snmp_type, 'Tie::IxHash',
20 #  i => INTEGER,
21 #  u => UNSIGNED32,
22 #  s => OCTET_STRING,
23 #  n => NULL,
24 #  o => OBJECT_IDENTIFIER,
25 #  t => TIMETICKS,
26 #  a => IPADDRESS,
27 #  # others not implemented yet
28 #;
29
30 tie my %options, 'Tie::IxHash',
31   'version' => { label=>'SNMP version', 
32     type => 'select',
33     options => [ keys %snmp_version ],
34    },
35   'community' => { label=>'Community', default=>'public' },
36
37   'action' => { multiple=>1 },
38   'oid'    => { multiple=>1 },
39   'value'  => { multiple=>1 },
40
41   'ip_addr_change_to_new' => { 
42     label=>'Send IP address changes to new address',
43     type=>'checkbox'
44   },
45   'timeout' => { label=>'Timeout (seconds)' },
46 ;
47
48 %info = (
49   'svc'     => 'svc_broadband',
50   'desc'    => 'Send SNMP requests to the service IP address',
51   'config_element' => '/edit/elements/part_export/broadband_snmp.html',
52   'options' => \%options,
53   'weight'  => 10,
54   'notes'   => <<'END'
55 Send one or more SNMP SET requests to the IP address registered to the service.
56 The value may interpolate fields from svc_broadband by prefixing the field 
57 name with <b>$</b>, or <b>$new_</b> and <b>$old_</b> for replace operations.
58 END
59 );
60
61 sub export_insert {
62   my $self = shift;
63   $self->export_command('insert', @_);
64 }
65
66 sub export_delete {
67   my $self = shift;
68   $self->export_command('delete', @_);
69 }
70
71 sub export_replace {
72   my $self = shift;
73   $self->export_command('replace', @_);
74 }
75
76 sub export_suspend {
77   my $self = shift;
78   $self->export_command('suspend', @_);
79 }
80
81 sub export_unsuspend {
82   my $self = shift;
83   $self->export_command('unsuspend', @_);
84 }
85
86 sub export_command {
87   my $self = shift;
88   my ($action, $svc_new, $svc_old) = @_;
89
90   my @a = split("\n", $self->option('action'));
91   my @o = split("\n", $self->option('oid'));
92   my @v = split("\n", $self->option('value'));
93   my @commands;
94   warn "$me parsing $action commands:\n" if $DEBUG;
95   while (@a) {
96     my $oid = shift @o;
97     my $value = shift @v;
98     next unless shift(@a) eq $action; # ignore commands for other actions
99     $value = $self->substitute($value, $svc_new, $svc_old);
100     warn "$me     $oid :=$value\n" if $DEBUG;
101     push @commands, $oid, $value;
102   }
103
104   my $ip_addr = $svc_new->ip_addr;
105   # ip address change: send to old address unless told otherwise
106   if ( defined $svc_old and ! $self->option('ip_addr_change_to_new') ) {
107     $ip_addr = $svc_old->ip_addr;
108   }
109   warn "$me opening session to $ip_addr\n" if $DEBUG;
110
111   my %opt = (
112     DestHost  => $ip_addr,
113     Community => $self->option('community'),
114     Timeout   => ($self->option('timeout') || 20) * 1000,
115   );
116   my $version = $self->option('version');
117   $opt{Version} = $snmp_version{$version} or die 'invalid version';
118   $opt{VarList} = \@commands; # for now
119
120   $self->snmp_queue( $svc_new->svcnum, %opt );
121 }
122
123 sub snmp_queue {
124   my $self = shift;
125   my $svcnum = shift;
126   my $queue = new FS::queue {
127     'svcnum'  => $svcnum,
128     'job'     => 'FS::part_export::broadband_snmp::snmp_request',
129   };
130   $queue->insert(@_);
131 }
132
133 sub snmp_request {
134   my %opt = @_;
135   my $flatvarlist = delete $opt{VarList};
136   my $session = SNMP::Session->new(%opt);
137
138   warn "$me sending SET request\n" if $DEBUG;
139
140   my @varlist;
141   while (@$flatvarlist) {
142     my @this = splice(@$flatvarlist, 0, 2);
143     push @varlist, [ $this[0], 0, $this[1], undef ];
144     # XXX new option to choose the IID (array index) of the object?
145   }
146
147   $session->set(\@varlist);
148   my $error = $session->{ErrorStr};
149
150   if ( $session->{ErrorNum} ) {
151     die "SNMP request failed: $error\n";
152   }
153 }
154
155 sub substitute {
156   # double-quote-ish interpolation of service fields
157   # accepts old_ and new_ for replace actions, like shellcommands
158   my $self = shift;
159   my ($value, $svc_new, $svc_old) = @_;
160   foreach my $field ( $svc_new->fields ) {
161     my $new_val = $svc_new->$field;
162     $value =~ s/\$(new_)?$field/$new_val/g;
163     if ( $svc_old ) { # replace only
164       my $old_val = $svc_old->$field;
165       $value =~ s/\$old_$field/$old_val/g;
166     }
167   }
168   $value;
169 }
170
171 sub _upgrade_exporttype {
172   eval 'use FS::Record qw(qsearch qsearchs)';
173   # change from old style with numeric oid, data type flag, and value
174   # on consecutive lines
175   foreach my $export (qsearch('part_export',
176                       { exporttype => 'broadband_snmp' } ))
177   {
178     # for the new options
179     my %new_options = (
180       'action' => [],
181       'oid'    => [],
182       'value'  => [],
183     );
184     foreach my $action (qw(insert replace delete suspend unsuspend)) {
185       my $old_option = qsearchs('part_export_option',
186                       { exportnum   => $export->exportnum,
187                         optionname  => $action.'_command' } );
188       next if !$old_option;
189       my $text = $old_option->optionvalue;
190       my @commands = split("\n", $text);
191       foreach (@commands) {
192         my ($oid, $type, $value) = split /\s/, $_, 3;
193         push @{$new_options{action}}, $action;
194         push @{$new_options{oid}},    $oid;
195         push @{$new_options{value}},   $value;
196       }
197       my $error = $old_option->delete;
198       warn "error migrating ${action}_command option: $error\n" if $error;
199     }
200     foreach (keys(%new_options)) {
201       my $new_option = FS::part_export_option->new({
202           exportnum   => $export->exportnum,
203           optionname  => $_,
204           optionvalue => join("\n", @{ $new_options{$_} })
205       });
206       my $error = $new_option->insert;
207       warn "error inserting '$_' option: $error\n" if $error;
208     }
209   } #foreach $export
210   '';
211 }
212
213 1;