#!/usr/bin/perl
#
# Query an lbcd server and display the results.
#
# This is a simple command-line client to query an lbcd server and return
# whatever information it provides.  It is intended for debugging and does not
# provide machine-parsable output.

##############################################################################
# Modules and declarations
##############################################################################

require 5.006;
use strict;
use warnings;

use Carp qw(croak);
use File::Basename qw(basename);
use Getopt::Long qw(GetOptions);
use IO::File;
use POSIX qw(strftime);

# lbcd protocol status codes mapped to error messages.
my %STATUS_MESSAGE = (
    0 => 'request packet',
    1 => 'okay',
    2 => 'generic error',
    3 => 'protocol version error',
    4 => 'generic protocol error',
    5 => 'unknown operation requested',
);

##############################################################################
# Network protocol
##############################################################################

# Create an IO::Socket object that will be used to query the server and return
# it.  Prefer IO::Socket::INET6 if it's available, but fall back on
# IO::Socket::INET.
#
# $hostname - The hostname to which the query will be sent
# $port     - The port on that server to which to send the query
# $timeout  - Timeout to use for all socket operations
#
# Returns: Newly-created IO::Socket object
#  Throws: Text exception on failure to create the socket
sub udp_socket {
    my ($hostname, $port, $timeout) = @_;

    # Check whether we have IO::Socket::INET6 available and choose the class.
    my $class;
    if (eval { require IO::Socket::INET6 }) {
        $class = 'IO::Socket::INET6';
    } else {
        require IO::Socket::INET;
        $class = 'IO::Socket::INET';
    }

    # Create and return the object.
    my @args = (
        PeerAddr => $hostname,
        PeerPort => $port,
        Proto    => 'udp',
        Timeout  => $timeout,
    );
    return $class->new(@args);
}

# Send the lbcd query to the server.  The request starts with a header:
#
#     uint16_t version;           /* Protocol version */
#     uint16_t id;                /* Requestor's unique request id */
#     uint16_t op;                /* Operation requested */
#     uint16_t status;            /* Number of services requested */
#
# and then (only for protocol v3 queries) a sequence of 32-character service
# names, padded with nul bytes.  The number sent is equal to the value of the
# status field.
#
# $socket   - IO::Socket object representing the connected UDP socket
# $protocol - Protocol version, which must be either 2 or 3
# @services - List of services to query for protocol v3 queries
#
# Returns: undef
#  Throws: Text exception on any failure to send the query
#          Text exception on invalid protocols or services
sub send_query {
    my ($socket, $protocol, @services) = @_;

    # Sanity-check the arguments.
    if ($protocol ne '2' && $protocol ne '3') {
        croak("protocol must be 2 or 3, not $protocol");
    }
    if ($protocol eq '2' && @services) {
        die "$0: lbcd protocol version 2 cannot query specific services\n";
    }
    if (@services > 5) {
        die "$0: lbcd protocol is limited to five specified services\n";
    }
    for my $service (@services) {
        if (length($service) > 31) {
            die "$0: lbcd protocol restricts service names to 31 octets\n";
        }
    }

    # Create the query packet.
    my $tmpl = 'nnnn' . 'a32' x scalar(@services);
    my $packet = pack($tmpl, $protocol, 0, 1, scalar(@services), @services);

    # Send the query packet.
    $socket->send($packet, 0) or die "$0: cannot send request packet: $!\n";
    return;
}

