Jayanth Othayoth | 5ce1550 | 2017-10-16 00:02:44 -0500 | [diff] [blame] | 1 | #! /bin/bash |
| 2 | |
Patrick Williams | 9d26e4f | 2022-12-08 06:46:44 -0600 | [diff] [blame] | 3 | help=$(cat << EOF |
Jayanth Othayoth | 5ce1550 | 2017-10-16 00:02:44 -0500 | [diff] [blame] | 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 |
| 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", "elog". |
| 23 | Default type is "user" initiated. |
| 24 | -p, —-path <path> Optional contents to be included in the archive. |
| 25 | Valid paths are absolute file path or d-bus path |
| 26 | based on type parameter. |
| 27 | -Absolute file path for "core" type. |
| 28 | -elog d-bus object for "elog" type. |
| 29 | -s, --size <size> Maximum allowed size(in KB) of the archive. |
| 30 | Report will be truncated in case size exceeds |
| 31 | this limit. Default size is unlimited. |
| 32 | -v, —-verbose Increase logging verbosity. |
| 33 | -V, --version Output version information. |
| 34 | -q, —-quiet Only log fatal errors to stderr |
| 35 | -h, —-help Display this help and exit. |
Patrick Williams | 9d26e4f | 2022-12-08 06:46:44 -0600 | [diff] [blame] | 36 | EOF |
| 37 | ) |
Jayanth Othayoth | 5ce1550 | 2017-10-16 00:02:44 -0500 | [diff] [blame] | 38 | |
| 39 | #CONSTANTS |
| 40 | declare -rx TRUE=1 |
| 41 | declare -rx FALSE=0 |
| 42 | declare -rx UNLIMITED="unlimited" |
| 43 | declare -rx SUMMARY_DUMP="summary" |
| 44 | declare -rx TYPE_USER="user" |
| 45 | declare -rx TYPE_CORE="core" |
| 46 | declare -rx TYPE_ELOG="elog" |
Marri Devender Rao | 0deb287 | 2018-11-12 07:45:54 -0600 | [diff] [blame] | 47 | declare -rx TYPE_CHECKSTOP="checkstop" |
George Liu | ff92ffe | 2021-02-09 15:01:53 +0800 | [diff] [blame] | 48 | declare -rx TYPE_RAMOOPS="ramoops" |
Jayanth Othayoth | 5ce1550 | 2017-10-16 00:02:44 -0500 | [diff] [blame] | 49 | declare -rx SUMMARY_LOG="summary.log" |
| 50 | declare -rx DREPORT_LOG="dreport.log" |
| 51 | declare -rx TMP_DIR="/tmp" |
| 52 | declare -rx EPOCHTIME=$(date +"%s") |
| 53 | declare -rx TIME_STAMP="date -u" |
| 54 | declare -rx PLUGIN="pl_" |
| 55 | declare -rx DREPORT_SOURCE="/usr/share/dreport.d" |
| 56 | declare -rx DREPORT_INCLUDE="$DREPORT_SOURCE/include.d" |
Jayanth Othayoth | aa146d6 | 2018-01-08 06:57:53 -0600 | [diff] [blame] | 57 | declare -rx ZERO="0" |
| 58 | declare -rx JOURNAL_LINE_LIMIT="500" |
Chirag Sharma | c237e3d | 2020-09-15 12:01:01 -0500 | [diff] [blame] | 59 | declare -rx HEADER_EXTENSION="$DREPORT_INCLUDE/gendumpheader" |
Jayanth Othayoth | 5ce1550 | 2017-10-16 00:02:44 -0500 | [diff] [blame] | 60 | |
| 61 | #Error Codes |
| 62 | declare -rx SUCCESS="0" |
| 63 | declare -rx INTERNAL_FAILURE="1" |
| 64 | declare -rx RESOURCE_UNAVAILABLE="2" |
| 65 | |
| 66 | #VARIABLES |
| 67 | declare -x name="" |
| 68 | declare -x dump_dir="/tmp" |
| 69 | declare -x dump_id="00000000" |
| 70 | declare -x dump_type=$TYPE_USER |
| 71 | declare -x verbose=$FALSE |
| 72 | declare -x quiet=$FALSE |
| 73 | declare -x dump_size="unlimited" |
| 74 | declare -x name_dir="" |
| 75 | declare -x optional_path="" |
| 76 | declare -x dreport_log="" |
| 77 | declare -x summary_log="" |
| 78 | declare -x cur_dump_size=0 |
Jayanth Othayoth | aa146d6 | 2018-01-08 06:57:53 -0600 | [diff] [blame] | 79 | declare -x pid=$ZERO |
Jayanth Othayoth | 5ce1550 | 2017-10-16 00:02:44 -0500 | [diff] [blame] | 80 | declare -x elog_id="" |
| 81 | |
| 82 | #Source dreport common functions |
| 83 | . $DREPORT_INCLUDE/functions |
| 84 | |
| 85 | # @brief Initiate data collection based on the type. |
| 86 | # @return 0 on success, error code otherwise |
| 87 | function collect_data() |
| 88 | { |
| 89 | case $dump_type in |
| 90 | $TYPE_USER) |
| 91 | ;; |
| 92 | $TYPE_CORE) |
| 93 | log_summary "Core: $optional_path" |
| 94 | set_core_pid |
| 95 | ;; |
George Liu | ff92ffe | 2021-02-09 15:01:53 +0800 | [diff] [blame] | 96 | $TYPE_RAMOOPS) |
| 97 | log_summary "Ramoops: $optional_path" |
| 98 | ;; |
Jayanth Othayoth | 5ce1550 | 2017-10-16 00:02:44 -0500 | [diff] [blame] | 99 | $TYPE_ELOG) |
| 100 | log_summary "ELOG: $optional_path" |
| 101 | elog_id=$(basename "$optional_path") |
| 102 | set_elog_pid |
| 103 | ;; |
Marri Devender Rao | 0deb287 | 2018-11-12 07:45:54 -0600 | [diff] [blame] | 104 | $TYPE_CHECKSTOP) |
| 105 | log_summary "CHECKSTOP: $optional_path" |
| 106 | elog_id=$(basename "$optional_path") |
| 107 | set_elog_pid |
| 108 | ;; |
Jayanth Othayoth | 5ce1550 | 2017-10-16 00:02:44 -0500 | [diff] [blame] | 109 | |
| 110 | $SUMMARY_DUMP) |
| 111 | #No data collection is required. |
| 112 | return |
| 113 | ;; |
| 114 | *) # unknown option |
| 115 | log_error "Skipping: Unknown dump type: $dump_type" |
| 116 | return |
| 117 | ;; |
| 118 | esac |
| 119 | |
| 120 | plugin_path=$DREPORT_SOURCE/$PLUGIN$dump_type.d |
| 121 | |
| 122 | # check plugin directory for this dump type? |
| 123 | if [ ! -d $plugin_path ]; then |
| 124 | log_error "$plugin_path does not exist, skipping dump collection" |
| 125 | return 0 |
| 126 | fi |
| 127 | |
| 128 | #Executes plugins based on the type. |
| 129 | for i in $plugin_path/* ; do |
Patrick Williams | 9d26e4f | 2022-12-08 06:46:44 -0600 | [diff] [blame] | 130 | $i |
Jayanth Othayoth | 5ce1550 | 2017-10-16 00:02:44 -0500 | [diff] [blame] | 131 | done |
| 132 | } |
| 133 | |
| 134 | # @brief set pid by reading information from the optional path. |
| 135 | # dreport "core" type user provides core file as optional path parameter. |
| 136 | # As per coredump source code systemd-coredump uses below format |
| 137 | # https://github.com/systemd/systemd/blob/master/src/coredump/coredump.c |
| 138 | # /var/lib/systemd/coredump/core.%s.%s." SD_ID128_FORMAT_STR “. |
| 139 | # <process ID>.%s000000" |
| 140 | function set_core_pid() |
| 141 | { |
| 142 | #Escape bash characters in file name |
| 143 | file=$(printf %q "$optional_path") |
| 144 | |
| 145 | #matching systemd-coredump core file format. |
| 146 | pid=$(echo $file | awk -F . '{ print $5}') |
| 147 | } |
| 148 | |
| 149 | # @brief set elog pid by reading _PID information from the elog d-bus object. |
| 150 | # _PID information is stored elog Additional data field |
| 151 | # Data format "_PID=<pid>" |
| 152 | function set_elog_pid() |
| 153 | { |
| 154 | additional_data=$(busctl get-property xyz.openbmc_project.Logging \ |
Patrick Williams | 9d26e4f | 2022-12-08 06:46:44 -0600 | [diff] [blame] | 155 | $optional_path \ |
| 156 | xyz.openbmc_project.Logging.Entry \ |
| 157 | AdditionalData) |
Jayanth Othayoth | 5ce1550 | 2017-10-16 00:02:44 -0500 | [diff] [blame] | 158 | |
| 159 | #read _PID data. |
| 160 | if [ ! -z "$additional_data" ]; then |
| 161 | pid=$(echo $additional_data | \ |
Patrick Williams | 9d26e4f | 2022-12-08 06:46:44 -0600 | [diff] [blame] | 162 | awk -F _PID= '{ print ($2+0)}') |
Jayanth Othayoth | 5ce1550 | 2017-10-16 00:02:44 -0500 | [diff] [blame] | 163 | fi |
| 164 | } |
| 165 | |
| 166 | # @brief Initial version of the summary log |
Patrick Williams | 9d26e4f | 2022-12-08 06:46:44 -0600 | [diff] [blame] | 167 | function init_summary() |
Jayanth Othayoth | 5ce1550 | 2017-10-16 00:02:44 -0500 | [diff] [blame] | 168 | { |
Patrick Williams | 9d26e4f | 2022-12-08 06:46:44 -0600 | [diff] [blame] | 169 | log_summary "Name: $name.tar.xz" |
| 170 | log_summary "Epochtime: $EPOCHTIME" |
| 171 | log_summary "ID: $dump_id" |
| 172 | log_summary "Type: $dump_type" |
Jayanth Othayoth | 5ce1550 | 2017-10-16 00:02:44 -0500 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | # @brief Check the validity of user inputs and initialize global |
| 176 | # variables. Create directory for temporary data collection |
| 177 | # @return 0 on success, error code otherwise |
| 178 | |
| 179 | function initialize() |
| 180 | { |
| 181 | #Dump file name |
| 182 | if [ -z $name ]; then |
| 183 | name=$"obmcdump_"$dump_id"_$EPOCHTIME" |
| 184 | fi |
| 185 | |
| 186 | #Create temporary data directory. |
| 187 | mkdir -p "$TMP_DIR/$name" |
| 188 | if [ $? -ne 0 ]; then |
| 189 | echo "Error: Failed to create the temporary directory." |
| 190 | return $RESOURCE_UNAVAILABLE; |
| 191 | fi |
| 192 | |
| 193 | #name directory |
| 194 | name_dir="$TMP_DIR/$name" |
| 195 | |
| 196 | #dreport log file |
| 197 | dreport_log="$name_dir/$DREPORT_LOG" |
| 198 | |
| 199 | #summary log file |
| 200 | summary_log="$name_dir/$SUMMARY_LOG" |
| 201 | |
| 202 | #Type |
Jayanth Othayoth | 40d6719 | 2023-02-01 06:36:38 -0600 | [diff] [blame] | 203 | if ! { [[ $dump_type = "$TYPE_USER" ]] || \ |
| 204 | [[ $dump_type = "$TYPE_CORE" ]] || \ |
| 205 | [[ $dump_type = "$TYPE_ELOG" ]] || \ |
| 206 | [[ $dump_type = "$TYPE_RAMOOPS" ]] || \ |
| 207 | [[ $dump_type = "$TYPE_CHECKSTOP" ]]; }; then |
Patrick Williams | 9d26e4f | 2022-12-08 06:46:44 -0600 | [diff] [blame] | 208 | log_error "Invalid -type, Only summary log is available" |
| 209 | dump_type=$SUMMARY_DUMP |
Jayanth Othayoth | 5ce1550 | 2017-10-16 00:02:44 -0500 | [diff] [blame] | 210 | fi |
| 211 | |
| 212 | #Size |
| 213 | #Check the input is integer. |
| 214 | if [ "$dump_size" -eq "$dump_size" ] 2>/dev/null; then |
Patrick Williams | 9d26e4f | 2022-12-08 06:46:44 -0600 | [diff] [blame] | 215 | #Converts in to bytes. |
| 216 | dump_size="$((dump_size * 1024))" |
Jayanth Othayoth | 5ce1550 | 2017-10-16 00:02:44 -0500 | [diff] [blame] | 217 | else |
Patrick Williams | 9d26e4f | 2022-12-08 06:46:44 -0600 | [diff] [blame] | 218 | dump_size=$UNLIMITED |
Jayanth Othayoth | 5ce1550 | 2017-10-16 00:02:44 -0500 | [diff] [blame] | 219 | fi |
| 220 | |
| 221 | return $SUCCESS |
| 222 | } |
| 223 | |
| 224 | # @brief Packaging the dump and transferring to dump location. |
| 225 | function package() |
| 226 | { |
| 227 | mkdir -p "$dump_dir" |
| 228 | if [ $? -ne 0 ]; then |
| 229 | log_error "Could not create the destination directory $dump_dir" |
| 230 | dest_dir=$TMP_DIR |
| 231 | fi |
| 232 | |
| 233 | #tar and compress the files. |
Chirag Sharma | c237e3d | 2020-09-15 12:01:01 -0500 | [diff] [blame] | 234 | if [ -f "$HEADER_EXTENSION" ]; then |
| 235 | tar -Jcf "$name_dir.tar.xz" -C \ |
Patrick Williams | 9d26e4f | 2022-12-08 06:46:44 -0600 | [diff] [blame] | 236 | $(dirname "$name_dir") $(basename "$name_dir") |
Chirag Sharma | c237e3d | 2020-09-15 12:01:01 -0500 | [diff] [blame] | 237 | echo "Adding Dump Header :"$HEADER_EXTENSION |
| 238 | ("$HEADER_EXTENSION") |
| 239 | cat "$name_dir.tar.xz" | tee -a "/tmp/dumpheader_$EPOCHTIME" > /dev/null |
| 240 | mv "/tmp/dumpheader_$EPOCHTIME" "$name_dir.tar.xz" |
| 241 | else |
| 242 | tar -Jcf "$name_dir.tar.xz" -C \ |
Patrick Williams | 9d26e4f | 2022-12-08 06:46:44 -0600 | [diff] [blame] | 243 | $(dirname "$name_dir") $(basename "$name_dir") |
Chirag Sharma | c237e3d | 2020-09-15 12:01:01 -0500 | [diff] [blame] | 244 | fi |
Jayanth Othayoth | 5ce1550 | 2017-10-16 00:02:44 -0500 | [diff] [blame] | 245 | |
| 246 | if [ $? -ne 0 ]; then |
| 247 | echo $($TIME_STAMP) "Could not create the compressed tar file" |
| 248 | rm -r "$name_dir" |
| 249 | return $INTERNAL_FAILURE |
| 250 | fi |
| 251 | |
| 252 | #remove the temporary name specific directory |
| 253 | rm -r "$name_dir" |
| 254 | |
| 255 | echo $($TIME_STAMP) "Report is available in $dump_dir" |
| 256 | |
Chirag Sharma | a63f4a6 | 2020-07-13 06:14:22 -0500 | [diff] [blame] | 257 | if [ "$TMP_DIR" == "$dump_dir" ] || [ "$TMP_DIR/" == "$dump_dir" ]; then |
Patrick Williams | 9d26e4f | 2022-12-08 06:46:44 -0600 | [diff] [blame] | 258 | return $SUCCESS |
Jayanth Othayoth | 5ce1550 | 2017-10-16 00:02:44 -0500 | [diff] [blame] | 259 | fi |
| 260 | |
| 261 | #copy the compressed tar file into the destination |
| 262 | cp "$name_dir.tar.xz" "$dump_dir" |
| 263 | if [ $? -ne 0 ]; then |
| 264 | echo "Failed to copy the $name_dir.tar.xz to $dump_dir" |
| 265 | rm "$name_dir.tar.xz" |
| 266 | return $INTERNAL_FAILURE |
| 267 | fi |
| 268 | |
| 269 | #Remove the temporary copy of the file |
| 270 | rm "$name_dir.tar.xz" |
| 271 | } |
| 272 | |
| 273 | # @brief Main function |
| 274 | function main() |
| 275 | { |
| 276 | #initialize the global variables and |
| 277 | #create temporary storage locations |
| 278 | initialize |
| 279 | result=$? |
| 280 | if [[ ${result} -ne $SUCCESS ]]; then |
| 281 | echo $($TIME_STAMP) "Error: Failed to initialize, Exiting" |
| 282 | exit; |
| 283 | fi |
| 284 | |
Gunnar Mills | 95a7298 | 2017-10-25 17:00:14 -0500 | [diff] [blame] | 285 | #Initialize the summary log |
Jayanth Othayoth | 5ce1550 | 2017-10-16 00:02:44 -0500 | [diff] [blame] | 286 | init_summary |
| 287 | |
| 288 | #collect data based on the type. |
| 289 | collect_data |
| 290 | |
| 291 | package #package the dump |
| 292 | result=$? |
| 293 | if [[ ${result} -ne $SUCCESS ]]; then |
| 294 | echo $($TIME_STAMP) "Error: Failed to package, Exiting" |
| 295 | else |
Gunnar Mills | 95a7298 | 2017-10-25 17:00:14 -0500 | [diff] [blame] | 296 | echo $($TIME_STAMP) "Successfully completed" |
Jayanth Othayoth | 5ce1550 | 2017-10-16 00:02:44 -0500 | [diff] [blame] | 297 | exit; |
| 298 | fi |
| 299 | } |
| 300 | |
| 301 | TEMP=`getopt -o n:d:i:t:s:p:vVqh \ |
Patrick Williams | 9d26e4f | 2022-12-08 06:46:44 -0600 | [diff] [blame] | 302 | --long name:,dir:,dumpid:,type:,size:,path:,verbose,version,quiet,help \ |
| 303 | -- "$@"` |
Jayanth Othayoth | 5ce1550 | 2017-10-16 00:02:44 -0500 | [diff] [blame] | 304 | |
| 305 | if [ $? -ne 0 ] |
| 306 | then |
| 307 | echo "Error: Invalid options" |
| 308 | exit 1 |
| 309 | fi |
| 310 | |
| 311 | eval set -- "$TEMP" |
| 312 | |
| 313 | while [[ $# -gt 1 ]]; do |
| 314 | key="$1" |
| 315 | case $key in |
| 316 | -n|--name) |
| 317 | name=$2 |
Patrick Williams | 9d26e4f | 2022-12-08 06:46:44 -0600 | [diff] [blame] | 318 | shift 2 ;; |
Jayanth Othayoth | 5ce1550 | 2017-10-16 00:02:44 -0500 | [diff] [blame] | 319 | -d|--dir) |
| 320 | dump_dir=$2 |
Patrick Williams | 9d26e4f | 2022-12-08 06:46:44 -0600 | [diff] [blame] | 321 | shift 2 ;; |
Jayanth Othayoth | 5ce1550 | 2017-10-16 00:02:44 -0500 | [diff] [blame] | 322 | -i|--dumpid) |
| 323 | dump_id=$2 |
Patrick Williams | 9d26e4f | 2022-12-08 06:46:44 -0600 | [diff] [blame] | 324 | shift 2 ;; |
Jayanth Othayoth | 5ce1550 | 2017-10-16 00:02:44 -0500 | [diff] [blame] | 325 | -t|--type) |
| 326 | dump_type=$2 |
Patrick Williams | 9d26e4f | 2022-12-08 06:46:44 -0600 | [diff] [blame] | 327 | shift 2 ;; |
Jayanth Othayoth | 5ce1550 | 2017-10-16 00:02:44 -0500 | [diff] [blame] | 328 | -s|--size) |
| 329 | dump_size=$2 |
Patrick Williams | 9d26e4f | 2022-12-08 06:46:44 -0600 | [diff] [blame] | 330 | shift 2 ;; |
Jayanth Othayoth | 5ce1550 | 2017-10-16 00:02:44 -0500 | [diff] [blame] | 331 | -p|--path) |
| 332 | optional_path=$2 |
Patrick Williams | 9d26e4f | 2022-12-08 06:46:44 -0600 | [diff] [blame] | 333 | shift 2 ;; |
Jayanth Othayoth | 5ce1550 | 2017-10-16 00:02:44 -0500 | [diff] [blame] | 334 | -v|—-verbose) |
| 335 | verbose=$TRUE |
Patrick Williams | 9d26e4f | 2022-12-08 06:46:44 -0600 | [diff] [blame] | 336 | shift ;; |
Jayanth Othayoth | 5ce1550 | 2017-10-16 00:02:44 -0500 | [diff] [blame] | 337 | -V|--version) |
Patrick Williams | 9d26e4f | 2022-12-08 06:46:44 -0600 | [diff] [blame] | 338 | shift ;; |
Jayanth Othayoth | 5ce1550 | 2017-10-16 00:02:44 -0500 | [diff] [blame] | 339 | -q|—-quiet) |
| 340 | quiet=$TRUE |
Patrick Williams | 9d26e4f | 2022-12-08 06:46:44 -0600 | [diff] [blame] | 341 | shift ;; |
Jayanth Othayoth | 5ce1550 | 2017-10-16 00:02:44 -0500 | [diff] [blame] | 342 | -h|--help) |
| 343 | echo "$help" |
Patrick Williams | 9d26e4f | 2022-12-08 06:46:44 -0600 | [diff] [blame] | 344 | exit ;; |
Jayanth Othayoth | 5ce1550 | 2017-10-16 00:02:44 -0500 | [diff] [blame] | 345 | *) # unknown option |
| 346 | log_error "Unknown argument: $1" |
| 347 | log_info "$help" |
Patrick Williams | 9d26e4f | 2022-12-08 06:46:44 -0600 | [diff] [blame] | 348 | exit 1 ;; |
Jayanth Othayoth | 5ce1550 | 2017-10-16 00:02:44 -0500 | [diff] [blame] | 349 | esac |
| 350 | done |
| 351 | |
| 352 | main #main program |
| 353 | exit $? |