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