#!/usr/bin/perl

require 5.004;
use strict;
use Socket;

#------------#
# Prototypes #
#------------#

sub send_query($$$$@);		# perform queries
sub udp_connect($$$$);		# connect to host using udp
sub print_results($);		# print reply packet
sub usage();			# program usage

#------#
# MAIN #
#------#
{
    my ($reply);
    my ($version) = 3;
    if ($ARGV[0] =~ /^-.*2/) {
	shift;
	$version = 2;
    }
    my (@services);
    if ($ARGV[0] =~ /^-s(.*)/) {
	shift;
	@services = split(',',$1);
    }
    my $port = 4330;
    if ($ARGV[0] =~ /^-p(.*)/) {
	shift;
	$port = $1;
    }
    foreach my $server (@ARGV) {
	next unless udp_connect(\*SH,$server,"lbcd",$port);
	next unless send_query(\*SH,\$reply,$version,10,@services) == 0;
	&print_results($reply);
    }
    exit(0);
}

#-------------#
# Subroutines #
#-------------#

sub send_query ($$$$@) {
    my($SH,$reply,$version,$timeout,@services) = @_;
    my($num_services);

    # struct lbcd_header
    # u_short   version;  /* protocol version */
    # u_short   id;       /* requestor's uniq request id */
    # u_short   op;       /* operation requested */
    # u_short   status;   /* set on reply */ (used for extended protocol)

    # Are we using the extended protocol?
    if ($version > 2) {
	$num_services = $#services + 1;
    }

    # Form register packet ("version operation operation status")
    my($template) = "nnnn" . " a32" x $num_services;
    my($packet) = pack($template,$version,0,1,$num_services,@services);

    # Send registration
    unless (send($SH,$packet,0)) {
	warn "send: $!\n";
	return 0;
    }

    # Obtain reply (with timeout)
    my($rin,$nfound) = ('',0);
    vec($rin,fileno($SH),1) = 1;
    if (($nfound = select($rin,undef,undef,$timeout)) != 1) {
	warn "timeout\n";
	return 1;		# timeout
    }
    $$reply = '';
    unless (recv($SH,$$reply,256,0)) {
	warn "recv: $!\n";
	return 1;
    }
    0;				# zero signifies no error
}


#---------------------#
# Auxilliary Routines #
#---------------------#
sub udp_connect ($$$$) {
    my($SH,$server,$port,$defaultport) = @_;

    my ($proto,$iaddr,$sin);

    $proto = getprotobyname('udp');
    unless (socket($SH, PF_INET, SOCK_DGRAM, $proto)) {
	return 0;
    }
    unless ($iaddr = gethostbyname($server)) {
	return 0;
    }
    $port = getservbyname($port, 'udp') || $defaultport;
    unless ($sin = sockaddr_in($port, $iaddr)) {
	return 0;
    }
    unless (connect($SH,$sin)) {
	close $SH; return undef;
    }
    1;
}


sub usage() {
    print STDOUT "lbcdclient hostname [hostname2 ... hostnameN]\n";
    exit;
}

sub print_results($) {
    my ($reply) = @_;

    # Response packet
    #  struct lbcd_header;
    #  u_int boot_time;
    #  u_int current_time;
    #  u_int user_mtime;  /* time user information last changed */
    #  u_short l1; /* (int) (load*100) */
    #  u_short l5;
    #  u_short l15;
    #  u_short tot_users;  /* total number of users logged in */
    #  u_short uniq_users; /* total number of uniq users */
    #  u_char  on_console; /* true if somone on console */
    #  u_char  reserved;   /* future use, padding ... */
    #  u_char  tmp_full;   /* percernt of tmp full */
    #  u_char  tmpdir_full;  /* percernt of P_tmpdir full */
    #  u_char pad;         /* padding */
    #  u_char services;    /* number of service requests */
    #  LBCD_SERVICE weights[LBCD_MAX_SERVICES+1];
    my ($version,$op,$id,$status,
	$btime,$ctime,$utime,
	$l1,$l5,$l15,$tot_user,$uniq_user,
	$on_console,$reserved,$tmp_full,
	$tmpdir_full,$pad,$services) =
	    unpack("nnnnNNNnnnnnCCCCCC",$reply);

    print "btime $btime ctime $ctime utime $utime\n";
    print "load l1 $l1 l5 $l5 l15 $l15\n";
    print "total $tot_user unique $uniq_user console $on_console\n";
    print "tmp full $tmp_full P_tmpdir full $tmpdir_full\n";

    # Extended package:
    # struct LBCD_SERVICE;
    # u_int host_weight;		/* computed host lb weight */
    # u_int host_incr;		/* computed host lb increment */
    if ($version > 2) {
	print "services $services pads $reserved $pad\n";
	$reply = substr($reply,36);
	for (my $i = 0; $i <= $services; $i++) {
	    my($weight,$incr) = unpack("NN",$reply);
	    print "service $i: weight $weight increment $incr\n";
	    $reply = substr($reply,8);
	}
    }
}
__END__

=for stopwords
lbcdclient lbcd btime tmp Schwimmer Allbery

=head1 NAME

lbcdclient - Query a remote lbcd daemon for system load

=head1 SYNOPSIS

lbcdclient [B<-2>] [B<-s> I<service>[,I<service> ...]] I<host> ...

=head1 DESCRIPTION

B<lbcdclient> sends a query packet to a remote B<lbcd> server and prints the
results.  The result output will look something like this:

    btime 1092858021 ctime 1092960298 utime 1092955199
    load l1 96 l5 69 l15 57
    total 0 unique 0 console 0
    tmp full 0 P_tmpdir full 2
    services 0 pads 0 0
    service 0: weight 1 increment 1

btime is the time of the last system boot in seconds since epoch.  ctime is
the current system time in seconds since epoch.  utime is the last
modification time of the information about logged in users in seconds since
epoch.

The load line gives the one-minute (l1), five-minute (l5), and
fifteen-minute (l15) load averages, multiplied by 100.

The next line gives the count of logged-in users.  total gives the count of
all logged-in users, unique lists the total number of unique users, and
console is 1 if a user is logged in to the console of the system.

tmp full is the percentage used in the system F</tmp> directory and P_tmpdir
full is the percentage full in the system F</var/tmp> directory.

Finally, the last lines give information for each service, using the
extended service response for the version three packet format.  The first
line gives the count of number of configured services minus one.  (The pads
numbers are reserved space in the packet and will generally always be zero.)
Then, each subsequent line lists the weights and increments for each service
B<lbcd> is monitoring on the system.

If the B<-2> option is used, B<lbcdclient> will send a version two packet
instead, and the returned results will not include the services line and
everything after that.

=head1 OPTIONS

=over 4

=item B<-2>

Send a version two protocol packet instead of a version three packet.
Version two doesn't support the separate service weights.

=item B<-s> I<service>,[I<service> ...]

Request information for the specified service names.  It's not entirely
clear whether this works correctly.

=back

=head1 AUTHORS

Written by Larry Schwimmer.  Currently maintained by Russ Allbery
<rra@stanford.edu>.

=head1 COPYRIGHT AND LICENSE

Copyright 2000, 2004, 2006, 2012, 2013 The Board of Trustees of the Leland
Stanford Junior University

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

=head1 SEE ALSO

lbcd(8)

The current version of this program is available from its web page at
L<http://www.eyrie.org/~eagle/software/lbcd/>.

=cut
