1 package FS::NetworkMonitoringSystem::Torrus_Internal;
4 #use vars qw( $DEBUG $me );
7 use File::Slurp qw(slurp);
10 use FS::Record qw(qsearch qsearchs dbh);
12 use FS::torrus_srvderive;
13 use FS::torrus_srvderive_component;
14 #use Torrus::ConfigTree;
17 #$me = '[FS::NetworkMonitoringSystem::Torrus_Internal]';
20 our $lockfile = '/usr/local/etc/torrus/discovery/FSLOCK';
21 our $ddxfile = '/usr/local/etc/torrus/discovery/routers.ddx';
32 my $ddx_xml = slurp($ddxfile);
33 my $xs = new XML::Simple(RootName=> undef, SuppressEmpty => '',
35 return $xs->XMLin($ddx_xml);
38 sub get_router_serviceids {
41 my $find_serviceid = shift;
42 my $found_serviceid = 0;
43 my $ddx_hash = $self->ddx2hash;
44 return '' unless $ddx_hash->{'host'};
46 my @hosts = @{$ddx_hash->{host}};
47 foreach my $host ( @hosts ) {
48 my $param = $host->{param};
49 if($param && $param->{'snmp-host'}
50 && (!$router || $param->{'snmp-host'}->{'value'} eq $router)
51 && $param->{'RFC2863_IF_MIB::external-serviceid'}) {
53 $param->{'RFC2863_IF_MIB::external-serviceid'}->{'content'};
56 my @serviceids = split(',',$serviceids);
57 foreach my $serviceid ( @serviceids ) {
58 $serviceid =~ s/^\s+|\s+$//g;
59 my @s = split(':',$serviceid);
60 next unless scalar(@s) == 4;
61 $hash{$s[1]} = $s[0] if $router;
62 if ($find_serviceid && $find_serviceid eq $s[0]) {
63 $hash{$param->{'snmp-host'}->{'value'}} = $s[1];
68 return \%hash if ($router || $found_serviceid);
74 #false laziness and probably should be merged w/above, but didn't want to mess
76 sub all_router_serviceids {
78 my $ddx_hash = $self->ddx2hash;
79 return () unless $ddx_hash->{'host'};
82 my @hosts = @{$ddx_hash->{host}};
83 foreach my $host ( @hosts ) {
84 my $param = $host->{param};
85 if($param && $param->{'snmp-host'}
86 && $param->{'RFC2863_IF_MIB::external-serviceid'}) {
88 $param->{'RFC2863_IF_MIB::external-serviceid'}->{'content'};
90 my @serviceids = split(',',$serviceids);
91 foreach my $serviceid ( @serviceids ) {
92 $serviceid =~ s/^\s+|\s+$//g;
93 my @s = split(':',$serviceid);
94 next unless scalar(@s) == 4;
100 return sort keys %hash;
103 sub port_graphs_link {
104 # hardcoded for 'main' tree for now
106 my $serviceid = shift;
107 my $hash = $self->get_router_serviceids(undef,$serviceid) or return '';
108 my @keys = keys %$hash; # yeah this is weird...
110 my $iface = $hash->{$keys[0]};
112 #Torrus::ConfigTree is only available when running under the web UI
113 eval 'use Torrus::ConfigTree;';
116 my $config_tree = new Torrus::ConfigTree( -TreeName => 'main' );
117 my $token = $config_tree->token("/Routers/$host/Interface_Counters/$iface/InOut_bps");
118 return $Torrus::Freeside::FSURL."/torrus/main?token=$token";
123 my $serviceid = shift;
124 return '' unless $serviceid =~ /^[0-9A-Za-z_\-.\\\/ ]+$/;
126 my @svc_port = qsearch('svc_port', { 'serviceid' => $serviceid });
127 return '' unless scalar(@svc_port);
129 # for now it's like this, later on just change to qsearchs
134 sub find_torrus_srvderive_component {
136 my $serviceid = shift;
137 return '' unless $serviceid =~ /^[0-9A-Za-z_\-.\\\/ ]+$/;
139 qsearchs('torrus_srvderive_component', { 'serviceid' => $serviceid });
145 my @ls = localtime(time);
146 my ($d,$m,$y) = ($ls[3], $ls[4]+1, $ls[5]+1900);
149 if ($m == 0) { $m=12; $y-- }
150 #i should have better error checking
151 system('torrus', 'report', '--report=MonthlyUsage', "--date=$y-$m-01");
152 system('torrus', 'report', '--genhtml', '--all2tree=main');
158 my($self, $ip, $community) = @_;
160 $community = qq!<param name="snmp-community" value="$community"/>\n !
161 if length($community) > 1;
165 qq( <param name="snmp-host" value="$ip"/>\n).$community.
168 my $ddx = $self->_torrus_loadddx;
170 $ddx =~ s{(</snmp-discovery>)}{$newhost$1};
172 $self->_torrus_newddx($ddx);
177 my($self, $router_ip, $interface, $serviceid ) = @_;
179 $interface =~ s(\/)(_)g; #slashes become underscores
180 $interface =~ s(\.)(_)g; #periods too, huh
182 #should just use a proper XML parser huh
184 my @ddx = split(/\n/, $self->_torrus_loadddx);
186 die "Torrus Service ID $serviceid in use\n"
187 if grep /^\s*$serviceid:/, @ddx;
189 my $newline = " $serviceid:$interface:Both:main,";
195 while ( my $line = shift(@ddx) ) {
197 next unless $line =~ /^\s*<param\s+name="snmp-host"\s+value="$router_ip"\/?>/i;
199 while ( my $hostline = shift(@ddx) ) {
200 $new .= "$hostline\n" unless $hostline =~ /^\s+<\/host>\s*/i;
201 if ( $hostline =~ /^\s*<param name="RFC2863_IF_MIB::external-serviceid"\/?>/i ) {
203 while ( my $paramline = shift(@ddx) ) {
204 if ( $paramline =~ /^\s*<\/param>/ ) {
205 $new .= "$newline\n$paramline\n";
208 $new .= "$paramline\n";
214 } elsif ( $hostline =~ /^\s+<\/host>\s*/i ) {
217 qq( <param name="RFC2863_IF_MIB::external-serviceid">\n).
221 $new .= "$hostline\n";
229 $self->_torrus_newddx($new);
234 $lock = new IO::File ">>$lockfile" or die $!;
235 flock($lock, LOCK_EX);
239 flock($lock, LOCK_UN);
243 sub _torrus_loadddx {
246 return slurp($ddxfile);
250 my($self, $ddx) = @_;
252 my $new = new IO::File ">$ddxfile.new"
253 or die "can't write to $ddxfile.new: $!";
257 my $tmpname = $ddxfile . Date::Format::time2str('%Y%m%d%H%M%S',time);
258 rename("$ddxfile", $tmpname) or die $!;
259 rename("$ddxfile.new", $ddxfile) or die $!;
261 $self->_torrus_reload;
267 #i should use IPC::Run and have better error checking (commands are silent
268 # for success, or output errors)
270 system('torrus', 'devdiscover', "--in=$ddxfile");
272 system('torrus', 'compile', '--tree=main'); # , '--verbose'
274 $self->_torrus_unlock;
278 #sub torrus_serviceids {
281 # #is this going to get too slow or will the index make it okay?
282 # my $sth = dbh->prepare("SELECT DISTINCT(serviceid) FROM srvexport")
283 # or die dbh->errstr;
284 # $sth->execute or die $sth->errstr;
285 # my %serviceid = ();
286 # while ( my $row = $sth->fetchrow_arrayref ) {
287 # my $serviceid = $row->[0];
288 # $serviceid =~ s/_(IN|OUT)$//;
289 # $serviceid{$serviceid}=1;
291 # my @serviceids = sort keys %serviceid;
297 sub torrus_serviceids {
299 my @serviceids = $self->all_router_serviceids;
300 push @serviceids, map $_->serviceid, qsearch('torrus_srvderive', {});
301 return sort @serviceids;