fix newlines in interface addition, throw some sort of error for duplicate addition...
[freeside.git] / FS / FS / NetworkMonitoringSystem / Torrus_Internal.pm
index 736d178..4c3264e 100644 (file)
@@ -9,6 +9,7 @@ use Date::Format;
 use XML::Simple;
 use FS::svc_port;
 use FS::Record qw(qsearch);
+use Torrus::ConfigTree;
 
 #$DEBUG = 0;
 #$me = '[FS::NetworkMonitoringSystem::Torrus_Internal]';
@@ -24,20 +25,26 @@ sub new {
     return $self;
 }
 
-sub get_router_serviceids {
+sub ddx2hash {
     my $self = shift;
-    my $router = shift;
-
     my $ddx_xml = slurp($ddxfile);
     my $xs = new XML::Simple(RootName=> undef, SuppressEmpty => '', 
                                ForceArray => 1, );
-    my $ddx_hash = $xs->XMLin($ddx_xml);
+    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'} 
-                     && $param->{'snmp-host'}->{'value'} eq $router
+                     && (!$router || $param->{'snmp-host'}->{'value'} eq $router)
                      && $param->{'RFC2863_IF_MIB::external-serviceid'}) {
                my $serviceids = $param->{'RFC2863_IF_MIB::external-serviceid'}->{'content'};
                my %hash = ();
@@ -47,16 +54,33 @@ sub get_router_serviceids {
                        $serviceid =~ s/^\s+|\s+$//g;
                        my @s = split(':',$serviceid);
                        next unless scalar(@s) == 4;
-                       $hash{$s[1]} = $s[0];
+                       $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;
+               return \%hash if ($router || $found_serviceid);
            }
        }
     }
     '';
 }
 
+sub port_graphs_link {
+    # hardcoded for 'main' tree for now 
+    my $self = shift;
+    my $serviceid = shift;
+    my $hash = $self->get_router_serviceids(undef,$serviceid);
+    my @keys = keys %$hash; # yeah this is weird...
+    my $host = $keys[0];
+    my $iface = $hash->{$keys[0]};
+    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";
+}
+
 sub find_svc {
     my $self = shift;
     my $serviceid = shift;
@@ -70,6 +94,21 @@ sub find_svc {
     return $svc_port[0];
 }
 
+sub report {
+  my $self = shift;
+
+  my @ls = localtime(time);
+  my ($d,$m,$y) = ($ls[3], $ls[4]+1, $ls[5]+1900);
+  if ( $ls[3] == 1 ) {
+    $m--;
+    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');
+  }
+
+}
+
 sub add_router {
   my($self, $ip) = @_;
 
@@ -93,9 +132,13 @@ sub add_interface {
 
   #should just use a proper XML parser huh
 
+  my @ddx = split(/\n/, $self->_torrus_loadddx);
+
+  die "Torrus Service ID $serviceid in use\n"
+    if grep /^\s*$serviceid:/, @ddx;
+
   my $newline = "     $serviceid:$interface:Both:main,";
 
-  my @ddx = split(/\n/, $self->_torrus_loadddx);
   my $new = '';
 
   my $added = 0;
@@ -110,10 +153,10 @@ sub add_interface {
 
         while ( my $paramline = shift(@ddx) ) {
           if ( $paramline =~ /^\s*<\/param>/ ) {
-            $new .= "$newline\n$paramline";
+            $new .= "$newline\n$paramline\n";
             last; #paramline
           } else {
-            $new .= $paramline;
+            $new .= "$paramline\n";
           }
         }
 
@@ -126,7 +169,7 @@ sub add_interface {
             qq(     $newline\n").
             qq(   </param>\n);
         }
-        $new .= $hostline;
+        $new .= "$hostline\n";
         last; #hostline
       }
  
@@ -173,7 +216,7 @@ sub _torrus_newddx {
 sub _torrus_reload {
   my($self) = @_;
 
-  #i should have better error checking
+  #i should use IPC::Run and have better error checking
 
   system('torrus', 'devdiscover', "--in=$ddxfile");