4 use Sys::SigAction qw( set_sig_handler );
7 use FS::Daemon ':all'; #daemonize1 drop_root daemonize2 myexit logfile sig*
8 use FS::UID qw( adminsuidsetup dbh driver_name );
9 use FS::Record qw( qsearch str2time_sql str2time_sql_closing concat_sql );
10 use FS::torrus_srvderive;
14 my $user = shift or die &usage;
15 $FS::Daemon::PID_NEWSTYLE = 1;
16 daemonize1('torrus-srvderive');
20 adminsuidsetup($user);
22 logfile( "%%%FREESIDE_LOG%%%/torrus-srvderive-log.". $FS::UID::datasrc );
26 our $conf = new FS::Conf;
28 die "not running: network_monitoring_system not Torrus_Internal\n"
33 my $str2time = str2time_sql();
34 my $c = str2time_sql_closing();
36 my $_date = concat_sql([ 'srvexport.srv_date', "' '", 'srvexport.srv_time' ]);
37 $_date = "CAST( $_date AS TIMESTAMP )" if driver_name =~ /^Pg/i;
38 $_date = str2time_sql. $_date. str2time_sql_closing;
40 my $other_date = concat_sql([ 'other.srv_date', "' '", 'other.srv_time' ]);
41 $other_date = "CAST( $other_date AS TIMESTAMP )" if driver_name =~ /^Pg/i;
42 $other_date = str2time_sql. $other_date. str2time_sql_closing;
44 my $in = concat_sql([ '?', "'_IN'" ]);
45 my $out = concat_sql([ '?', "'_OUT'" ]);
48 SELECT DISTINCT srv_date, srv_time FROM srvexport
50 SELECT 1 FROM srvexport AS other
51 WHERE other.serviceid IN ( $in, $out )
52 AND srvexport.srv_date = other.srv_date
53 AND ABS( $_date - $other_date ) <= 60
58 ORDER BY srv_date, srv_time
67 foreach my $torrus_srvderive ( qsearch('torrus_srvderive', {}) ) {
69 my $serviceid = $torrus_srvderive->serviceid;
71 my @serviceids = $torrus_srvderive->component_serviceids;
74 for my $dir ('_IN', '_OUT') {
75 push @in, map dbh->quote("$_$dir"), @serviceids;
77 my $in = join(',', @in);
81 SELECT 1 FROM srvexport AS other
82 WHERE other.serviceid IN ($in)
83 AND srvexport.srv_date = other.srv_date
84 AND ABS( $_date - $other_date ) <= 60
88 $ssql .= " AND srv_date >= '". $torrus_srvderive->last_srv_date. "' "
89 if $torrus_srvderive->last_srv_date;
93 warn "searching for times to add $serviceid\n" if $DEBUG;
94 warn $ssql if $DEBUG > 2;
95 my $sth = dbh->prepare($ssql) or die $DBI::errstr; #better recovery here?
97 warn "executing search\n" if $DEBUG;
101 my $h = set_sig_handler( 'ALRM', sub { $alarm = 1; } );
102 alarm(5*60); # 15*60);
103 $sth->execute($serviceid, $serviceid) or die $sth->errstr;
110 warn "search timed out; reconnecting and restarting\n";
111 dbh->clone()->do("KILL QUERY ". dbh->{"mysql_thread_id"})
112 if driver_name eq 'mysql';
113 adminsuidsetup($user);
117 warn "search executed; checking results\n" if $DEBUG;
120 while ( my $row = $sth->fetchrow_arrayref ) {
121 last if sigterm() || sigint();
123 my( $srv_date, $srv_time ) = @$row;
124 my $cur = str2time( "$srv_date $srv_time" );
125 next if $cur-$prev <= 60;
126 last if time - $cur <= 300;
128 warn "no $serviceid for $srv_date $srv_time; adding\n"
132 for my $dir ('_IN', '_OUT') {
134 my $sin = join(',', map dbh->quote("$_$dir"), @serviceids);
137 SELECT COALESCE(SUM(value),0) FROM srvexport AS other
138 WHERE other.serviceid IN ($sin)
139 AND ABS( $cur - $other_date ) <= 60
143 INSERT INTO srvexport ( srv_date, srv_time, serviceid, value, intvl )
144 VALUES ( ?, ?, ?, ($sum), ? )
146 my @param = ( time2str('%Y-%m-%d', $cur), #srv_date
147 time2str('%X', $cur), #srv_time
151 warn $isql. ' with param '. join(',',@param). "\n"
154 my $isth = dbh->prepare($isql) or die $DBI::errstr; #better recovery?
156 #stupid mysql deadlocks all the time on insert, so we need to recover
157 unless ( $isth->execute(@param) ) {
158 warn "Error inserting data for $serviceid$dir (restarting): ".
160 dbh->rollback; #or die dbh->errstr;
167 if ( $srv_date ne $torrus_srvderive->last_srv_date ) {
168 warn "updating last_srv_date of $serviceid to $srv_date\n" if $DEBUG;
169 $torrus_srvderive->last_srv_date($srv_date);
170 my $error = $torrus_srvderive->replace;
171 die $error if $error;
173 dbh->commit or die dbh->errstr;
177 warn "done with $serviceid\n" if $DEBUG;
179 } #foreach my $torrus_srvderive
180 dbh->commit or die dbh->errstr;
182 myexit() if sigterm() || sigint();
183 warn "restarting main loop\n" if $DEBUG > 1;
184 sleep 60 unless $found;
188 $conf->exists('network_monitoring_system')
189 && $conf->config('network_monitoring_system') eq 'Torrus_Internal';
193 die "Usage:\n\n freeside-cdrrewrited user\n";
199 freeside-torrus-srvderive - Freeside's Torrus virtual port daemon.
203 freeside-torrus-srvderive
207 Runs continuously, searches for samples in the srvexport table which do not
208 have an entry for combined virtual ports, and adds them.