add unchecked vs. ok distinction to lint
[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   close WRITER;
57
58   unless ( keys %hash ) {
59     return "error scanning $machine\n";
60   }
61
62   # store the results
63   foreach my $option ( keys %hash ) {
64     my %opthash = ( 'optionname' => $option,
65                     'svcnum'     => $cust_svc->svcnum,
66                   );
67     my $cust_svc_option =  qsearchs('cust_svc_option', \%opthash )
68                           || new FS::cust_svc_option   \%opthash;
69     next if $cust_svc_option->optionvalue eq $hash{$option};
70     $cust_svc_option->optionvalue( $hash{$option} );
71     my $error = $cust_svc_option->optionnum
72                   ? $cust_svc_option->replace
73                   : $cust_svc_option->insert;
74     return $error if $error;
75   }
76   
77   '';
78
79 }
80
81 sub tron_lint {
82   my $cust_svc = shift;
83
84   my @lint;
85   foreach my $option ( keys %desired ) {
86     my $current = $cust_svc->option($option);
87     push @lint, "$option is $current" unless $current =~ $desired{$option};
88   }
89
90   push @lint, 'unchecked' unless scalar($cust_svc->options);
91
92   @lint;
93
94 }
95
96 1;