#!/bin/bash # Script to display the resource usage of running/pending jobs, users, and nodes # on a Slurm-based HPC system. # Parse command line arguments filter_user="" filter_node="" # Help message show_help() { echo "Usage: $0 [options]" echo "Options:" echo " -u, --user [USER] Filter results by username (defaults to $USER if no argument provided)" echo " -n, --node [NODE] Filter results by node name (defaults to $HOSTNAME if no argument provided)" echo " -h, --help Show this help message" exit 0 } # Parse command line options while [[ $# -gt 0 ]]; do case "$1" in -u|--user) if [[ -n "$2" && ! "$2" =~ ^- ]]; then filter_user="$2" shift 2 else # Use current user as default filter_user="$USER" shift 1 fi ;; -n|--node) if [[ -n "$2" && ! "$2" =~ ^- ]]; then filter_node="$2" shift 2 else # Use current hostname as default filter_node="$HOSTNAME" shift 1 fi ;; -h|--help) show_help ;; *) echo "Unknown option: $1" show_help ;; esac done # Print active filters if [[ -n "$filter_user" || -n "$filter_node" ]]; then echo -e "\033[1;35mActive filters:\033[0m" [[ -n "$filter_user" ]] && echo -e " User: \033[1;35m$filter_user\033[0m" [[ -n "$filter_node" ]] && echo -e " Node: \033[1;35m$filter_node\033[0m" echo fi # Set output format for squeue command # JOBID | PARTITION | NAME | USER | STATE | TIME | NODES | REASON | NODELIST | CPUS | TRES_PER_NODE | MIN_MEMORY | REQ_NODES | COMMAND | WORK_DIR squeue_format="%i | %P | %j | %u | %T | %M | %D | %r | %N | %C | %b | %m | %n | %o | %Z" # Get squeue output for the specified partition squeue_output=$(squeue -p q-g7hdnrvb --states=RUNNING,PENDING --format="$squeue_format") declare -a HEADERS declare -A HEADER_INDEX declare -i HEADER_COUNT declare -A jobidx_statidx declare -i job_count # Parse header and data using awk parse_squeue_output() { local command_output="$1" echo "$command_output" | awk -F' *\\| *' ' NR == 1 { for (i = 1; i <= NF; i++) { gsub(/^[ \t]+|[ \t]+$/, "", $i); # Trim whitespace printf("HEADERS[%d]=\"%s\"\n", i-1, $i); printf("HEADER_INDEX[\"%s\"]=%d\n", $i, i-1); } printf("HEADER_COUNT=%d\n", NF); } NR > 1 { # Store all fields for this job for (i = 1; i <= NF; i++) { gsub(/^[ \t]+|[ \t]+$/, "", $i); # Trim whitespace printf("jobidx_statidx[\"%d,%d\"]=\"%s\"\n", NR-2, i-1, $i); } } END { printf("job_count=%d\n", NR-1); }' } eval "$(parse_squeue_output "$squeue_output")" # Get list of nodes and their number of GPUs sinfo_format="NodeHost: | ,Gres: | ,GresUsed:30" sinfo_output=$(sinfo -p q-g7hdnrvb -h -N --Format="$sinfo_format") declare -a node_names declare -A gpu_counts declare -A used_gpu_counts # Process the output to extract node names and GPU counts while IFS=" | " read -r node gres gres_used; do # Extract just the number from gpu:8(S:0-1) format gpu_count=$(echo "$gres" | grep -o "gpu:[0-9]*" | cut -d':' -f2) # Extract just the number from gpu:(null):8(IDX:0-7) format gpu_used=$(echo "$gres_used" | grep -o "gpu:(null):[0-9]*" | cut -d':' -f3) # Handle case where gpu_count is empty (no GPUs or different format) if [[ -z "$gpu_count" ]]; then gpu_count=0 fi # Store in arrays node_names+=("$node") gpu_counts["$node"]="$gpu_count" used_gpu_counts["$node"]="$gpu_used" done < <(echo "$sinfo_output") # Sort the node names array IFS=$'\n' node_names=($(sort <<<"${node_names[*]}")) # Function used to expand host patterns (e.g., hk01dgx[025-027, 030, 032-033] -> hk01dgx025, hk01dgx026, hk01dgx027, hk01dgx030, hk01dgx032, hk01dgx033) get_expanded_hosts() { local input="$1" local result=() # Check if input contains bracket pattern if [[ "$input" == *"["*"]"* ]]; then # Extract prefix and the content inside brackets local prefix="${input%%\[*}" local bracket_content="${input#*\[}" bracket_content="${bracket_content%\]*}" # Split the bracket content by comma IFS=',' read -ra patterns <<< "$bracket_content" for pattern in "${patterns[@]}"; do # Trim whitespace pattern=$(echo "$pattern" | sed 's/^ *//;s/ *$//') # Check if pattern contains range (e.g., 025-027) if [[ "$pattern" == *"-"* ]]; then local start="${pattern%-*}" local end="${pattern#*-}" # Convert to integers (removing leading zeros) local start_num=$((10#${start})) local end_num=$((10#${end})) # Generate range for ((i=start_num; i<=end_num; i++)); do # Format the number with leading zeros formatted_num=$(printf "%0${#start}d" $i) result+=("$prefix$formatted_num") done else # Single item result+=("$prefix$pattern") fi done else # No pattern, just return the input as is result+=("$input") fi # Return each array element on a separate line printf '%s\n' "${result[@]}" } # Load user list from file userlist_file="/home/dyvm6xra/dyvm6xrauser04/bin/sgpu.userlist" declare -A username_realname while read -r line; do username="${line%% *}" # 第一个字段(用户名) realname="${line#"$username"}" # 剩下的部分(姓名) realname="${realname#"${realname%%[![:space:]]*}"}" # 去掉前导空格 username_realname["$username"]="$realname" done < "$userlist_file" # Add Job Summary Section Header - bright yellow echo -e "\033[1;93m■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ SUMMARY BY JOB ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■\033[0m" users_with_job="" declare -A user_stats declare -A node_stats declare -A user_node_stats # Get running jobs - bright green echo -e "\033[1;92m▶▶▶ RUNNING JOBS ◀◀◀\033[0m" # Column headers for running jobs echo -e "\033[1;36m------------------------------------------------------------------------------------------------------------\033[0m" echo -e "\033[1;36mJOBID PARTITION NAME USER STATE TIME NODES CPUS MIN_MEMORY GPUS NODELIST\033[0m" echo -e "\033[1;36m------------------------------------------------------------------------------------------------------------\033[0m" # Loop through each running job, record user-node-specific information and print running jobs details, apply filters if specified num_displayed_running=0 for ((i=0; i