Debian Codegraph @

pg_lsclusters

raw
1 #!/usr/bin/perl -wT
2 # Show all PostgreSQL clusters in a list
3 #
4 # (C) 2005-2009 Martin Pitt <[email protected]>
5 # (C) 2013-2018 Christoph Berg <[email protected]>
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16
17 use strict;
18 use PgCommon;
19 use Getopt::Long;
20
21 sub help ($)
22 {
23 my $exit = shift;
24 print "Usage: $0 [-hjs]
25
26 Options:
27 -h --no-header Omit column headers in output
28 -j --json JSON output
29 -s --start-conf Include start.conf information in status column
30 --help Print help\n";
31
32 exit $exit;
33 }
34
35 # option handling
36 my $no_header;
37 my $json;
38 my $start_conf;
39 help(1) unless GetOptions (
40 'help' => sub { help(0); },
41 'h|no-header' => \$no_header,
42 'j|json' => \$json,
43 's|start-conf' => \$start_conf,
44 );
45
46 my (@versions, $ls_cluster);
47 if (@ARGV == 1 and $ARGV[0] =~ m{^(\d+(?:\.\d+)?)[-/]([-.\w]+)$}) {
48 push @versions, $1;
49 $ls_cluster = $2;
50 } elsif (@ARGV >= 1) {
51 $ARGV[0] =~ /^(\d+(?:\.\d+)?)$/ or error "Invalid version number \"$ARGV[0]\"";
52 push @versions, $1;
53 } else {
54 @versions = get_versions();
55 }
56 if (@ARGV >= 2 and $ARGV[1] =~ /^([-.\w]+)$/) {
57 $ls_cluster = $1;
58 }
59
60 error "Cluster @versions $ls_cluster does not exist"
61 if ($ls_cluster and not cluster_exists(@versions, $ls_cluster));
62
63 # data collection
64 my @lines;
65 push @lines, ['Ver', 'Cluster', 'Port', 'Status', 'Owner', 'Data directory', 'Log file']
66 unless ($no_header);
67 my $jsoninfo = [];
68
69 foreach my $v (@versions) {
70 my $pg_log = $v >= 10 ? 'log' : 'pg_log'; # log directory in PGDATA changed in PG 10
71 my @clusters = $ls_cluster ? $ls_cluster : get_version_clusters $v;
72 foreach my $c (@clusters) {
73 my %info = cluster_info $v, $c;
74
75 if (not -r $info{'configfile'}) {
76 print STDERR "Warning: configfile $info{'configfile'} not readable\n";
77 }
78
79 my $status = $info{'running'} ? "online" : "down";
80 $status .= ",recovery" if ($info{'recovery'});
81 $status .= ",$info{supervisor}" if ($info{'supervisor'});
82 $status .= ",$info{start}" if ($start_conf);
83 unless (-e "${PgCommon::binroot}$v/bin/postgres") {
84 $status .= ",binaries_missing";
85 $info{binaries_missing} = 1;
86 }
87
88 my $logfile = $info{logfile} // '<unknown>'; # default logfile in /var/log/postgresql
89 if (config_bool ($info{config}->{logging_collector})) {
90 my $path = $info{config}->{log_directory} || $pg_log;
91 my $file = $info{config}->{log_filename} || 'postgresql-%Y-%m-%d_%H%M%S.log';
92 $logfile = "$path/$file";
93 }
94 my $destination = $info{config}->{log_destination} || 'stderr';
95 $destination =~ s/stderr/$logfile/;
96 my $csvlog = $logfile;
97 $csvlog =~ s/(?:\.log)?$/.csv/;
98 $destination =~ s/csvlog/$csvlog/;
99
100 push @lines, [$v, $c, $info{'port'} // '<unknown>', $status,
101 defined $info{'owneruid'} ? (getpwuid $info{'owneruid'})[0] : '<unknown>',
102 $info{'pgdata'} || '<unknown>', $destination];
103 $info{version} = $v;
104 $info{cluster} = $c;
105 push @$jsoninfo, \%info;
106 }
107 }
108
109 # output
110 if ($json) {
111 eval { require JSON; };
112 error 'Please install JSON.pm for JSON output (Debian: libjson-perl)'
113 if ($@);
114 print JSON::encode_json($jsoninfo) . "\n";
115 exit 0;
116 }
117
118 my @colwidth = qw(1 1 1 1 1 1 1);
119 foreach my $line (@lines) {
120 for (my $i = 0; $i < @$line - 1; $i++) { # skip adjustment for last column
121 my $len = length @$line[$i];
122 $colwidth[$i] = $len if ($len > $colwidth[$i]);
123 }
124 }
125
126 my $color = -t 1; # color output if stdout is a terminal
127 my $fmtstring = join ' ', map { "%-${_}s" } @colwidth;
128
129 foreach my $line (@lines) {
130 if ($color and $line->[0] ne 'Ver') { # don't color header
131 printf "\033[%dm$fmtstring\033[0m\n",
132 ($line->[3] =~ /^online/ ? 32 : 31), # green, red
133 @$line;
134 } else {
135 printf "$fmtstring\n", @$line;
136 }
137 }
138
139 __END__
140
141 =head1 NAME
142
143 pg_lsclusters - show information about all PostgreSQL clusters
144
145 =head1 SYNOPSIS
146
147 B<pg_lsclusters> [I<options>] [I<version> [I<cluster>]]
148
149 =head1 DESCRIPTION
150
151 This command list the status and some configuration details of all clusters.
152 If a version and optionally a cluster name are given, only these are shown.
153
154 =head1 OPTIONS
155
156 =over 4
157
158 =item B<-h>, B<--no-header>
159
160 Do not print the column header line.
161
162 =item B<-j>, B<--json>
163
164 Output information in JSON format. Needs JSON.pm installed.
165 (Debian package: libjson-perl)
166
167 =item B<-s>, B<--start-conf>
168
169 Include F<start.conf> information in status column.
170
171 =item B<--help>
172
173 Print usage help.
174
175 =back
176
177 =head1 NOTES
178
179 The cluster status is shown as B<online> or B<down>. If a F<recovery.conf> file
180 is found in the data directory, B<,recovery> is appended. The latter needs read
181 access to the data directory, which only root and the cluster owner have.
182
183 The output lines are colored green and red to indicate the cluster status
184 visually.
185
186 =head1 AUTHOR
187
188 Martin Pitt L<E<lt>[email protected]<gt>>