4 use POSIX qw( :sys_wait_h );
5 use Sys::SigAction qw( set_sig_handler );
8 use IPC::Run qw( run ); #timeout );
9 use FS::Daemon ':all'; #daemonize1 drop_root daemonize2 myexit logfile sig*
10 use FS::UID qw( adminsuidsetup forksuidsetup dbh driver_name );
11 use FS::Record qw( qsearch );
12 use FS::torrus_srvderive;
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"
42 #SERVICEID: foreach my $torrus_srvderive ( qsearch('torrus_srvderive', {}) ) {
43 foreach my $torrus_srvderive ( qsearch('torrus_srvderive', {}) ) {
46 if ( $kids >= $max_kids ) {
48 myexit() if sigterm() || sigint();
52 defined( my $pid = fork ) or do {
53 #warn "WARNING: can't fork: $!\n";
54 #next; #don't increment the kid counter
55 die "can't fork: $!\n";
64 $FS::UID::dbh->{InactiveDestroy} = 1;
68 my $serviceid = $torrus_srvderive->serviceid;
70 my @serviceids = $torrus_srvderive->component_serviceids;
71 exit unless @serviceids; #don't try to search for empty virtual ports
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 $row = $dsth->fetchrow_arrayref;
86 my $date = (defined($row) && ref($row)) ? $row->[0] : '';
88 warn "found initial last_srv_date of $date; updating $serviceid\n"
90 $torrus_srvderive->last_srv_date($date);
91 my $error = $torrus_srvderive->replace;
94 warn "no initial last_srv_date for $serviceid; skipping\n" if $DEBUG;
99 #if we're more than a day behind, call native torrus srvderive
100 my $last_srv_date = $torrus_srvderive->last_srv_date;
101 my $currdate = time2str('%Y-%m-%d', time);
102 if ( $last_srv_date ne $currdate
103 && time - str2time($last_srv_date) > (24)*60*60
105 warn "serviceid $serviceid last_srv_date != $currdate\n" if $DEBUG;
107 #delete records from last_srv_date
109 'DELETE FROM srvexport WHERE srv_date = ? AND serviceid IN(?,?)';
110 my $dsth = dbh->prepare($dsql) or die $DBI::errstr;
111 warn "$dsql $last_srv_date ${serviceid}_IN ${serviceid}_OUT\n"
113 $dsth->execute($last_srv_date, $serviceid.'_IN', $serviceid.'_OUT')
114 or die $dsth->errstr;
115 dbh->commit or die dbh->errstr;
117 foreach my $dir ('_IN','_OUT') {
120 my @cmd = ( '/usr/local/bin/torrus', 'srvderive',
121 "--start=$last_srv_date", "--end=$currdate",
122 "--out=$serviceid$dir", "--func=SUM",
123 map "$_$dir", @serviceids
126 warn "calling native srvderive: ". join(' ', @cmd). "\n" if $DEBUG;
127 my $out_and_err = '';
128 run( \@cmd, '>&', \$out_and_err ) #verbose? timeout?
129 or die "error calling native srvderive: $out_and_err\n";
132 warn "native srvderive done" if $DEBUG;
134 $torrus_srvderive->last_srv_date($currdate);
135 my $error = $torrus_srvderive->replace;
136 die $error if $error;
138 #don't bother trying today's immediately
139 warn "done with $serviceid for now\n" if $DEBUG;
143 warn "serviceid $serviceid last_srv_date == $currdate; skipping\n"
152 } #foreach my $torrus_srvderive
153 dbh->commit or die dbh->errstr;
155 myexit() if sigterm() || sigint();
156 warn "sleeping an hour\n" if $DEBUG > 1;
158 warn "restarting main loop\n" if $DEBUG > 1;
162 $conf->exists('network_monitoring_system')
163 && $conf->config('network_monitoring_system') eq 'Torrus_Internal';
167 die "Usage:\n\n freeside-cdrrewrited user\n";
171 foreach my $pid ( keys %kids ) {
172 my $kid = waitpid($pid, WNOHANG);
182 freeside-torrus-srvderive - Freeside's Torrus virtual port daemon.
186 freeside-torrus-srvderive
190 Runs continuously, searches for samples in the srvexport table which do not
191 have an entry for combined virtual ports, and adds them.