From afadd61aabd5edc04d7a01e47489d215db0688bf Mon Sep 17 00:00:00 2001
From: brinn <brinn>
Date: Thu, 1 Aug 2013 13:11:22 +0000
Subject: [PATCH] [BIS-300] Add database connection logging to AS and fix /
 improve script for DSS.

SVN: 29601
---
 datastore_server/dist/datastore_server.sh     | 47 +++++++------------
 openbis/dist/admin/list-postgres-queries.sh   |  2 +-
 openbis/dist/server/debug-db-connections.sh   | 22 +++++++++
 openbis/dist/server/log-db-connections.sh     | 21 +++++++++
 openbis/dist/server/log-thread-dump.sh        | 21 +++++++++
 .../record-stacktrace-db-connections.sh       | 22 +++++++++
 openbis/dist/server/setup-env                 | 21 +++++++++
 openbis/dist/server/status.sh                 | 21 ---------
 8 files changed, 126 insertions(+), 51 deletions(-)
 create mode 100755 openbis/dist/server/debug-db-connections.sh
 create mode 100755 openbis/dist/server/log-db-connections.sh
 create mode 100755 openbis/dist/server/log-thread-dump.sh
 create mode 100755 openbis/dist/server/record-stacktrace-db-connections.sh

diff --git a/datastore_server/dist/datastore_server.sh b/datastore_server/dist/datastore_server.sh
index 650a61eb98e..64efcec4ba4 100755
--- a/datastore_server/dist/datastore_server.sh
+++ b/datastore_server/dist/datastore_server.sh
@@ -262,9 +262,8 @@ case "$command" in
     echo "  $0 show-command-queue  -  show the queue of commands from openBIS AS waiting to be executed"
     echo "  $0 log-db-connections  -  log the currently active database connections to log/startup_log.txt"
     echo "  $0 log-thread-dump  -  log the current thread dump to log/startup_log.txt"
-    echo "  $0 debug-db-connections  -  switch on database connection debug logging"
-    echo "  $0 no-debug-db-connections  -  switch off database connection debug logging"
-    echo "  $0 record-stacktrace-db-connections  -  switch on database connection stacktrace recording"
+    echo "  $0 debug-db-connections on / off -  switch on / off database connection debug logging"
+    echo "  $0 record-stacktrace-db-connections on / off -  switch on / off database connection stacktrace recording"
     echo "  $0 no-record-stacktrace-db-connections  -  switch off database connection stacktrace recording"
     ;;
   version)
@@ -303,8 +302,8 @@ case "$command" in
       exit 100
     fi
   	mkdir -p .control
-  	if [ "$1" != "" ]; then
-    	touch .control/db-connections-print-active.$1
+  	if [ "$2" != "" ]; then
+    	touch .control/db-connections-print-active.$2
    	else
     	touch .control/db-connections-print-active
    	fi
@@ -318,19 +317,13 @@ case "$command" in
       exit 100
     fi
   	mkdir -p .control
-  	touch .control/db-connections-debug-on
-  	echo "Switched on debug logging for database connections."
-    ;;
-  no-debug-db-connections)
-    getStatus
-    EXIT_STATUS=$?
-    if [ $EXIT_STATUS -ne 0 ]; then
-      echo "Error: Data Store Server not running."
-      exit 100
-    fi
-  	mkdir -p .control
-  	touch .control/db-connections-debug-off
-  	echo "Switched off debug logging for database connections."
+  	if [ "$2" == "off" ]; then
+	  	touch .control/db-connections-debug-off
+  		echo "Switched off debug logging for database connections."
+  	else
+	  	touch .control/db-connections-debug-on
+  		echo "Switched on debug logging for database connections."
+  	fi
     ;;
   record-stacktrace-db-connections)
     getStatus
@@ -340,17 +333,13 @@ case "$command" in
       exit 100
     fi
   	mkdir -p .control
-  	touch .control/db-connections-stacktrace-on
-    ;;
-  no-record-stacktrace-db-connections)
-    getStatus
-    EXIT_STATUS=$?
-    if [ $EXIT_STATUS -ne 0 ]; then
-      echo "Error: Data Store Server not running."
-      exit 100
-    fi
-  	mkdir -p .control
-  	touch .control/db-connections-stacktrace-off
+  	if [ "$2" == "off" ]; then
+	  	touch .control/db-connections-stacktrace-off
+  		echo "Switched off stacktrace recording for database connections."
+	  else
+	  	touch .control/db-connections-stacktrace-on
+  		echo "Switched on stacktrace recording for database connections."
+	  fi
     ;;
   *)
     echo "Usage: $0 {start|stop|restart|status|help|version}"
diff --git a/openbis/dist/admin/list-postgres-queries.sh b/openbis/dist/admin/list-postgres-queries.sh
index 1bf6da09a83..42d6ec6004b 100755
--- a/openbis/dist/admin/list-postgres-queries.sh
+++ b/openbis/dist/admin/list-postgres-queries.sh
@@ -8,4 +8,4 @@ fi
 
 source $BASE/env
 
