the master control program has chosen YOU to serve your system on the game grid
[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   #stuff to add/replace later
21   #'pg_version'       => qr/^8\.[1-9]/,
22   #'apache_version'   => qr/^2/,
23   #'apache_mpm'       => qw/^Prefork/,
24 );
25
26 sub tron_scan {
27   my $cust_svc = shift;
28
29   my $svc_external;
30   if ( ref($cust_svc) ) {
31     $svc_external = $cust_svc->svc_x;
32   } else {
33     $svc_external = qsearchs('svc_external', { 'svcnum' => $cust_svc } );
34     $cust_svc = $svc_external->cust_svc;
35   }
36
37   #don't scan again if things are okay
38   my $bad = 0;
39   foreach my $option ( keys %desired ) {
40     my $current = $cust_svc->option($option);
41     $bad++ unless $current =~ $desired{$option};
42   }
43   return '' unless $bad;
44
45   #do the scan
46   my %hash = ();
47   my $machine = $svc_external->title; # or better as a cust_svc_option??
48   sshopen2($machine, *READER, *WRITER, '/usr/local/bin/freeside-yori all');
49   while (<READER>) {
50     chomp;
51     my($option, $value) = split(/: ?/);
52     next unless defined($option) && exists($desired{$option});
53     $hash{$option} = $value;
54   }
55   close READER;
56
57   unless ( keys %hash ) {
58     return "error scanning $machine\n";
59   }
60
61   # store the results
62   foreach my $option ( keys %hash ) {
63     my %opthash = ( 'optionname' => $option,
64                     'svcnum'     => $cust_svc->svcnum,
65                   );
66     my $cust_svc_option =  qsearchs('cust_svc_option', \%opthash )
67                           || new FS::cust_svc_option   \%opthash;
68     next if $cust_svc_option->optionvalue eq $hash{$option};
69     $cust_svc_option->optionvalue( $hash{$option} );
70     my $error = $cust_svc_option->optionnum
71                   ? $cust_svc_option->replace
72                   : $cust_svc_option->insert;
73     return $error if $error;
74   }
75   
76   '';
77
78 }
79
80 sub tron_lint {
81   my $cust_svc = shift;
82
83   my @lint;
84   foreach my $option ( keys %desired ) {
85     my $current = $cust_svc->option($option);
86     push @lint, "$option is $current" unless $current =~ $desired{$option};
87   }
88
89   @lint;
90
91 }
92
93 1;