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 FS::Daemon ':all'; #daemonize1 drop_root daemonize2 myexit logfile sig*
6 use FS::UID qw( adminsuidsetup dbh driver_name );
7 use FS::Record qw( qsearch str2time_sql str2time_sql_closing concat_sql );
8 use FS::torrus_srvderive;
9
10 our $DEBUG = 2;
11
12 my $user = shift or die &usage;
13 $FS::Daemon::PID_NEWSTYLE = 1;
14 daemonize1('freeside-torrus-srvderive');
15
16 drop_root();
17
18 adminsuidsetup($user);
19
20 logfile( "%%%FREESIDE_LOG%%%/torrus-srvderive-log.". $FS::UID::datasrc );
21
22 daemonize2();
23
24 our $conf = new FS::Conf;
25
26 die "not running: network_monitoring_system not Torrus_Internal\n"
27   unless _shouldrun();
28
29 #--
30
31 my $str2time = str2time_sql();
32 my $c = str2time_sql_closing();
33
34 my $_date = concat_sql([ 'srvexport.srv_date', "' '", 'srvexport.srv_time' ]);
35 $_date = "CAST( $_date AS TIMESTAMP )" if driver_name =~ /^Pg/i;
36 $_date = str2time_sql. $_date.  str2time_sql_closing;
37
38 my $other_date = concat_sql([ 'other.srv_date', "' '", 'other.srv_time' ]);
39 $other_date = "CAST( $other_date AS TIMESTAMP )" if driver_name =~ /^Pg/i;
40 $other_date = str2time_sql. $other_date.  str2time_sql_closing;
41
42 my $in  = concat_sql([ '?', "'_IN'" )
43 my $out = concat_sql([ '?', "'_OUT'" )
44
45 my $sql = "
46   SELECT DISTINCT srv_date, srv_time FROM srvexport
47     WHERE NOT EXISTS (
48                        SELECT 1 FROM srvexport AS other
49                          WHERE other.serviceid IN ( $in, $out )
50                            AND ABS( $_date - $other_date ) <= 60
51                      )
52     ORDER BY id
53     LIMIT 10
54 ";
55
56 while (1) {
57
58   my $found = 0;
59
60   foreach my $torrus_srvderive ( qsearch('torrus_srvderive', {}) ) {
61
62     my $serviceid = $torrus_srvderive->serviceid;
63
64     warn $sql if $DEBUG > 1;
65     my $sth = dbh->prepare($sql) or die $DBI::errstr; #better recovery?
66     $sth->execute($serviceid, $serviceid) or die $sth->errstr;
67     my $prev = 0;
68     while ( my $row = $sth->fetchrow_arrayref ) {
69       $found++;
70       my( $srv_date, $srv_time ) = @$row;
71       my $cur = str2time( "$srv_date $srv_time" );
72       next if $cur-$prev <= 60;
73       
74       warn "no $serviceid for $srv_date $srv_time; adding\n"
75         if $DEBUG;
76
77
78       $prev = $cur;
79     }
80
81   }
82
83
84   myexit() if sigterm() || sigint();
85   sleep 60 unless $found;
86 }
87
88 sub _shouldrun {
89      $conf->exists('network_monitoring_system')
90   && $conf->config('network_monitoring_system') eq 'Torrus_Internal';
91 }
92
93 sub usage { 
94   die "Usage:\n\n  freeside-cdrrewrited user\n";
95 }
96
97
98 =head1 NAME
99
100 freeside-torrus-srvderive - Freeside's Torrus virtual port daemon.
101
102 =head1 SYNOPSIS
103
104   freeside-torrus-srvderive
105
106 =head1 DESCRIPTION
107
108 Runs continuously, searches for samples in the srvexport table which do not
109 have an entry for combined virtual ports, and adds them.
110
111 =head1 SEE ALSO
112
113 =cut
114
115 1;
116