From b7a31e7c01e7db6b196e162abf1f2aaa3120e773 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 26 Nov 2024 13:13:21 +0100 Subject: [PATCH] apex and apache sessions etc --- src/config.json | 40 +++++++++++---- src/scripts/apache_connections.sh | 9 ++++ src/scripts/apexBots.sh | 3 +- src/scripts/apexUsage.sh | 1 + src/scripts/apex_activity.sh | 82 +++++++++++++++++++++++++++++++ src/scripts/apex_mail_queue.sh | 1 + src/scripts/apex_smtp_settings.sh | 1 + src/scripts/check_email_ports.sh | 13 +++++ src/scripts/datafileUsage.sh | 1 + src/scripts/db_version_info.sh | 1 + src/scripts/os_session_summary.sh | 76 ++++++++++++++++++++++++++++ src/scripts/processfinder.sh | 4 +- src/scripts/showdbparameter.sh | 2 + src/scripts/tablespaceUsage.sh | 1 + src/scripts/topRedoSQL.sh | 1 + 15 files changed, 224 insertions(+), 12 deletions(-) create mode 100755 src/scripts/apache_connections.sh create mode 100755 src/scripts/apex_activity.sh create mode 100755 src/scripts/check_email_ports.sh create mode 100755 src/scripts/os_session_summary.sh diff --git a/src/config.json b/src/config.json index c45146f..d875cf7 100644 --- a/src/config.json +++ b/src/config.json @@ -8,42 +8,42 @@ { "tag": "tablespace_usage", "type": "command", - "content": "/opt/ticket-ai/src/scripts/tablespaceUsage.sh;sleep 5;cat /tmp/tablespace_usage.tmp" + "content": "output=$(/opt/ticket-ai/src/scripts/tablespaceUsage.sh) && echo \"$output\"" }, { "tag": "datafile_usage", "type": "command", - "content": "/opt/ticket-ai/src/scripts/datafileUsage.sh;sleep 5;cat /tmp/datafile_usage.tmp" + "content": "output=$(/opt/ticket-ai/src/scripts/datafileUsage.sh) && echo \"$output\"" }, { "tag": "db_version_info", "type": "command", - "content": "/opt/ticket-ai/src/scripts/db_version_info.sh;sleep 5;cat /tmp/db_version_info.tmp" + "content": "output=$(/opt/ticket-ai/src/scripts/db_version_info.sh) && echo \"$output\"" }, { "tag": "apex_usage", "type": "command", - "content": "/opt/ticket-ai/src/scripts/apexUsage.sh;sleep 5;cat /tmp/apex_usage.tmp" + "content": "output=$(/opt/ticket-ai/src/scripts/apexUsage.sh) && echo \"$output\"" }, { "tag": "apex_mail_queue", "type": "command", - "content": "/opt/ticket-ai/src/scripts/apex_mail_queue.sh;sleep 5;cat /tmp/apex_mail_queue.tmp" + "content": "output=$(/opt/ticket-ai/src/scripts/apex_mail_queue.sh) && echo \"$output\"" }, { "tag": "apex_smtp_settings", "type": "command", - "content": "/opt/ticket-ai/src/scripts/apex_smtp_settings.sh;sleep 5;cat /tmp/apex_smtp_settings.tmp" + "content": "output=$(/opt/ticket-ai/src/scripts/apex_smtp_settings.sh) && echo \"$output\"" }, { "tag": "apex_bot_hits", "type": "command", - "content": "/opt/ticket-ai/src/scripts/apexBots.sh;sleep 5;cat /tmp/apex_bots.tmp" + "content": "output=$(/opt/ticket-ai/src/scripts/apexBots.sh) && echo \"$output\"" }, { "tag": "top_redo_sql", "type": "command", - "content": "/opt/ticket-ai/src/scripts/topRedoSQL.sh;sleep 5;cat /tmp/top_redo_sql.tmp" + "content": "output=$(/opt/ticket-ai/src/scripts/topRedoSQL.sh) && echo \"$output\"" }, { "tag": "top", @@ -58,7 +58,7 @@ { "tag": "process_finder", "type": "command", - "content": "/opt/ticket-ai/src/scripts/processfinder.sh;sleep 5;cat /tmp/findsql.tmp" + "content": "output=$(/opt/ticket-ai/src/scripts/processfinder.sh) && echo \"$output\"" }, { "tag": "ords_config", @@ -93,7 +93,27 @@ { "tag": "journalctl", "type": "command", - "content": "output=$(journalctl -p 3 -xb) && echo \"$output\"" + "content": "output=$(journalctl -p 3 -xb | tail -n 200) && echo \"$output\"" + }, + { + "tag": "apache_connections", + "type": "command", + "content": "output=$(/opt/ticket-ai/src/scripts/apache_connections.sh) && echo \"$output\"" + }, + { + "tag": "os_session_summary", + "type": "command", + "content": "output=$(/opt/ticket-ai/src/scripts/os_session_summary.sh) && echo \"$output\"" + }, + { + "tag": "check_email_ports", + "type": "command", + "content": "output=$(/opt/ticket-ai/src/scripts/check_email_ports.sh) && echo \"$output\"" + }, + { + "tag": "apex_activity", + "type": "command", + "content": "output=$(/opt/ticket-ai/src/scripts/apex_activity.sh) && echo \"$output\"" }, { "tag": "postqueue", diff --git a/src/scripts/apache_connections.sh b/src/scripts/apache_connections.sh new file mode 100755 index 0000000..1eaee5d --- /dev/null +++ b/src/scripts/apache_connections.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +# Count established connections on ports 80 and 443 +connections=$(netstat -an | grep ':80 ' | grep ESTABLISHED | wc -l) +https_connections=$(netstat -an | grep ':443 ' | grep ESTABLISHED | wc -l) + +echo "Active HTTP connections: $connections" +echo "Active HTTPS connections: $https_connections" + diff --git a/src/scripts/apexBots.sh b/src/scripts/apexBots.sh index eeeed04..e4212d6 100755 --- a/src/scripts/apexBots.sh +++ b/src/scripts/apexBots.sh @@ -26,7 +26,7 @@ run_query() { ALTER SESSION SET CONTAINER = $container_name; -- Print the container name for clarity - PROMPT Bot Hits on APEX for container: $container_name; + PROMPT Bot Hits on APEX in last 1 hour for container: $container_name; SELECT AGENT, @@ -86,5 +86,6 @@ else echo "No PDBs found or could not access v\$pdbs view" >> "$OUTPUT_FILE" fi +cat "$OUTPUT_FILE" # Clean up the temporary file rm -f "$PDB_LIST_FILE" diff --git a/src/scripts/apexUsage.sh b/src/scripts/apexUsage.sh index fdefc67..16e0e69 100755 --- a/src/scripts/apexUsage.sh +++ b/src/scripts/apexUsage.sh @@ -76,5 +76,6 @@ else echo "No PDBs found or could not access v\$pdbs view" >> "$OUTPUT_FILE" fi +cat "$OUTPUT_FILE" # Clean up the temporary file rm -f "$PDB_LIST_FILE" diff --git a/src/scripts/apex_activity.sh b/src/scripts/apex_activity.sh new file mode 100755 index 0000000..5024951 --- /dev/null +++ b/src/scripts/apex_activity.sh @@ -0,0 +1,82 @@ +#!/bin/bash + +# File paths for output +OUTPUT_FILE="/tmp/apex_activity.tmp" +PDB_LIST_FILE="/tmp/pdb_list.tmp" +> "$OUTPUT_FILE" # Clear the output file before starting +touch $OUTPUT_FILE +chown oracle $OUTPUT_FILE + +# Function to execute the APEX User Hits query in a specific container +run_query() { + local container_name="$1" + su - oracle -s /bin/bash -c " + sqlplus -s / as sysdba <> \"$OUTPUT_FILE\" + whenever sqlerror exit sql.sqlcode; + SET ECHO ON + SET HEADING ON + SET UNDERLINE '=' + SET FEEDBACK OFF + SET LINESIZE 150 + SET PAGESIZE 100 + COLUMN WORKSPACE FORMAT A40 + COLUMN HIT_COUNT FORMAT 999999 + + -- Switch to the specified container + ALTER SESSION SET CONTAINER = $container_name; + + -- Print the container name for clarity + PROMPT APEX Session Count in last 1 hour for container: $container_name; + + SELECT + WORKSPACE, + COUNT(*) AS HIT_COUNT + FROM + APEX_WORKSPACE_ACTIVITY_LOG + WHERE + VIEW_DATE BETWEEN SYSDATE - (1/24) AND SYSDATE + GROUP BY + WORKSPACE + ORDER BY + HIT_COUNT DESC; + + + exit; +EOF + " +} + +# Run the query for the CDB (root container) +# echo "Gathering Hits for CDB (root container)..." >> "$OUTPUT_FILE" +# run_query 'CDB\$ROOT' + +# Get a list of all PDBs, excluding PDB$SEED, and write to a temporary file +su - oracle -s /bin/bash -c " + sqlplus -s / as sysdba <<'EOF' + set heading off + set feedback off + set pagesize 0 + spool $PDB_LIST_FILE + SELECT NAME FROM v\$pdbs WHERE NAME NOT IN ('PDB\$SEED'); + spool off + exit; +EOF +" + +# Verify that the PDB_LIST_FILE was created and contains data +if [[ -f "$PDB_LIST_FILE" && -s "$PDB_LIST_FILE" ]]; then + # Read each valid PDB name from the temporary file + while IFS= read -r PDB; do + PDB=$(echo "$PDB" | xargs) # Trim any leading/trailing whitespace + if [[ -n "$PDB" ]]; then + echo "Gathering APEX User Hits for PDB: $PDB..." >> "$OUTPUT_FILE" + run_query "$PDB" + fi + done < "$PDB_LIST_FILE" +else + echo "No PDBs found or could not access v\$pdbs view" >> "$OUTPUT_FILE" +fi + +# Clean up the temporary file +cat "$OUTPUT_FILE" +rm -f "$PDB_LIST_FILE" diff --git a/src/scripts/apex_mail_queue.sh b/src/scripts/apex_mail_queue.sh index 7641363..e0ebbaa 100755 --- a/src/scripts/apex_mail_queue.sh +++ b/src/scripts/apex_mail_queue.sh @@ -75,5 +75,6 @@ else echo "No PDBs found or could not access v\$pdbs view" >> "$OUTPUT_FILE" fi +cat "$OUTPUT_FILE" # Clean up the temporary file rm -f "$PDB_LIST_FILE" diff --git a/src/scripts/apex_smtp_settings.sh b/src/scripts/apex_smtp_settings.sh index e7082cc..638d88b 100755 --- a/src/scripts/apex_smtp_settings.sh +++ b/src/scripts/apex_smtp_settings.sh @@ -88,5 +88,6 @@ else echo "No PDBs found or could not access v\$pdbs view" >> "$OUTPUT_FILE" fi +cat "$OUTPUT_FILE" # Clean up the temporary file rm -f "$PDB_LIST_FILE" diff --git a/src/scripts/check_email_ports.sh b/src/scripts/check_email_ports.sh new file mode 100755 index 0000000..7254b72 --- /dev/null +++ b/src/scripts/check_email_ports.sh @@ -0,0 +1,13 @@ +#!/bin/bash +ports="25 465 587 143 110 993 995" +file="/etc/sysconfig/iptables" + +echo "Checking iptables rules for required ports:" +for port in $ports; do + if grep -q -- "--dport $port" $file; then + echo "Port $port is allowed in $file" + else + echo "Port $port is NOT allowed in $file" + fi +done + diff --git a/src/scripts/datafileUsage.sh b/src/scripts/datafileUsage.sh index 0813612..2152b4a 100755 --- a/src/scripts/datafileUsage.sh +++ b/src/scripts/datafileUsage.sh @@ -83,5 +83,6 @@ else echo "No PDBs found or could not access v\$pdbs view" >> "$OUTPUT_FILE" fi +cat "$OUTPUT_FILE" # Clean up the temporary file rm -f "$PDB_LIST_FILE" diff --git a/src/scripts/db_version_info.sh b/src/scripts/db_version_info.sh index 9d84ca5..918f5a7 100755 --- a/src/scripts/db_version_info.sh +++ b/src/scripts/db_version_info.sh @@ -92,5 +92,6 @@ else echo "No PDBs found or could not access v\$pdbs view" >> "$OUTPUT_FILE" fi +cat "$OUTPUT_FILE" # Clean up the temporary file rm -f "$PDB_LIST_FILE" diff --git a/src/scripts/os_session_summary.sh b/src/scripts/os_session_summary.sh new file mode 100755 index 0000000..95425eb --- /dev/null +++ b/src/scripts/os_session_summary.sh @@ -0,0 +1,76 @@ +#!/bin/bash + +# Declare an associative array for grouping +declare -A summary + +# Iterate through each running process +while read -r process; do + if [[ $process == ora_* || $process == oracle* ]]; then + summary["Oracle"]=$((summary["Oracle"] + 1)) + elif [[ $process == php-fpm* ]]; then + summary["PHP-FPM"]=$((summary["PHP-FPM"] + 1)) + elif [[ $process == httpd* ]]; then + summary["HTTPD"]=$((summary["HTTPD"] + 1)) + elif [[ $process == sshd* ]]; then + summary["SSHD"]=$((summary["SSHD"] + 1)) + elif [[ $process == systemd* ]]; then + summary["SystemD"]=$((summary["SystemD"] + 1)) + elif [[ $process == zabbix* ]]; then + summary["Zabbix"]=$((summary["Zabbix"] + 1)) + elif [[ $process == python* || $process == python3* ]]; then + summary["Python"]=$((summary["Python"] + 1)) + elif [[ $process == node* ]]; then + summary["Node.js"]=$((summary["Node.js"] + 1)) + elif [[ $process == bash* || $process == sh* ]]; then + summary["Shell"]=$((summary["Shell"] + 1)) + elif [[ $process == java* ]]; then + summary["Java"]=$((summary["Java"] + 1)) + elif [[ $process == tnslsnr* ]]; then + summary["TNS Listener"]=$((summary["TNS Listener"] + 1)) + elif [[ $process == dovecot* || $process == postfix* || $process == saslauthd* ]]; then + summary["Mail"]=$((summary["Mail"] + 1)) + elif [[ $process == NetworkManager* ]]; then + summary["NetworkManager"]=$((summary["NetworkManager"] + 1)) + elif [[ $process == cron* || $process == crond* ]]; then + summary["Cron"]=$((summary["Cron"] + 1)) + else + summary["Uncategorized"]=$((summary["Uncategorized"] + 1)) + fi +done < <(ps -eo comm=) + +# Print the summary +echo "Program-wise session summary:" +for program in "${!summary[@]}"; do + printf "%-15s %s\n" "$program" "${summary[$program]}" +done + + + +# Declare an associative array for user sessions +declare -A user_sessions +declare -A active_users + +# Fetch loginctl session details and process them +while read -r session user_id state; do + # Get username from user ID using getent + username=$(getent passwd "$user_id" | cut -d: -f1) + # If username is valid, count sessions + if [[ -n $username ]]; then + user_sessions["$username"]=$((user_sessions["$username"] + 1)) + # Check if the session is active + if [[ $state == "active" ]]; then + active_users["$username"]=1 + fi + fi +done < <(loginctl list-sessions --no-legend | awk '{print $1, $2, $3}') + +# Print the user-wise session summary +echo "User-wise session summary from loginctl (with usernames):" +printf "%-15s %-10s %-10s\n" "Username" "Sessions" "Connected" +for username in "${!user_sessions[@]}"; do + connected="No" + if [[ ${active_users[$username]} -eq 1 ]]; then + connected="Yes" + fi + printf "%-15s %-10s %-10s\n" "$username" "${user_sessions[$username]}" "$connected" +done diff --git a/src/scripts/processfinder.sh b/src/scripts/processfinder.sh index 81a150a..9f005f1 100755 --- a/src/scripts/processfinder.sh +++ b/src/scripts/processfinder.sh @@ -1,7 +1,7 @@ #!/bin/bash # Define the CPU usage threshold -CPU_THRESHOLD=60 +CPU_THRESHOLD=50 # File path for output OUTPUT_FILE="/tmp/findsql.tmp" @@ -51,3 +51,5 @@ EOF else echo "No Oracle processes consuming significant CPU" > "$OUTPUT_FILE" fi + +cat "$OUTPUT_FILE" \ No newline at end of file diff --git a/src/scripts/showdbparameter.sh b/src/scripts/showdbparameter.sh index ef690a6..169b664 100755 --- a/src/scripts/showdbparameter.sh +++ b/src/scripts/showdbparameter.sh @@ -24,3 +24,5 @@ EOF # Notify user about the location of the output file echo "Database parameters have been saved to $OUTPUT_FILE" + +cat "$OUTPUT_FILE" \ No newline at end of file diff --git a/src/scripts/tablespaceUsage.sh b/src/scripts/tablespaceUsage.sh index f41868b..3ee7067 100755 --- a/src/scripts/tablespaceUsage.sh +++ b/src/scripts/tablespaceUsage.sh @@ -86,5 +86,6 @@ else echo "No PDBs found or could not access v\$pdbs view" >> "$OUTPUT_FILE" fi +cat "$OUTPUT_FILE" # Clean up the temporary file rm -f "$PDB_LIST_FILE" diff --git a/src/scripts/topRedoSQL.sh b/src/scripts/topRedoSQL.sh index 35aa872..613a925 100755 --- a/src/scripts/topRedoSQL.sh +++ b/src/scripts/topRedoSQL.sh @@ -103,5 +103,6 @@ else echo "No PDBs found or could not access v\$pdbs view" >> "$OUTPUT_FILE" fi +cat "$OUTPUT_FILE" # Clean up the temporary file rm -f