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 SERVICEID: 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);
79 if ( ! $torrus_srvderive->last_srv_date ) {
80 warn "finding initial last_srv_date for $serviceid\n" if $DEBUG;
81 my $dsql = "SELECT srv_date FROM srvexport WHERE serviceid IN ($in)
82 ORDER BY srv_date LIMIT 1";
83 my $dsth = dbh->prepare($dsql) or die $DBI::errstr;
84 $dsth->execute or die $dsth->errstr;
85 my $date = $dsth->fetchrow_arrayref->[0];
87 warn "found initial last_srv_date of $date; updating $serviceid\n"
89 $torrus_srvderive->last_srv_date($date);
90 my $error = $torrus_srvderive->replace;
93 warn "no initial last_srv_date for $serviceid; skipping\n" if $DEBUG;
100 SELECT 1 FROM srvexport AS other
101 WHERE other.serviceid IN ($in)
102 AND srvexport.srv_date = other.srv_date
103 AND ABS( $_date - $other_date ) <= 60
107 $ssql .= " AND srv_date >= '". $torrus_srvderive->last_srv_date. "' "
108 if $torrus_srvderive->last_srv_date;
110 $ssql .= $orderlimit;
112 warn "searching for times to add $serviceid\n" if $DEBUG;
113 warn $ssql if $DEBUG > 2;
114 my $sth = dbh->prepare($ssql) or die $DBI::errstr; #better recovery here?
117 my $h = set_sig_handler( 'ALRM', sub { die "_timeout\n"; } );
118 alarm(10*60); #5*60); #$torrus_srvderive->last_srv_date ? 5*60 : 15*60);
119 $sth->execute($serviceid, $serviceid) or die $sth->errstr;
124 if ( $@ && $@ eq "_timeout\n" ) {
125 warn "search timed out; reconnecting and restarting\n";
126 dbh->clone()->do("KILL QUERY ". dbh->{"mysql_thread_id"})
127 if driver_name eq 'mysql';
128 dbh->rollback; #or die dbh->errstr;
129 adminsuidsetup($user);
130 next SERVICEID; #MAIN;
135 warn "search finished; checking results\n" if $DEBUG;
138 while ( my $row = $sth->fetchrow_arrayref ) {
139 last if sigterm() || sigint();
141 my( $srv_date, $srv_time ) = @$row;
142 my $cur = str2time( "$srv_date $srv_time" );
143 next if $cur-$prev <= 60;
144 last if time - $cur <= 300;
146 warn "no $serviceid for $srv_date $srv_time; adding\n"
150 for my $dir ('_IN', '_OUT') {
152 my $sin = join(',', map dbh->quote("$_$dir"), @serviceids);
155 SELECT COALESCE(SUM(value),0) FROM srvexport AS other
156 WHERE other.serviceid IN ($sin)
157 AND ABS( $cur - $other_date ) <= 60
161 INSERT INTO srvexport ( srv_date, srv_time, serviceid, value, intvl )
162 VALUES ( ?, ?, ?, ($sum), ? )
164 my @param = ( time2str('%Y-%m-%d', $cur), #srv_date
165 time2str('%X', $cur), #srv_time
169 warn $isql. ' with param '. join(',',@param). "\n"
172 my $isth = dbh->prepare($isql) or die $DBI::errstr; #better recovery?
174 #stupid mysql deadlocks all the time on insert, so we need to recover
175 unless ( $isth->execute(@param) ) {
176 warn "Error inserting data for $serviceid$dir (restarting): ".
178 dbh->rollback; #or die dbh->errstr;
180 next SERVICEID; #MAIN;
185 if ( $srv_date ne $torrus_srvderive->last_srv_date ) {
186 warn "updating last_srv_date of $serviceid to $srv_date\n" if $DEBUG;
187 $torrus_srvderive->last_srv_date($srv_date);
188 my $error = $torrus_srvderive->replace;
189 die $error if $error;
191 dbh->commit or die dbh->errstr;
195 warn "done with $serviceid\n" if $DEBUG;
197 } #foreach my $torrus_srvderive
198 dbh->commit or die dbh->errstr;
200 myexit() if sigterm() || sigint();
201 warn "restarting main loop\n" if $DEBUG > 1;
202 sleep 60 unless $found;
206 $conf->exists('network_monitoring_system')
207 && $conf->config('network_monitoring_system') eq 'Torrus_Internal';
211 die "Usage:\n\n freeside-cdrrewrited user\n";
217 freeside-torrus-srvderive - Freeside's Torrus virtual port daemon.
221 freeside-torrus-srvderive
225 Runs continuously, searches for samples in the srvexport table which do not
226 have an entry for combined virtual ports, and adds them.