# Receive a reply packet from the server and parse the returned packet from
# the server into a key/value data structure.  The reply packet is:
#
#     uint16_t version;           /* Protocol version */
#     uint16_t id;                /* Requestor's unique request id */
#     uint16_t op;                /* Operation requested */
#     uint16_t status;            /* Ignored */
#     uint32_t boot_time;         /* Boot time */
#     uint32_t current_time;      /* Host time */
#     uint32_t user_mtime;        /* Time user information last changed */
#     uint16_t l1;                /* 1 minute load (int) (load*100) */
#     uint16_t l5;                /* 5 minute load */
#     uint16_t l15;               /* 15 minute load */
#     uint16_t tot_users;         /* Total number of users logged in */
#     uint16_t uniq_users;        /* Total number of uniq users */
#     uint8_t on_console;         /* True if somone on console */
#     uint8_t reserved;           /* Future use, padding ... */
#     uint8_t tmp_full;           /* Percent of tmp full */
#     uint8_t tmpdir_full;        /* Percent of P_tmpdir full */
#     uint8_t pad;                /* Padding */
#     uint8_t services;           /* Number of service requests */
#
# followed by some number (given by the services field) of data pairs in the
# format:
#
#     uint32_t host_weight;       /* Computed host lb weight */
#     uint32_t host_incr;         /* Computed host lb increment */
#
# A protocol version 2 reply will have an all-zero services field and no
# additional data.
#
# $socket  - IO::Socket object representing the connected UDP socket
# $timeout - Timeout for the reply in seconds
#
# Returns: A reference to a hash containing the above values, with id, op,
#          status, reserved, and padding removed and with the value of the
#          services hash key replaced with a list of pairs of weight and
#          increment
#  Throws: Text exception on error reading from the server
#          Text exception on error reply from the server
sub read_reply {
    my ($socket, $timeout) = @_;

    # Receive the reply from the server with timeout in the object.
    local $SIG{ALRM} = sub {
        die "$0: timed out waiting for reply from server\n";
    };
    alarm($timeout);
    my $reply;
    $socket->recv($reply, 256, 0)
      or die "$0: cannot receive reply from server: $!\n";
    alarm(0);

    # Unpack the header and check that the id, operation, and status are
    # correct.
    my ($version, $id, $op, $status) = unpack('n n n n', $reply);
    if ($id != 0) {
        die "$0: server reply for wrong query ID ($id)\n";
    }
    if ($op != 1) {
        die "$0: server reply for unknown operation ($op)\n";
    }
    if ($status != 1) {
        my $error = $STATUS_MESSAGE{$status} || "unknown status $status";
        die "$0: error from server: $error\n";
    }

    # Unpack the basic reply, without any extended data.  That will be
    # extracted later.
    $reply = substr($reply, 8);
    my @fields = unpack('N N N n n n n n C x C C x C', $reply);

    # Map this into our data hash.
    my @keys = qw(
      boot_time current_time user_mtime l1 l5 l15 tot_users uniq_users
      on_console tmp_full tmpdir_full services
    );
    my %result = (version => $version);
    for my $i (0 .. $#keys) {
        $result{ $keys[$i] } = $fields[$i];
    }

    # If version is 3, we may have supplemental service data.  If so, parse it
    # out and create the services data.  The supplemental data will start at
    # octet 36 of the reply.
    if ($result{version} == 3) {
        $reply = substr($reply, 28);
        my @weights;
        for my $i (0 .. $result{services}) {
            my ($weight, $incr) = unpack('NN', $reply);
            push(@weights, [$weight, $incr]);
            $reply = substr($reply, 8);
        }
        $result{services} = \@weights;
    }

    # Return the resulting hash.
    return \%result;
}

##############################################################################
# Output formatting
##############################################################################

# say with error checking to standard output.  autodie unfortunately can't
# help us with these because they can't be prototyped and hence can't be
# overridden.
#
# $fh   - Output file handle
# @args - Remaining arguments to say
#
# Returns: undef
#  Throws: Text exception on output failure
sub say_stdout {
    my (@args) = @_;
    say {*STDOUT} @args or croak('say failed');
    return;
}

# Likewise for printf.
sub printf_stdout {
    my ($format, @args) = @_;
    printf {*STDOUT} $format, @args or croak('printf failed');
    return;
}

# Given a hash of the results from the lbcd server, print out those results in
# a human-readable format to standard output.  This uses the same format as
# lbcd -t.
#
# $result_ref - Hash of results from the query
# @services   - List of services in our query, for the name information
#
# Returns: undef
#  Throws: Text exception on output failure
sub print_reply {
    my ($result_ref, @services) = @_;

    # Protocol version.
    say_stdout("PROTOCOL $result_ref->{version}\n");

    # Base system data.
    say_stdout('MACHINE STATUS:');
    for my $type (qw(l1 l5 l15)) {
        my $load = $result_ref->{$type} / 100.0;
        printf_stdout("%-12s = %.2f\n", $type, $load);
    }
    for my $type (qw(current_time boot_time user_mtime)) {
        my $time = $result_ref->{$type};
        my $date = strftime('%Y-%m-%d %T', localtime($time));
        printf_stdout("%-12s = %-12d (%s)\n", $type, $time, $date);
    }
    printf_stdout("%-12s = %d\n", 'tot_users',  $result_ref->{tot_users});
    printf_stdout("%-12s = %d\n", 'uniq_users', $result_ref->{uniq_users});
    printf_stdout("%-12s = %s\n", 'on_console',
        $result_ref->{on_console} ? 'true' : 'false');
    printf_stdout("%-12s = %d%%\n", 'tmp_full',    $result_ref->{tmp_full});
    printf_stdout("%-12s = %d%%\n", 'tmpdir_full', $result_ref->{tmpdir_full});

    # For protocol version two, print out the service information.
    if ($result_ref->{services}) {
        my @weights = @{ $result_ref->{services} };

        # Be a little fancy here: walk through the services, determine the
        # maximum length of the name, weight, and increment, and choose widths
        # accordingly.
        my ($service_width, $weight_width, $incr_width) = (0, 0, 0);
        for my $service (@services) {
            if (length($service) > $service_width) {
                $service_width = length($service);
            }
        }
        for my $data (@weights) {
            my ($weight, $incr) = @{$data};
            if (length($weight) > $weight_width) {
                $weight_width = length($weight);
            }
            if (length($incr) > $incr_width) {
                $incr_width = length($incr);
            }
        }
        my $format = "%-${service_width}s (%d):";
        $format .= " weight %${weight_width}d";
        $format .= ", increment %${incr_width}d\n";

        # Print out all of the weight data now that we know the format.
        say_stdout("\nSERVICES (" . scalar(@weights) . '):');
        for my $i (0 .. $#weights) {
            my ($weight, $incr) = @{ $weights[$i] };
            printf_stdout($format, $services[$i], $i, $weight, $incr);
        }
    }
    return;
}

