1 package FS::part_export::broadband_snmp;
4 use vars qw(%info $DEBUG);
5 use base 'FS::part_export';
11 my $me = '['.__PACKAGE__.']';
13 tie my %snmp_version, 'Tie::IxHash',
19 #tie my %snmp_type, 'Tie::IxHash',
24 # o => OBJECT_IDENTIFIER,
27 # # others not implemented yet
30 tie my %options, 'Tie::IxHash',
31 'version' => { label=>'SNMP version',
33 options => [ keys %snmp_version ],
35 'community' => { label=>'Community', default=>'public' },
37 'action' => { multiple=>1 },
38 'oid' => { multiple=>1 },
39 'value' => { multiple=>1 },
40 'datatype'=> { multiple=>1 },
42 'ip_addr_change_to_new' => {
43 label=>'Send IP address changes to new address',
46 'timeout' => { label=>'Timeout (seconds)' },
50 'svc' => 'svc_broadband',
51 'desc' => 'Send SNMP requests to the service IP address',
52 'config_element' => '/edit/elements/part_export/broadband_snmp.html',
53 'options' => \%options,
57 Send one or more SNMP SET requests to the IP address registered to the service.
58 The value may interpolate fields from svc_broadband by prefixing the field
59 name with <b>$</b>, or <b>$new_</b> and <b>$old_</b> for replace operations.
65 $self->export_command('insert', @_);
70 $self->export_command('delete', @_);
75 $self->export_command('replace', @_);
80 $self->export_command('suspend', @_);
83 sub export_unsuspend {
85 $self->export_command('unsuspend', @_);
90 my ($action, $svc_new, $svc_old) = @_;
92 my @a = split("\n", $self->option('action'));
93 my @o = split("\n", $self->option('oid'));
94 my @v = split("\n", $self->option('value'));
96 warn "$me parsing $action commands:\n" if $DEBUG;
100 next unless shift(@a) eq $action; # ignore commands for other actions
101 $value = $self->substitute($value, $svc_new, $svc_old);
102 warn "$me $oid :=$value\n" if $DEBUG;
103 push @commands, $oid, $value;
106 my $ip_addr = $svc_new->ip_addr;
107 # ip address change: send to old address unless told otherwise
108 if ( defined $svc_old and ! $self->option('ip_addr_change_to_new') ) {
109 $ip_addr = $svc_old->ip_addr;
111 warn "$me opening session to $ip_addr\n" if $DEBUG;
114 DestHost => $ip_addr,
115 Community => $self->option('community'),
116 Timeout => ($self->option('timeout') || 20) * 1000,
118 my $version = $self->option('version');
119 $opt{Version} = $snmp_version{$version} or die 'invalid version';
120 $opt{VarList} = \@commands; # for now
122 $self->snmp_queue( $svc_new->svcnum, %opt );
128 my $queue = new FS::queue {
130 'job' => 'FS::part_export::broadband_snmp::snmp_request',
137 my $flatvarlist = delete $opt{VarList};
138 my $session = SNMP::Session->new(%opt);
140 warn "$me sending SET request\n" if $DEBUG;
143 while (@$flatvarlist) {
144 my @this = splice(@$flatvarlist, 0, 2);
145 push @varlist, [ $this[0], 0, $this[1], undef ];
146 # XXX new option to choose the IID (array index) of the object?
149 $session->set(\@varlist);
150 my $error = $session->{ErrorStr};
152 if ( $session->{ErrorNum} ) {
153 die "SNMP request failed: $error\n";
158 # double-quote-ish interpolation of service fields
159 # accepts old_ and new_ for replace actions, like shellcommands
161 my ($value, $svc_new, $svc_old) = @_;
162 foreach my $field ( $svc_new->fields ) {
163 my $new_val = $svc_new->$field;
164 $value =~ s/\$(new_)?$field/$new_val/g;
165 if ( $svc_old ) { # replace only
166 my $old_val = $svc_old->$field;
167 $value =~ s/\$old_$field/$old_val/g;
173 sub _upgrade_exporttype {
174 eval 'use FS::Record qw(qsearch qsearchs)';
175 # change from old style with numeric oid, data type flag, and value
176 # on consecutive lines
177 foreach my $export (qsearch('part_export',
178 { exporttype => 'broadband_snmp' } ))
180 # for the new options
186 foreach my $action (qw(insert replace delete suspend unsuspend)) {
187 my $old_option = qsearchs('part_export_option',
188 { exportnum => $export->exportnum,
189 optionname => $action.'_command' } );
190 next if !$old_option;
191 my $text = $old_option->optionvalue;
192 my @commands = split("\n", $text);
193 foreach (@commands) {
194 my ($oid, $type, $value) = split /\s/, $_, 3;
195 push @{$new_options{action}}, $action;
196 push @{$new_options{oid}}, $oid;
197 push @{$new_options{value}}, $value;
199 my $error = $old_option->delete;
200 warn "error migrating ${action}_command option: $error\n" if $error;
202 foreach (keys(%new_options)) {
203 my $new_option = FS::part_export_option->new({
204 exportnum => $export->exportnum,
206 optionvalue => join("\n", @{ $new_options{$_} })
208 my $error = $new_option->insert;
209 warn "error inserting '$_' option: $error\n" if $error;