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;
17 my $user = shift or die &usage;
18 $FS::Daemon::PID_NEWSTYLE = 1;
19 daemonize1('torrus-srvderive');
23 adminsuidsetup($user);
25 logfile( "%%%FREESIDE_LOG%%%/torrus-srvderive-log.". $FS::UID::datasrc );
29 our $conf = new FS::Conf;
31 die "not running: network_monitoring_system not Torrus_Internal\n"
36 my $str2time = str2time_sql();
37 my $c = str2time_sql_closing();
39 my $_date = concat_sql([ 'srvexport.srv_date', "' '", 'srvexport.srv_time' ]);
40 $_date = "CAST( $_date AS TIMESTAMP )" if driver_name =~ /^Pg/i;
41 $_date = str2time_sql. $_date. str2time_sql_closing;
43 my $other_date = concat_sql([ 'other.srv_date', "' '", 'other.srv_time' ]);
44 $other_date = "CAST( $other_date AS TIMESTAMP )" if driver_name =~ /^Pg/i;
45 $other_date = str2time_sql. $other_date. str2time_sql_closing;
47 my $in = concat_sql([ '?', "'_IN'" ]);
48 my $out = concat_sql([ '?', "'_OUT'" ]);
51 SELECT DISTINCT srv_date, srv_time FROM srvexport
53 SELECT 1 FROM srvexport AS other
54 WHERE other.serviceid IN ( $in, $out )
55 AND srvexport.srv_date = other.srv_date
56 AND ABS( $_date - $other_date ) <= 60
61 ORDER BY srv_date, srv_time
72 #SERVICEID: foreach my $torrus_srvderive ( qsearch('torrus_srvderive', {}) ) {
73 foreach my $torrus_srvderive ( qsearch('torrus_srvderive', {}) ) {
76 if ( $kids >= $max_kids ) {
81 defined( my $pid = fork ) or do {
82 #warn "WARNING: can't fork: $!\n";
83 #next; #don't increment the kid counter
84 die "can't fork: $!\n";
93 $FS::UID::dbh->{InactiveDestroy} = 1;
97 my $serviceid = $torrus_srvderive->serviceid;
99 my @serviceids = $torrus_srvderive->component_serviceids;
100 exit unless @serviceids; #don't try to search for empty virtual ports
103 for my $dir ('_IN', '_OUT') {
104 push @in, map dbh->quote("$_$dir"), @serviceids;
106 my $in = join(',', @in);
108 if ( ! $torrus_srvderive->last_srv_date ) {
109 warn "finding initial last_srv_date for $serviceid\n" if $DEBUG;
110 my $dsql = "SELECT srv_date FROM srvexport WHERE serviceid IN ($in)
111 ORDER BY srv_date LIMIT 1";
112 my $dsth = dbh->prepare($dsql) or die $DBI::errstr;
113 $dsth->execute or die $dsth->errstr;
114 my $date = $dsth->fetchrow_arrayref->[0];
116 warn "found initial last_srv_date of $date; updating $serviceid\n"
118 $torrus_srvderive->last_srv_date($date);
119 my $error = $torrus_srvderive->replace;
120 die $error if $error;
122 warn "no initial last_srv_date for $serviceid; skipping\n" if $DEBUG;
129 SELECT 1 FROM srvexport AS other
130 WHERE other.serviceid IN ($in)
131 AND srvexport.srv_date = other.srv_date
132 AND ABS( $_date - $other_date ) <= 60
136 $ssql .= " AND srv_date >= '". $torrus_srvderive->last_srv_date. "' "
137 if $torrus_srvderive->last_srv_date;
139 $ssql .= $orderlimit;
141 warn "searching for times to add $serviceid\n" if $DEBUG;
142 warn $ssql if $DEBUG > 2;
143 my $sth = dbh->prepare($ssql) or die $DBI::errstr; #better recovery here?
146 my $h = set_sig_handler( 'ALRM', sub { die "_timeout\n"; } );
147 alarm(10*60); #5*60); #$torrus_srvderive->last_srv_date ? 5*60 : 15*60);
148 $sth->execute($serviceid, $serviceid) or die $sth->errstr;
153 if ( $@ && $@ eq "_timeout\n" ) {
154 #warn "search timed out; reconnecting and restarting\n";
155 warn "search timed out\n";
156 dbh->clone()->do("KILL QUERY ". dbh->{"mysql_thread_id"})
157 if driver_name eq 'mysql';
158 dbh->rollback; #or die dbh->errstr;
159 #adminsuidsetup($user);
160 #next SERVICEID; #MAIN;
166 warn "search finished; checking results\n" if $DEBUG;
169 while ( my $row = $sth->fetchrow_arrayref ) {
170 last if sigterm() || sigint();
172 my( $srv_date, $srv_time ) = @$row;
173 my $cur = str2time( "$srv_date $srv_time" );
174 next if $cur-$prev <= 60;
175 last if time - $cur <= 300;
177 warn "no $serviceid for $srv_date $srv_time; adding\n"
181 for my $dir ('_IN', '_OUT') {
183 my $sin = join(',', map dbh->quote("$_$dir"), @serviceids);
186 SELECT COALESCE(SUM(value),0) FROM srvexport AS other
187 WHERE other.serviceid IN ($sin)
188 AND ABS( $cur - $other_date ) <= 60
192 INSERT INTO srvexport ( srv_date, srv_time, serviceid, value, intvl )
193 VALUES ( ?, ?, ?, ($sum), ? )
195 my @param = ( time2str('%Y-%m-%d', $cur), #srv_date
196 time2str('%X', $cur), #srv_time
200 warn $isql. ' with param '. join(',',@param). "\n"
203 my $isth = dbh->prepare($isql) or die $DBI::errstr; #better recovery?
205 #stupid mysql deadlocks all the time on insert, so we need to recover
206 unless ( $isth->execute(@param) ) {
207 #warn "Error inserting data for $serviceid$dir (restarting): ".
209 warn "Error inserting data for $serviceid$dir: ". $isth->errstr;
210 dbh->rollback; #or die dbh->errstr;
212 #next SERVICEID; #MAIN;
218 if ( $srv_date ne $torrus_srvderive->last_srv_date ) {
219 warn "updating last_srv_date of $serviceid to $srv_date\n" if $DEBUG;
220 $torrus_srvderive->last_srv_date($srv_date);
221 my $error = $torrus_srvderive->replace;
222 die $error if $error;
224 dbh->commit or die dbh->errstr;
228 warn "done with $serviceid\n" if $DEBUG;
234 } #foreach my $torrus_srvderive
235 dbh->commit or die dbh->errstr;
237 myexit() if sigterm() || sigint();
238 warn "restarting main loop\n" if $DEBUG > 1;
239 sleep 60 unless $found;
243 $conf->exists('network_monitoring_system')
244 && $conf->config('network_monitoring_system') eq 'Torrus_Internal';
248 die "Usage:\n\n freeside-cdrrewrited user\n";
252 foreach my $pid ( keys %kids ) {
253 my $kid = waitpid($pid, WNOHANG);
263 freeside-torrus-srvderive - Freeside's Torrus virtual port daemon.
267 freeside-torrus-srvderive
271 Runs continuously, searches for samples in the srvexport table which do not
272 have an entry for combined virtual ports, and adds them.