1 package FS::NetworkMonitoringSystem::Torrus_Internal;
4 #use vars qw( $DEBUG $me );
7 use File::Slurp qw(slurp);
11 use FS::Record qw(qsearch qsearchs dbh);
13 use FS::torrus_srvderive;
14 use FS::torrus_srvderive_component;
15 #use Torrus::ConfigTree;
18 #$me = '[FS::NetworkMonitoringSystem::Torrus_Internal]';
21 our $lockfile = '/usr/local/etc/torrus/discovery/FSLOCK';
22 our $ddxfile = '/usr/local/etc/torrus/discovery/routers.ddx';
33 my $ddx_xml = slurp($ddxfile);
34 my $xs = new XML::Simple(RootName=> undef, SuppressEmpty => '',
36 return $xs->XMLin($ddx_xml);
39 sub get_router_serviceids {
42 my $find_serviceid = shift;
43 my $found_serviceid = 0;
44 my $ddx_hash = $self->ddx2hash;
45 return '' unless $ddx_hash->{'host'};
47 my @hosts = @{$ddx_hash->{host}};
48 foreach my $host ( @hosts ) {
49 my $param = $host->{param};
50 if($param && $param->{'snmp-host'}
51 && (!$router || $param->{'snmp-host'}->{'value'} eq $router)
52 && $param->{'RFC2863_IF_MIB::external-serviceid'}) {
54 $param->{'RFC2863_IF_MIB::external-serviceid'}->{'content'};
57 my @serviceids = split(',',$serviceids);
58 foreach my $serviceid ( @serviceids ) {
59 $serviceid =~ s/^\s+|\s+$//g;
60 my @s = split(':',$serviceid);
61 next unless scalar(@s) == 4;
62 $hash{$s[1]} = $s[0] if $router;
63 if ($find_serviceid && $find_serviceid eq $s[0]) {
64 $hash{$param->{'snmp-host'}->{'value'}} = $s[1];
69 return \%hash if ($router || $found_serviceid);
75 #false laziness and probably should be merged w/above, but didn't want to mess
77 sub all_router_serviceids {
79 my $ddx_hash = $self->ddx2hash;
80 return () unless $ddx_hash->{'host'};
83 my @hosts = @{$ddx_hash->{host}};
84 foreach my $host ( @hosts ) {
85 my $param = $host->{param};
86 if($param && $param->{'snmp-host'}
87 && $param->{'RFC2863_IF_MIB::external-serviceid'}) {
89 $param->{'RFC2863_IF_MIB::external-serviceid'}->{'content'};
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;
101 return sort keys %hash;
104 sub port_graphs_link {
105 # hardcoded for 'main' tree for now
107 my $serviceid = shift;
108 my $hash = $self->get_router_serviceids(undef,$serviceid) or return '';
109 my @keys = keys %$hash; # yeah this is weird...
111 my $iface = $hash->{$keys[0]};
113 #Torrus::ConfigTree is only available when running under the web UI
114 eval 'use Torrus::ConfigTree;';
117 my $config_tree = new Torrus::ConfigTree( -TreeName => 'main' );
118 my $token = $config_tree->token("/Routers/$host/Interface_Counters/$iface/InOut_bps");
119 return $Torrus::Freeside::FSURL."/torrus/main?token=$token";
124 my $serviceid = shift;
125 return '' unless $serviceid =~ /^[0-9A-Za-z_\-.\\\/ ]+$/;
127 my @svc_port = qsearch('svc_port', { 'serviceid' => $serviceid });
128 return '' unless scalar(@svc_port);
130 # for now it's like this, later on just change to qsearchs
135 sub find_torrus_srvderive_component {
137 my $serviceid = shift;
138 return '' unless $serviceid =~ /^[0-9A-Za-z_\-.\\\/ ]+$/;
140 qsearchs('torrus_srvderive_component', { 'serviceid' => $serviceid });
146 my @ls = localtime(time);
147 my ($d,$m,$y) = ($ls[3], $ls[4]+1, $ls[5]+1900);
150 if ($m == 0) { $m=12; $y-- }
151 #i should have better error checking
152 system('torrus', 'report', '--report=MonthlyUsage', "--date=$y-$m-01");
153 system('torrus', 'report', '--genhtml', '--all2tree=main');
158 sub queued_add_router {
160 my $error = $self->add_router(@_);
161 die $error if $error;
165 my($self, $ip, $community) = @_;
167 $community = (defined($community) && length($community) > 1)
168 ? qq!<param name="snmp-community" value="$community"/>\n !
173 qq( <param name="snmp-host" value="$ip"/>\n).$community.
176 my $ddx = $self->_torrus_loadddx;
178 $ddx =~ s{(</snmp-discovery>)}{$newhost$1};
180 $self->_torrus_newddx($ddx);
185 my($self, $router_ip, $interface, $serviceid ) = @_;
187 #false laziness w/torrus/perllib/Torrus/Renderer.pm iface_underscore, update both
188 $interface =~ s(\/)(_)g; #slashes become underscores
189 $interface =~ s(\.)(_)g; #periods too, huh
190 $interface =~ s(\-)(_)g; #yup, and dashes
192 #should just use a proper XML parser huh
194 my @ddx = split(/\n/, $self->_torrus_loadddx);
196 die "Torrus Service ID $serviceid in use\n"
197 if grep /^\s*$serviceid:/, @ddx;
199 my $newline = " $serviceid:$interface:Both:main,";
205 while ( my $line = shift(@ddx) ) {
207 next unless $line =~ /^\s*<param\s+name="snmp-host"\s+value="$router_ip"\/?>/i;
209 while ( my $hostline = shift(@ddx) ) {
210 $new .= "$hostline\n" unless $hostline =~ /^\s+<\/host>\s*/i;
211 if ( $hostline =~ /^\s*<param name="RFC2863_IF_MIB::external-serviceid"\/?>/i ) {
213 while ( my $paramline = shift(@ddx) ) {
214 if ( $paramline =~ /^\s*<\/param>/ ) {
215 $new .= "$newline\n$paramline\n";
218 $new .= "$paramline\n";
224 } elsif ( $hostline =~ /^\s+<\/host>\s*/i ) {
227 qq( <param name="RFC2863_IF_MIB::external-serviceid">\n).
231 $new .= "$hostline\n";
239 $self->_torrus_newddx($new);
244 $lock = new IO::File ">>$lockfile" or die $!;
245 flock($lock, LOCK_EX);
249 flock($lock, LOCK_UN);
253 sub _torrus_loadddx {
256 return slurp($ddxfile);
260 my($self, $ddx) = @_;
262 my $new = new IO::File ">$ddxfile.new"
263 or die "can't write to $ddxfile.new: $!";
267 my $tmpname = $ddxfile . Date::Format::time2str('%Y%m%d%H%M%S',time);
268 rename($ddxfile, $tmpname) or die $!;
269 rename("$ddxfile.new", $ddxfile) or die $!;
271 my $error = $self->_torrus_reload;
272 if ( $error ) { #revert routers.ddx
273 rename($ddxfile, "$tmpname.FAILED") or die $!;
274 rename($tmpname, $ddxfile) or die $!;
277 $self->_torrus_unlock;
286 run( ['torrus', 'devdiscover', "--in=$ddxfile"], '2>'=>\$stderr );
287 return $stderr if $stderr;
289 run( ['torrus', 'compile', '--tree=main'] ); # , '--verbose'
290 #typically the errors happen at the discover stage...
296 #sub torrus_serviceids {
299 # #is this going to get too slow or will the index make it okay?
300 # my $sth = dbh->prepare("SELECT DISTINCT(serviceid) FROM srvexport")
301 # or die dbh->errstr;
302 # $sth->execute or die $sth->errstr;
303 # my %serviceid = ();
304 # while ( my $row = $sth->fetchrow_arrayref ) {
305 # my $serviceid = $row->[0];
306 # $serviceid =~ s/_(IN|OUT)$//;
307 # $serviceid{$serviceid}=1;
309 # my @serviceids = sort keys %serviceid;
315 sub torrus_serviceids {
317 my @serviceids = $self->all_router_serviceids;
318 push @serviceids, map $_->serviceid, qsearch('torrus_srvderive', {});
319 return sort @serviceids;