Jayanth Othayoth | 9e95f4b | 2017-07-24 06:42:24 -0500 | [diff] [blame] | 1 | #! /bin/bash |
| 2 | |
| 3 | help=$" |
| 4 | dreport creates an archive(xz compressed) consisting of the following: |
| 5 | * Configuration information |
| 6 | * Debug information |
| 7 | * A summary report |
| 8 | The type parameter controls the content of the data. The generated |
| 9 | archive is stored in the user specified location. |
| 10 | |
| 11 | usage: dreport [OPTION] |
| 12 | |
| 13 | Options: |
| 14 | -n, —-name <name> Name to be used for the archive. |
| 15 | Default name format obmcdump_<id>_<epochtime> |
| 16 | -d, —-dir <directory> Archive directory to copy the compressed report. |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame^] | 17 | Default output directory is /tmp |
Jayanth Othayoth | 9e95f4b | 2017-07-24 06:42:24 -0500 | [diff] [blame] | 18 | -i, —-id <id> Dump identifier to associate with the archive. |
| 19 | Identifiers include numeric characters. |
| 20 | Default dump identifier is 0 |
| 21 | -t, —-type <type> Data collection type. Valid types are |
| 22 | "user", "core". |
| 23 | Default type is "user" initiated. |
| 24 | -f, —-file <file> Optional file to be included in the archive. |
| 25 | Absolute path of the file must be passed as |
| 26 | parameter. This is useful to include application |
| 27 | core in the dump. |
| 28 | -s, --size <size> Maximum allowed size(in KB) of the archive. |
| 29 | Report will be truncated in case size exceeds |
| 30 | this limit. Default size is 500KB. |
| 31 | -v, —-verbose Increase logging verbosity. |
| 32 | -V, --version Output version information. |
| 33 | -q, —-quiet Only log fatal errors to stderr |
| 34 | -h, —-help Display this help and exit. |
| 35 | " |
| 36 | |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 37 | #CONSTANTS |
| 38 | declare -r TRUE=1 |
| 39 | declare -r FALSE=0 |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame^] | 40 | declare -r UNLIMITED="unlimited" |
| 41 | declare -r SUMMARY_DUMP="summary" |
| 42 | declare -r TYPE_USER="user" |
| 43 | declare -r TYPE_CORE="core" |
Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 44 | declare -r SUMMARY_LOG="summary.log" |
| 45 | declare -r DREPORT_LOG="dreport.log" |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame^] | 46 | declare -r TMP_DIR="/tmp" |
| 47 | declare -r EPOCHTIME=$(date +"%s") |
| 48 | |
| 49 | #Error Codes |
| 50 | declare -r SUCCESS="0" |
| 51 | declare -r INTERNAL_FAILURE="1" |
| 52 | declare -r RESOURCE_UNAVAILABLE="2" |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 53 | |
| 54 | #VARIABLES |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame^] | 55 | declare -x name="" |
Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 56 | declare -x dump_dir="/tmp" |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame^] | 57 | declare -x dump_id="00000000" |
| 58 | declare -x dump_type=$TYPE_USER |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 59 | declare -x verbose=$FALSE |
| 60 | declare -x quiet=$FALSE |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame^] | 61 | declare -x dump_size="unlimited" |
| 62 | declare -x name_dir="" |
| 63 | declare -x optional_file="" |
| 64 | declare -x dreport_log="" |
| 65 | declare -x summary_log="" |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 66 | |
| 67 | # PACKAGE VERSION |
| 68 | PACKAGE_VERSION="0.0.1" |
| 69 | |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame^] | 70 | # @brief Check the validity of user inputs and initialize global |
| 71 | # variables. Create directory for temporary data collection |
| 72 | # @return 0 on success, error code otherwise |
| 73 | |
| 74 | function initialize() |
| 75 | { |
| 76 | #Dump file name |
| 77 | if [ -z $name ]; then |
| 78 | name=$"obmcdump_"$dump_id"_$EPOCHTIME" |
| 79 | fi |
| 80 | |
| 81 | #Create temporary data directory. |
| 82 | mkdir -p "$TMP_DIR/$name" |
| 83 | if [ $? -ne 0 ]; then |
| 84 | echo "Error: Failed to create the temporary directory." |
| 85 | return $RESOURCE_UNAVAILABLE; |
| 86 | fi |
| 87 | |
| 88 | #name directory |
| 89 | name_dir="$TMP_DIR/$name" |
| 90 | |
| 91 | #dreport log file |
| 92 | dreport_log="$name_dir/$DREPORT_LOG" |
| 93 | |
| 94 | #summary log file |
| 95 | summary_log="$name_dir/$SUMMARY_LOG" |
| 96 | |
| 97 | #Type |
| 98 | if [[ !($dump_type = $TYPE_USER || $dump_type = $TYPE_CORE) ]]; then |
| 99 | log_error "Invalid -type, Only summary log is available" |
| 100 | dump_type=$SUMMARY_DUMP |
| 101 | fi |
| 102 | |
| 103 | #Size |
| 104 | #Check the input is integer. |
| 105 | if [ "$dump_size" -eq "$dump_size" ] 2>/dev/null; then |
| 106 | #Converts in to bytes. |
| 107 | dump_size="$((dump_size * 1024))" |
| 108 | else |
| 109 | dump_size=$UNLIMITED |
| 110 | fi |
| 111 | |
| 112 | return $SUCCESS |
| 113 | } |
| 114 | |
Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 115 | # @brief Packaging the dump and transferring to dump location. |
| 116 | function package() |
| 117 | { |
| 118 | mkdir -p "$dump_dir" |
| 119 | if [ $? -ne 0 ]; then |
| 120 | log_error "Could not create the destination directory $dump_dir" |
| 121 | dest_dir=$TMP_DIR |
| 122 | fi |
| 123 | |
| 124 | #TODO openbmc/openbmc#1506 Enable file level compression. |
| 125 | #tar and compress the files. |
| 126 | tar_file="$name_dir.tar.xz" |
| 127 | tar -Jcf "$tar_file" -C "$TMP_DIR" "$name" |
| 128 | |
| 129 | #remove the temporary name specific directory |
| 130 | rm -r "$name_dir" |
| 131 | |
| 132 | #check the file size is in the allowed limit |
| 133 | if [ $(stat -c%s "$tar_file") -gt $dump_size ]; then |
| 134 | echo "File size exceeds the limit allowed" |
| 135 | rm -rf "$TMP_DIR" |
| 136 | exit 1 |
| 137 | #TODO openbmc/openbmc#1506 Revisit the error handling |
| 138 | fi |
| 139 | |
| 140 | echo "Report is available in $dump_dir" |
| 141 | |
| 142 | if [ "$TMP_DIR" == "$dump_dir" ]; then |
| 143 | return |
| 144 | fi |
| 145 | |
| 146 | #copy the compressed tar file into the destination |
| 147 | cp "$tar_file" "$dump_dir" |
| 148 | if [ $? -ne 0 ]; then |
| 149 | echo "Failed to copy the $tar_file to $dump_dir" |
| 150 | return |
| 151 | else |
| 152 | rm -rf "$TMP_DIR" |
| 153 | fi |
| 154 | } |
Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 155 | # @brief log the error message |
| 156 | # @param error message |
| 157 | function log_error() |
| 158 | { |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame^] | 159 | echo "ERROR: $@" >> $dreport_log |
Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 160 | if ((quiet != TRUE)); then |
| 161 | echo "ERROR: $@" >&2 |
| 162 | fi |
| 163 | } |
| 164 | |
| 165 | # @brief log warning message |
| 166 | # @param warning message |
| 167 | function log_warning() |
| 168 | { |
| 169 | if ((verbose == TRUE)); then |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame^] | 170 | echo "WARNING: $@" >> $dreport_log |
Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 171 | if ((quiet != TRUE)); then |
| 172 | echo "WARNING: $@" >&2 |
| 173 | fi |
| 174 | fi |
| 175 | } |
| 176 | |
| 177 | # @brief log info message |
| 178 | # @param info message |
| 179 | function log_info() |
| 180 | { |
| 181 | if ((verbose == TRUE)); then |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame^] | 182 | echo "INFO: $@" >> $dreport_log |
Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 183 | if ((quiet != TRUE)); then |
| 184 | echo "INFO: $@" >&1 |
| 185 | fi |
| 186 | fi |
| 187 | } |
| 188 | |
| 189 | # @brief log summary message |
| 190 | # @param message |
| 191 | function log_summary() |
| 192 | { |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame^] | 193 | echo "$@" >> $summary_log |
| 194 | if ((quiet != TRUE)); then |
Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 195 | echo "$@" >&1 |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame^] | 196 | fi |
Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 197 | } |
| 198 | |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 199 | # @brief Main function |
| 200 | function main() |
| 201 | { |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame^] | 202 | #initialize the global variables and |
| 203 | #create temporary storage locations |
| 204 | initialize |
| 205 | result=$? |
| 206 | if [[ ${result} -ne $SUCCESS ]]; then |
| 207 | echo $(date -u)" Error: Failed to initialize, Exiting" |
| 208 | exit; |
| 209 | fi |
Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 210 | |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame^] | 211 | #TODO Add Dump report generating script. |
Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 212 | |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame^] | 213 | package #package the dump |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | TEMP=`getopt -o n:d:i:t:s:f:vVqh \ |
| 217 | --long name:,dir:,dumpid:,type:,size:,file:,verbose,version,quiet,help \ |
| 218 | -- "$@"` |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame^] | 219 | |
| 220 | if [ $? -ne 0 ] |
| 221 | then |
| 222 | echo "Error: Invalid options" |
| 223 | exit 1 |
| 224 | fi |
| 225 | |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 226 | eval set -- "$TEMP" |
| 227 | |
| 228 | while [[ $# -gt 1 ]]; do |
| 229 | key="$1" |
| 230 | case $key in |
| 231 | -n|--name) |
| 232 | name=$2 |
| 233 | shift 2;; |
| 234 | -d|--dir) |
Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 235 | dump_dir=$2 |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 236 | shift 2;; |
| 237 | -i|--dumpid) |
| 238 | dump_id=$2 |
| 239 | shift 2;; |
| 240 | -t|--type) |
| 241 | dump_type=$2 |
| 242 | shift 2;; |
| 243 | -s|--size) |
| 244 | dump_size=$2 |
| 245 | shift 2;; |
| 246 | -f|--file) |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame^] | 247 | optional_file=$2 |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 248 | shift 2;; |
| 249 | -v|—-verbose) |
| 250 | verbose=$TRUE |
| 251 | shift;; |
| 252 | -V|--version) |
| 253 | shift;; |
| 254 | -q|—-quiet) |
| 255 | quiet=$TRUE |
| 256 | shift;; |
| 257 | -h|--help) |
| 258 | echo "$help" |
| 259 | exit;; |
| 260 | *) # unknown option |
Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 261 | log_error "Unknown argument: $1" |
| 262 | log_info "$help" |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 263 | exit 1;; |
| 264 | esac |
Jayanth Othayoth | 9e95f4b | 2017-07-24 06:42:24 -0500 | [diff] [blame] | 265 | done |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 266 | |
| 267 | main #main program |
| 268 | exit $? |