torrus virtual ports, RT#10574
[freeside.git] / FS / FS / NetworkMonitoringSystem / Torrus_Internal.pm
index d55bfc3..b55d0aa 100644 (file)
@@ -8,7 +8,7 @@ use File::Slurp qw(slurp);
 use Date::Format;
 use XML::Simple;
 use FS::svc_port;
-use FS::Record qw(qsearch);
+use FS::Record qw(qsearch dbh);
 use Torrus::ConfigTree;
 
 #$DEBUG = 0;
@@ -72,7 +72,7 @@ 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 $hash = $self->get_router_serviceids(undef,$serviceid) or return '';
     my @keys = keys %$hash; # yeah this is weird...
     my $host = $keys[0];
     my $iface = $hash->{$keys[0]};
@@ -94,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) = @_;
 
@@ -117,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;
@@ -134,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";
           }
         }
 
@@ -150,7 +169,7 @@ sub add_interface {
             qq(     $newline\n").
             qq(   </param>\n);
         }
-        $new .= $hostline;
+        $new .= "$hostline\n";
         last; #hostline
       }
  
@@ -186,7 +205,6 @@ sub _torrus_newddx {
   print $new $ddx;
   close $new;
 
-  # `date ...` created file names with weird chars in them
   my $tmpname = $ddxfile . Date::Format::time2str('%Y%m%d%H%M%S',time);
   rename("$ddxfile", $tmpname) or die $!;
   rename("$ddxfile.new", $ddxfile) or die $!;
@@ -197,7 +215,8 @@ sub _torrus_newddx {
 sub _torrus_reload {
   my($self) = @_;
 
-  #i should have better error checking
+  #i should use IPC::Run and have better error checking (commands are silent
+  # for success, or output errors)
 
   system('torrus', 'devdiscover', "--in=$ddxfile");
 
@@ -207,4 +226,23 @@ 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;
+
+}
+
 1;