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 },
41 'ip_addr_change_to_new' => {
42 label=>'Send IP address changes to new address',
45 'timeout' => { label=>'Timeout (seconds)' },
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,
56 Send one or more SNMP SET requests to the IP address registered to the service.
57 The value may interpolate fields from svc_broadband by prefixing the field
58 name with <b>$</b>, or <b>$new_</b> and <b>$old_</b> for replace operations.
64 $self->export_command('insert', @_);
69 $self->export_command('delete', @_);
74 $self->export_command('replace', @_);
79 $self->export_command('suspend', @_);
82 sub export_unsuspend {
84 $self->export_command('unsuspend', @_);
89 my ($action, $svc_new, $svc_old) = @_;
91 my @a = split("\n", $self->option('action'));
92 my @o = split("\n", $self->option('oid'));
93 my @v = split("\n", $self->option('value'));
95 warn "$me parsing $action commands:\n" if $DEBUG;
99 next unless shift(@a) eq $action; # ignore commands for other actions
100 $value = $self->substitute($value, $svc_new, $svc_old);
101 warn "$me $oid :=$value\n" if $DEBUG;
102 push @commands, $oid, $value;
105 my $ip_addr = $svc_new->ip_addr;
106 # ip address change: send to old address unless told otherwise
107 if ( defined $svc_old and ! $self->option('ip_addr_change_to_new') ) {
108 $ip_addr = $svc_old->ip_addr;
110 warn "$me opening session to $ip_addr\n" if $DEBUG;
113 DestHost => $ip_addr,
114 Community => $self->option('community'),
115 Timeout => ($self->option('timeout') || 20) * 1000,
117 my $version = $self->option('version');
118 $opt{Version} = $snmp_version{$version} or die 'invalid version';
119 $opt{VarList} = \@commands; # for now
121 $self->snmp_queue( $svc_new->svcnum, %opt );
127 my $queue = new FS::queue {
129 'job' => 'FS::part_export::broadband_snmp::snmp_request',
136 my $flatvarlist = delete $opt{VarList};
137 my $session = SNMP::Session->new(%opt);
139 warn "$me sending SET request\n" if $DEBUG;
142 while (@$flatvarlist) {
143 my @this = splice(@$flatvarlist, 0, 2);
144 push @varlist, [ $this[0], 0, $this[1], undef ];
145 # XXX new option to choose the IID (array index) of the object?
148 $session->set(\@varlist);
149 my $error = $session->{ErrorStr};
151 if ( $session->{ErrorNum} ) {
152 die "SNMP request failed: $error\n";
157 # double-quote-ish interpolation of service fields
158 # accepts old_ and new_ for replace actions, like shellcommands
160 my ($value, $svc_new, $svc_old) = @_;
161 foreach my $field ( $svc_new->fields ) {
162 my $new_val = $svc_new->$field;
163 $value =~ s/\$(new_)?$field/$new_val/g;
164 if ( $svc_old ) { # replace only
165 my $old_val = $svc_old->$field;
166 $value =~ s/\$old_$field/$old_val/g;
172 sub _upgrade_exporttype {
173 eval 'use FS::Record qw(qsearch qsearchs)';
174 # change from old style with numeric oid, data type flag, and value
175 # on consecutive lines
176 foreach my $export (qsearch('part_export',
177 { exporttype => 'broadband_snmp' } ))
179 # for the new options
185 foreach my $action (qw(insert replace delete suspend unsuspend)) {
186 my $old_option = qsearchs('part_export_option',
187 { exportnum => $export->exportnum,
188 optionname => $action.'_command' } );
189 next if !$old_option;
190 my $text = $old_option->optionvalue;
191 my @commands = split("\n", $text);
192 foreach (@commands) {
193 my ($oid, $type, $value) = split /\s/, $_, 3;
194 push @{$new_options{action}}, $action;
195 push @{$new_options{oid}}, $oid;
196 push @{$new_options{value}}, $value;
198 my $error = $old_option->delete;
199 warn "error migrating ${action}_command option: $error\n" if $error;
201 foreach (keys(%new_options)) {
202 my $new_option = FS::part_export_option->new({
203 exportnum => $export->exportnum,
205 optionvalue => join("\n", @{ $new_options{$_} })
207 my $error = $new_option->insert;
208 warn "error inserting '$_' option: $error\n" if $error;