Skip to content

Commit

Permalink
add SSH options (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfcoz authored Dec 21, 2017
1 parent 12c250f commit 8e85348
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,20 @@ docker run -it --rm --link your-postgresql-container:dbhost jfcoz/postgresqltune

When using it remotly, postgresqltuner.pl will use ssh to collect OS informations. You must configure ssh to connect to remote host with private key authentication.

You can add options to ssh via two ways :

- Option :
```
--sshopt=Port=2200 --sshopt=IdentityFile=...
```

- Configure your SSH client via ~/.ssh/config :
```
Host my-database-host
IdentityFile=...
Port=2200
```

### Passwords

For better security use a `~/.pgpass` file containing passwords, so password will not be saved in the shell history nor in the process list. [.pgpass documentation](https://www.postgresql.org/docs/current/static/libpq-pgpass.html)
Expand Down
41 changes: 38 additions & 3 deletions postgresqltuner.pl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
exit 1;
}

my $script_version="0.0.11";
my $script_version="0.0.12";
my $script_name="postgresqltuner.pl";
my $min_s=60;
my $hour_s=60*$min_s;
Expand All @@ -62,6 +62,7 @@
my $pgpassfile=$ENV{HOME}.'/.pgpass';
my $help=0;
my $work_mem_per_connection_percent=150;
my @Ssh_opts=('BatchMode=yes');
GetOptions (
"host=s" => \$host,
"user=s" => \$username,
Expand All @@ -73,13 +74,20 @@
"port=i" => \$port,
"help" => \$help,
"wmp=i" => \$work_mem_per_connection_percent,
"sshopt=s" => \@Ssh_opts
) or usage(1);

print "$script_name version $script_version\n";
if ($help) {
usage(0);
}

# ssh options
my $ssh_opts='';
foreach my $ssh_opt (@Ssh_opts) {
$ssh_opts.=' -o '.$ssh_opt;
}

# host
if (!defined($host)) {
if (defined($ENV{PGHOST})) {
Expand Down Expand Up @@ -141,17 +149,44 @@
}
close(PGPASS);
}

# default
if (!defined($password)) {
$password='';
}
}

if (!defined($host)) {
print STDERR "Missing host\n";
print STDERR "\tset \$PGHOST environnement variable\n";
print STDERR "or\tadd --host option\n";
usage(1);
}

if (!defined($username)) {
print STDERR "Missing username\n";
print STDERR "\tset \$PGUSER environnement variable\n";
print STDERR "or\tadd --user option\n";
usage(1);
}

usage(1) if (!defined($host) or !defined($username) or !defined($password));
if (!defined($password)) {
print STDERR "Missing password\n";
print STDERR "\tconfigure ~/.pgpass\n";
print STDERR "or\tset \$PGPASSWORD environnement variable\n";
print STDERR "or\tadd --password option\n";
usage(1);
}

sub usage {
my $return=shift;
print STDERR "usage: $script_name --host [ hostname | /var/run/postgresql ] [--user username] [--password password] [--database database] [--port port] [--wmp 150]\n";
print STDERR "\t[--sshopt=Name=Value]...\n";
print STDERR "If available connection informations can be read from \$PGHOST, \$PGPORT, \$PGDATABASE, \$PGUSER, \$PGPASSWORD\n";
print STDERR "For security reasons, prefer usage of password in ~/.pgpass\n";
print STDERR "\thost:port:database:username:password\n";
print STDERR " --wmp: average number of work_mem buffers per connection in percent (default 150)\n";
print STDERR " --sshopt: pass options to ssh (example --sshopt=Port=2200)\n";
exit $return;
}

Expand Down Expand Up @@ -189,7 +224,7 @@ sub usage {
} elsif ($host =~ /^127\.[0-9]+\.[0-9]+\.[0-9]+$/) {
$os_cmd_prefix='';
} elsif ($host =~ /^[a-zA-Z0-9.-]+$/) {
$os_cmd_prefix="ssh -o BatchMode=yes $host ";
$os_cmd_prefix="ssh $ssh_opts $host ";
} else {
die("Invalid host $host");
}
Expand Down

0 comments on commit 8e85348

Please sign in to comment.