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