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 && (!$router || $param->{'snmp-host'}->{'value'} eq $router)
87 && $param->{'RFC2863_IF_MIB::external-serviceid'}) {
89 $param->{'RFC2863_IF_MIB::external-serviceid'}->{'content'};
91 my @serviceids = split(',',$serviceids);
92 foreach my $serviceid ( @serviceids ) {
93 $serviceid =~ s/^\s+|\s+$//g;
94 my @s = split(':',$serviceid);
95 next unless scalar(@s) == 4;
101 return sort keys %hash;
104 sub port_graphs_link {
105 # hardcoded for 'main' tree for now
107 my $serviceid = shift;
108 my $hash = $self->get_router_serviceids(undef,$serviceid) or return '';
109 my @keys = keys %$hash; # yeah this is weird...
111 my $iface = $hash->{$keys[0]};
112 my $config_tree = new Torrus::ConfigTree( -TreeName => 'main' );
113 my $token = $config_tree->token("/Routers/$host/Interface_Counters/$iface/InOut_bps");
114 return $Torrus::Freeside::FSURL."/torrus/main?token=$token";
119 my $serviceid = shift;
120 return '' unless $serviceid =~ /^[0-9A-Za-z_\-.\\\/ ]+$/;
122 my @svc_port = qsearch('svc_port', { 'serviceid' => $serviceid });
123 return '' unless scalar(@svc_port);
125 # for now it's like this, later on just change to qsearchs
130 sub find_torrus_srvderive_component {
132 my $serviceid = shift;
133 return '' unless $serviceid =~ /^[0-9A-Za-z_\-.\\\/ ]+$/;
135 qsearchs('torrus_srvderive_component', { 'serviceid' => $serviceid });
141 my @ls = localtime(time);
142 my ($d,$m,$y) = ($ls[3], $ls[4]+1, $ls[5]+1900);
145 if ($m == 0) { $m=12; $y-- }
146 #i should have better error checking
147 system('torrus', 'report', '--report=MonthlyUsage', "--date=$y-$m-01");
148 system('torrus', 'report', '--genhtml', '--tree=main');
158 qq( <param name="snmp-host" value="$ip"/>\n).
161 my $ddx = $self->_torrus_loadddx;
163 $ddx =~ s{(</snmp-discovery>)}{$newhost$1};
165 $self->_torrus_newddx($ddx);
170 my($self, $router_ip, $interface, $serviceid ) = @_;
172 $interface =~ s(\/)(_)g; #slashes become underscores
173 $interface =~ s(\.)(_)g; #periods too, huh
175 #should just use a proper XML parser huh
177 my @ddx = split(/\n/, $self->_torrus_loadddx);
179 die "Torrus Service ID $serviceid in use\n"
180 if grep /^\s*$serviceid:/, @ddx;
182 my $newline = " $serviceid:$interface:Both:main,";
188 while ( my $line = shift(@ddx) ) {
190 next unless $line =~ /^\s*<param\s+name="snmp-host"\s+value="$router_ip"\/?>/i;
192 while ( my $hostline = shift(@ddx) ) {
193 $new .= "$hostline\n" unless $hostline =~ /^\s+<\/host>\s*/i;
194 if ( $hostline =~ /^\s*<param name="RFC2863_IF_MIB::external-serviceid"\/?>/i ) {
196 while ( my $paramline = shift(@ddx) ) {
197 if ( $paramline =~ /^\s*<\/param>/ ) {
198 $new .= "$newline\n$paramline\n";
201 $new .= "$paramline\n";
207 } elsif ( $hostline =~ /^\s+<\/host>\s*/i ) {
210 qq( <param name="RFC2863_IF_MIB::external-serviceid">\n).
214 $new .= "$hostline\n";
222 $self->_torrus_newddx($new);
227 $lock = new IO::File ">>$lockfile" or die $!;
228 flock($lock, LOCK_EX);
232 flock($lock, LOCK_UN);
236 sub _torrus_loadddx {
239 return slurp($ddxfile);
243 my($self, $ddx) = @_;
245 my $new = new IO::File ">$ddxfile.new"
246 or die "can't write to $ddxfile.new: $!";
250 my $tmpname = $ddxfile . Date::Format::time2str('%Y%m%d%H%M%S',time);
251 rename("$ddxfile", $tmpname) or die $!;
252 rename("$ddxfile.new", $ddxfile) or die $!;
254 $self->_torrus_reload;
260 #i should use IPC::Run and have better error checking (commands are silent
261 # for success, or output errors)
263 system('torrus', 'devdiscover', "--in=$ddxfile");
265 system('torrus', 'compile', '--tree=main'); # , '--verbose'
267 $self->_torrus_unlock;
271 #sub torrus_serviceids {
274 # #is this going to get too slow or will the index make it okay?
275 # my $sth = dbh->prepare("SELECT DISTINCT(serviceid) FROM srvexport")
276 # or die dbh->errstr;
277 # $sth->execute or die $sth->errstr;
278 # my %serviceid = ();
279 # while ( my $row = $sth->fetchrow_arrayref ) {
280 # my $serviceid = $row->[0];
281 # $serviceid =~ s/_(IN|OUT)$//;
282 # $serviceid{$serviceid}=1;
284 # my @serviceids = sort keys %serviceid;
290 sub torrus_serviceids {
292 my @serviceids = $self->all_router_serviceids;
293 push @serviceids, map $_->serviceid, qsearch('torrus_srvderive', {});
294 return sort @serviceids;