-psql -U $DB_USER_NAME -d $OPENBIS_DB -c "select datname,procpid,usename,current_query,waiting,xact_start FROM pg_catalog.pg_stat_activity"
\ No newline at end of file
+psql -U $DB_USER_NAME -d $OPENBIS_DB -c "select datname,pid,usename,query,state FROM pg_catalog.pg_stat_activity"
\ No newline at end of file
diff --git a/openbis/dist/server/debug-db-connections.sh b/openbis/dist/server/debug-db-connections.sh
new file mode 100755
index 00000000000..4789b4a36d6
--- /dev/null
+++ b/openbis/dist/server/debug-db-connections.sh
@@ -0,0 +1,22 @@
+#! /bin/bash
+
+# Script for CISD openBIS Application Server to switch on / off debugging of database connections on Unix / Linux systems
+# -----------------------------------------------------------------------------------------------------------------------
+
+source `dirname "$0"`/setup-env
+
+printStatus -q "$1"
+    EXIT_STATUS=$?
+if [ $EXIT_STATUS -ne 0 ]; then
+  echo "Error: Application Server not running."
+  exit 100
+fi
+
+mkdir -p .control
+if [ "$1" == "off" ]; then
+ 	touch .control/db-connections-debug-off
+	echo "Switched off debug logging for database connections."
+else
+ 	touch .control/db-connections-debug-on
+	echo "Switched on debug logging for database connections."
+fi
diff --git a/openbis/dist/server/log-db-connections.sh b/openbis/dist/server/log-db-connections.sh
new file mode 100755
index 00000000000..f2b0b2f8a76
--- /dev/null
+++ b/openbis/dist/server/log-db-connections.sh
@@ -0,0 +1,21 @@
+#! /bin/bash
+
+# Script for CISD openBIS Application Server to log open database connections on Unix / Linux systems
+# ---------------------------------------------------------------------------------------------------
+
+source `dirname "$0"`/setup-env
+
+printStatus -q "$1"
+    EXIT_STATUS=$?
+if [ $EXIT_STATUS -ne 0 ]; then
+  echo "Error: Application Server not running."
+  exit 100
+fi
+
+mkdir -p .control
+if [ "$1" != "" ]; then
+ 	touch .control/db-connections-print-active.$1
+else
+ 	touch .control/db-connections-print-active
+fi
+echo "Active database connections logged to logs/jetty.out"
diff --git a/openbis/dist/server/log-thread-dump.sh b/openbis/dist/server/log-thread-dump.sh
new file mode 100755
index 00000000000..1f175d172c0
--- /dev/null
+++ b/openbis/dist/server/log-thread-dump.sh
@@ -0,0 +1,21 @@
+#! /bin/bash
+
+# Script for CISD openBIS Application Server to log thread dump on Unix / Linux systems
+# -------------------------------------------------------------------------------------
+
+source `dirname "$0"`/setup-env
+
+if [ -f $JETTY_PID_FILE ]; then
+  PID=`cat $JETTY_PID_FILE 2> /dev/null`
+  isPIDRunning $PID
+  if [ $? -eq 0 ]; then
+    kill -3 $PID
+		echo "Thread dump logged to logs/jetty.out"
+  else
+  	echo "Error: Application Server not running."
+  	exit 100
+  fi
+else
+	echo "Error: Application Server not running."
+ 	exit 100
+fi
diff --git a/openbis/dist/server/record-stacktrace-db-connections.sh b/openbis/dist/server/record-stacktrace-db-connections.sh
new file mode 100755
index 00000000000..32ed6b76396
--- /dev/null
+++ b/openbis/dist/server/record-stacktrace-db-connections.sh
@@ -0,0 +1,22 @@
+#! /bin/bash
+
+# Script for CISD openBIS Application Server to switch on / off recording of stacktraces of database connections on Unix / Linux systems
+# --------------------------------------------------------------------------------------------------------------------------------------
+
+source `dirname "$0"`/setup-env
+
+printStatus -q "$1"
+    EXIT_STATUS=$?
+if [ $EXIT_STATUS -ne 0 ]; then
+  echo "Error: Application Server not running."
+  exit 100
+fi
+
+mkdir -p .control
+if [ "$1" == "off" ]; then
+ 	touch .control/db-connections-stacktrace-off
+	echo "Switched off stacktrace recording for database connections."
+else
+ 	touch .control/db-connections-stacktrace-on
+	echo "Switched on stacktrace recording for database connections."
+fi
diff --git a/openbis/dist/server/setup-env b/openbis/dist/server/setup-env
index 275c4116a52..c488c62ea85 100644
--- a/openbis/dist/server/setup-env
+++ b/openbis/dist/server/setup-env
@@ -66,3 +66,24 @@ checkNotRoot()
     exit 1
   fi
 }
+
+printStatus()
+{
+  if [ "$1" == "-q" ]; then
+    QUIET=1
+  fi
+  if [ -f $JETTY_PID_FILE ]; then
+    PID=`cat $JETTY_PID_FILE`
+    isPIDRunning $PID
+    if [ $? -eq 0 ]; then
+      test -z "$QUIET" && echo "openBIS Application Server is running (pid $PID)"
+      return 0
+    else
+      test -z "$QUIET" && echo "openBIS Application is dead (stale pid $PID)"
+      return 1
+    fi
+  else
+    test -z "$QUIET" && echo "openBIS Application is not running."
+    return 2
+  fi
+}
diff --git a/openbis/dist/server/status.sh b/openbis/dist/server/status.sh
index 3aa6070657b..729294fcdf4 100755
--- a/openbis/dist/server/status.sh
+++ b/openbis/dist/server/status.sh
@@ -5,25 +5,4 @@
 
 source `dirname "$0"`/setup-env
 
-printStatus()
-{
-  if [ "$1" == "-q" ]; then
-    QUIET=1
-  fi
-  if [ -f $JETTY_PID_FILE ]; then
-    PID=`cat $JETTY_PID_FILE`
-    isPIDRunning $PID
-    if [ $? -eq 0 ]; then
-      test -z "$QUIET" && echo "openBIS Application Server is running (pid $PID)"
-      return 0
-    else
-      test -z "$QUIET" && echo "openBIS Application is dead (stale pid $PID)"
-      return 1
-    fi
-  else
-    test -z "$QUIET" && echo "openBIS Application is not running."
-    return 2
-  fi
-}
-
 printStatus "$1"
\ No newline at end of file
-- 
GitLab