4 use POSIX qw( :sys_wait_h );
5 use Sys::SigAction qw( set_sig_handler );
8 use FS::Daemon ':all'; #daemonize1 drop_root daemonize2 myexit logfile sig*
9 use FS::UID qw( adminsuidsetup forksuidsetup dbh driver_name );
10 use FS::Record qw( qsearch str2time_sql str2time_sql_closing concat_sql );
11 use FS::torrus_srvderive;
15 our $search_timeout = 30*60; # 15*60 5*60; #$torrus_srvderive->last_srv_date ? 5*60 : 15*60);
18 my $user = shift or die &usage;
19 $FS::Daemon::PID_NEWSTYLE = 1;
20 daemonize1('torrus-srvderive');
24 adminsuidsetup($user);
26 logfile( "%%%FREESIDE_LOG%%%/torrus-srvderive-log.". $FS::UID::datasrc );
30 our $conf = new FS::Conf;
32 die "not running: network_monitoring_system not Torrus_Internal\n"
37 my $str2time = str2time_sql();
38 my $c = str2time_sql_closing();
40 my $_date = concat_sql([ 'srvexport.srv_date', "' '", 'srvexport.srv_time' ]);
41 $_date = "CAST( $_date AS TIMESTAMP )" if driver_name =~ /^Pg/i;
42 $_date = str2time_sql. $_date. str2time_sql_closing;
44 my $other_date = concat_sql([ 'other.srv_date', "' '", 'other.srv_time' ]);
45 $other_date = "CAST( $other_date AS TIMESTAMP )" if driver_name =~ /^Pg/i;
46 $other_date = str2time_sql. $other_date. str2time_sql_closing;
48 my $in = concat_sql([ '?', "'_IN'" ]);
49 my $out = concat_sql([ '?', "'_OUT'" ]);
52 SELECT DISTINCT srv_date, srv_time FROM srvexport
54 SELECT 1 FROM srvexport AS other
55 WHERE other.serviceid IN ( $in, $out )
56 AND srvexport.srv_date = other.srv_date
57 AND ABS( $_date - $other_date ) <= 60
62 ORDER BY srv_date, srv_time
73 #SERVICEID: foreach my $torrus_srvderive ( qsearch('torrus_srvderive', {}) ) {
74 foreach my $torrus_srvderive ( qsearch('torrus_srvderive', {}) ) {
77 if ( $kids >= $max_kids ) {
79 myexit() if sigterm() || sigint();
83 defined( my $pid = fork ) or do {
84 #warn "WARNING: can't fork: $!\n";
85 #next; #don't increment the kid counter
86 die "can't fork: $!\n";
95 $FS::UID::dbh->{InactiveDestroy} = 1;
99 my $serviceid = $torrus_srvderive->serviceid;
101 my @serviceids = $torrus_srvderive->component_serviceids;
102 exit unless @serviceids; #don't try to search for empty virtual ports
104 #nonlocking select statements; rows in this table never change
105 dbh->do('SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED')
106 if driver_name eq 'mysql';
109 for my $dir ('_IN', '_OUT') {
110 push @in, map dbh->quote("$_$dir"), @serviceids;
112 my $in = join(',', @in);
114 if ( ! $torrus_srvderive->last_srv_date ) {
115 warn "finding initial last_srv_date for $serviceid\n" if $DEBUG;
116 my $dsql = "SELECT srv_date FROM srvexport WHERE serviceid IN ($in)
117 ORDER BY srv_date LIMIT 1";
118 my $dsth = dbh->prepare($dsql) or die $DBI::errstr;
119 $dsth->execute or die $dsth->errstr;
120 my $date = $dsth->fetchrow_arrayref->[0];
122 warn "found initial last_srv_date of $date; updating $serviceid\n"
124 $torrus_srvderive->last_srv_date($date);
125 my $error = $torrus_srvderive->replace;
126 die $error if $error;
128 warn "no initial last_srv_date for $serviceid; skipping\n" if $DEBUG;
135 SELECT 1 FROM srvexport AS other
136 WHERE other.serviceid IN ($in)
137 AND srvexport.srv_date = other.srv_date
138 AND ABS( $_date - $other_date ) <= 60
142 $ssql .= " AND srv_date >= '". $torrus_srvderive->last_srv_date. "' "
143 if $torrus_srvderive->last_srv_date;
145 $ssql .= $orderlimit;
147 warn "searching for times to add $serviceid\n" if $DEBUG;
148 warn $ssql if $DEBUG > 2;
149 my $sth = dbh->prepare($ssql) or die $DBI::errstr; #better recovery here?
152 my $h = set_sig_handler( 'ALRM', sub { die "_timeout\n"; } );
153 alarm($search_timeout);
154 $sth->execute($serviceid, $serviceid) or die $sth->errstr;
159 if ( $@ && $@ eq "_timeout\n" ) {
160 #warn "search timed out; reconnecting and restarting\n";
161 warn "search timed out\n";
162 dbh->clone()->do("KILL QUERY ". dbh->{"mysql_thread_id"})
163 if driver_name eq 'mysql';
164 dbh->rollback; #or die dbh->errstr;
165 #adminsuidsetup($user);
166 #next SERVICEID; #MAIN;
172 warn "search for $serviceid finished; checking results\n" if $DEBUG;
175 while ( my $row = $sth->fetchrow_arrayref ) {
176 last if sigterm() || sigint();
178 my( $srv_date, $srv_time ) = @$row;
179 my $cur = str2time( "$srv_date $srv_time" );
180 next if $cur-$prev <= 60;
181 last if time - $cur <= 300;
183 warn "no $serviceid for $srv_date $srv_time; adding\n"
187 for my $dir ('_IN', '_OUT') {
189 my $sin = join(',', map dbh->quote("$_$dir"), @serviceids);
192 SELECT COALESCE(SUM(value),0) FROM srvexport AS other
193 WHERE other.serviceid IN ($sin)
194 AND ABS( $cur - $other_date ) <= 60
198 INSERT INTO srvexport ( srv_date, srv_time, serviceid, value, intvl )
199 VALUES ( ?, ?, ?, ($sum), ? )
201 my @param = ( time2str('%Y-%m-%d', $cur), #srv_date
202 time2str('%X', $cur), #srv_time
206 warn $isql. ' with param '. join(',',@param). "\n"
209 my $isth = dbh->prepare($isql) or die $DBI::errstr; #better recovery?
211 #stupid mysql deadlocks all the time on insert, so we need to recover
212 unless ( $isth->execute(@param) ) {
213 #warn "Error inserting data for $serviceid$dir (restarting): ".
215 warn "Error inserting data for $serviceid$dir: ". $isth->errstr;
216 dbh->rollback; #or die dbh->errstr;
218 #next SERVICEID; #MAIN;
224 if ( $srv_date ne $torrus_srvderive->last_srv_date ) {
225 warn "updating last_srv_date of $serviceid to $srv_date\n" if $DEBUG;
226 $torrus_srvderive->last_srv_date($srv_date);
227 my $error = $torrus_srvderive->replace;
228 die $error if $error;
230 dbh->commit or die dbh->errstr;
234 warn "done with $serviceid\n" if $DEBUG;
240 } #foreach my $torrus_srvderive
241 dbh->commit or die dbh->errstr;
243 myexit() if sigterm() || sigint();
244 warn "restarting main loop\n" if $DEBUG > 1;
245 #sleep 60 unless $found;
249 $conf->exists('network_monitoring_system')
250 && $conf->config('network_monitoring_system') eq 'Torrus_Internal';
254 die "Usage:\n\n freeside-cdrrewrited user\n";
258 foreach my $pid ( keys %kids ) {
259 my $kid = waitpid($pid, WNOHANG);
269 freeside-torrus-srvderive - Freeside's Torrus virtual port daemon.
273 freeside-torrus-srvderive
277 Runs continuously, searches for samples in the srvexport table which do not
278 have an entry for combined virtual ports, and adds them.