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_component;
13 use Torrus::ConfigTree;
16 #$me = '[FS::NetworkMonitoringSystem::Torrus_Internal]';
19 our $lockfile = '/usr/local/etc/torrus/discovery/FSLOCK';
20 our $ddxfile = '/usr/local/etc/torrus/discovery/routers.ddx';
31 my $ddx_xml = slurp($ddxfile);
32 my $xs = new XML::Simple(RootName=> undef, SuppressEmpty => '',
34 return $xs->XMLin($ddx_xml);
37 sub get_router_serviceids {
40 my $find_serviceid = shift;
41 my $found_serviceid = 0;
42 my $ddx_hash = $self->ddx2hash;
43 if($ddx_hash->{host}){
44 my @hosts = @{$ddx_hash->{host}};
45 foreach my $host ( @hosts ) {
46 my $param = $host->{param};
47 if($param && $param->{'snmp-host'}
48 && (!$router || $param->{'snmp-host'}->{'value'} eq $router)
49 && $param->{'RFC2863_IF_MIB::external-serviceid'}) {
50 my $serviceids = $param->{'RFC2863_IF_MIB::external-serviceid'}->{'content'};
53 my @serviceids = split(',',$serviceids);
54 foreach my $serviceid ( @serviceids ) {
55 $serviceid =~ s/^\s+|\s+$//g;
56 my @s = split(':',$serviceid);
57 next unless scalar(@s) == 4;
58 $hash{$s[1]} = $s[0] if $router;
59 if ($find_serviceid && $find_serviceid eq $s[0]) {
60 $hash{$param->{'snmp-host'}->{'value'}} = $s[1];
65 return \%hash if ($router || $found_serviceid);
72 sub port_graphs_link {
73 # hardcoded for 'main' tree for now
75 my $serviceid = shift;
76 my $hash = $self->get_router_serviceids(undef,$serviceid) or return '';
77 my @keys = keys %$hash; # yeah this is weird...
79 my $iface = $hash->{$keys[0]};
80 my $config_tree = new Torrus::ConfigTree( -TreeName => 'main' );
81 my $token = $config_tree->token("/Routers/$host/Interface_Counters/$iface/InOut_bps");
82 return $Torrus::Freeside::FSURL."/torrus/main?token=$token";
87 my $serviceid = shift;
88 return '' unless $serviceid =~ /^[0-9A-Za-z_\-.\\\/ ]+$/;
90 my @svc_port = qsearch('svc_port', { 'serviceid' => $serviceid });
91 return '' unless scalar(@svc_port);
93 # for now it's like this, later on just change to qsearchs
98 sub find_torrus_srvderive_component {
100 my $serviceid = shift;
101 return '' unless $serviceid =~ /^[0-9A-Za-z_\-.\\\/ ]+$/;
103 qsearchs('torrus_srvderive_component', { 'serviceid' => $serviceid });
109 my @ls = localtime(time);
110 my ($d,$m,$y) = ($ls[3], $ls[4]+1, $ls[5]+1900);
113 if ($m == 0) { $m=12; $y-- }
114 #i should have better error checking
115 system('torrus', 'report', '--report=MonthlyUsage', "--date=$y-$m-01");
116 system('torrus', 'report', '--genhtml', '--tree=main');
126 qq( <param name="snmp-host" value="$ip"/>\n).
129 my $ddx = $self->_torrus_loadddx;
131 $ddx =~ s{(</snmp-discovery>)}{$newhost$1};
133 $self->_torrus_newddx($ddx);
138 my($self, $router_ip, $interface, $serviceid ) = @_;
140 $interface =~ s(\/)(_)g;
142 #should just use a proper XML parser huh
144 my @ddx = split(/\n/, $self->_torrus_loadddx);
146 die "Torrus Service ID $serviceid in use\n"
147 if grep /^\s*$serviceid:/, @ddx;
149 my $newline = " $serviceid:$interface:Both:main,";
155 while ( my $line = shift(@ddx) ) {
157 next unless $line =~ /^\s*<param\s+name="snmp-host"\s+value="$router_ip"\/?>/i;
159 while ( my $hostline = shift(@ddx) ) {
160 $new .= "$hostline\n" unless $hostline =~ /^\s+<\/host>\s*/i;
161 if ( $hostline =~ /^\s*<param name="RFC2863_IF_MIB::external-serviceid"\/?>/i ) {
163 while ( my $paramline = shift(@ddx) ) {
164 if ( $paramline =~ /^\s*<\/param>/ ) {
165 $new .= "$newline\n$paramline\n";
168 $new .= "$paramline\n";
174 } elsif ( $hostline =~ /^\s+<\/host>\s*/i ) {
177 qq( <param name="RFC2863_IF_MIB::external-serviceid">\n).
181 $new .= "$hostline\n";
189 $self->_torrus_newddx($new);
194 $lock = new IO::File ">>$lockfile" or die $!;
195 flock($lock, LOCK_EX);
199 flock($lock, LOCK_UN);
203 sub _torrus_loadddx {
206 return slurp($ddxfile);
210 my($self, $ddx) = @_;
212 my $new = new IO::File ">$ddxfile.new"
213 or die "can't write to $ddxfile.new: $!";
217 my $tmpname = $ddxfile . Date::Format::time2str('%Y%m%d%H%M%S',time);
218 rename("$ddxfile", $tmpname) or die $!;
219 rename("$ddxfile.new", $ddxfile) or die $!;
221 $self->_torrus_reload;
227 #i should use IPC::Run and have better error checking (commands are silent
228 # for success, or output errors)
230 system('torrus', 'devdiscover', "--in=$ddxfile");
232 system('torrus', 'compile', '--tree=main'); # , '--verbose'
234 $self->_torrus_unlock;
238 sub torrus_serviceids {
241 #is this going to get too slow or will the index make it okay?
242 my $sth = dbh->prepare("SELECT DISTINCT(serviceid) FROM srvexport")
244 $sth->execute or die $sth->errstr;
246 while ( my $row = $sth->fetchrow_arrayref ) {
247 my $serviceid = $row->[0];
248 $serviceid =~ s/_(IN|OUT)$//;
249 $serviceid{$serviceid}=1;
251 my @serviceids = sort keys %serviceid;