summaryrefslogtreecommitdiff
path: root/FS/FS/part_export/broadband_snmp.pm
blob: 8ebc716e782a7907adc69dbf4be969d23a280d85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package FS::part_export::broadband_snmp;

use strict;
use vars qw(%info $DEBUG);
use base 'FS::part_export';
use SNMP;
use Tie::IxHash;

$DEBUG = 0;

my $me = '['.__PACKAGE__.']';

tie my %snmp_version, 'Tie::IxHash',
  v1  => '1',
  v2c => '2c',
  # v3 unimplemented
;

#tie my %snmp_type, 'Tie::IxHash',
#  i => INTEGER,
#  u => UNSIGNED32,
#  s => OCTET_STRING,
#  n => NULL,
#  o => OBJECT_IDENTIFIER,
#  t => TIMETICKS,
#  a => IPADDRESS,
#  # others not implemented yet
#;

tie my %options, 'Tie::IxHash',
  'version' => { label=>'SNMP version', 
    type => 'select',
    options => [ keys %snmp_version ],
   },
  'community' => { label=>'Community', default=>'public' },

  'action'  => { multiple=>1 },
  'oid'     => { multiple=>1 },
  'value'   => { multiple=>1 },
  'datatype'=> { multiple=>1 },

  'ip_addr_change_to_new' => { 
    label=>'Send IP address changes to new address',
    type=>'checkbox'
  },
  'timeout' => { label=>'Timeout (seconds)' },
;

%info = (
  'svc'     => 'svc_broadband',
  'desc'    => 'Send SNMP requests to the service IP address',
  'config_element' => '/edit/elements/part_export/broadband_snmp.html',
  'options' => \%options,
  'no_machine' => 1,
  'weight'  => 10,
  'notes'   => <<'END'
Send one or more SNMP SET requests to the IP address registered to the service.
The value may interpolate fields from svc_broadband, cust_location, or
cust_main by prefixing the field name with <b>$</b>. For replace operations,
svc_broadband fields may be prefixed with <b>$new_</b> and <b>$old_</b>
(e.g. "$old_mac_addr").
END
);

sub _export_insert {
  my $self = shift;
  $self->export_command('insert', @_);
}

sub _export_delete {
  my $self = shift;
  $self->export_command('delete', @_);
}

sub _export_replace {
  my $self = shift;
  $self->export_command('replace', @_);
}

sub _export_suspend {
  my $self = shift;
  $self->export_command('suspend', @_);
}

sub _export_unsuspend {
  my $self = shift;
  $self->export_command('unsuspend', @_);
}

sub export_command {
  my $self = shift;
  my ($action, $svc_new, $svc_old) = @_;

  my @a = split("\n", $self->option('action'));
  my @o = split("\n", $self->option('oid'));
  my @v = split("\n", $self->option('value'));
  my @commands;
  warn "$me parsing $action commands:\n" if $DEBUG;
  while (@a) {
    my $oid = shift @o;
    my $value = shift @v;
    next unless shift(@a) eq $action; # ignore commands for other actions
    $value = $self->substitute($value, $svc_new, $svc_old);
    warn "$me     $oid :=$value\n" if $DEBUG;
    push @commands, $oid, $value;
  }

  my $ip_addr = $svc_new->ip_addr;
  # ip address change: send to old address unless told otherwise
  if ( defined $svc_old and ! $self->option('ip_addr_change_to_new') ) {
    $ip_addr = $svc_old->ip_addr;
  }
  warn "$me opening session to $ip_addr\n" if $DEBUG;

  my %opt = (
    DestHost  => $ip_addr,
    Community => $self->option('community'),
    Timeout   => ($self->option('timeout') || 20) * 1000,
  );
  my $version = $self->option('version');
  $opt{Version} = $snmp_version{$version} or die 'invalid version';
  $opt{VarList} = \@commands; # for now

  $self->snmp_queue( $svc_new->svcnum, %opt );
}

sub snmp_queue {
  my $self = shift;
  my $svcnum = shift;
  my $queue = new FS::queue {
    'svcnum'  => $svcnum,
    'job'     => 'FS::part_export::broadband_snmp::snmp_request',
  };
  $queue->insert(@_);
}

sub snmp_request {
  my %opt = @_;
  my $flatvarlist = delete $opt{VarList};
  my $session = SNMP::Session->new(%opt);

  warn "$me sending SET request\n" if $DEBUG;

  my @varlist;
  while (@$flatvarlist) {
    my @this = splice(@$flatvarlist, 0, 2);
    push @varlist, [ $this[0], 0, $this[1], undef ];
    # XXX new option to choose the IID (array index) of the object?
  }

  $session->set(\@varlist);
  my $error = $session->{ErrorStr};

  if ( $session->{ErrorNum} ) {
    die "SNMP request failed: $error\n";
  }
}

sub substitute {
  # double-quote-ish interpolation of service fields
  # accepts old_ and new_ for replace actions, like shellcommands
  my $self = shift;
  my ($value, $svc_new, $svc_old) = @_;

  my $location = $svc_new->cust_svc->cust_pkg->cust_location;
  my $cust_main = $location->cust_main;

  foreach my $field ( $svc_new->fields ) {
    my $new_val = $svc_new->$field;
    $value =~ s/\$(new_)?$field/$new_val/g;
    if ( $svc_old ) { # replace only
      my $old_val = $svc_old->$field;
      $value =~ s/\$old_$field/$old_val/g;
    }
  }

  # we don't yet have export_relocate hooks in here, so there's no old/new
  # cust_location. do cust_location before cust_main, since cust_main has
  # a bunch of empty fields with the same names.
  
  foreach my $field ( $location->fields ) {
    my $curr_val = $location->get($field);
    $value =~ s/\$$field/$curr_val/g;
  }

  foreach my $field ( $cust_main->fields ) {
    my $curr_val = $cust_main->get($field);
    $value =~ s/\$$field/$curr_val/g;
  }

  $value;
}

sub _upgrade_exporttype {
  eval 'use FS::Record qw(qsearch qsearchs)';
  # change from old style with numeric oid, data type flag, and value
  # on consecutive lines
  foreach my $export (qsearch('part_export',
                      { exporttype => 'broadband_snmp' } ))
  {
    # for the new options
    my %new_options = (
      'action' => [],
      'oid'    => [],
      'value'  => [],
    );
    foreach my $action (qw(insert replace delete suspend unsuspend)) {
      my $old_option = qsearchs('part_export_option',
                      { exportnum   => $export->exportnum,
                        optionname  => $action.'_command' } );
      next if !$old_option;
      my $text = $old_option->optionvalue;
      my @commands = split("\n", $text);
      foreach (@commands) {
        my ($oid, $type, $value) = split /\s/, $_, 3;
        push @{$new_options{action}}, $action;
        push @{$new_options{oid}},    $oid;
        push @{$new_options{value}},   $value;
      }
      my $error = $old_option->delete;
      warn "error migrating ${action}_command option: $error\n" if $error;
    }
    foreach (keys(%new_options)) {
      my $new_option = FS::part_export_option->new({
          exportnum   => $export->exportnum,
          optionname  => $_,
          optionvalue => join("\n", @{ $new_options{$_} })
      });
      my $error = $new_option->insert;
      warn "error inserting '$_' option: $error\n" if $error;
    }
  } #foreach $export
  '';
}

1;