| 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 | |
| Jayanth Othayoth | e20d5e0 | 2017-08-09 06:48:45 -0500 | [diff] [blame^] | 67 | # @brief Initial version of the summary log |
| 68 | init_summary() |
| 69 | { |
| 70 | log_summary "Name: $name.tar.xz" |
| 71 | log_summary "Epochtime: $EPOCHTIME" |
| 72 | log_summary "ID: $dump_id" |
| 73 | log_summary "Type: $dump_type" |
| 74 | log_summary "Optional file: $optional_file" |
| 75 | } |
| Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 76 | |
| Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 77 | # @brief Check the validity of user inputs and initialize global |
| 78 | # variables. Create directory for temporary data collection |
| 79 | # @return 0 on success, error code otherwise |
| 80 | |
| 81 | function initialize() |
| 82 | { |
| 83 | #Dump file name |
| 84 | if [ -z $name ]; then |
| 85 | name=$"obmcdump_"$dump_id"_$EPOCHTIME" |
| 86 | fi |
| 87 | |
| 88 | #Create temporary data directory. |
| 89 | mkdir -p "$TMP_DIR/$name" |
| 90 | if [ $? -ne 0 ]; then |
| 91 | echo "Error: Failed to create the temporary directory." |
| 92 | return $RESOURCE_UNAVAILABLE; |
| 93 | fi |
| 94 | |
| 95 | #name directory |
| 96 | name_dir="$TMP_DIR/$name" |
| 97 | |
| 98 | #dreport log file |
| 99 | dreport_log="$name_dir/$DREPORT_LOG" |
| 100 | |
| 101 | #summary log file |
| 102 | summary_log="$name_dir/$SUMMARY_LOG" |
| 103 | |
| 104 | #Type |
| 105 | if [[ !($dump_type = $TYPE_USER || $dump_type = $TYPE_CORE) ]]; then |
| 106 | log_error "Invalid -type, Only summary log is available" |
| 107 | dump_type=$SUMMARY_DUMP |
| 108 | fi |
| 109 | |
| 110 | #Size |
| 111 | #Check the input is integer. |
| 112 | if [ "$dump_size" -eq "$dump_size" ] 2>/dev/null; then |
| 113 | #Converts in to bytes. |
| 114 | dump_size="$((dump_size * 1024))" |
| 115 | else |
| 116 | dump_size=$UNLIMITED |
| 117 | fi |
| 118 | |
| 119 | return $SUCCESS |
| 120 | } |
| 121 | |
| Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 122 | # @brief Packaging the dump and transferring to dump location. |
| 123 | function package() |
| 124 | { |
| 125 | mkdir -p "$dump_dir" |
| 126 | if [ $? -ne 0 ]; then |
| 127 | log_error "Could not create the destination directory $dump_dir" |
| 128 | dest_dir=$TMP_DIR |
| 129 | fi |
| 130 | |
| 131 | #TODO openbmc/openbmc#1506 Enable file level compression. |
| 132 | #tar and compress the files. |
| 133 | tar_file="$name_dir.tar.xz" |
| 134 | tar -Jcf "$tar_file" -C "$TMP_DIR" "$name" |
| 135 | |
| 136 | #remove the temporary name specific directory |
| 137 | rm -r "$name_dir" |
| 138 | |
| 139 | #check the file size is in the allowed limit |
| 140 | if [ $(stat -c%s "$tar_file") -gt $dump_size ]; then |
| 141 | echo "File size exceeds the limit allowed" |
| 142 | rm -rf "$TMP_DIR" |
| 143 | exit 1 |
| 144 | #TODO openbmc/openbmc#1506 Revisit the error handling |
| 145 | fi |
| 146 | |
| 147 | echo "Report is available in $dump_dir" |
| 148 | |
| 149 | if [ "$TMP_DIR" == "$dump_dir" ]; then |
| 150 | return |
| 151 | fi |
| 152 | |
| 153 | #copy the compressed tar file into the destination |
| 154 | cp "$tar_file" "$dump_dir" |
| 155 | if [ $? -ne 0 ]; then |
| 156 | echo "Failed to copy the $tar_file to $dump_dir" |
| 157 | return |
| 158 | else |
| 159 | rm -rf "$TMP_DIR" |
| 160 | fi |
| 161 | } |
| Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 162 | # @brief log the error message |
| 163 | # @param error message |
| 164 | function log_error() |
| 165 | { |
| Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 166 | echo "ERROR: $@" >> $dreport_log |
| Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 167 | if ((quiet != TRUE)); then |
| 168 | echo "ERROR: $@" >&2 |
| 169 | fi |
| 170 | } |
| 171 | |
| 172 | # @brief log warning message |
| 173 | # @param warning message |
| 174 | function log_warning() |
| 175 | { |
| 176 | if ((verbose == TRUE)); then |
| Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 177 | echo "WARNING: $@" >> $dreport_log |
| Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 178 | if ((quiet != TRUE)); then |
| 179 | echo "WARNING: $@" >&2 |
| 180 | fi |
| 181 | fi |
| 182 | } |
| 183 | |
| 184 | # @brief log info message |
| 185 | # @param info message |
| 186 | function log_info() |
| 187 | { |
| 188 | if ((verbose == TRUE)); then |
| Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 189 | echo "INFO: $@" >> $dreport_log |
| Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 190 | if ((quiet != TRUE)); then |
| 191 | echo "INFO: $@" >&1 |
| 192 | fi |
| 193 | fi |
| 194 | } |
| 195 | |
| 196 | # @brief log summary message |
| 197 | # @param message |
| 198 | function log_summary() |
| 199 | { |
| Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 200 | echo "$@" >> $summary_log |
| 201 | if ((quiet != TRUE)); then |
| Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 202 | echo "$@" >&1 |
| Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 203 | fi |
| Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 204 | } |
| 205 | |
| Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 206 | # @brief Main function |
| 207 | function main() |
| 208 | { |
| Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 209 | #initialize the global variables and |
| 210 | #create temporary storage locations |
| 211 | initialize |
| 212 | result=$? |
| 213 | if [[ ${result} -ne $SUCCESS ]]; then |
| 214 | echo $(date -u)" Error: Failed to initialize, Exiting" |
| 215 | exit; |
| 216 | fi |
| Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 217 | |
| Jayanth Othayoth | e20d5e0 | 2017-08-09 06:48:45 -0500 | [diff] [blame^] | 218 | #Initilize the summary log |
| 219 | init_summary |
| 220 | |
| Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 221 | #TODO Add Dump report generating script. |
| Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 222 | |
| Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 223 | package #package the dump |
| Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | TEMP=`getopt -o n:d:i:t:s:f:vVqh \ |
| 227 | --long name:,dir:,dumpid:,type:,size:,file:,verbose,version,quiet,help \ |
| 228 | -- "$@"` |
| Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 229 | |
| 230 | if [ $? -ne 0 ] |
| 231 | then |
| 232 | echo "Error: Invalid options" |
| 233 | exit 1 |
| 234 | fi |
| 235 | |
| Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 236 | eval set -- "$TEMP" |
| 237 | |
| 238 | while [[ $# -gt 1 ]]; do |
| 239 | key="$1" |
| 240 | case $key in |
| 241 | -n|--name) |
| 242 | name=$2 |
| 243 | shift 2;; |
| 244 | -d|--dir) |
| Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 245 | dump_dir=$2 |
| Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 246 | shift 2;; |
| 247 | -i|--dumpid) |
| 248 | dump_id=$2 |
| 249 | shift 2;; |
| 250 | -t|--type) |
| 251 | dump_type=$2 |
| 252 | shift 2;; |
| 253 | -s|--size) |
| 254 | dump_size=$2 |
| 255 | shift 2;; |
| 256 | -f|--file) |
| Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 257 | optional_file=$2 |
| Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 258 | shift 2;; |
| 259 | -v|—-verbose) |
| 260 | verbose=$TRUE |
| 261 | shift;; |
| 262 | -V|--version) |
| 263 | shift;; |
| 264 | -q|—-quiet) |
| 265 | quiet=$TRUE |
| 266 | shift;; |
| 267 | -h|--help) |
| 268 | echo "$help" |
| 269 | exit;; |
| 270 | *) # unknown option |
| Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 271 | log_error "Unknown argument: $1" |
| 272 | log_info "$help" |
| Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 273 | exit 1;; |
| 274 | esac |
| Jayanth Othayoth | 9e95f4b | 2017-07-24 06:42:24 -0500 | [diff] [blame] | 275 | done |
| Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 276 | |
| 277 | main #main program |
| 278 | exit $? |