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('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 srvexport.srv_date = other.srv_date
52 AND ABS( $_date - $other_date ) <= 60
57 ORDER BY srv_date, srv_time
66 foreach my $torrus_srvderive ( qsearch('torrus_srvderive', {}) ) {
68 my $serviceid = $torrus_srvderive->serviceid;
70 my @serviceids = $torrus_srvderive->component_serviceids;
73 for my $dir ('_IN', '_OUT') {
74 push @in, map dbh->quote("$_$dir"), @serviceids;
76 my $in = join(',', @in);
80 SELECT 1 FROM srvexport AS other
81 WHERE other.serviceid IN ($in)
82 AND srvexport.srv_date = other.srv_date
83 AND ABS( $_date - $other_date ) <= 60
87 $ssql .= " AND srv_date >= '". $torrus_srvderive->last_srv_date. "' "
88 if $torrus_srvderive->last_srv_date;
92 warn "searching for times to add $serviceid\n" if $DEBUG;
93 warn $ssql if $DEBUG > 2;
94 my $sth = dbh->prepare($ssql) or die $DBI::errstr; #better recovery?
95 $sth->execute($serviceid, $serviceid) or die $sth->errstr;
98 while ( my $row = $sth->fetchrow_arrayref ) {
99 last if sigterm() || sigint();
101 my( $srv_date, $srv_time ) = @$row;
102 my $cur = str2time( "$srv_date $srv_time" );
103 next if $cur-$prev <= 60;
104 last if time - $cur <= 300;
106 warn "no $serviceid for $srv_date $srv_time; adding\n"
110 for my $dir ('_IN', '_OUT') {
112 my $sin = join(',', map dbh->quote("$_$dir"), @serviceids);
115 SELECT COALESCE(SUM(value),0) FROM srvexport AS other
116 WHERE other.serviceid IN ($sin)
117 AND ABS( $cur - $other_date ) <= 60
121 INSERT INTO srvexport ( srv_date, srv_time, serviceid, value, intvl )
122 VALUES ( ?, ?, ?, ($sum), ? )
124 my @param = ( time2str('%Y-%m-%d', $cur), #srv_date
125 time2str('%X', $cur), #srv_time
129 warn $isql. ' with param '. join(',',@param). "\n"
132 my $isth = dbh->prepare($isql) or die $DBI::errstr; #better recovery?
134 #stupid mysql deadlocks all the time on insert, so we need to recover
135 unless ( $isth->execute(@param) ) {
136 warn "Error inserting data for $serviceid$dir (restarting): ".
138 dbh->rollback; #or die dbh->errstr;
145 if ( $srv_date ne $torrus_srvderive->last_srv_date ) {
146 warn "updating last_srv_date of $serviceid to $srv_date\n" if $DEBUG;
147 $torrus_srvderive->last_srv_date($srv_date);
148 my $error = $torrus_srvderive->replace;
149 die $error if $error;
151 dbh->commit or die dbh->errstr;
155 warn "done with $serviceid\n" if $DEBUG;
157 } #foreach my $torrus_srvderive
158 dbh->commit or die dbh->errstr;
160 myexit() if sigterm() || sigint();
161 warn "restarting main loop\n" if $DEBUG > 1;
162 sleep 60 unless $found;
166 $conf->exists('network_monitoring_system')
167 && $conf->config('network_monitoring_system') eq 'Torrus_Internal';
171 die "Usage:\n\n freeside-cdrrewrited user\n";
177 freeside-torrus-srvderive - Freeside's Torrus virtual port daemon.
181 freeside-torrus-srvderive
185 Runs continuously, searches for samples in the srvexport table which do not
186 have an entry for combined virtual ports, and adds them.