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,
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.
63 $self->export_command('insert', @_);
68 $self->export_command('delete', @_);
73 $self->export_command('replace', @_);
78 $self->export_command('suspend', @_);
81 sub export_unsuspend {
83 $self->export_command('unsuspend', @_);
88 my ($action, $svc_new, $svc_old) = @_;
90 my @a = split("\n", $self->option('action'));
91 my @o = split("\n", $self->option('oid'));
92 my @v = split("\n", $self->option('value'));
94 warn "$me parsing $action commands:\n" if $DEBUG;
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;
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;
109 warn "$me opening session to $ip_addr\n" if $DEBUG;
112 DestHost => $ip_addr,
113 Community => $self->option('community'),
114 Timeout => ($self->option('timeout') || 20) * 1000,
116 my $version = $self->option('version');
117 $opt{Version} = $snmp_version{$version} or die 'invalid version';
118 $opt{VarList} = \@commands; # for now
120 $self->snmp_queue( $svc_new->svcnum, %opt );
126 my $queue = new FS::queue {
128 'job' => 'FS::part_export::broadband_snmp::snmp_request',
135 my $flatvarlist = delete $opt{VarList};
136 my $session = SNMP::Session->new(%opt);
138 warn "$me sending SET request\n" if $DEBUG;
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?
147 $session->set(\@varlist);
148 my $error = $session->{ErrorStr};
150 if ( $session->{ErrorNum} ) {
151 die "SNMP request failed: $error\n";
156 # double-quote-ish interpolation of service fields
157 # accepts old_ and new_ for replace actions, like shellcommands
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;
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' } ))
178 # for the new options
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;
197 my $error = $old_option->delete;
198 warn "error migrating ${action}_command option: $error\n" if $error;
200 foreach (keys(%new_options)) {
201 my $new_option = FS::part_export_option->new({
202 exportnum => $export->exportnum,
204 optionvalue => join("\n", @{ $new_options{$_} })
206 my $error = $new_option->insert;
207 warn "error inserting '$_' option: $error\n" if $error;