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