oops, hopefully fix sg cron check, RT#4610
[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 FS::Conf;
10 use FS::Record qw(qsearch);
11 use FS::cust_pay_pending;
12
13 @ISA = qw( Exporter );
14 @EXPORT_OK = qw(
15   check_queued check_selfservice check_apache check_bop_failures
16   check_sg check_sgng
17   alert error_msg
18 );
19
20 $DEBUG = 0;
21
22 $FS_RUN = '/var/run';
23
24 sub check_queued {
25   _check_fsproc('queued');
26 }
27
28 $SELFSERVICE_USER = '%%%SELFSERVICE_USER%%%';
29
30 $SELFSERVICE_MACHINES = '%%%SELFSERVICE_MACHINES%%%'; #substituted by Makefile
31 $SELFSERVICE_MACHINES =~ s/^\s+//;
32 $SELFSERVICE_MACHINES =~ s/\s+$//;
33 @SELFSERVICE_MACHINES = split(/\s+/, $SELFSERVICE_MACHINES);
34 @SELFSERVICE_MACHINES = ()
35   if scalar(@SELFSERVICE_MACHINES) == 1
36   && $SELFSERVICE_MACHINES[0] eq '%%%'.'SELFSERVICE_MACHINES'.'%%%';
37
38 sub check_selfservice {
39   foreach my $machine ( @SELFSERVICE_MACHINES ) {
40     unless ( _check_fsproc("selfservice-server.$SELFSERVICE_USER.$machine") ) {
41       $error_msg = "Self-service daemon not running for $machine";
42       return 0;
43     }
44   }
45   return 1;
46 }
47
48 sub check_sg {
49   my $conf = new FS::Conf;
50   #different trigger if they ever stop using multicustomer_hack ?
51   return 1 unless $conf->exists('sg-multicustomer_hack');
52
53   my $ua = new LWP::UserAgent;
54   $ua->agent("FreesideCronCheck/0.1 " . $ua->agent);
55
56   my $USER = $conf->config('sg-ping_username');
57   my $PASS = $conf->config('sg-ping_password');
58   my $req = new HTTP::Request GET=>"https://$USER:$PASS\@localhost/sg/ping.cgi";
59   my $res = $ua->request($req);
60
61   return 1 if $res->is_success
62            && $res->content =~ /OK/;
63
64   $error_msg = $res->is_success ? $res->content : $res->status_line;
65   return 0;
66
67 }
68
69 sub check_sgng {
70   my $conf = new FS::Conf;
71   #different trigger if they ever stop using multicustomer_hack ?
72   return 1 unless $conf->exists('sg-multicustomer_hack');
73
74   eval 'use RPC::XML; use RPC::XML::Client;';
75   if ($@) { $error_msg = $@; return 0; };
76
77   my $cli = RPC::XML::Client->new('https://localhost/selfservice/xmlrpc.cgi');
78   my $resp = $cli->send_request('FS.SelfService.XMLRPC.ping');
79
80   return 1 if ref($resp)
81            && ! $resp->is_fault
82            && ref($resp->value)
83            && $resp->value->{'pong'} == 1;
84
85   #hua
86   $error_msg = ref($resp)
87                  ? ( $resp->is_fault
88                        ? $resp->string
89                        : ( ref($resp->value) ? $resp->value->{'error'}
90                                              : $resp->value
91                          )
92                  )
93                  : $resp;
94   return 0;
95 }
96
97 sub _check_fsproc {
98   my $arg = shift;
99   _check_pidfile( "freeside-$arg.pid" );
100 }
101
102 sub _check_pidfile {
103   my $pidfile = shift;
104   open(PID, "$FS_RUN/$pidfile") or return 0;
105   chomp( my $pid = scalar(<PID>) );
106   close PID; # or return 0;
107
108   $pid && kill 0, $pid;
109 }
110
111 sub check_apache {
112   my $ua = new LWP::UserAgent;
113   $ua->agent("FreesideCronCheck/0.1 " . $ua->agent);
114
115   my $req = new HTTP::Request GET => 'https://localhost/';
116   my $res = $ua->request($req);
117
118   return 1 if $res->is_success || $res->status_line =~ /^403/;
119   $error_msg = $res->status_line;
120   return 0;
121
122 }
123
124 #and now for something entirely different...
125 my $num_consecutive_bop_failures = 50;
126 sub check_bop_failures {
127
128   return 1 if grep { $_->statustext eq 'captured' }
129                    qsearch({
130                      'table'    => 'cust_pay_pending',
131                      'hashref'  => { 'status' => 'done' },
132                      'order_by' => 'ORDER BY paypendingnum DESC'.
133                                    " LIMIT $num_consecutive_bop_failures",
134                    });
135   $error_msg = "Last $num_consecutive_bop_failures real-time payments failed";
136   return 0;
137 }
138
139 #
140
141 sub error_msg {
142   $error_msg;
143 }
144
145 sub alert {
146   my( $alert, @emails ) = @_;
147
148   my $conf = new FS::Conf;
149   my $smtpmachine = $conf->config('smtpmachine');
150   my $company_name = $conf->config('company_name');
151
152   foreach my $email (@emails) {
153     warn "warning $email about $alert\n" if $DEBUG;
154
155     my $message = <<"__MESSAGE__";
156 From: support\@freeside.biz
157 To: $email
158 Subject: FREESIDE ALERT for $company_name
159
160 FREESIDE ALERT: $alert
161
162 __MESSAGE__
163
164     my $sender = Email::Send->new({ mailer => 'SMTP' });
165     $sender->mailer_args([ Host => $smtpmachine ]);
166     $sender->send($message);
167
168   }
169
170 }
171
172 1;
173