Skip to content
Snippets Groups Projects
getClients.pl 4.23 KiB
Newer Older
Tomáš Plesník's avatar
Tomáš Plesník committed
#!/usr/bin/perl -w
#
# getClients.pl
#
# Copyright (C) 2011-2013 Cesnet z.s.p.o
# Use of this source is governed by a BSD-style license, see LICENSE file.
Tomáš Plesník's avatar
Tomáš Plesník committed

use strict;
use Getopt::Std;
use File::Basename;
use DBI;
use DBD::mysql;
Tomáš Plesník's avatar
Tomáš Plesník committed

################################################################################
#				GLOBAL VARIABLES
################################################################################
our $VERSION = "2.2";
Tomáš Plesník's avatar
Tomáš Plesník committed
my $filename = basename($0);



################################################################################
Tomáš Plesník's avatar
Tomáš Plesník committed
#                               Functions
################################################################################
Tomáš Plesník's avatar
Tomáš Plesník committed
sub usage {
  print "Usage: $filename [without parameters]\n";
  exit 1;
}


#-------------------------------------------------------------------------------
# errMsg - print error message and die
#-------------------------------------------------------------------------------
sub errMsg
{
  my $msg = shift;
  $msg = trim($msg);
  print $msg . "\n";
  exit 1;
} # End of errMsg


#-------------------------------------------------------------------------------
# trim - remove whitespace from the start and end of the string
#-------------------------------------------------------------------------------
sub trim
{
  my $string = shift;
  $string =~ s/^\s+//;
  $string =~ s/\s+$//;
  return $string;
} # End of trim



################################################################################
Tomáš Plesník's avatar
Tomáš Plesník committed
#                               MAIN
################################################################################
Tomáš Plesník's avatar
Tomáš Plesník committed
our ($opt_h);

die usage unless getopts("h");
my $help = $opt_h;

# catch help param
if ($help) {
  usage;
}

# superuser controle
my $UID = $<;
if ($UID != 0) {
  die errMsg("You must be root for running this script!")
}

# read config file
my $conf_file = "/opt/warden-server/etc/warden-server.conf"; # path is updated by install.sh
our $DB_NAME            = undef;
our $DB_USER            = undef;
our $DB_PASS            = undef;
our $DB_HOST            = undef;
unless (do $conf_file) {
  die("Errors in config file '$conf_file': $@") if $@;
  die("Can't read config file '$conf_file': $!") unless defined $_;
  # if $_ defined, it's retvalue of last statement of conf, for which we don't care
}

# connect to DB
our $DBH = DBI->connect("DBI:mysql:database=$DB_NAME;host=$DB_HOST", $DB_USER, $DB_PASS, {RaiseError => 1, mysql_auto_reconnect => 1}) || die "Could not connect to database: $DBH->errstr";

# obtain data from DB
my $sth = $DBH->prepare("SELECT * FROM clients ORDER BY client_id ASC;") or die "Cannot prepare statement: " . $DBH->errstr;
$sth->execute or die "Cannot execute statement: " . $sth->errstr;
my @clients = $sth->fetchall_arrayref();
# print table of clients
print "+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n";
print "| Client ID  | Hostname                       | Registered          | Requestor               | Service                   | CT | Type            | ROE  | Description tags                                   | IP Net Client      |\n";
print "+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n";
Tomáš Plesník's avatar
Tomáš Plesník committed
foreach (@clients) {
  my @client = @$_;
  foreach (@client) {
    printf("| %-10s ", @$_[0] || "unknown");
    printf("| %-30s ", @$_[1] || "unknown");
    printf("| %19s ", @$_[2]  || "unknown");
    printf("| %-23s ", @$_[3] || "unknown");
    printf("| %-25s ", @$_[4] || "-");
    printf("| %-2s ", @$_[5]  || "unknown");
    printf("| %-15s ", @$_[6] || "-");
    printf("| %-4s ", @$_[7]  || "-");
    printf("| %-50s ", @$_[8] || "-");
    printf("| %-18s |\n", @$_[9] || "unknown");
  }
print "+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n";
Tomáš Plesník's avatar
Tomáš Plesník committed
print "\n";
print "Current registered clients in: " . scalar localtime(time) . "\n";

exit 0;