#!/usr/bin/perl -w
#
# Copyright 2006 VMware, Inc.  All rights reserved.
#
# VMware ESX Server syslog server configuration tool

use strict;
use warnings;

use VMware::VIRuntime;
use VMware::VILib;
use VMware::VIExt;

my $SYSLOGD_HOST_KEY = "Syslog.Remote.Hostname";
my $SYSLOGD_PORT_KEY = "Syslog.Remote.Port";

my %opts = (
   vihost => {
      alias => "h",
      type => "=s",
      help => qq!  The host to use when connecting via a vCenter Server. !,
      required => 0,
   },
   'setserver' => {
      alias => "s",
      type => "=s",
      help => qq!  Sets the host name of the syslog server (only supported for ESXi). 
        Can be used with -p. (IPv6 address valid for vSphere 4.0 and later)!,
      required => 0,
   },
   'setport' => {
      alias => "p",
      type => "=i",
      help => qq!  Sets the port of the syslog server (only supported for ESXi). Can be used with -s.!,
      required => 0,
   },
   'show' => {
      alias => "i",
      type => "",
      help => qq!  Displays syslog server configuration. Should be supplied alone.!,
      required => 0,
   },
);

Opts::add_options(%opts);
Opts::parse();
Opts::validate();

my $setserver = Opts::get_option('setserver');
my $setport = Opts::get_option('setport');
my $show = Opts::get_option('show');

Util::connect();

my $host_view = VIExt::get_host_view(1, ['config.product.version', 'configManager.advancedOption']);
Opts::assert_usage(defined($host_view), "Invalid host.");

check_version();

my $adv_opt =
   Vim::get_view (mo_ref => $host_view->{'configManager.advancedOption'});

Opts::assert_usage(defined($setport) || defined($setserver) || defined($show),
   "At least one of '--setserver,--setport,--show' must be supplied.");

if (defined $show) {
   Opts::assert_usage(!defined($setport) && !defined($setserver),
      "--setserver or --setport should not be supplied with --show");
   my ($name1, $host) = VIExt::get_advoption($adv_opt, $SYSLOGD_HOST_KEY);
   my ($name2, $port) = VIExt::get_advoption($adv_opt, $SYSLOGD_PORT_KEY);
   if ($port && $host =~ /\S/) {
      print "Current remote syslog server settings:\n";
      print "Server name : ($host)\n";
      print "Server port : $port\n";
   } else {
      print "No remote syslog server configured.\n";
   }
} else {
   my $retlabel = $SYSLOGD_HOST_KEY;
   if (defined $setserver) {
      Opts::assert_usage(defined($setserver), "Must specify a value to set to.\n");
      $retlabel = VIExt::set_advoption($adv_opt, $SYSLOGD_HOST_KEY, $setserver);
      if (defined $retlabel) {
         print "Successfully set syslog host\n";
      } else {
         print "Failed to set syslog host\n";
      }
   }
   if (defined $setport) {
      Opts::assert_usage(defined($setport), "Must specify a value to set to.\n");
      Opts::assert_usage($setport > 0 && $setport <= 65535, 
         "Must port must be between 1 and 65535.\n");
      $retlabel = $SYSLOGD_PORT_KEY;
      $retlabel = VIExt::set_advoption($adv_opt, $SYSLOGD_PORT_KEY, $setport);
      if (defined $retlabel) {
         print "Successfully set syslog port\n";
      } else {
         print "Failed to set syslog port\n";
      }
   }
}

Util::disconnect();

sub check_version {
   my $host_version = $host_view->{'config.product.version'};
   if ($host_version ne 'e.x.p' && $host_version !~ /^4./ && $host_version !~ /^5./ && $host_version !~ /^6./) {
      VIExt::fail("ESX host version is $host_version. " .
                  "This operation is supported on ESX 4.x, ESXi 4.x, ESXi 5.x, ESXi 6.x or " .
                  "through VC 4.x, 5.x, 6.x");
   }
}

__END__

=head1 NAME

vicfg-syslog - get and set syslog server configuration

=head1 SYNOPSIS

 vicfg-syslog [<connection_options>]
   [--help |
    --setport <port> |
    --setserver <name_or_ip> |
    --show |
    --vihost <esx_host> ]

=head1 DESCRIPTION

The vicfg-syslog command specifies a remote syslog server for an ESXi host. 

You cannot run this command against an ESX host. ESX administrators can edit 
the syslog configuration file to customize the syslog server. 

The I<Basic System Administration> manual discusses system logs in more detail 
and explains how to set them up using the vSphere Client

=head1 OPTIONS

=over

=item B<connection_options>

Specifies the target server and authentication information if required. Run C<vicfg-syslog --help>
for a list of all connection options.


=item B<--help>

Prints a help message for each command-specific and each connection option. 
Calling the script with no arguments or with C<--help> has the same effect.


=item B<--setport | -p E<lt>portE<gt>>

Sets the port number for the syslog server. Can be used with C<--setserver>.


=item B<--setserver | -s E<lt>host_or_IPE<gt>>

Set the host name or IP address of the syslog server. Can be used with --setport>.


=item B<--show | -i>

Displays the syslog server configuration if a syslog server has been set up.


=item B<--vihost | -h E<lt>esx_hostE<gt>>

When you run a vSphere CLI command with the C<--server> option pointing to 
a vCenter Server system, use C<--vihost> to specify the ESX/ESXi host to run the command against. 

=back

=head1 EXAMPLES

The following examples assume you are specifying connection options, either 
explicitly or, for example, by specifying the server, user name, and password. 
Run C<vicfg-syslog --help> for a list of common options including connection options.

Display the syslog server configuration:

 vicfg-syslog <conn_options> -i

Set the host name of the syslog server:

 vicfg-syslog <conn_options> -s <syslog server>

Set the port used by the syslog server:

 vicfg-syslog <conn_options> -p <port>

