5 use FS::Daemon ':all'; #daemonize1 drop_root daemonize2 myexit logfile sig*
6 use FS::UID qw( adminsuidsetup dbh driver_name );
7 use FS::Record qw( qsearch str2time_sql str2time_sql_closing );
8 use FS::torrus_srvderive;
12 my $user = shift or die &usage;
13 $FS::Daemon::PID_NEWSTYLE = 1;
14 daemonize1('freeside-torrus-srvderive');
18 adminsuidsetup($user);
20 logfile( "%%%FREESIDE_LOG%%%/torrus-srvderive-log.". $FS::UID::datasrc );
24 our $conf = new FS::Conf;
26 die "not running: network_monitoring_system not Torrus_Internal\n"
31 my $str2time = str2time_sql();
32 my $c = str2time_sql_closing();
34 my $_date = concat_sql([ 'srvexport.srv_date', "' '", 'srvexport.srv_time' ]);
35 $_date = "CAST( $_date AS TIMESTAMP )" if driver_name =~ /^Pg/i;
36 $_date = str2time_sql. $_date. str2time_sql_closing;
38 my $other_date = concat_sql([ 'other.srv_date', "' '", 'other.srv_time' ]);
39 $other_date = "CAST( $other_date AS TIMESTAMP )" if driver_name =~ /^Pg/i;
40 $other_date = str2time_sql. $other_date. str2time_sql_closing;
43 SELECT DISTINCT srv_date, srv_time FROM srvexport
45 SELECT 1 FROM srvexport AS other
46 WHERE other.serviceid IN ( ?||'_IN', ?||'_OUT')
47 AND ABS( $_date - $other_date ) <= 60
56 foreach my $torrus_srvderive ( qsearch('torrus_srvderive', {}) ) {
58 my $serviceid = $torrus_srvderive->serviceid;
60 my $sth = dbh->prepare($sql) or die $DBI::errstr; #better recovery?
61 $sth->execute($serviceid, $serviceid) or die $sth->errstr;
63 while ( my $row = $sth->fetchrow_arrayref ) {
64 my( $srv_date, $srv_time ) = @$row;
65 my $cur = str2time( "$srv_date $srv_time" );
66 next if $cur-$prev <= 60;
68 warn "no $serviceid for $srv_date $srv_time; adding\n"
76 myexit() if sigterm() || sigint();
77 sleep 60; #unless $found
81 $conf->exists('network_monitoring_system')
82 && $conf->config('network_monitoring_system') eq 'Torrus_Internal';
86 die "Usage:\n\n freeside-cdrrewrited user\n";
92 freeside-torrus-srvderive - Freeside's Torrus virtual port daemon.
96 freeside-torrus-srvderive
100 Runs continuously, searches for samples in the srvexport table which do not
101 have an entry for combined virtual ports, and adds them.