|
47 | 47 | exit 1;
|
48 | 48 | }
|
49 | 49 |
|
50 |
| -my $script_version="0.0.10"; |
| 50 | +my $script_version="0.0.11"; |
51 | 51 | my $script_name="postgresqltuner.pl";
|
52 | 52 | my $min_s=60;
|
53 | 53 | my $hour_s=60*$min_s;
|
54 | 54 | my $day_s=24*$hour_s;
|
55 | 55 | my $os_cmd_prefix='';
|
56 | 56 |
|
57 |
| -my $host='/var/run/postgresql'; |
58 |
| -my $username='postgres'; |
59 |
| -my $password=''; |
60 |
| -my $database="template1"; |
61 |
| -my $port=5432; |
| 57 | +my $host=undef; |
| 58 | +my $username=undef; |
| 59 | +my $password=undef; |
| 60 | +my $database=undef; |
| 61 | +my $port=undef; |
| 62 | +my $pgpassfile=$ENV{HOME}.'/.pgpass'; |
62 | 63 | my $help=0;
|
63 | 64 | my $work_mem_per_connection_percent=150;
|
64 | 65 | GetOptions (
|
65 | 66 | "host=s" => \$host,
|
66 | 67 | "user=s" => \$username,
|
67 | 68 | "username=s" => \$username,
|
68 |
| - "pass=s" => \$password, |
69 |
| - "password=s" => \$password, |
| 69 | + "pass:s" => \$password, |
| 70 | + "password:s" => \$password, |
70 | 71 | "db=s" => \$database,
|
71 | 72 | "database=s" => \$database,
|
72 | 73 | "port=i" => \$port,
|
|
79 | 80 | usage(0);
|
80 | 81 | }
|
81 | 82 |
|
| 83 | +# host |
| 84 | +if (!defined($host)) { |
| 85 | + if (defined($ENV{PGHOST})) { |
| 86 | + $host=$ENV{PGHOST}; |
| 87 | + } else { |
| 88 | + $host='/var/run/postgresql'; |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +# port |
| 93 | +if (!defined($port)) { |
| 94 | + if (defined($ENV{PGPORT})) { |
| 95 | + $port=$ENV{PGPORT}; |
| 96 | + } else { |
| 97 | + $port=5432; |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +# database |
| 102 | +if (!defined($database)) { |
| 103 | + if (defined($ENV{PGDATABASE})) { |
| 104 | + $database=$ENV{PGDATABASE}; |
| 105 | + } else { |
| 106 | + $database='template1'; |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +# user |
| 111 | +if (!defined($username)) { |
| 112 | + if (defined($ENV{PGUSER})) { |
| 113 | + $username=$ENV{PGUSER}; |
| 114 | + } else { |
| 115 | + $username='postgres'; |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +# if needed, get password from ~/.pgpass |
| 120 | +if (!defined($password)) { |
| 121 | + if (defined($ENV{PGPASSWORD})) { |
| 122 | + $password=$ENV{PGPASSWORD}; |
| 123 | + } else { |
| 124 | + if (defined($ENV{PGPASSFILE})) { |
| 125 | + $pgpassfile=$ENV{PGPASSFILE}; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + if (open(PGPASS,'<',$pgpassfile)) { |
| 130 | + while (my $line=<PGPASS>) { |
| 131 | + chomp($line); |
| 132 | + next if $line =~ /^\s*#/; |
| 133 | + my ($pgp_host,$pgp_port,$pgp_database,$pgp_username,$pgp_password,$pgp_more)=split(/(?<!\\):/,$line); # split except after escape char |
| 134 | + next if (!defined($pgp_password) or defined($pgp_more)); # skip malformated line |
| 135 | + next if (!pgpass_match('host',$host,$pgp_host)); |
| 136 | + next if (!pgpass_match('port',$port,$pgp_port)); |
| 137 | + next if (!pgpass_match('database',$database,$pgp_database)); |
| 138 | + next if (!pgpass_match('username',$username,$pgp_username)); |
| 139 | + $password=pgpass_unescape($pgp_password); |
| 140 | + last; |
| 141 | + } |
| 142 | + close(PGPASS); |
| 143 | + } |
| 144 | +} |
| 145 | + |
82 | 146 | usage(1) if (!defined($host) or !defined($username) or !defined($password));
|
83 | 147 |
|
84 | 148 | sub usage {
|
85 | 149 | my $return=shift;
|
86 | 150 | print STDERR "usage: $script_name --host [ hostname | /var/run/postgresql ] [--user username] [--password password] [--database database] [--port port] [--wmp 150]\n";
|
87 |
| - print STDERR "\twmp: average number of work_mem buffers per connection in percent (default 150)\n"; |
| 151 | + print STDERR "If available connection informations can be read from \$PGHOST, \$PGPORT, \$PGDATABASE, \$PGUSER, \$PGPASSWORD\n"; |
| 152 | + print STDERR "For security reasons, prefer usage of password in ~/.pgpass\n"; |
| 153 | + print STDERR "\thost:port:database:username:password\n"; |
| 154 | + print STDERR " --wmp: average number of work_mem buffers per connection in percent (default 150)\n"; |
88 | 155 | exit $return;
|
89 | 156 | }
|
90 | 157 |
|
@@ -887,3 +954,18 @@ sub print_advices {
|
887 | 954 | print color("green")."Everything is good".color("reset")."\n";
|
888 | 955 | }
|
889 | 956 | }
|
| 957 | + |
| 958 | +sub pgpass_match { |
| 959 | + my ($type,$var,$pgp_var)=@_; |
| 960 | + $pgp_var=pgpass_unescape($pgp_var); |
| 961 | + return 1 if $pgp_var eq '*'; |
| 962 | + return 1 if $pgp_var eq $var; |
| 963 | + return 1 if $type eq 'host' and $pgp_var eq 'localhost' and $var=~m/^\//; # allow sockets if host=localhost |
| 964 | + return 0; |
| 965 | +} |
| 966 | + |
| 967 | +sub pgpass_unescape { |
| 968 | + my ($value)=@_; |
| 969 | + $value=~s/\\(.)/$1/g; |
| 970 | + return $value; |
| 971 | +} |
0 commit comments