github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/deploy/dev/docker/logs.sh (about) 1 #!/bin/bash 2 # The following script is used to view logs of docker containers. 3 name=`basename "$0"` 4 usage() { 5 echo "=============================================================== USAGE =========================================================" 6 echo "$name [-c=container_name] [-d] [-f] [-n=1000] [-t=<error|warning|info|all>] [-w]" 7 echo " -c=NAME or --container=NAME -> To view the log files of container named 'NAME'." 8 echo " Note, if no container is provided, then all log files are shown for all containers" 9 echo " -d or --default -> default resolves to: $0 --type=all --lines=1000." 10 echo " -f or --follow -> Follow the log file (essentially tail -f)." 11 echo " -n=NUM or --lines=NUM -> Prints the last 'NUM' lines instead of last 100 lines of the log file." 12 echo " -t=TYPE or --type=TYPE -> the type of log file to view where 'TYPE' is one of error(e), warning(w), info(i) or all(a)." 13 echo " -w or --watch -> Use the Linux watch command to avoid cluttering the terminal window with too many logs." 14 echo " Note cannot watch all containers." 15 echo "Note: Providing both the watch and follow flags will result in the watch flag taking precedence." 16 exit 1 17 } 18 19 # valid_container_name checks if $1 is a valid container name 20 valid_container_name() { 21 if [ "$1" == "all" ]; then 22 return 23 fi 24 found=FALSE 25 for container_name in $(docker ps --format "{{.Names}}"); do 26 if [ "$1" == "$container_name" ]; then 27 found=TRUE 28 break 29 fi 30 done 31 32 if [ "$found" == "FALSE" ]; then 33 echo "Not a valid container name." 34 exit 1 35 fi 36 } 37 38 # valid_log_file_type checks if $1 is a supported log level 39 valid_log_file_type() { 40 if [ "$1" == "all" ] || [ "$1" == "a" ]; then 41 log_file_name="all" 42 return 43 fi 44 if [ "$1" == "error" ] || [ "$1" == "e" ]; then 45 log_file_name='ais.ERROR' 46 return 47 fi 48 if [ "$1" == "warning" ] || [ "$1" == "w" ]; then 49 log_file_name='ais.WARNING' 50 return 51 fi 52 if [ "$1" == "info" ] || [ "$1" == "i" ]; then 53 log_file_name='ais.INFO' 54 return 55 fi 56 echo "Not a valid log file type" 57 exit 1 58 } 59 60 # container_name_to_folder converts a container name to its respective log directory 61 container_name_to_folder() { 62 directories=("${directories[@]}" "/tmp/ais/$1/log/") 63 } 64 65 # get_container_names gets the container names of all ais containers 66 # and adds their log directories to $directories 67 get_container_names() { 68 for container_name in $(docker ps --format "{{.Names}}"); do 69 if [[ $container_name == ais* ]]; then 70 if [[ $container_name =~ ^ais[0-9]*_(proxy|target)_[0-9]* ]]; then 71 container_name=${BASH_REMATCH[0]} 72 else 73 echo Invalid container name format 74 exit 1 75 fi 76 id=`docker ps | grep $container_name | awk '{print $1}'` # get id of container 77 container_name_to_folder $id 78 fi 79 done 80 } 81 82 # file_path_join joins log directory with a log file in that directory and adds 83 # the file path to array $files 84 file_path_join() { 85 if [ "$2" == "all" ] ; then 86 file_path_join $1 ais.ERROR 87 file_path_join $1 ais.WARNING 88 file_path_join $1 ais.INFO 89 else 90 combined="$1$2" 91 if [ -f $combined ]; then 92 files=("${files[@]}" "$combined") 93 fi 94 fi 95 } 96 97 numArgs=$# 98 line_count=100 99 follow=FALSE 100 watch=FALSE 101 DEFAULT=FALSE 102 container="all" 103 LOG_TYPE="none" 104 105 ################# Parse Arguments ################# 106 for i in "$@" 107 do 108 case $i in 109 -n=*|--lines=*) 110 line_count="${i#*=}" 111 shift # past argument=value 112 ;; 113 114 -c=*|--container=*) 115 container="${i#*=}" 116 shift # past argument=value 117 valid_container_name $container 118 ;; 119 120 -f|--follow) 121 follow=TRUE 122 watch=FALSE 123 shift # past argument 124 ;; 125 126 -w|--watch) 127 watch=TRUE 128 follow=FALSE 129 shift # past argument 130 ;; 131 132 -t=*|--type=*) 133 LOG_TYPE="${i#*=}" 134 shift # past argument=value 135 valid_log_file_type $LOG_TYPE 136 ;; 137 138 -d|--default) 139 shift # past argument 140 line_count=1000 141 break 142 ;; 143 144 *) 145 usage 146 ;; 147 esac 148 done 149 150 # If no arguments are provided, interactively ask for the parameters 151 if [ "$numArgs" -eq 0 ]; then 152 ################# Containers ################# 153 echo "Enter name of container you would like to see logs for ('all' for all containers):" 154 read container 155 if [ -z "$container" ]; then 156 echo "No container name supplied. Defaulting to all" 157 container="all" 158 fi 159 valid_container_name $container 160 161 ################# Log Type ################# 162 echo "Enter type of log file you would like to view. Valid options and their short forms: error(e), warning(w), info(i) or all(a)" 163 read LOG_TYPE 164 if [ -z "$LOG_TYPE" ]; then 165 echo "No log file type supplied. Defaulting to error" 166 LOG_TYPE="error" 167 fi 168 valid_log_file_type $LOG_TYPE 169 170 ################# Number of Lines ################# 171 echo "Enter the number of lines you would like to view" 172 read line_count 173 if ! [[ "$line_count" =~ ^[0-9]+$ ]] ; then 174 echo "Error: '$line_count' is not a number"; exit 1 175 fi 176 177 if [ ! "$container" == "all" ]; then 178 ################# Follow? ################# 179 read -p "Follow the log file (y/n)?" choice 180 case "$choice" in 181 y|Y ) 182 follow=TRUE 183 ;; 184 n|N ) 185 follow=FALSE 186 ;; 187 * ) 188 echo "Invalid input, defaulting to no following" 189 follow=FALSE 190 ;; 191 esac 192 193 ################# Watch? ################# 194 read -p "Watch the log file (y/n)?" choice 195 case "$choice" in 196 y|Y ) 197 watch=TRUE 198 follow=FALSE 199 ;; 200 n|N ) 201 watch=FALSE 202 ;; 203 * ) 204 echo "Invalid input, defaulting to no watching" 205 watch=FALSE 206 ;; 207 esac 208 fi 209 fi 210 211 if [ "$LOG_TYPE" == "none" ]; then 212 LOG_TYPE="all" 213 log_file_name="all" 214 fi 215 216 ################# Determine Command ################# 217 commandPrefix="tail -n $line_count" 218 if [ "$watch" = TRUE ] && [ "$container" != "all" ]; then 219 commandPrefix="watch -n 2 $commandPrefix" 220 elif [ "$follow" = TRUE ] ; then 221 commandPrefix="$commandPrefix -f" 222 fi 223 224 declare -a directories=() 225 if [ "$container" == "all" ]; then 226 get_container_names 227 else 228 container_name_to_folder $container 229 fi 230 231 ################# Determine Log Files ################# 232 declare -a files=() 233 for dir in "${directories[@]}" 234 do 235 file_path_join $dir $log_file_name 236 done 237 238 if [ ${#files[@]} -eq 0 ]; then 239 echo No valid log files found. Exiting... 240 else 241 command="$commandPrefix ${files[@]}" 242 eval $command 243 fi