6 use FS::Daemon ':all'; #daemonize1 drop_root daemonize2 myexit logfile sig*
7 use FS::UID qw( adminsuidsetup dbh driver_name );
8 use FS::Record qw( qsearch str2time_sql str2time_sql_closing concat_sql );
9 use FS::torrus_srvderive;
13 my $user = shift or die &usage;
14 $FS::Daemon::PID_NEWSTYLE = 1;
15 daemonize1('freeside-torrus-srvderive');
19 adminsuidsetup($user);
21 logfile( "%%%FREESIDE_LOG%%%/torrus-srvderive-log.". $FS::UID::datasrc );
25 our $conf = new FS::Conf;
27 die "not running: network_monitoring_system not Torrus_Internal\n"
32 my $str2time = str2time_sql();
33 my $c = str2time_sql_closing();
35 my $_date = concat_sql([ 'srvexport.srv_date', "' '", 'srvexport.srv_time' ]);
36 $_date = "CAST( $_date AS TIMESTAMP )" if driver_name =~ /^Pg/i;
37 $_date = str2time_sql. $_date. str2time_sql_closing;
39 my $other_date = concat_sql([ 'other.srv_date', "' '", 'other.srv_time' ]);
40 $other_date = "CAST( $other_date AS TIMESTAMP )" if driver_name =~ /^Pg/i;
41 $other_date = str2time_sql. $other_date. str2time_sql_closing;
43 my $in = concat_sql([ '?', "'_IN'" ]);
44 my $out = concat_sql([ '?', "'_OUT'" ]);
47 SELECT DISTINCT srv_date, srv_time FROM srvexport
49 SELECT 1 FROM srvexport AS other
50 WHERE other.serviceid IN ( $in, $out )
51 AND ABS( $_date - $other_date ) <= 60
53 ORDER BY srv_date, srv_time
61 foreach my $torrus_srvderive ( qsearch('torrus_srvderive', {}) ) {
63 my $serviceid = $torrus_srvderive->serviceid;
65 warn $sql if $DEBUG > 2;
66 my $sth = dbh->prepare($sql) or die $DBI::errstr; #better recovery?
67 $sth->execute($serviceid, $serviceid) or die $sth->errstr;
69 while ( my $row = $sth->fetchrow_arrayref ) {
71 my( $srv_date, $srv_time ) = @$row;
72 my $cur = str2time( "$srv_date $srv_time" );
73 next if $cur-$prev <= 60;
74 last if time - $cur <= 300;
76 warn "no $serviceid for $srv_date $srv_time; adding\n"
79 my @serviceids = $torrus_srvderive->component_serviceids;
81 for my $dir ('_IN', '_OUT') {
83 my $sin = join(',', map dbh->quote("$_$dir"), @serviceids);
86 SELECT COALESCE(SUM(value),0) FROM srvexport AS other
87 WHERE other.serviceid IN ($sin)
88 AND ABS( $cur - $other_date ) <= 60
92 INSERT INTO srvexport ( srv_date, srv_time, serviceid, value, intvl )
93 VALUES ( ?, ?, ?, ($sum), ? )
95 my @param = ( time2str('%Y-%m-%d', $cur), #srv_date
96 time2str('%X', $cur), #srv_time
100 warn $isql. ' with param '. @param. "\n"
103 my $isth = dbh->prepare($isql) or die $DBI::errstr; #better recovery?
104 $isth->execute( @param ) or die $isth->errstr;
114 myexit() if sigterm() || sigint();
115 sleep 60; # unless $found;
119 $conf->exists('network_monitoring_system')
120 && $conf->config('network_monitoring_system') eq 'Torrus_Internal';
124 die "Usage:\n\n freeside-cdrrewrited user\n";
130 freeside-torrus-srvderive - Freeside's Torrus virtual port daemon.
134 freeside-torrus-srvderive
138 Runs continuously, searches for samples in the srvexport table which do not
139 have an entry for combined virtual ports, and adds them.