get serviceids from torrus config + our torrus_srvderive table, not from srvexport
[freeside.git] / FS / FS / NetworkMonitoringSystem / Torrus_Internal.pm
1 package FS::NetworkMonitoringSystem::Torrus_Internal;
2
3 use strict;
4 #use vars qw( $DEBUG $me );
5 use Fcntl qw(:flock);
6 use IO::File;
7 use File::Slurp qw(slurp);
8 use Date::Format;
9 use XML::Simple;
10 use FS::Record qw(qsearch qsearchs dbh);
11 use FS::svc_port;
12 use FS::torrus_srvderive;
13 use FS::torrus_srvderive_component;
14 use Torrus::ConfigTree;
15
16 #$DEBUG = 0;
17 #$me = '[FS::NetworkMonitoringSystem::Torrus_Internal]';
18
19 our $lock;
20 our $lockfile = '/usr/local/etc/torrus/discovery/FSLOCK';
21 our $ddxfile  = '/usr/local/etc/torrus/discovery/routers.ddx';
22
23 sub new {
24     my $class = shift;
25     my $self = {};
26     bless $self, $class;
27     return $self;
28 }
29
30 sub ddx2hash {
31     my $self = shift;
32     my $ddx_xml = slurp($ddxfile);
33     my $xs = new XML::Simple(RootName=> undef, SuppressEmpty => '', 
34                                 ForceArray => 1, );
35     return $xs->XMLin($ddx_xml);
36 }
37
38 sub get_router_serviceids {
39   my $self = shift;
40   my $router = shift;
41   my $find_serviceid = shift;
42   my $found_serviceid = 0;
43   my $ddx_hash = $self->ddx2hash;
44   return '' unless $ddx_hash->{'host'};
45
46   my @hosts = @{$ddx_hash->{host}};
47   foreach my $host ( @hosts ) {
48     my $param = $host->{param};
49     if($param && $param->{'snmp-host'} 
50               && (!$router || $param->{'snmp-host'}->{'value'} eq $router)
51               && $param->{'RFC2863_IF_MIB::external-serviceid'}) {
52       my $serviceids =
53         $param->{'RFC2863_IF_MIB::external-serviceid'}->{'content'};
54       my %hash = ();
55       if ($serviceids) {
56         my @serviceids = split(',',$serviceids);
57         foreach my $serviceid ( @serviceids ) {
58           $serviceid =~ s/^\s+|\s+$//g;
59           my @s = split(':',$serviceid);
60           next unless scalar(@s) == 4;
61           $hash{$s[1]} = $s[0] if $router;
62           if ($find_serviceid && $find_serviceid eq $s[0]) {
63             $hash{$param->{'snmp-host'}->{'value'}} = $s[1];
64             $found_serviceid = 1;
65           }
66         }
67       }
68       return \%hash if ($router || $found_serviceid);
69     }
70   }
71   '';
72 }
73
74 #false laziness and probably should be merged w/above, but didn't want to mess
75 # that up
76 sub all_router_serviceids {
77   my $self = shift;
78   my $ddx_hash = $self->ddx2hash;
79   return () unless $ddx_hash->{'host'};
80
81   my %hash = ();
82   my @hosts = @{$ddx_hash->{host}};
83   foreach my $host ( @hosts ) {
84     my $param = $host->{param};
85     if($param && $param->{'snmp-host'} 
86               && (!$router || $param->{'snmp-host'}->{'value'} eq $router)
87               && $param->{'RFC2863_IF_MIB::external-serviceid'}) {
88       my $serviceids =
89         $param->{'RFC2863_IF_MIB::external-serviceid'}->{'content'};
90       if ($serviceids) {
91         my @serviceids = split(',',$serviceids);
92         foreach my $serviceid ( @serviceids ) {
93           $serviceid =~ s/^\s+|\s+$//g;
94           my @s = split(':',$serviceid);
95           next unless scalar(@s) == 4;
96           $hash{$s[0]}=1;
97         }
98       }
99     }
100   }
101   return sort keys %hash;
102 }
103
104 sub port_graphs_link {
105     # hardcoded for 'main' tree for now 
106     my $self = shift;
107     my $serviceid = shift;
108     my $hash = $self->get_router_serviceids(undef,$serviceid) or return '';
109     my @keys = keys %$hash; # yeah this is weird...
110     my $host = $keys[0];
111     my $iface = $hash->{$keys[0]};
112     my $config_tree = new Torrus::ConfigTree( -TreeName => 'main' );
113     my $token = $config_tree->token("/Routers/$host/Interface_Counters/$iface/InOut_bps");
114     return $Torrus::Freeside::FSURL."/torrus/main?token=$token";
115 }
116
117 sub find_svc {
118     my $self = shift;
119     my $serviceid = shift;
120     return '' unless $serviceid =~ /^[0-9A-Za-z_\-.\\\/ ]+$/;
121   
122     my @svc_port = qsearch('svc_port', { 'serviceid' => $serviceid });
123     return '' unless scalar(@svc_port);
124
125     # for now it's like this, later on just change to qsearchs
126
127     return $svc_port[0];
128 }
129
130 sub find_torrus_srvderive_component {
131     my $self = shift;
132     my $serviceid = shift;
133     return '' unless $serviceid =~ /^[0-9A-Za-z_\-.\\\/ ]+$/;
134   
135     qsearchs('torrus_srvderive_component', { 'serviceid' => $serviceid });
136 }
137
138 sub report {
139   my $self = shift;
140
141   my @ls = localtime(time);
142   my ($d,$m,$y) = ($ls[3], $ls[4]+1, $ls[5]+1900);
143   if ( $ls[3] == 1 ) {
144     $m--;
145     if ($m == 0) { $m=12; $y-- }
146     #i should have better error checking
147     system('torrus', 'report', '--report=MonthlyUsage', "--date=$y-$m-01");
148     system('torrus', 'report', '--genhtml', '--tree=main');
149   }
150
151 }
152
153 sub add_router {
154   my($self, $ip) = @_;
155
156   my $newhost = 
157     qq(  <host>\n).
158     qq(    <param name="snmp-host" value="$ip"/>\n).
159     qq(  </host>\n);
160
161   my $ddx = $self->_torrus_loadddx;
162
163   $ddx =~ s{(</snmp-discovery>)}{$newhost$1};
164
165   $self->_torrus_newddx($ddx);
166
167 }
168
169 sub add_interface {
170   my($self, $router_ip, $interface, $serviceid ) = @_;
171
172   $interface =~ s(\/)(_)g; #slashes become underscores
173   $interface =~ s(\.)(_)g; #periods too, huh
174
175   #should just use a proper XML parser huh
176
177   my @ddx = split(/\n/, $self->_torrus_loadddx);
178
179   die "Torrus Service ID $serviceid in use\n"
180     if grep /^\s*$serviceid:/, @ddx;
181
182   my $newline = "     $serviceid:$interface:Both:main,";
183
184   my $new = '';
185
186   my $added = 0;
187
188   while ( my $line = shift(@ddx) ) {
189     $new .= "$line\n";
190     next unless $line =~ /^\s*<param\s+name="snmp-host"\s+value="$router_ip"\/?>/i;
191
192     while ( my $hostline = shift(@ddx) ) {
193       $new .= "$hostline\n" unless $hostline =~ /^\s+<\/host>\s*/i;
194       if ( $hostline =~ /^\s*<param name="RFC2863_IF_MIB::external-serviceid"\/?>/i ) {
195
196         while ( my $paramline = shift(@ddx) ) {
197           if ( $paramline =~ /^\s*<\/param>/ ) {
198             $new .= "$newline\n$paramline\n";
199             last; #paramline
200           } else {
201             $new .= "$paramline\n";
202           }
203         }
204
205         $added++;
206
207       } elsif ( $hostline =~ /^\s+<\/host>\s*/i ) {
208         unless ( $added ) {
209           $new .= 
210             qq(   <param name="RFC2863_IF_MIB::external-serviceid">\n).
211             qq(     $newline\n").
212             qq(   </param>\n);
213         }
214         $new .= "$hostline\n";
215         last; #hostline
216       }
217  
218     }
219
220   }
221
222   $self->_torrus_newddx($new);
223
224 }
225
226 sub _torrus_lock {
227   $lock = new IO::File ">>$lockfile" or die $!;
228   flock($lock, LOCK_EX);
229 }
230
231 sub _torrus_unlock {
232   flock($lock, LOCK_UN);
233   close $lock;
234 }
235
236 sub _torrus_loadddx {
237   my($self) = @_;
238   $self->_torrus_lock;
239   return slurp($ddxfile);
240 }
241
242 sub _torrus_newddx {
243   my($self, $ddx) = @_;
244
245   my $new = new IO::File ">$ddxfile.new"
246     or die "can't write to $ddxfile.new: $!";
247   print $new $ddx;
248   close $new;
249
250   my $tmpname = $ddxfile . Date::Format::time2str('%Y%m%d%H%M%S',time);
251   rename("$ddxfile", $tmpname) or die $!;
252   rename("$ddxfile.new", $ddxfile) or die $!;
253
254   $self->_torrus_reload;
255 }
256
257 sub _torrus_reload {
258   my($self) = @_;
259
260   #i should use IPC::Run and have better error checking (commands are silent
261   # for success, or output errors)
262
263   system('torrus', 'devdiscover', "--in=$ddxfile");
264
265   system('torrus', 'compile', '--tree=main'); # , '--verbose'
266
267   $self->_torrus_unlock;
268
269 }
270
271 #sub torrus_serviceids {
272 #  my $self = shift;
273 #
274 #  #is this going to get too slow or will the index make it okay?
275 #  my $sth = dbh->prepare("SELECT DISTINCT(serviceid) FROM srvexport")
276 #    or die dbh->errstr;
277 #  $sth->execute or die $sth->errstr;
278 #  my %serviceid = ();
279 #  while ( my $row = $sth->fetchrow_arrayref ) {
280 #    my $serviceid = $row->[0];
281 #    $serviceid =~ s/_(IN|OUT)$//;
282 #    $serviceid{$serviceid}=1;
283 #  }
284 #  my @serviceids = sort keys %serviceid;
285 #
286 #  @serviceids;
287 #
288 #}
289
290 sub torrus_serviceids {
291   my $self = shift;
292   my @serviceids = $self->all_router_serviceids;
293   push @serviceids, map $_->serviceid, qsearch('torrus_srvderive', {});
294   return sort @serviceids;
295 }
296
297 1;