File: moncmd

package info (click to toggle)
mon 0.99.2-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 896 kB
  • ctags: 300
  • sloc: perl: 9,754; ansic: 778; sh: 372; makefile: 122
file content (253 lines) | stat: -rwxr-xr-x 4,998 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/usr/bin/perl
#
# moncmd - send a command to the mon server
#
# Jim Trocki, trockij@transmeta.com
#
# $Id: moncmd 1.3 Sun, 24 Jun 2001 22:41:40 -0400 trockij $
#
#    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 ("ahf:l:s:p:rd");

sub usage;
sub do_cmd;

$MONSERVER = "localhost";
$MONSERVER = $ENV{"MONHOST"}
    if (defined ($ENV{"MONHOST"}));
$MONSERVER = $opt_s if ($opt_s);
$MONPORT   = $opt_p || getservbyname ("mon", "tcp") || 2583;

if ($opt_h) {
    usage;
}

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

$SIG{INT} = \&handle_sig;
$SIG{TERM} = \&handle_sig;

#
# does the input come from stdin or a file?
#
if ($opt_f) {
    if ($opt_f eq "-") {
    	$H = STDIN;
    } else {
    	open (IN, $opt_f) ||
	    die "could not open input file: $!\n";
	$H = IN;
    }

} elsif (!@ARGV) {
    if (-t STDIN) {
      print <<EOF
You did not give a command on the command line nor a -f flag and
the program is running interactively (e.g. reading from terminal).
This is not supported.  Exiting
EOF
    ;
        exit 1;
    }

    $H = STDIN;
}

#
# get auth info
#
if ($opt_a) {
    if ($opt_l) {
    	$USER = $opt_l;
    } else {
	die "could not determine username\n"
	    unless defined ($USER = getpwuid($EUID));
    }

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

    } elsif (!@ARGV) {
	$cmd = <$H>;
	while (defined ($cmd) && $cmd =~ /user=|pass=/i) {
	    chomp $cmd;
	    if ($cmd =~ /^user=(\S+)$/i) {
		$USER=$1 if (!defined ($USER));
	    } elsif ($cmd =~ /^pass=(\S+)$/i) {
		$PASS=$1;
	    }
	    
	    $cmd = <$H>;
	}

    }

    die "inadequate authentication information supplied\n"
    	if ($USER eq "" || $PASS eq "");
}

#
# set up TCP socket
#
$iaddr = inet_aton ($MONSERVER) ||
	die "Unable to find server '$MONSERVER'\n";

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);

#if( defined(my $line = <MON>)) {
#    chomp $line;
#    unless( $line =~ /^220\s/) {
#	die "didn't receive expected welcome message\n";
#    }
#} else {
#    die "error communicating with mon server: $!\n";
#}

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


if ($opt_f or !@ARGV) {
    $cmd = <$H> if ($opt_f || !@ARGV);
    $l = "";
    while (defined ($cmd) && defined ($l)) {
	#
	# send the command
	#
	chomp $cmd;
	($l, @out) = do_cmd (MON, $cmd);
	last if (!defined ($l));
	for (@out) {
	    print "$_\n";
	}
	print "$l\n";

	$cmd = <$H>;
    }
    close ($H);

} else {
    ($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);

    return ("", undef) if ($cmd =~ /^\s*$/);

    @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] [-f file] 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
    exit 0;
}


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