have torrus-srvderive just use the native torrus srvderive instead of sucking CPU...
[freeside.git] / FS / bin / freeside-torrus-srvderive
1 #!/usr/bin/perl -w
2
3 use strict;
4 use POSIX qw( :sys_wait_h );
5 use Sys::SigAction qw( set_sig_handler );
6 use Date::Parse;
7 use Date::Format;
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;
13
14 our $DEBUG = 2;
15 our $max_kids = 4;
16 our %kids;
17
18 my $user = shift or die &usage;
19 $FS::Daemon::PID_NEWSTYLE = 1;
20 daemonize1('torrus-srvderive');
21
22 drop_root();
23
24 adminsuidsetup($user);
25
26 logfile( "%%%FREESIDE_LOG%%%/torrus-srvderive-log.". $FS::UID::datasrc );
27
28 daemonize2();
29
30 our $conf = new FS::Conf;
31
32 die "not running: network_monitoring_system not Torrus_Internal\n"
33   unless _shouldrun();
34
35 #--
36
37 our $kids = 0;
38
39 #MAIN: while (1) {
40 while (1) {
41
42   #SERVICEID: foreach my $torrus_srvderive ( qsearch('torrus_srvderive', {}) ) {
43   foreach my $torrus_srvderive ( qsearch('torrus_srvderive', {}) ) {
44
45     &reap_kids;
46     if ( $kids >= $max_kids ) {
47       sleep 5;
48       myexit() if sigterm() || sigint();
49       redo;
50     }
51
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";
56     };
57
58     if ( $pid ) {
59       $kids++;
60       $kids{$pid} = 1;
61     } else { #kid time
62
63       #get new db handle
64       $FS::UID::dbh->{InactiveDestroy} = 1;
65
66       forksuidsetup($user);
67
68       my $serviceid = $torrus_srvderive->serviceid;
69
70       my @serviceids = $torrus_srvderive->component_serviceids;
71       exit unless @serviceids; #don't try to search for empty virtual ports
72
73       my @in = ();
74       for my $dir ('_IN', '_OUT') {
75         push @in, map dbh->quote("$_$dir"), @serviceids;
76       }
77       my $in = join(',', @in);
78
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];
86         if ( $date ) {
87           warn "found initial last_srv_date of $date; updating $serviceid\n"
88             if $DEBUG;
89           $torrus_srvderive->last_srv_date($date);
90           my $error = $torrus_srvderive->replace;
91           die $error if $error;
92         } else {
93           warn "no initial last_srv_date for $serviceid; skipping\n" if $DEBUG;
94           exit;
95         }
96       }
97
98       #if we're more than a day behind, call native torrus srvderive
99       my $last_srv_date = $torrus_srvderive->last_srv_date;
100       my $currdate = time2str('%Y-%m-%d', time);
101       if ( $last_srv_date ne $currdate
102            && time - str2time($last_srv_date) > (24)*60*60
103          ) {
104         warn "serviceid $serviceid last_srv_date != $currdate\n" if $DEBUG;
105
106         #delete records from last_srv_date
107         my $dsql =
108           'DELETE FROM srvexport WHERE srv_date = ? AND serviceid IN(?,?)';
109         my $dsth = dbh->prepare($dsql) or die $DBI::errstr;
110         warn "$dsql $last_srv_date ${serviceid}_IN ${serviceid}_OUT\n"
111           if $DEBUG;
112         $dsth->execute($last_srv_date, $serviceid.'_IN', $serviceid.'_OUT')
113           or die $dsth->errstr;
114         dbh->commit or die dbh->errstr;
115
116         foreach my $dir ('_IN','_OUT') {
117
118           #XX TORRUS_PATH
119           my @cmd = ( '/usr/local/bin/torrus', 'srvderive',
120                         "--start=$last_srv_date", "--end=$currdate",
121                         "--out=$serviceid$dir", "--func=SUM",
122                         map "$_$dir", @serviceids
123                     );
124
125           warn "calling native srvderive: ". join(' ', @cmd). "\n" if $DEBUG;
126           my $out_and_err = '';
127           run( \@cmd, '>&', \$out_and_err ) #verbose?  timeout?
128             or die "error calling native srvderive: $out_and_err\n";
129
130         }
131         warn "native srvderive done" if $DEBUG;
132
133         $torrus_srvderive->last_srv_date($currdate);
134         my $error = $torrus_srvderive->replace;
135         die $error if $error;
136
137         #don't bother trying today's immediately
138         warn "done with $serviceid for now\n" if $DEBUG;
139
140       } else {
141
142         warn "serviceid $serviceid last_srv_date == $currdate; skipping\n"
143           if $DEBUG;
144
145       }
146
147       exit; #end-of-kid
148
149     }
150
151   } #foreach my $torrus_srvderive
152   dbh->commit or die dbh->errstr;
153
154   myexit() if sigterm() || sigint();
155   warn "sleeping an hour\n" if $DEBUG > 1;
156   sleep 60*60;
157   warn "restarting main loop\n" if $DEBUG > 1;
158 }
159
160 sub _shouldrun {
161      $conf->exists('network_monitoring_system')
162   && $conf->config('network_monitoring_system') eq 'Torrus_Internal';
163 }
164
165 sub usage { 
166   die "Usage:\n\n  freeside-cdrrewrited user\n";
167 }
168
169 sub reap_kids {
170   foreach my $pid ( keys %kids ) {
171     my $kid = waitpid($pid, WNOHANG);
172     if ( $kid > 0 ) {
173       $kids--;
174       delete $kids{$kid};
175     }
176   }
177 }
178
179 =head1 NAME
180
181 freeside-torrus-srvderive - Freeside's Torrus virtual port daemon.
182
183 =head1 SYNOPSIS
184
185   freeside-torrus-srvderive
186
187 =head1 DESCRIPTION
188
189 Runs continuously, searches for samples in the srvexport table which do not
190 have an entry for combined virtual ports, and adds them.
191
192 =head1 SEE ALSO
193
194 =cut
195
196 1;
197