##############################################################################
# Main routine
##############################################################################

# Always flush output.
STDOUT->autoflush;

# Clean up the script name for error reporting.
my $fullpath = $0;
local $0 = basename($0);

# Parse the argument list.
my ($manual, @services, $v2);
my $port    = 4330;
my $timeout = 10;
Getopt::Long::config('bundling', 'no_ignore_case');
GetOptions(
    'manual|man|m' => \$manual,
    'port|p=i'     => \$port,
    'services|s=s' => \@services,
    'timeout|t=i'  => \$timeout,
    'v2|2'         => \$v2,
);
if ($manual) {
    say_stdout('Feeding myself to perldoc, please wait...');
    exec('perldoc', '-t', $fullpath);
}
if (@ARGV != 1) {
    die "Usage: lbcdclient [-2] [-p <port>] [-s <service>] <host>\n";
}
my $protocol = $v2 ? 2 : 3;
my ($host) = @ARGV;

# Allow for comma-separated services as well as multiple -s options.
@services = map { split(m{,}xms) } @services;

# Send the query and print the results.
my $socket = udp_socket($host, $port, $timeout);
send_query($socket, $protocol, @services);
my $reply_ref = read_reply($socket, $timeout);
print_reply($reply_ref, 'default', @services);
exit(0);

__END__

##############################################################################
# Documentation
##############################################################################

=for stopwords
lbcdclient lbcd Schwimmer Allbery MERCHANTABILITY NONINFRINGEMENT sublicense
uptime

=head1 NAME

lbcdclient - Query a remote lbcd daemon for system load

=head1 SYNOPSIS

lbcdclient [B<-2>] [B<-p> I<port>] [B<-s> I<service>[,I<service> ...]]
    [B<-t> I<timeout>] 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:

    PROTOCOL 3

    MACHINE STATUS:
    l1           = 16           (0.16)
    l5           = 5            (0.05)
    l15          = 6            (0.06)
    current_time = 1387677198   (2013-12-21 17:53:18)
    boot_time    = 1386357534   (2013-12-06 11:18:54)
    user_mtime   = 1387250916   (2013-12-16 19:28:36)
    tot_users    = 12
    uniq_users   = 1
    on_console   = false
    tmp_full     = 8%
    tmpdir_full  = 8%

    SERVICES (1):
    default (0): weight 368, increment 200

C<l1>, C<l5>, and C<l15> are the one-minute, five-minute, and
fifteen-minute load averages, times 100, as integers.  The conventional
load averages, as seen with B<uptime>, are shown on the right.

C<current_time> is the current system time in seconds since epoch.
C<boot_time> is the time of the last system boot.  C<user_mtime> is the
time information about logged-in users was last modified.  A translation
into a date and time is given on the right.

C<tot_users> is the total number of logged-in users, and C<uniq_users> is
the number of unique logged-in users.  C<on_console> will be C<true> if a
user is logged into the console and C<false> otherwise.

C<tmp_full> is the percentage of space used in the system F</tmp>
directory and C<tmpdir_full> full is the percentage used in the system
F</var/tmp> directory.

Finally, for protocol version three queries (the default), the last lines
give information for each service queried, using the extended service
response for the version three packet format.  For each service, its name,
sequence number in the reply, current weight, and current increment are
given.

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

=head1 OPTIONS

=over 4

=item B<--v2>, 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<-m>, B<--man>, B<--manual>

Print out this documentation (which is done simply by feeding the script
to C<perldoc -t>).

=item B<--port>=I<port>, B<-p> I<port>

Send the query to I<port> instead of the default of 4330.

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

Request information for the specified service names.  This option can be
given multiple times to request information about multiple services.  The
services can also be specified as a comma-separated list, or a combination
of multiple options and comma-separated lists.

=item B<--timeout>=I<timeout>, B<-t> I<timeout>

Use a timeout of I<timeout> seconds instead of the default of 10 seconds.

=back

=head1 EXAMPLES

Send a version three query to www.example.com and print the results:

    lbcdclient www.example.com

Send a version two query to foo.example.org on port 14330, asking for
information about the C<smtp> and C<http> services in addition to the
default service, with a timeout of five seconds.

    lbcdclient -2 -p 14330 -t 5 -s smtp -s http foo.example.org

=head1 AUTHORS

Written by Russ Allbery <eagle@eyrie.org> based on an earlier version
by Larry Schwimmer.

=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
