pg_lsclusters.sh
| 1 |
#!/usr/bin/env bash |
|---|---|
| 2 |
set -euo pipefail |
| 3 |
|
| 4 |
find_pg_clusters() |
| 5 |
{
|
| 6 |
local root="${1:-/pgdata}"
|
| 7 |
local dir |
| 8 |
local version |
| 9 |
local port |
| 10 |
local status |
| 11 |
local pid |
| 12 |
|
| 13 |
printf '%-3s %-10s %-5s %-8s %s\n' "Ver" "Cluster" "Port" "Status" "Data directory" |
| 14 |
|
| 15 |
while IFS= read -r -d '' dir |
| 16 |
do |
| 17 |
version="$(cat "$dir/PG_VERSION")" |
| 18 |
|
| 19 |
port="$( |
| 20 |
awk -F= ' |
| 21 |
/^[[:space:]]*port[[:space:]]*=/ {
|
| 22 |
gsub(/[[:space:]]/, "", $2) |
| 23 |
gsub(/#.*/, "", $2) |
| 24 |
print $2 |
| 25 |
exit |
| 26 |
} |
| 27 |
' "$dir/postgresql.conf" 2>/dev/null || true |
| 28 |
)" |
| 29 |
port="${port:-5432}"
|
| 30 |
|
| 31 |
status="down" |
| 32 |
if [[ -f "$dir/postmaster.pid" ]] |
| 33 |
then |
| 34 |
pid="$(head -n 1 "$dir/postmaster.pid" || true)" |
| 35 |
if [[ -n "$pid" ]] && kill -0 "$pid" 2>/dev/null |
| 36 |
then |
| 37 |
status="online" |
| 38 |
else |
| 39 |
status="stale-pid" |
| 40 |
fi |
| 41 |
fi |
| 42 |
|
| 43 |
printf '%-3s %-10s %-5s %-8s %s\n' \ |
| 44 |
"$version" \ |
| 45 |
"$(basename "$dir")" \ |
| 46 |
"$port" \ |
| 47 |
"$status" \ |
| 48 |
"$dir" |
| 49 |
|
| 50 |
done < <( |
| 51 |
find "$root" -type f -name PG_VERSION -print0 2>/dev/null | |
| 52 |
while IFS= read -r -d '' file |
| 53 |
do |
| 54 |
printf '%s\0' "$(dirname "$file")" |
| 55 |
done |
| 56 |
) |
| 57 |
} |
| 58 |
|
| 59 |
find_pg_clusters "${1:-/pgdata}"
|