ACL for hardware class config, RT#85057
[freeside.git] / FS / FS / Cron / check.pm
1 package FS::Cron::check;
2
3 use strict;
4 use vars qw( @ISA @EXPORT_OK $DEBUG $FS_RUN $error_msg
5              $SELFSERVICE_USER $SELFSERVICE_MACHINES @SELFSERVICE_MACHINES
6            );
7 use Exporter;
8 use LWP::UserAgent;
9 use HTTP::Request;
10 use URI::Escape;
11 use Email::Send;
12 use FS::Conf;
13 use FS::Record qw(qsearch);
14 use FS::cust_pay_pending;
15
16 @ISA = qw( Exporter );
17 @EXPORT_OK = qw(
18   check_queued check_selfservice check_apache check_bop_failures
19   alert error_msg
20 );
21
22 $DEBUG = 0;
23
24 $FS_RUN = '/var/run';
25
26 sub check_queued {
27   _check_fsproc('queued');
28 }
29
30 $SELFSERVICE_USER = '%%%SELFSERVICE_USER%%%';
31
32 $SELFSERVICE_MACHINES = '%%%SELFSERVICE_MACHINES%%%'; #substituted by Makefile
33 $SELFSERVICE_MACHINES =~ s/^\s+//;
34 $SELFSERVICE_MACHINES =~ s/\s+$//;
35 @SELFSERVICE_MACHINES = split(/\s+/, $SELFSERVICE_MACHINES);
36 @SELFSERVICE_MACHINES = ()
37   if scalar(@SELFSERVICE_MACHINES) == 1
38   && $SELFSERVICE_MACHINES[0] eq '%%%'.'SELFSERVICE_MACHINES'.'%%%';
39
40 sub check_selfservice {
41   foreach my $machine ( @SELFSERVICE_MACHINES ) {
42     unless ( _check_fsproc("selfservice-server.$SELFSERVICE_USER.$machine") ) {
43       $error_msg = "Self-service daemon not running for $machine";
44       return 0;
45     }
46   }
47   return 1;
48 }
49
50 sub _check_fsproc {
51   my $arg = shift;
52   _check_pidfile( "freeside-$arg.pid" );
53 }
54
55 sub _check_pidfile {
56   my $pidfile = shift;
57   open(PID, "$FS_RUN/$pidfile") or return 0;
58   chomp( my $pid = scalar(<PID>) );
59   close PID; # or return 0;
60
61   $pid && kill 0, $pid;
62 }
63
64 sub check_apache {
65   my $ua = new LWP::UserAgent;
66   $ua->agent("FreesideCronCheck/0.1 " . $ua->agent);
67
68   my $req = new HTTP::Request GET => 'https://localhost/';
69   my $res = $ua->request($req);
70
71   return 1 if $res->is_success || $res->status_line =~ /^403/;
72   $error_msg = $res->status_line;
73   return 0;
74
75 }
76
77 #and now for something entirely different...
78 my $num_consecutive_bop_failures = 60;
79 sub check_bop_failures {
80
81   return 1 if grep { $_->statustext eq 'captured' }
82                    qsearch({
83                      'table'    => 'cust_pay_pending',
84                      'hashref'  => { 'status' => 'done' },
85                      'order_by' => 'ORDER BY paypendingnum DESC'.
86                                    " LIMIT $num_consecutive_bop_failures",
87                    });
88   $error_msg = "Last $num_consecutive_bop_failures real-time payments failed";
89   return 0;
90 }
91
92 #
93
94 sub error_msg {
95   $error_msg;
96 }
97
98 sub alert {
99   my( $alert, @emails ) = @_;
100
101   my $conf = new FS::Conf;
102   my $smtpmachine = $conf->config('smtpmachine');
103   my $company_name = $conf->config('company_name');
104
105   foreach my $email (@emails) {
106     warn "warning $email about $alert\n" if $DEBUG;
107
108     my $message = <<"__MESSAGE__";
109 From: support\@freeside.biz
110 To: $email
111 Subject: FREESIDE ALERT for $company_name
112
113 FREESIDE ALERT: $alert
114
115 __MESSAGE__
116
117     my $sender = Email::Send->new({ mailer => 'SMTP' });
118     $sender->mailer_args([ Host => $smtpmachine ]);
119     $sender->send($message);
120
121   }
122
123 }
124
125 1;
126