| 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. |
| 17 | Default output directory is /tmp/dreport |
| 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 |
| 40 | declare -r USERINITIATED_TYPE=0 |
| 41 | declare -r APPLICATIONCORED_TYPE=1 |
| 42 | declare -r DUMP_MAX_SIZE=500 #in KB |
| Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame^] | 43 | declare -r SUMMARY_LOG="summary.log" |
| 44 | declare -r DREPORT_LOG="dreport.log" |
| 45 | declare -r TMP_DIR="/tmp/dreport" |
| Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 46 | |
| 47 | #VARIABLES |
| 48 | declare -x name=$"obmcdump_00000000_$(date +"%s")" |
| 49 | declare -x dump_dir=$TMP_DIR |
| Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame^] | 50 | declare -x dump_id=0 |
| Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 51 | declare -x dump_type=$USERINITIATED_TYPE |
| 52 | declare -x verbose=$FALSE |
| 53 | declare -x quiet=$FALSE |
| 54 | declare -x dump_size=$DUMP_MAX_SIZE |
| Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame^] | 55 | declare -x name_dir="$TMP_DIR/$dump_id/$name" |
| Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 56 | |
| 57 | # PACKAGE VERSION |
| 58 | PACKAGE_VERSION="0.0.1" |
| 59 | |
| Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame^] | 60 | # @brief log the error message |
| 61 | # @param error message |
| 62 | function log_error() |
| 63 | { |
| 64 | echo "ERROR: $@" >>"$name_dir/$DREPORT_LOG" |
| 65 | if ((quiet != TRUE)); then |
| 66 | echo "ERROR: $@" >&2 |
| 67 | fi |
| 68 | } |
| 69 | |
| 70 | # @brief log warning message |
| 71 | # @param warning message |
| 72 | function log_warning() |
| 73 | { |
| 74 | if ((verbose == TRUE)); then |
| 75 | echo "WARNING: $@" >>"$name_dir/$DREPORT_LOG" |
| 76 | if ((quiet != TRUE)); then |
| 77 | echo "WARNING: $@" >&2 |
| 78 | fi |
| 79 | fi |
| 80 | } |
| 81 | |
| 82 | # @brief log info message |
| 83 | # @param info message |
| 84 | function log_info() |
| 85 | { |
| 86 | if ((verbose == TRUE)); then |
| 87 | echo "INFO: $@" >>"$name_dir/$DREPORT_LOG" |
| 88 | if ((quiet != TRUE)); then |
| 89 | echo "INFO: $@" >&1 |
| 90 | fi |
| 91 | fi |
| 92 | } |
| 93 | |
| 94 | # @brief log summary message |
| 95 | # @param message |
| 96 | function log_summary() |
| 97 | { |
| 98 | echo "$@" >> "$name_dir/$SUMMARY_LOG" |
| 99 | if ((quiet != TRUE)); then |
| 100 | echo "$@" >&1 |
| 101 | fi |
| 102 | } |
| 103 | |
| Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 104 | # @brief Main function |
| 105 | function main() |
| 106 | { |
| Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame^] | 107 | mkdir -p "$TMP_DIR/$dump_id/$name" |
| 108 | if [ $? -ne 0 ]; then |
| 109 | log_error "Failed to create the temporary directory." |
| 110 | exit; |
| 111 | fi |
| 112 | |
| 113 | log_summary "Version: $PACKAGE_VERSION" |
| Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | TEMP=`getopt -o n:d:i:t:s:f:vVqh \ |
| 117 | --long name:,dir:,dumpid:,type:,size:,file:,verbose,version,quiet,help \ |
| 118 | -- "$@"` |
| 119 | eval set -- "$TEMP" |
| 120 | |
| 121 | while [[ $# -gt 1 ]]; do |
| 122 | key="$1" |
| 123 | case $key in |
| 124 | -n|--name) |
| 125 | name=$2 |
| 126 | shift 2;; |
| 127 | -d|--dir) |
| 128 | dir=$2 |
| 129 | shift 2;; |
| 130 | -i|--dumpid) |
| 131 | dump_id=$2 |
| 132 | shift 2;; |
| 133 | -t|--type) |
| 134 | dump_type=$2 |
| 135 | shift 2;; |
| 136 | -s|--size) |
| 137 | dump_size=$2 |
| 138 | shift 2;; |
| 139 | -f|--file) |
| 140 | dump_file=$2 |
| 141 | shift 2;; |
| 142 | -v|—-verbose) |
| 143 | verbose=$TRUE |
| 144 | shift;; |
| 145 | -V|--version) |
| 146 | shift;; |
| 147 | -q|—-quiet) |
| 148 | quiet=$TRUE |
| 149 | shift;; |
| 150 | -h|--help) |
| 151 | echo "$help" |
| 152 | exit;; |
| 153 | *) # unknown option |
| Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame^] | 154 | log_error "Unknown argument: $1" |
| 155 | log_info "$help" |
| Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 156 | exit 1;; |
| 157 | esac |
| Jayanth Othayoth | 9e95f4b | 2017-07-24 06:42:24 -0500 | [diff] [blame] | 158 | done |
| Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 159 | |
| 160 | main #main program |
| 161 | exit $? |