1 package FS::NetworkMonitoringSystem::Torrus_Internal;
4 #use vars qw( $DEBUG $me );
7 use File::Slurp qw(slurp);
11 use FS::Record qw(qsearch);
14 #$me = '[FS::NetworkMonitoringSystem::Torrus_Internal]';
17 our $lockfile = '/usr/local/etc/torrus/discovery/FSLOCK';
18 our $ddxfile = '/usr/local/etc/torrus/discovery/routers.ddx';
27 sub get_router_serviceids {
31 my $ddx_xml = slurp($ddxfile);
32 my $xs = new XML::Simple(RootName=> undef, SuppressEmpty => '',
34 my $ddx_hash = $xs->XMLin($ddx_xml);
35 if($ddx_hash->{host}){
36 my @hosts = @{$ddx_hash->{host}};
37 foreach my $host ( @hosts ) {
38 my $param = $host->{param};
39 if($param && $param->{'snmp-host'}
40 && $param->{'snmp-host'}->{'value'} eq $router
41 && $param->{'RFC2863_IF_MIB::external-serviceid'}) {
42 my $serviceids = $param->{'RFC2863_IF_MIB::external-serviceid'}->{'content'};
45 my @serviceids = split(',',$serviceids);
46 foreach my $serviceid ( @serviceids ) {
47 $serviceid =~ s/^\s+|\s+$//g;
48 my @s = split(':',$serviceid);
49 next unless scalar(@s) == 4;
62 my $serviceid = shift;
63 return '' unless $serviceid =~ /^[0-9A-Za-z_\-.\\\/ ]+$/;
65 my @svc_port = qsearch('svc_port', { 'serviceid' => $serviceid });
66 return '' unless scalar(@svc_port);
68 # for now it's like this, later on just change to qsearchs
78 qq( <param name="snmp-host" value="$ip"/>\n).
81 my $ddx = $self->_torrus_loadddx;
83 $ddx =~ s{(</snmp-discovery>)}{$newhost$1};
85 $self->_torrus_newddx($ddx);
90 my($self, $router_ip, $interface, $serviceid ) = @_;
92 $interface =~ s(\/)(_)g;
94 #should just use a proper XML parser huh
96 my $newline = " $serviceid:$interface:Both:main,";
98 my @ddx = split(/\n/, $self->_torrus_loadddx);
103 while ( my $line = shift(@ddx) ) {
105 next unless $line =~ /^\s*<param\s+name="snmp-host"\s+value="$router_ip"\/?>/i;
107 while ( my $hostline = shift(@ddx) ) {
108 $new .= "$hostline\n" unless $hostline =~ /^\s+<\/host>\s*/i;
109 if ( $hostline =~ /^\s*<param name="RFC2863_IF_MIB::external-serviceid"\/?>/i ) {
111 while ( my $paramline = shift(@ddx) ) {
112 if ( $paramline =~ /^\s*<\/param>/ ) {
113 $new .= "$newline\n$paramline";
122 } elsif ( $hostline =~ /^\s+<\/host>\s*/i ) {
125 qq( <param name="RFC2863_IF_MIB::external-serviceid">\n).
137 $self->_torrus_newddx($new);
142 $lock = new IO::File ">>$lockfile" or die $!;
143 flock($lock, LOCK_EX);
147 flock($lock, LOCK_UN);
151 sub _torrus_loadddx {
154 return slurp($ddxfile);
158 my($self, $ddx) = @_;
160 my $new = new IO::File ">$ddxfile.new"
161 or die "can't write to $ddxfile.new: $!";
165 # `date ...` created file names with weird chars in them
166 my $tmpname = $ddxfile . Date::Format::time2str('%Y%m%d%H%M%S',time);
167 rename("$ddxfile", $tmpname) or die $!;
168 rename("$ddxfile.new", $ddxfile) or die $!;
170 $self->_torrus_reload;
176 #i should have better error checking
178 system('torrus', 'devdiscover', "--in=$ddxfile");
180 system('torrus', 'compile', '--tree=main'); # , '--verbose'
182 $self->_torrus_unlock;