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 = 2;
12
13 my $user = shift or die &usage;
14 $FS::Daemon::PID_NEWSTYLE = 1;
15 daemonize1('freeside-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 $within = ABS( $_date - $other_date ) <= 60;
44
45 my $in  = concat_sql([ '?', "'_IN'" ]);
46 my $out = concat_sql([ '?', "'_OUT'" ]);
47
48 my $sql = "
49   SELECT DISTINCT srv_date, srv_time FROM srvexport
50     WHERE NOT EXISTS (
51                        SELECT 1 FROM srvexport AS other
52                          WHERE other.serviceid IN ( $in, $out )
53                            AND $within
54                      )
55     ORDER BY srv_date, srv_time
56     LIMIT 100
57 ";
58
59 while (1) {
60
61   my $found = 0;
62
63   foreach my $torrus_srvderive ( qsearch('torrus_srvderive', {}) ) {
64
65     my $serviceid = $torrus_srvderive->serviceid;
66
67     warn $sql if $DEBUG > 1;
68     my $sth = dbh->prepare($sql) or die $DBI::errstr; #better recovery?
69     $sth->execute($serviceid, $serviceid) or die $sth->errstr;
70     my $prev = 0;
71     while ( my $row = $sth->fetchrow_arrayref ) {
72       $found++;
73       my( $srv_date, $srv_time ) = @$row;
74       my $cur = str2time( "$srv_date $srv_time" );
75       next if $cur-$prev <= 60;
76       
77       warn "no $serviceid for $srv_date $srv_time; adding\n"
78         if $DEBUG;
79
80       my @serviceids = $torrus_srvderive->component_serviceids;
81
82       for my $dir ('_IN', '_OUT') {
83
84         my $sin = join(',', map dbh->quote("$_$dir"), @serviceids);
85
86         my $sum = "
87           SELECT SUM(value) FROM srvexport AS other
88             WHERE other.serviceid IN ($sin)
89               AND $within
90         ";
91
92         my $isql = "
93           INSERT INTO srvexport ( srv_date, srv_time, serviceid, value, intvl )
94             VALUES ( ?, ?, ?, ($sum), ? )
95         ";
96
97         my $isth = dbh->prepare($isql) or die $DBI::errstr; #better recovery?
98         $isth->execute( time2str('%Y-%m-%d', $cur), #srv_date
99                         time2str('%X', $cur),       #srv_time
100                         $serviceid,
101                         300, #intvl ... 
102                       )
103           or die $isth->errstr;
104                         
105       }
106
107       $prev = $cur;
108     }
109
110   }
111
112
113   myexit() if sigterm() || sigint();
114   sleep 60; # unless $found;
115 }
116
117 sub _shouldrun {
118      $conf->exists('network_monitoring_system')
119   && $conf->config('network_monitoring_system') eq 'Torrus_Internal';
120 }
121
122 sub usage { 
123   die "Usage:\n\n  freeside-cdrrewrited user\n";
124 }
125
126
127 =head1 NAME
128
129 freeside-torrus-srvderive - Freeside's Torrus virtual port daemon.
130
131 =head1 SYNOPSIS
132
133   freeside-torrus-srvderive
134
135 =head1 DESCRIPTION
136
137 Runs continuously, searches for samples in the srvexport table which do not
138 have an entry for combined virtual ports, and adds them.
139
140 =head1 SEE ALSO
141
142 =cut
143
144 1;
145