add start of torrus srvderive daemon, RT#10574
[freeside.git] / FS / bin / freeside-torrus-srvderive
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Date::Parse;
5 use Date::Format;
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;
10
11 our $DEBUG = 3;
12
13 my $user = shift or die &usage;
14 $FS::Daemon::PID_NEWSTYLE = 1;
15 daemonize1('torrus-srvderive');
16
17 drop_root();
18
19 adminsuidsetup($user);
20
21 logfile( "%%%FREESIDE_LOG%%%/torrus-srvderive-log.". $FS::UID::datasrc );
22
23 daemonize2();
24
25 our $conf = new FS::Conf;
26
27 die "not running: network_monitoring_system not Torrus_Internal\n"
28   unless _shouldrun();
29
30 #--
31
32 my $str2time = str2time_sql();
33 my $c = str2time_sql_closing();
34
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;
38
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;
42
43 my $in  = concat_sql([ '?', "'_IN'" ]);
44 my $out = concat_sql([ '?', "'_OUT'" ]);
45
46 my $sql = "
47   SELECT DISTINCT srv_date, srv_time FROM srvexport
48     WHERE NOT EXISTS (
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
53                      )
54 ";
55
56 my $orderlimit = "
57     ORDER BY srv_date, srv_time
58     LIMIT 100
59 ";
60
61
62 while (1) {
63
64   my $found = 0;
65
66   foreach my $torrus_srvderive ( qsearch('torrus_srvderive', {}) ) {
67
68     my $serviceid = $torrus_srvderive->serviceid;
69
70     my @serviceids = $torrus_srvderive->component_serviceids;
71
72     my @in = ();
73     for my $dir ('_IN', '_OUT') {
74       push @in, map dbh->quote("$_$dir"), @serviceids;
75     }
76     my $in = join(',', @in);
77
78     my $ssql = "
79       $sql AND EXISTS (
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
84                      )
85     ";
86
87     $ssql .= " AND srv_date >= '". $torrus_srvderive->last_srv_date. "' "
88       if $torrus_srvderive->last_srv_date;
89
90     $ssql .= $orderlimit;
91
92     warn $ssql if $DEBUG > 1;
93     my $sth = dbh->prepare($ssql) or die $DBI::errstr; #better recovery?
94     $sth->execute($serviceid, $serviceid) or die $sth->errstr;
95     my $prev = 0;
96     while ( my $row = $sth->fetchrow_arrayref ) {
97       last if sigterm() || sigint();
98
99       my( $srv_date, $srv_time ) = @$row;
100       my $cur = str2time( "$srv_date $srv_time" );
101       next if $cur-$prev <= 60;
102       last if time - $cur <= 300;
103
104       warn "no $serviceid for $srv_date $srv_time; adding\n"
105         if $DEBUG;
106       $found++;
107
108       for my $dir ('_IN', '_OUT') {
109
110         my $sin = join(',', map dbh->quote("$_$dir"), @serviceids);
111
112         my $sum = "
113           SELECT COALESCE(SUM(value),0) FROM srvexport AS other
114             WHERE other.serviceid IN ($sin)
115               AND ABS( $cur - $other_date ) <= 60
116         ";
117
118         my $isql = "
119           INSERT INTO srvexport ( srv_date, srv_time, serviceid, value, intvl )
120             VALUES ( ?, ?, ?, ($sum), ? )
121         ";
122         my @param = ( time2str('%Y-%m-%d', $cur), #srv_date
123                       time2str('%X', $cur),       #srv_time
124                       "$serviceid$dir",
125                       300, #intvl ... 
126                     );
127         warn $isql. ' with param '. join(',',@param). "\n"
128           if $DEBUG > 2;
129
130         my $isth = dbh->prepare($isql) or die $DBI::errstr; #better recovery?
131         $isth->execute( @param )       or die $isth->errstr;
132                         
133       }
134
135       if ( $srv_date ne $torrus_srvderive->last_srv_date ) {
136         warn "updating last_srv_date of $serviceid to $srv_date\n" if $DEBUG;
137         $torrus_srvderive->last_srv_date($srv_date);
138         my $error = $torrus_srvderive->replace;
139         die $error if $error;
140       }
141       dbh->commit or die dbh->errstr;
142
143       $prev = $cur;
144     }
145
146   }
147
148
149   myexit() if sigterm() || sigint();
150   sleep 60 unless $found;
151 }
152
153 sub _shouldrun {
154      $conf->exists('network_monitoring_system')
155   && $conf->config('network_monitoring_system') eq 'Torrus_Internal';
156 }
157
158 sub usage { 
159   die "Usage:\n\n  freeside-cdrrewrited user\n";
160 }
161
162
163 =head1 NAME
164
165 freeside-torrus-srvderive - Freeside's Torrus virtual port daemon.
166
167 =head1 SYNOPSIS
168
169   freeside-torrus-srvderive
170
171 =head1 DESCRIPTION
172
173 Runs continuously, searches for samples in the srvexport table which do not
174 have an entry for combined virtual ports, and adds them.
175
176 =head1 SEE ALSO
177
178 =cut
179
180 1;
181