# -*- perl -*-
#
# $Revision: 1.3 $
package spamd;

require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(&print_usage &print_version &service_test);
use lib $main::runtimedir;
use csl_utils qw($TIMEOUT %ERRORS);

use strict;


sub print_usage ();
sub print_version ();
sub service_test (%);

my $badipok = 17;
my $warning = 9;
my $critical = 3;

sub print_usage () {

    print "
Usage:  Check the spamd service.
Options:
    -H <hostname> -P <portnumber> [--verbose]
    --help
    --version
    --usage
";
    return $ERRORS{'OK'};
}

sub print_version () {
    my $version = '$Revision: 1.3 $';
    $version =~ s/^\$Revision: //;
    $version =~ s/ \$\s*$//;

    print "afsd.pm - $version\n";
    return $ERRORS{'OK'};
}

# Basic script is:
# cat /s/spamassassin-csl/common/conf/gtube | /s/spamassassin/bin/spamc -f -d spam -p 49302
#
sub service_test (%) {
    my %options = @_;

    if ($options{"verbose"}) {
        foreach my $opt (sort keys(%options)) {
            if ($options{$opt}) {
                print "$opt - $options{$opt}\n";
            }
        }
    }

    my $host = $options{"opt_H"};

    if ((!$host) || ($host eq "")) {
        print "\nHost name not specified.\n";
        &print_usage();
        return $ERRORS{'OK'};
    }

    my $port = $options{"opt_P"};
    ($port) || ($port = 49302);

    my $file = "/s/spamassassin/conf/gtube";
    my $spamc = "/s/spamassassin/bin/spamc -f -d $host -p $port -c";

    my $message = "No data returned.";
    my $status = "CRITICAL";

    print "/bin/cat $file | $spamc\n" if $options{"verbose"};

    my $output = `/bin/cat $file | $spamc`;
    chomp $output;

    my ($value, $default) = split ("\/", $output);
    if ($default) {
        if ($value > $default) {
            $message = "Recognized as spam: $output";
            $status = "OK";
        } else {
            $message = "returned a value, but NOT spam: $output";
        }
    }

    print "$status: $message\n";
    print "Exit status: $status\n" if $options{"verbose"};

    exit $ERRORS{$status};
}

