fix ports with dashes (-) not showing their monitoring status, RT#14926
[freeside.git] / FS / FS / NetworkMonitoringSystem / Torrus_Internal.pm
index f2194f8..04a6b29 100644 (file)
@@ -9,8 +9,9 @@ use Date::Format;
 use XML::Simple;
 use FS::Record qw(qsearch qsearchs dbh);
 use FS::svc_port;
+use FS::torrus_srvderive;
 use FS::torrus_srvderive_component;
-use Torrus::ConfigTree;
+#use Torrus::ConfigTree;
 
 #$DEBUG = 0;
 #$me = '[FS::NetworkMonitoringSystem::Torrus_Internal]';
@@ -30,43 +31,73 @@ sub ddx2hash {
     my $self = shift;
     my $ddx_xml = slurp($ddxfile);
     my $xs = new XML::Simple(RootName=> undef, SuppressEmpty => '', 
-                               ForceArray => 1, );
+                                ForceArray => 1, );
     return $xs->XMLin($ddx_xml);
 }
 
 sub get_router_serviceids {
-    my $self = shift;
-    my $router = shift;
-    my $find_serviceid = shift;
-    my $found_serviceid = 0;
-    my $ddx_hash = $self->ddx2hash;
-    if($ddx_hash->{host}){
-       my @hosts = @{$ddx_hash->{host}};
-       foreach my $host ( @hosts ) {
-           my $param = $host->{param};
-           if($param && $param->{'snmp-host'} 
-                     && (!$router || $param->{'snmp-host'}->{'value'} eq $router)
-                     && $param->{'RFC2863_IF_MIB::external-serviceid'}) {
-               my $serviceids = $param->{'RFC2863_IF_MIB::external-serviceid'}->{'content'};
-               my %hash = ();
-               if($serviceids) {
-                   my @serviceids = split(',',$serviceids);
-                   foreach my $serviceid ( @serviceids ) {
-                       $serviceid =~ s/^\s+|\s+$//g;
-                       my @s = split(':',$serviceid);
-                       next unless scalar(@s) == 4;
-                       $hash{$s[1]} = $s[0] if $router;
-                       if ($find_serviceid && $find_serviceid eq $s[0]) {
-                           $hash{$param->{'snmp-host'}->{'value'}} = $s[1];
-                           $found_serviceid = 1;
-                       }
-                   }
-               }
-               return \%hash if ($router || $found_serviceid);
-           }
-       }
+  my $self = shift;
+  my $router = shift;
+  my $find_serviceid = shift;
+  my $found_serviceid = 0;
+  my $ddx_hash = $self->ddx2hash;
+  return '' unless $ddx_hash->{'host'};
+
+  my @hosts = @{$ddx_hash->{host}};
+  foreach my $host ( @hosts ) {
+    my $param = $host->{param};
+    if($param && $param->{'snmp-host'} 
+              && (!$router || $param->{'snmp-host'}->{'value'} eq $router)
+              && $param->{'RFC2863_IF_MIB::external-serviceid'}) {
+      my $serviceids =
+        $param->{'RFC2863_IF_MIB::external-serviceid'}->{'content'};
+      my %hash = ();
+      if ($serviceids) {
+        my @serviceids = split(',',$serviceids);
+        foreach my $serviceid ( @serviceids ) {
+          $serviceid =~ s/^\s+|\s+$//g;
+          my @s = split(':',$serviceid);
+          next unless scalar(@s) == 4;
+          $hash{$s[1]} = $s[0] if $router;
+          if ($find_serviceid && $find_serviceid eq $s[0]) {
+            $hash{$param->{'snmp-host'}->{'value'}} = $s[1];
+            $found_serviceid = 1;
+          }
+        }
+      }
+      return \%hash if ($router || $found_serviceid);
     }
-    '';
+  }
+  '';
+}
+
+#false laziness and probably should be merged w/above, but didn't want to mess
+# that up
+sub all_router_serviceids {
+  my $self = shift;
+  my $ddx_hash = $self->ddx2hash;
+  return () unless $ddx_hash->{'host'};
+
+  my %hash = ();
+  my @hosts = @{$ddx_hash->{host}};
+  foreach my $host ( @hosts ) {
+    my $param = $host->{param};
+    if($param && $param->{'snmp-host'} 
+              && $param->{'RFC2863_IF_MIB::external-serviceid'}) {
+      my $serviceids =
+        $param->{'RFC2863_IF_MIB::external-serviceid'}->{'content'};
+      if ($serviceids) {
+        my @serviceids = split(',',$serviceids);
+        foreach my $serviceid ( @serviceids ) {
+          $serviceid =~ s/^\s+|\s+$//g;
+          my @s = split(':',$serviceid);
+          next unless scalar(@s) == 4;
+          $hash{$s[0]}=1;
+        }
+      }
+    }
+  }
+  return sort keys %hash;
 }
 
 sub port_graphs_link {
@@ -77,6 +108,11 @@ sub port_graphs_link {
     my @keys = keys %$hash; # yeah this is weird...
     my $host = $keys[0];
     my $iface = $hash->{$keys[0]};
+
+    #Torrus::ConfigTree is only available when running under the web UI
+    eval 'use Torrus::ConfigTree;';
+    die $@ if $@;
+
     my $config_tree = new Torrus::ConfigTree( -TreeName => 'main' );
     my $token = $config_tree->token("/Routers/$host/Interface_Counters/$iface/InOut_bps");
     return $Torrus::Freeside::FSURL."/torrus/main?token=$token";
@@ -113,17 +149,20 @@ sub report {
     if ($m == 0) { $m=12; $y-- }
     #i should have better error checking
     system('torrus', 'report', '--report=MonthlyUsage', "--date=$y-$m-01");
-    system('torrus', 'report', '--genhtml', '--tree=main');
+    system('torrus', 'report', '--genhtml', '--all2tree=main');
   }
 
 }
 
 sub add_router {
-  my($self, $ip) = @_;
+  my($self, $ip, $community) = @_;
+
+  $community = qq!<param name="snmp-community" value="$community"/>\n !
+    if length($community) > 1;
 
   my $newhost = 
     qq(  <host>\n).
-    qq(    <param name="snmp-host" value="$ip"/>\n).
+    qq(    <param name="snmp-host" value="$ip"/>\n).$community.
     qq(  </host>\n);
 
   my $ddx = $self->_torrus_loadddx;
@@ -137,8 +176,10 @@ sub add_router {
 sub add_interface {
   my($self, $router_ip, $interface, $serviceid ) = @_;
 
+  #false laziness w/torrus/perllib/Torrus/Renderer.pm iface_underscore, update both
   $interface =~ s(\/)(_)g; #slashes become underscores
   $interface =~ s(\.)(_)g; #periods too, huh
+  $interface =~ s(\-)(_)g; #yup, and dashes
 
   #should just use a proper XML parser huh
 
@@ -176,7 +217,7 @@ sub add_interface {
         unless ( $added ) {
           $new .= 
             qq(   <param name="RFC2863_IF_MIB::external-serviceid">\n).
-            qq(     $newline\n").
+            qq(     $newline\n).
             qq(   </param>\n);
         }
         $new .= "$hostline\n";
@@ -236,23 +277,30 @@ sub _torrus_reload {
 
 }
 
+#sub torrus_serviceids {
+#  my $self = shift;
+#
+#  #is this going to get too slow or will the index make it okay?
+#  my $sth = dbh->prepare("SELECT DISTINCT(serviceid) FROM srvexport")
+#    or die dbh->errstr;
+#  $sth->execute or die $sth->errstr;
+#  my %serviceid = ();
+#  while ( my $row = $sth->fetchrow_arrayref ) {
+#    my $serviceid = $row->[0];
+#    $serviceid =~ s/_(IN|OUT)$//;
+#    $serviceid{$serviceid}=1;
+#  }
+#  my @serviceids = sort keys %serviceid;
+#
+#  @serviceids;
+#
+#}
+
 sub torrus_serviceids {
   my $self = shift;
-
-  #is this going to get too slow or will the index make it okay?
-  my $sth = dbh->prepare("SELECT DISTINCT(serviceid) FROM srvexport")
-    or die dbh->errstr;
-  $sth->execute or die $sth->errstr;
-  my %serviceid = ();
-  while ( my $row = $sth->fetchrow_arrayref ) {
-    my $serviceid = $row->[0];
-    $serviceid =~ s/_(IN|OUT)$//;
-    $serviceid{$serviceid}=1;
-  }
-  my @serviceids = sort keys %serviceid;
-
-  @serviceids;
-
+  my @serviceids = $self->all_router_serviceids;
+  push @serviceids, map $_->serviceid, qsearch('torrus_srvderive', {});
+  return sort @serviceids;
 }
 
 1;