Skip to content

Instantly share code, notes, and snippets.

@artifactsauce
Last active November 14, 2016 01:52
Show Gist options
  • Save artifactsauce/2caaf2eb1806e8f13120f111feb28040 to your computer and use it in GitHub Desktop.
Save artifactsauce/2caaf2eb1806e8f13120f111feb28040 to your computer and use it in GitHub Desktop.
BitBarでPostgreSQLサーバーの状態を常に意識する ref: http://qiita.com/artifactsauce/items/445535ea8a58253c2f28
$ brew install postgresql
$ /usr/local/bin/initdb -D /usr/local/var/postgres
$ /usr/local/bin/pg_ctl -D /usr/local/var/postgres start
$ /usr/local/bin/createuser postgres
Start | bash=/usr/local/bin/pg_ctl param1=-D param2=$PGDATA param3=start refresh=true terminal=false
$ /usr/local/bin/psql -U postgres -w -q -A -F $'\x09' -c "select * from pg_stat_database;"
datid datname numbackends xact_commit xact_rollback blks_read blks_hit tup_returned tup_fetched tup_inserted tup_updated tup_deleted conflicts temp_files temp_bytes deadlocks blk_read_time blk_write_time stats_reset
1 template1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00
12636 template0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00
12641 postgres 1 6179 2 431 317062 1561344 123647 0 0 0 0 0 0 0 00 2016-10-31 20:33:03.144392+09
(3 rows)
$ /usr/local/bin/psql -U postgres -w -q -A -F $'\x09' -c "select * from pg_stat_database;" | perl -MData::Dumper -ne 'BEGIN { $_=<>; chomp; @headers = split "\t"} END { print Dumper \@headers }'
$VAR1 = [
'datid',
'datname',
'numbackends',
'xact_commit',
'xact_rollback',
'blks_read',
'blks_hit',
'tup_returned',
'tup_fetched',
'tup_inserted',
'tup_updated',
'tup_deleted',
'conflicts',
'temp_files',
'temp_bytes',
'deadlocks',
'blk_read_time',
'blk_write_time',
'stats_reset'
];
$ /usr/local/bin/psql -U postgres -w -q -A -F $'\x09' -c "select * from pg_stat_database;" | perl -MData::Dumper -ne 'BEGIN { $_=<>; chomp; @headers = split "\t"} @records{@headers} = split "\t"; print Dumper \%records'
$VAR1 = {
'blk_read_time' => '0',
'xact_commit' => '0',
'temp_files' => '0',
'conflicts' => '0',
'stats_reset' => '
',
'tup_deleted' => '0',
'xact_rollback' => '0',
'numbackends' => '0',
'tup_fetched' => '0',
(...)
'tup_returned' => undef,
'blk_write_time' => undef,
'datid' => '(3 rows)
',
'tup_inserted' => undef,
'tup_updated' => undef,
'datname' => undef,
'deadlocks' => undef
};
@record{@keys} = @values;
$ /usr/local/bin/psql -U postgres -w -q -A -F $'\x09' -c "select * from pg_stat_database;" | perl -MData::Dumper -nle 'BEGIN { $_=<>; chomp; @headers = split "\t"} last if eof(); @records{@headers} = split "\t"; next if $records{datname} =~/^template/; print Dumper \%records'
$VAR1 = {
'tup_fetched' => '45296',
'tup_updated' => '0',
'temp_bytes' => '0',
'tup_inserted' => '0',
'tup_returned' => '565944',
'datid' => '12641',
'blks_read' => '239',
'datname' => 'postgres',
'deadlocks' => '0',
'blk_write_time' => '0',
'tup_deleted' => '0',
'xact_commit' => '2209',
'numbackends' => '1',
'blk_read_time' => '0',
'conflicts' => '0',
'stats_reset' => '2016-11-09 01:06:07.773202+09',
'blks_hit' => '116199',
'temp_files' => '0',
'xact_rollback' => '0'
};
next if $records{datname} =~/^template/;
last if eof();
#!/usr/bin/env bash
# <bitbar.title>PostgreSQL server status</bitbar.title>
# <bitbar.version>v1.0</bitbar.version>
# <bitbar.author>Kenji Akiyama</bitbar.author>
# <bitbar.author.github>artifactsauce</bitbar.author.github>
# <bitbar.desc>Show the status of PostgreSQL server installed by Homebrew on localhost and manage server boot with shortcut menus</bitbar.desc>
# <bitbar.image>http://i.imgur.com/l5E4yg8.png</bitbar.image>
# <bitbar.dependencies>bash,perl,postgresql</bitbar.dependencies>
# TODO Selecting some menus will return warnings because the process has not been completed.
set -eu
# Change here depending on your preference
MENUBAR_ICON_ENABLED=":elephant:"
MENUBAR_ICON_DISABLED=":sleepy:"
STATUS_ITEM_COLOR="green"
DISABLED_ITEM_COLOR="#C0C0C0"
# Below is no need to change basically.
SERVER_CMD="/usr/local/bin/pg_ctl"
PGDATA="/usr/local/var/postgres"
SUBCMD_START="start"
SUBCMD_STOP="stop"
SUBCMD_RESTART="restart"
SUBCMD_RELOAD="reload"
SUBCMD_STATUS="status"
if $SERVER_CMD -D $PGDATA $SUBCMD_STATUS | grep -Fq 'server is running'; then
IS_SERVER_RUNNING=true
echo "$MENUBAR_ICON_ENABLED"
else
IS_SERVER_RUNNING=false
echo "$MENUBAR_ICON_DISABLED"
fi
echo "---"
echo "PostgreSQL Server"
# Server Status from PostgreSQL's `pg_stat_database` table
if $IS_SERVER_RUNNING; then
/usr/local/bin/psql -U postgres -w -q -A -F $'\x09' -c "select * from pg_stat_database;" | perl -nle 'BEGIN { $_=<>; chomp; @headers = split "\t"} last if eof(); @records{@headers} = split "\t"; next if $records{datname} =~/^template/; print "---"; print "$_: $records{$_} | color='$STATUS_ITEM_COLOR'" for @headers'
fi
# Server management shortcuts
echo "---"
if $IS_SERVER_RUNNING; then
echo "Start | color=$DISABLED_ITEM_COLOR"
echo "Stop | bash=$SERVER_CMD param1=-D param2=$PGDATA param4=$SUBCMD_STOP refresh=true terminal=false"
echo "Restart | bash=$SERVER_CMD param1=-D param2=$PGDATA param4=$SUBCMD_RESTART refresh=true terminal=false"
echo "Reload | bash=$SERVER_CMD param1=-D param2=$PGDATA param4=$SUBCMD_RELOAD refresh=true terminal=false"
else
echo "Start | bash=$SERVER_CMD param1=-D param2=$PGDATA param4=$SUBCMD_START refresh=true terminal=false"
echo "Stop | color=$DISABLED_ITEM_COLOR"
echo "Restart | color=$DISABLED_ITEM_COLOR"
echo "Reload | color=$DISABLED_ITEM_COLOR"
fi
echo "---"
echo "Refresh | refresh=true color=$DISABLED_ITEM_COLOR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment