torrus, UI, RT10574
[freeside.git] / FS / FS / NetworkMonitoringSystem / Torrus_Internal.pm
index 68356e7..736d178 100644 (file)
@@ -5,6 +5,10 @@ use strict;
 use Fcntl qw(:flock);
 use IO::File;
 use File::Slurp qw(slurp);
+use Date::Format;
+use XML::Simple;
+use FS::svc_port;
+use FS::Record qw(qsearch);
 
 #$DEBUG = 0;
 #$me = '[FS::NetworkMonitoringSystem::Torrus_Internal]';
@@ -13,6 +17,59 @@ our $lock;
 our $lockfile = '/usr/local/etc/torrus/discovery/FSLOCK';
 our $ddxfile  = '/usr/local/etc/torrus/discovery/routers.ddx';
 
+sub new {
+    my $class = shift;
+    my $self = {};
+    bless $self, $class;
+    return $self;
+}
+
+sub get_router_serviceids {
+    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);
+    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
+                     && $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];
+                   }
+               }
+               return \%hash;
+           }
+       }
+    }
+    '';
+}
+
+sub find_svc {
+    my $self = shift;
+    my $serviceid = shift;
+    return '' unless $serviceid =~ /^[0-9A-Za-z_\-.\\\/ ]+$/;
+  
+    my @svc_port = qsearch('svc_port', { 'serviceid' => $serviceid });
+    return '' unless scalar(@svc_port);
+
+    # for now it's like this, later on just change to qsearchs
+
+    return $svc_port[0];
+}
+
 sub add_router {
   my($self, $ip) = @_;
 
@@ -27,6 +84,8 @@ sub add_router {
 
   $self->_torrus_newddx($ddx);
 
+}
+
 sub add_interface {
   my($self, $router_ip, $interface, $serviceid ) = @_;
 
@@ -46,11 +105,11 @@ sub add_interface {
     next unless $line =~ /^\s*<param\s+name="snmp-host"\s+value="$router_ip"\/?>/i;
 
     while ( my $hostline = shift(@ddx) ) {
-      $new .= "$hostline\n";
+      $new .= "$hostline\n" unless $hostline =~ /^\s+<\/host>\s*/i;
       if ( $hostline =~ /^\s*<param name="RFC2863_IF_MIB::external-serviceid"\/?>/i ) {
 
         while ( my $paramline = shift(@ddx) ) {
-          if ( $paramline =~ /^\s*</param>/ ) {
+          if ( $paramline =~ /^\s*<\/param>/ ) {
             $new .= "$newline\n$paramline";
             last; #paramline
           } else {
@@ -65,7 +124,7 @@ sub add_interface {
           $new .= 
             qq(   <param name="RFC2863_IF_MIB::external-serviceid">\n).
             qq(     $newline\n").
-            qq(   </param>\n).
+            qq(   </param>\n);
         }
         $new .= $hostline;
         last; #hostline
@@ -80,7 +139,7 @@ sub add_interface {
 }
 
 sub _torrus_lock {
-  $lock = new IO:::File ">>$lockfile" or die $!;
+  $lock = new IO::File ">>$lockfile" or die $!;
   flock($lock, LOCK_EX);
 }
 
@@ -102,7 +161,10 @@ sub _torrus_newddx {
     or die "can't write to $ddxfile.new: $!";
   print $new $ddx;
   close $new;
-  rename("$ddxfile", $ddxfile.`date +%Y%m%d%H%M%S`) or die $!;
+
+  # `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 $!;
 
   $self->_torrus_reload;