#!/usr/bin/perl
#
# moncmd - send a command to the mon server
#
# Jim Trocki, trockij@transmeta.com
#
# $Id: moncmd,v 1.19 1998/07/13 11:41:09 trockij Exp $
#
#    Copyright (C) 1998, Jim Trocki
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
use Getopt::Std;
use Socket;
use English;

getopts ("ahl:s:p:rd");

sub usage;
sub do_cmd;

$MONSERVER = $ENV{"MONHOST"}
    if (defined ($ENV{"MONHOST"}));

$MONSERVER = $opt_s if ($opt_s);

$MONPORT   = $opt_p || 32777;

if ($opt_d) {
    $MONSERVER = "localhost";
}

if (!defined ($MONSERVER)) {
    die "No host specified or found in MONHOST\n";
}

if (!@ARGV || $opt_h) {
    usage;
    exit 1;
}

#
# get auth info
#
$SIG{IN} = \&handle_sig;
$SIG{TERM} = \&handle_sig;

if ($opt_a) {
    if ($opt_l) {
    	$USER = $opt_l;
    } else {
	die "could not determine username\n"
	    if (!defined ($USER = getpwuid($EUID)));
    }

    system "stty -echo";
    print "Password: ";
    chop ($PASS = <STDIN>);
    print "\n";
    system "stty echo";
    die "invalid password\n" if ($PASS =~ /^\s*$/);
}

#
# set up TCP socket
#
$iaddr = inet_aton ($MONSERVER);
if ($MONPORT =~ /\D/) { $MONPORT = getservbyname ($MONPORT, 'tcp') }
$paddr = sockaddr_in ($MONPORT, $iaddr);
$proto = getprotobyname ('tcp');

socket (MON, PF_INET, SOCK_STREAM, $proto) ||
    die "could not create socket: $!\n";
connect (MON, $paddr) ||
    die "could not connect: $!\n";

select (MON); $| = 1; select (STDOUT);

#
# authenticate if necessary
#
if ($opt_a) {
    ($l, @out) = do_cmd(MON, "login $USER $PASS");
    die "Could not authenticate\n"
	if ($l =~ /^530/);
}

#
# send the command
#
($l, @out) = do_cmd (MON, "@ARGV");
for (@out) {
    print "$_\n";
}
print "$l\n";

#
# log out
#
do_cmd (MON, "quit");

close(MON);


#
# submit a command to the server, wait for a response
#
sub do_cmd {
    my ($fd, $cmd) = @_;
    my ($l, @out);

    @out = ();
    print $fd "$cmd\n";
    while (defined($l = <$fd>)) {
        chomp $l;
        if ($l =~ /^(\d{3}\s)/) {
            last;
        }
        push (@out, $l);
    }

    ($l, @out);
}


#
# usage
#
sub usage {
    print <<EOF;

usage: moncmd [-a] [-l login] [-s host] [-p port] commands

Valid commands are:
    quit
    reset [stopped]
    term
    list group "groupname"
    list disabled
    list alerthist
    list failurehist
    list successes
    list failures
    list opstatus
    list pids
    list watch
    stop
    start
    loadstate
    savestate
    set "group" "service" "variable" "value"
    get "group" "service" "variable"
    disable service "group" "service"
    disable host "host" ["host"...]
    disable watch "watch"
    enable service "group" "service"
    enable host "host" ["host"...]
    enable watch "watch"
EOF
}


#
# signal handler
#
sub handle_sig {
    system "stty echo";
    exit;
}
