1 package FS::NetworkMonitoringSystem::Torrus_Internal;
4 #use vars qw( $DEBUG $me );
7 use File::Slurp qw(slurp);
11 #$me = '[FS::NetworkMonitoringSystem::Torrus_Internal]';
14 our $lockfile = '/usr/local/etc/torrus/discovery/FSLOCK';
15 our $ddxfile = '/usr/local/etc/torrus/discovery/routers.ddx';
29 qq( <param name="snmp-host" value="$ip"/>\n).
32 my $ddx = $self->_torrus_loadddx;
34 $ddx =~ s{(</snmp-discovery>)}{$newhost$1};
36 $self->_torrus_newddx($ddx);
41 my($self, $router_ip, $interface, $serviceid ) = @_;
43 $interface =~ s(\/)(_)g;
45 #should just use a proper XML parser huh
47 my $newline = " $serviceid:$interface:Both:main,";
49 my @ddx = split(/\n/, $self->_torrus_loadddx);
54 while ( my $line = shift(@ddx) ) {
56 next unless $line =~ /^\s*<param\s+name="snmp-host"\s+value="$router_ip"\/?>/i;
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 ) {
62 while ( my $paramline = shift(@ddx) ) {
63 if ( $paramline =~ /^\s*<\/param>/ ) {
64 $new .= "$newline\n$paramline";
73 } elsif ( $hostline =~ /^\s+<\/host>\s*/i ) {
76 qq( <param name="RFC2863_IF_MIB::external-serviceid">\n).
88 $self->_torrus_newddx($new);
93 $lock = new IO::File ">>$lockfile" or die $!;
94 flock($lock, LOCK_EX);
98 flock($lock, LOCK_UN);
102 sub _torrus_loadddx {
105 return slurp($ddxfile);
109 my($self, $ddx) = @_;
111 my $new = new IO::File ">$ddxfile.new"
112 or die "can't write to $ddxfile.new: $!";
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 $!;
121 $self->_torrus_reload;
127 #i should have better error checking
129 system('torrus', 'devdiscover', "--in=$ddxfile");
131 system('torrus', 'compile', '--tree=main'); # , '--verbose'
133 $self->_torrus_unlock;