Guest User

Untitled

a guest
Jan 9th, 2019
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. #!/bin/bash
  2. HOST='localhost'
  3. PORT='5432'
  4. USERNAME='postgres'
  5. PASSWORD='postgres'
  6. DATABASE='postgres'
  7.  
  8. while test $# -gt 0; do
  9. case "$1" in
  10. --help)
  11. echo "options:"
  12. echo "--help show brief help"
  13. echo "-h, --host (default='$HOST') database host"
  14. echo "-P, --port (default='$PORT') database port"
  15. echo "-u, --user (default='$USERNAME') database username"
  16. echo "-p, --pass (default='$PASSWORD') database password"
  17. echo "-d, --database (default='$DATABASE') database name"
  18. echo "-f, --filter filter ex: show_connections -f \"'foo', 'bar'\""
  19. exit 0
  20. ;;
  21. -f|--filter)
  22. shift
  23. if test $# -gt 0; then
  24. export WHERE="where usename in ($1)"
  25. else
  26. echo "filter not found"
  27. exit 1
  28. fi
  29. shift
  30. ;;
  31. -h|--host)
  32. shift
  33. if test $# -gt 0; then
  34. export HOST=$1
  35. else
  36. echo "host not found"
  37. exit 1
  38. fi
  39. shift
  40. ;;
  41. -P|--port)
  42. shift
  43. if test $# -gt 0; then
  44. export PORT=$1
  45. else
  46. echo "port not found"
  47. exit 1
  48. fi
  49. shift
  50. ;;
  51. -u|--user)
  52. shift
  53. if test $# -gt 0; then
  54. export USERNAME=$1
  55. else
  56. echo "user not found"
  57. exit 1
  58. fi
  59. shift
  60. ;;
  61. -p|--pass)
  62. shift
  63. if test $# -gt 0; then
  64. export PASSWORD=$1
  65. else
  66. echo "password not found"
  67. exit 1
  68. fi
  69. shift
  70. ;;
  71. -d|--database)
  72. shift
  73. if test $# -gt 0; then
  74. export DATABASE=$1
  75. else
  76. echo "database not found"
  77. exit 1
  78. fi
  79. shift
  80. ;;
  81. *)
  82. break
  83. ;;
  84. esac
  85. done
  86.  
  87. watch -c -t -n 1 "PGPASSWORD=$PASSWORD psql -h $HOST -U $USERNAME -d $DATABASE -c \"select usename, count(*) connections from pg_stat_activity $WHERE group by usename order by count(*) desc\""
Add Comment
Please, Sign In to add comment