SNMP export: allow manually entering OIDs, #20945
[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   'datatype'=> { multiple=>1 },
41
42   'ip_addr_change_to_new' => { 
43     label=>'Send IP address changes to new address',
44     type=>'checkbox'
45   },
46   'timeout' => { label=>'Timeout (seconds)' },
47 ;
48
49 %info = (
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,
54   'no_machine' => 1,
55   'weight'  => 10,
56   'notes'   => <<'END'
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.
60 END
61 );
62
63 sub export_insert {
64   my $self = shift;
65   $self->export_command('insert', @_);
66 }
67
68 sub export_delete {
69   my $self = shift;
70   $self->export_command('delete', @_);
71 }
72
73 sub export_replace {
74   my $self = shift;
75   $self->export_command('replace', @_);
76 }
77
78 sub export_suspend {
79   my $self = shift;
80   $self->export_command('suspend', @_);
81 }
82
83 sub export_unsuspend {
84   my $self = shift;
85   $self->export_command('unsuspend', @_);
86 }
87
88 sub export_command {
89   my $self = shift;
90   my ($action, $svc_new, $svc_old) = @_;
91
92   my @a = split("\n", $self->option('action'));
93   my @o = split("\n", $self->option('oid'));
94   my @v = split("\n", $self->option('value'));
95   my @commands;
96   warn "$me parsing $action commands:\n" if $DEBUG;
97   while (@a) {
98     my $oid = shift @o;
99     my $value = shift @v;
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;
104   }
105
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;
110   }
111   warn "$me opening session to $ip_addr\n" if $DEBUG;
112
113   my %opt = (
114     DestHost  => $ip_addr,
115     Community => $self->option('community'),
116     Timeout   => ($self->option('timeout') || 20) * 1000,
117   );
118   my $version = $self->option('version');
119   $opt{Version} = $snmp_version{$version} or die 'invalid version';
120   $opt{VarList} = \@commands; # for now
121
122   $self->snmp_queue( $svc_new->svcnum, %opt );
123 }
124
125 sub snmp_queue {
126   my $self = shift;
127   my $svcnum = shift;
128   my $queue = new FS::queue {
129     'svcnum'  => $svcnum,
130     'job'     => 'FS::part_export::broadband_snmp::snmp_request',
131   };
132   $queue->insert(@_);
133 }
134
135 sub snmp_request {
136   my %opt = @_;
137   my $flatvarlist = delete $opt{VarList};
138   my $session = SNMP::Session->new(%opt);
139
140   warn "$me sending SET request\n" if $DEBUG;
141
142   my @varlist;
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?
147   }
148
149   $session->set(\@varlist);
150   my $error = $session->{ErrorStr};
151
152   if ( $session->{ErrorNum} ) {
153     die "SNMP request failed: $error\n";
154   }
155 }
156
157 sub substitute {
158   # double-quote-ish interpolation of service fields
159   # accepts old_ and new_ for replace actions, like shellcommands
160   my $self = shift;
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;
168     }
169   }
170   $value;
171 }
172
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' } ))
179   {
180     # for the new options
181     my %new_options = (
182       'action' => [],
183       'oid'    => [],
184       'value'  => [],
185     );
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;
198       }
199       my $error = $old_option->delete;
200       warn "error migrating ${action}_command option: $error\n" if $error;
201     }
202     foreach (keys(%new_options)) {
203       my $new_option = FS::part_export_option->new({
204           exportnum   => $export->exportnum,
205           optionname  => $_,
206           optionvalue => join("\n", @{ $new_options{$_} })
207       });
208       my $error = $new_option->insert;
209       warn "error inserting '$_' option: $error\n" if $error;
210     }
211   } #foreach $export
212   '';
213 }
214
215 1;