payment gateway survey
[freeside.git] / FS / FS / Tron.pm
1 package FS::Tron;
2 # a program to monitor outside systems
3
4 use strict;
5 use warnings;
6 use base 'Exporter';
7 use Net::SSH qw( sshopen2 ); #sshopen3 );
8 use FS::Record qw( qsearchs );
9 use FS::svc_external;
10 use FS::cust_svc_option;
11
12 our @EXPORT_OK = qw( tron_scan tron_lint);
13
14 our %desired = (
15   #lenient for now, so we can fix up important stuff
16   'freeside_version' => qr/^1\.(7\.3|9\.0)/,
17   'debian_version'   => qr/^4/,
18   'apache_mpm'       => qw/^(Prefork|$)/,
19
20   #payment gateway survey
21   'payment_gateway'  => qw/^authorizenet$/,
22
23   #stuff to add/replace later
24   #'pg_version'       => qr/^8\.[1-9]/,
25   #'apache_version'   => qr/^2/,
26   #'apache_mpm'       => qw/^Prefork/,
27 );
28
29 sub tron_scan {
30   my $cust_svc = shift;
31
32   my $svc_external;
33   if ( ref($cust_svc) ) {
34     $svc_external = $cust_svc->svc_x;
35   } else {
36     $svc_external = qsearchs('svc_external', { 'svcnum' => $cust_svc } );
37     $cust_svc = $svc_external->cust_svc;
38   }
39
40   #don't scan again if things are okay
41   my $bad = 0;
42   foreach my $option ( keys %desired ) {
43     my $current = $cust_svc->option($option);
44     $bad++ unless $current =~ $desired{$option};
45   }
46   return '' unless $bad;
47
48   #do the scan
49   my %hash = ();
50   my $machine = $svc_external->title; # or better as a cust_svc_option??
51   sshopen2($machine, *READER, *WRITER, '/usr/local/bin/freeside-yori all');
52   while (<READER>) {
53     chomp;
54     my($option, $value) = split(/: ?/);
55     next unless defined($option) && exists($desired{$option});
56     $hash{$option} = $value;
57   }
58   close READER;
59   close WRITER;
60
61   unless ( keys %hash ) {
62     return "error scanning $machine\n";
63   }
64
65   # store the results
66   foreach my $option ( keys %hash ) {
67     my %opthash = ( 'optionname' => $option,
68                     'svcnum'     => $cust_svc->svcnum,
69                   );
70     my $cust_svc_option =  qsearchs('cust_svc_option', \%opthash )
71                           || new FS::cust_svc_option   \%opthash;
72     next if $cust_svc_option->optionvalue eq $hash{$option};
73     $cust_svc_option->optionvalue( $hash{$option} );
74     my $error = $cust_svc_option->optionnum
75                   ? $cust_svc_option->replace
76                   : $cust_svc_option->insert;
77     return $error if $error;
78   }
79   
80   '';
81
82 }
83
84 sub tron_lint {
85   my $cust_svc = shift;
86
87   my @lint;
88   foreach my $option ( keys %desired ) {
89     my $current = $cust_svc->option($option);
90     push @lint, "$option is $current" unless $current =~ $desired{$option};
91   }
92
93   push @lint, 'unchecked' unless scalar($cust_svc->options);
94
95   @lint;
96
97 }
98
99 1;