torrus, RT10574
[freeside.git] / FS / FS / NetworkMonitoringSystem / Torrus_Internal.pm
1 package FS::NetworkMonitoringSystem::Torrus_Internal;
2
3 use strict;
4 #use vars qw( $DEBUG $me );
5 use Fcntl qw(:flock);
6 use IO::File;
7 use File::Slurp qw(slurp);
8
9 #$DEBUG = 0;
10 #$me = '[FS::NetworkMonitoringSystem::Torrus_Internal]';
11
12 our $lock;
13 our $lockfile = '/usr/local/etc/torrus/discovery/FSLOCK';
14 our $ddxfile  = '/usr/local/etc/torrus/discovery/routers.ddx';
15
16 sub add_router {
17   my($self, $ip) = @_;
18
19   my $newhost = 
20     qq(  <host>\n).
21     qq(    <param name="snmp-host" value="$ip"/>\n).
22     qq(  </host>\n);
23
24   my $ddx = $self->_torrus_loadddx;
25
26   $ddx =~ s{(</snmp-discovery>)}{$newhost$1};
27
28   $self->_torrus_newddx($ddx);
29
30 }
31
32 sub add_interface {
33   my($self, $router_ip, $interface, $serviceid ) = @_;
34
35   $interface =~ s(\/)(_)g;
36
37   #should just use a proper XML parser huh
38
39   my $newline = "     $serviceid:$interface:Both:main,";
40
41   my @ddx = split(/\n/, $self->_torrus_loadddx);
42   my $new = '';
43
44   my $added = 0;
45
46   while ( my $line = shift(@ddx) ) {
47     $new .= "$line\n";
48     next unless $line =~ /^\s*<param\s+name="snmp-host"\s+value="$router_ip"\/?>/i;
49
50     while ( my $hostline = shift(@ddx) ) {
51       $new .= "$hostline\n";
52       if ( $hostline =~ /^\s*<param name="RFC2863_IF_MIB::external-serviceid"\/?>/i ) {
53
54         while ( my $paramline = shift(@ddx) ) {
55           if ( $paramline =~ /^\s*</param>/ ) {
56             $new .= "$newline\n$paramline";
57             last; #paramline
58           } else {
59             $new .= $paramline;
60           }
61         }
62
63         $added++;
64
65       } elsif ( $hostline =~ /^\s+<\/host>\s*/i ) {
66         unless ( $added ) {
67           $new .= 
68             qq(   <param name="RFC2863_IF_MIB::external-serviceid">\n).
69             qq(     $newline\n").
70             qq(   </param>\n).
71         }
72         $new .= $hostline;
73         last; #hostline
74       }
75  
76     }
77
78   }
79
80   $self->_torrus_newddx($new);
81
82 }
83
84 sub _torrus_lock {
85   $lock = new IO:::File ">>$lockfile" or die $!;
86   flock($lock, LOCK_EX);
87 }
88
89 sub _torrus_unlock {
90   flock($lock, LOCK_UN);
91   close $lock;
92 }
93
94 sub _torrus_loadddx {
95   my($self) = @_;
96   $self->_torrus_lock;
97   return slurp($ddxfile);
98 }
99
100 sub _torrus_newddx {
101   my($self, $ddx) = @_;
102
103   my $new = new IO::File ">$ddxfile.new"
104     or die "can't write to $ddxfile.new: $!";
105   print $new $ddx;
106   close $new;
107   rename("$ddxfile", $ddxfile.`date +%Y%m%d%H%M%S`) or die $!;
108   rename("$ddxfile.new", $ddxfile) or die $!;
109
110   $self->_torrus_reload;
111 }
112
113 sub _torrus_reload {
114   my($self) = @_;
115
116   #i should have better error checking
117
118   system('torrus', 'devdiscover', "--in=$ddxfile");
119
120   system('torrus', 'compile', '--tree=main'); # , '--verbose'
121
122   $self->_torrus_unlock;
123
124 }
125
126 1;