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 |
Jayanth Othayoth | d891643 | 2017-09-03 23:14:47 -0500 | [diff] [blame^] | 22 | "user", "core", "elog". |
Jayanth Othayoth | 9e95f4b | 2017-07-24 06:42:24 -0500 | [diff] [blame] | 23 | Default type is "user" initiated. |
Jayanth Othayoth | 2b05adc | 2017-09-03 22:15:01 -0500 | [diff] [blame] | 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. |
Jayanth Othayoth | d891643 | 2017-09-03 23:14:47 -0500 | [diff] [blame^] | 28 | -elog d-bus object for "elog" type. |
Jayanth Othayoth | 9e95f4b | 2017-07-24 06:42:24 -0500 | [diff] [blame] | 29 | -s, --size <size> Maximum allowed size(in KB) of the archive. |
| 30 | Report will be truncated in case size exceeds |
Jayanth Othayoth | ad516f4 | 2017-09-03 21:17:27 -0500 | [diff] [blame] | 31 | this limit. Default size is unlimited. |
Jayanth Othayoth | 9e95f4b | 2017-07-24 06:42:24 -0500 | [diff] [blame] | 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. |
| 36 | " |
| 37 | |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 38 | #CONSTANTS |
| 39 | declare -r TRUE=1 |
| 40 | declare -r FALSE=0 |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 41 | declare -r UNLIMITED="unlimited" |
| 42 | declare -r SUMMARY_DUMP="summary" |
| 43 | declare -r TYPE_USER="user" |
| 44 | declare -r TYPE_CORE="core" |
Jayanth Othayoth | d891643 | 2017-09-03 23:14:47 -0500 | [diff] [blame^] | 45 | declare -r TYPE_ELOG="elog" |
Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 46 | declare -r SUMMARY_LOG="summary.log" |
| 47 | declare -r DREPORT_LOG="dreport.log" |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 48 | declare -r TMP_DIR="/tmp" |
| 49 | declare -r EPOCHTIME=$(date +"%s") |
Jayanth Othayoth | 8d0446e | 2017-08-09 07:26:45 -0500 | [diff] [blame] | 50 | declare -r TIME_STAMP="date -u" |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 51 | |
| 52 | #Error Codes |
| 53 | declare -r SUCCESS="0" |
| 54 | declare -r INTERNAL_FAILURE="1" |
| 55 | declare -r RESOURCE_UNAVAILABLE="2" |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 56 | |
| 57 | #VARIABLES |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 58 | declare -x name="" |
Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 59 | declare -x dump_dir="/tmp" |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 60 | declare -x dump_id="00000000" |
| 61 | declare -x dump_type=$TYPE_USER |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 62 | declare -x verbose=$FALSE |
| 63 | declare -x quiet=$FALSE |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 64 | declare -x dump_size="unlimited" |
| 65 | declare -x name_dir="" |
Jayanth Othayoth | 2b05adc | 2017-09-03 22:15:01 -0500 | [diff] [blame] | 66 | declare -x optional_path="" |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 67 | declare -x dreport_log="" |
| 68 | declare -x summary_log="" |
Jayanth Othayoth | c2ece2d | 2017-08-09 06:57:12 -0500 | [diff] [blame] | 69 | declare -x cur_dump_size=0 |
Jayanth Othayoth | 0862c48 | 2017-08-09 07:02:36 -0500 | [diff] [blame] | 70 | declare -a command_list=("") |
Jayanth Othayoth | fa388d0 | 2017-09-03 22:34:14 -0500 | [diff] [blame] | 71 | declare -x pid="0" |
Jayanth Othayoth | d891643 | 2017-09-03 23:14:47 -0500 | [diff] [blame^] | 72 | declare -x elog_id="" |
| 73 | |
| 74 | # @brief Initialize general command array |
| 75 | # Set of commands, which provide general debug information |
| 76 | function runlevel_general() |
| 77 | { |
| 78 | command_list=( |
| 79 | get_fw_level |
| 80 | bmc_state |
| 81 | host_state |
| 82 | chassis_state |
| 83 | get_host_info |
| 84 | get_uname |
| 85 | get_uptime |
| 86 | get_disk_usage |
| 87 | get_host_info |
| 88 | get_cpuinfo |
| 89 | get_meminfo |
| 90 | get_top |
| 91 | ) |
| 92 | } |
Jayanth Othayoth | 0862c48 | 2017-08-09 07:02:36 -0500 | [diff] [blame] | 93 | |
| 94 | # @brief Initialize user type command array |
| 95 | function runlevel_user() |
| 96 | { |
| 97 | command_list=( |
Jayanth Othayoth | 0862c48 | 2017-08-09 07:02:36 -0500 | [diff] [blame] | 98 | get_journal |
Jayanth Othayoth | d891643 | 2017-09-03 23:14:47 -0500 | [diff] [blame^] | 99 | get_elog_all |
| 100 | get_obmc_console |
Jayanth Othayoth | 0862c48 | 2017-08-09 07:02:36 -0500 | [diff] [blame] | 101 | get_failed_services |
| 102 | ) |
| 103 | } |
| 104 | |
Jayanth Othayoth | e61aee3 | 2017-08-09 07:05:06 -0500 | [diff] [blame] | 105 | # @brief Initialize core type command array |
| 106 | function runlevel_core() |
| 107 | { |
| 108 | command_list=( |
| 109 | move_optional_file |
| 110 | get_proc_journal |
Jayanth Othayoth | e61aee3 | 2017-08-09 07:05:06 -0500 | [diff] [blame] | 111 | get_failed_services |
| 112 | get_obmc_console |
Jayanth Othayoth | d891643 | 2017-09-03 23:14:47 -0500 | [diff] [blame^] | 113 | ) |
| 114 | } |
| 115 | |
| 116 | # @brief Initialize elog type command array |
| 117 | function runlevel_elog() |
| 118 | { |
| 119 | command_list=( |
| 120 | get_proc_journal |
| 121 | get_elog |
| 122 | ) |
Jayanth Othayoth | e61aee3 | 2017-08-09 07:05:06 -0500 | [diff] [blame] | 123 | } |
| 124 | |
Jayanth Othayoth | 0862c48 | 2017-08-09 07:02:36 -0500 | [diff] [blame] | 125 | function get_fw_level() |
| 126 | { |
| 127 | desc="Firmware Release" |
| 128 | command="cat /etc/os-release" |
| 129 | copy_loc="firmware_release.log" |
| 130 | run_command "$command" "$copy_loc" "$desc" |
| 131 | } |
| 132 | |
| 133 | function get_uname() |
| 134 | { |
| 135 | desc="uname" |
| 136 | command="uname -a" |
| 137 | copy_loc="uname.log" |
| 138 | run_command "$command" "$copy_loc" "$desc" |
| 139 | } |
| 140 | |
| 141 | function get_uptime() |
| 142 | { |
| 143 | desc="uptime" |
| 144 | command="uptime" |
| 145 | copy_loc="uptime.log" |
| 146 | run_command "$command" "$copy_loc" "$desc" |
| 147 | } |
| 148 | |
| 149 | function get_disk_usage() |
| 150 | { |
| 151 | desc="Disk Usage" |
| 152 | command="df -hT" |
| 153 | copy_loc="disk_usage.log" |
| 154 | run_command "$command" "$copy_loc" "$desc" |
| 155 | } |
| 156 | |
| 157 | function get_journal() |
| 158 | { |
| 159 | desc="Journal log" |
| 160 | command="journalctl -o json-pretty -r" |
| 161 | copy_loc="journal.log" |
| 162 | run_command "$command" "$copy_loc" "$desc" |
| 163 | } |
| 164 | |
Jayanth Othayoth | e61aee3 | 2017-08-09 07:05:06 -0500 | [diff] [blame] | 165 | function get_proc_journal() |
| 166 | { |
| 167 | desc="Process Journal log" |
Jayanth Othayoth | fa388d0 | 2017-09-03 22:34:14 -0500 | [diff] [blame] | 168 | command="journalctl -o verbose _PID=$pid" |
Jayanth Othayoth | e61aee3 | 2017-08-09 07:05:06 -0500 | [diff] [blame] | 169 | copy_loc="proc_journal.log" |
| 170 | run_command "$command" "$copy_loc" "$desc" |
| 171 | } |
| 172 | |
Jayanth Othayoth | 0862c48 | 2017-08-09 07:02:36 -0500 | [diff] [blame] | 173 | function get_host_info() |
| 174 | { |
| 175 | desc="Host information" |
| 176 | command="hostnamectl status" |
| 177 | copy_loc="hostnamectl.log" |
| 178 | run_command "$command" "$copy_loc" "$desc" |
| 179 | } |
| 180 | |
| 181 | function get_failed_services() |
| 182 | { |
| 183 | desc="Failed Services" |
| 184 | command="systemctl --failed" |
| 185 | copy_loc="failed_services.log" |
| 186 | run_command "$command" "$copy_loc" "$desc" |
| 187 | } |
| 188 | |
| 189 | function get_obmc_console() |
| 190 | { |
| 191 | desc="OBMC Console" |
| 192 | command="cat /var/log/obmc-console.log" |
| 193 | copy_loc="obmc_console.log" |
| 194 | run_command "$command" "$copy_loc" "$desc" |
| 195 | } |
| 196 | |
| 197 | function get_cpuinfo() |
| 198 | { |
| 199 | desc="cpuinfo" |
| 200 | command="cat /proc/cpuinfo" |
| 201 | copy_loc="cpuinfo.log" |
| 202 | run_command "$command" "$copy_loc" "$desc" |
| 203 | } |
| 204 | |
| 205 | function get_meminfo() |
| 206 | { |
| 207 | desc="meminfo" |
| 208 | command="cat /proc/meminfo" |
| 209 | copy_loc="meminfo.log" |
| 210 | run_command "$command" "$copy_loc" "$desc" |
| 211 | } |
| 212 | |
| 213 | function get_top() |
| 214 | { |
| 215 | desc="top" |
| 216 | command="top -n 1 -b" |
| 217 | copy_loc="top.log" |
| 218 | run_command "$command" "$copy_loc" "$desc" |
| 219 | } |
| 220 | |
| 221 | function bmc_state() |
| 222 | { |
| 223 | desc="BMC State" |
| 224 | command="busctl get-property \ |
| 225 | xyz.openbmc_project.State.BMC \ |
| 226 | /xyz/openbmc_project/state/bmc0 \ |
| 227 | xyz.openbmc_project.State.BMC \ |
| 228 | CurrentBMCState" |
| 229 | copy_loc="BMCState.log" |
| 230 | run_command "$command" "$copy_loc" "$desc" |
| 231 | } |
| 232 | |
| 233 | function host_state() |
| 234 | { |
| 235 | desc="Host State" |
| 236 | command="busctl get-property \ |
| 237 | xyz.openbmc_project.State.Host \ |
| 238 | /xyz/openbmc_project/state/host0 \ |
| 239 | xyz.openbmc_project.State.Host \ |
| 240 | CurrentHostState" |
| 241 | copy_loc="HostState.log" |
| 242 | run_command "$command" "$copy_loc" "$desc" |
| 243 | } |
| 244 | |
| 245 | function chassis_state() |
| 246 | { |
| 247 | desc="Chassis State" |
| 248 | command="busctl get-property \ |
| 249 | xyz.openbmc_project.State.Chassis \ |
| 250 | /xyz/openbmc_project/state/chassis0 \ |
| 251 | xyz.openbmc_project.State.Chassis \ |
| 252 | CurrentPowerState" |
Jayanth Othayoth | 6cd3ca1 | 2017-09-01 01:21:28 -0500 | [diff] [blame] | 253 | copy_loc="ChassisState.log" |
Jayanth Othayoth | 0862c48 | 2017-08-09 07:02:36 -0500 | [diff] [blame] | 254 | run_command "$command" "$copy_loc" "$desc" |
| 255 | } |
| 256 | |
Jayanth Othayoth | d891643 | 2017-09-03 23:14:47 -0500 | [diff] [blame^] | 257 | # @brief get all available elogs in BMC. |
Jayanth Othayoth | 65a409f | 2017-09-03 21:26:27 -0500 | [diff] [blame] | 258 | function get_elog_all() |
Jayanth Othayoth | 0862c48 | 2017-08-09 07:02:36 -0500 | [diff] [blame] | 259 | { |
Jayanth Othayoth | 65a409f | 2017-09-03 21:26:27 -0500 | [diff] [blame] | 260 | desc="elog" |
Jayanth Othayoth | 0862c48 | 2017-08-09 07:02:36 -0500 | [diff] [blame] | 261 | |
| 262 | entries=$(busctl --list --no-pager tree \ |
| 263 | xyz.openbmc_project.Logging | grep \ |
| 264 | '/xyz/openbmc_project/logging/entry/') |
| 265 | |
| 266 | #check for eSEL entries. |
| 267 | if [ -z "$entries" ]; then |
| 268 | log_info "No $desc entries" |
| 269 | return 0 |
| 270 | fi |
| 271 | |
| 272 | command="busctl --list --no-pager tree \ |
| 273 | xyz.openbmc_project.Logging | grep \ |
| 274 | '/xyz/openbmc_project/logging/entry/' \ |
| 275 | | xargs -I {} busctl --verbose --no-pager \ |
| 276 | call xyz.openbmc_project.Logging {} \ |
| 277 | org.freedesktop.DBus.Properties GetAll s \ |
| 278 | xyz.openbmc_project.Logging.Entry" |
Jayanth Othayoth | 65a409f | 2017-09-03 21:26:27 -0500 | [diff] [blame] | 279 | copy_loc="elog.log" |
Jayanth Othayoth | 0862c48 | 2017-08-09 07:02:36 -0500 | [diff] [blame] | 280 | run_command "$command" "$copy_loc" "$desc" |
| 281 | } |
| 282 | |
Jayanth Othayoth | d891643 | 2017-09-03 23:14:47 -0500 | [diff] [blame^] | 283 | # @brief get elog specific to global elog_id |
| 284 | function get_elog() |
| 285 | { |
| 286 | desc="elog id:$elog_id" |
| 287 | |
| 288 | command="busctl --verbose --no-pager \ |
| 289 | call xyz.openbmc_project.Logging \ |
| 290 | $optional_path \ |
| 291 | org.freedesktop.DBus.Properties GetAll s \ |
| 292 | xyz.openbmc_project.Logging.Entry" |
| 293 | copy_loc="elog_$elog_id.log" |
| 294 | run_command "$command" "$copy_loc" "$desc" |
| 295 | } |
| 296 | |
Jayanth Othayoth | e61aee3 | 2017-08-09 07:05:06 -0500 | [diff] [blame] | 297 | function move_optional_file() |
| 298 | { |
| 299 | desc="Move Optional file" |
| 300 | |
Jayanth Othayoth | 2b05adc | 2017-09-03 22:15:01 -0500 | [diff] [blame] | 301 | mv $optional_path $name_dir |
Jayanth Othayoth | e61aee3 | 2017-08-09 07:05:06 -0500 | [diff] [blame] | 302 | if [ $? -ne 0 ]; then |
Jayanth Othayoth | 2b05adc | 2017-09-03 22:15:01 -0500 | [diff] [blame] | 303 | log_error "Failed to move file $optional_path" |
Jayanth Othayoth | e61aee3 | 2017-08-09 07:05:06 -0500 | [diff] [blame] | 304 | return 1 |
| 305 | fi |
Jayanth Othayoth | 2b05adc | 2017-09-03 22:15:01 -0500 | [diff] [blame] | 306 | if check_size "$name_dir/$(basename "$optional_path")"; then |
Jayanth Othayoth | e61aee3 | 2017-08-09 07:05:06 -0500 | [diff] [blame] | 307 | log_info "Moving file $file_name" |
| 308 | else |
| 309 | log_warning "Skipping $file_name move" |
| 310 | fi |
| 311 | } |
| 312 | |
Jayanth Othayoth | 0862c48 | 2017-08-09 07:02:36 -0500 | [diff] [blame] | 313 | # @brief Run the requested command and save the output |
| 314 | # into temporary location for successful size check |
| 315 | |
| 316 | function run_command() |
| 317 | { |
| 318 | command="$1" |
| 319 | copy_loc="$2" |
| 320 | desc="$3" |
| 321 | |
| 322 | eval $command >> "$name_dir/$copy_loc" |
| 323 | if [ $? -ne 0 ]; then |
| 324 | log_error "Failed to collect $desc" |
| 325 | return 1 |
| 326 | fi |
| 327 | |
| 328 | if check_size "$name_dir/$copy_loc"; then |
| 329 | log_info "Collected $desc" |
| 330 | else |
| 331 | log_warning "Skipping $desc" |
| 332 | fi |
| 333 | } |
Jayanth Othayoth | c2ece2d | 2017-08-09 06:57:12 -0500 | [diff] [blame] | 334 | |
Jayanth Othayoth | acf46e3 | 2017-08-09 07:15:18 -0500 | [diff] [blame] | 335 | # @brief Initiate data collection based on the type. |
| 336 | # @return 0 on success, error code otherwise |
| 337 | function collect_data() |
| 338 | { |
Jayanth Othayoth | d891643 | 2017-09-03 23:14:47 -0500 | [diff] [blame^] | 339 | runlevel_general |
| 340 | capture_data "${command_list[@]}" |
| 341 | |
Jayanth Othayoth | acf46e3 | 2017-08-09 07:15:18 -0500 | [diff] [blame] | 342 | case $dump_type in |
| 343 | $TYPE_USER) |
| 344 | runlevel_user |
| 345 | capture_data "${command_list[@]}" |
| 346 | ;; |
| 347 | $TYPE_CORE) |
Jayanth Othayoth | fa388d0 | 2017-09-03 22:34:14 -0500 | [diff] [blame] | 348 | set_core_pid |
Jayanth Othayoth | acf46e3 | 2017-08-09 07:15:18 -0500 | [diff] [blame] | 349 | runlevel_core |
| 350 | capture_data "${command_list[@]}" |
| 351 | ;; |
Jayanth Othayoth | d891643 | 2017-09-03 23:14:47 -0500 | [diff] [blame^] | 352 | $TYPE_ELOG) |
| 353 | elog_id=$(basename "$optional_path") |
| 354 | set_elog_pid |
| 355 | runlevel_elog |
| 356 | capture_data "${command_list[@]}" |
| 357 | ;; |
| 358 | |
Jayanth Othayoth | acf46e3 | 2017-08-09 07:15:18 -0500 | [diff] [blame] | 359 | $SUMMARY_DUMP) |
| 360 | #No data collection is required. |
| 361 | ;; |
| 362 | *) # unknown option |
| 363 | log_error "Skipping: Unknown dump type: $dump_type" |
| 364 | ;; |
| 365 | esac |
| 366 | } |
| 367 | |
Jayanth Othayoth | fa388d0 | 2017-09-03 22:34:14 -0500 | [diff] [blame] | 368 | # @brief set pid by reading information from the optional path. |
Jayanth Othayoth | 2b05adc | 2017-09-03 22:15:01 -0500 | [diff] [blame] | 369 | # dreport "core" type user provides core file as optional path parameter. |
Jayanth Othayoth | 47a1416 | 2017-08-09 07:10:39 -0500 | [diff] [blame] | 370 | # As per coredump source code systemd-coredump uses below format |
| 371 | # https://github.com/systemd/systemd/blob/master/src/coredump/coredump.c |
| 372 | # /var/lib/systemd/coredump/core.%s.%s." SD_ID128_FORMAT_STR “. |
| 373 | # <process ID>.%s000000" |
Jayanth Othayoth | fa388d0 | 2017-09-03 22:34:14 -0500 | [diff] [blame] | 374 | function set_core_pid() |
Jayanth Othayoth | 47a1416 | 2017-08-09 07:10:39 -0500 | [diff] [blame] | 375 | { |
Jayanth Othayoth | 47a1416 | 2017-08-09 07:10:39 -0500 | [diff] [blame] | 376 | #Escape bash characters in file name |
Jayanth Othayoth | 2b05adc | 2017-09-03 22:15:01 -0500 | [diff] [blame] | 377 | file=$(printf %q "$optional_path") |
Jayanth Othayoth | 47a1416 | 2017-08-09 07:10:39 -0500 | [diff] [blame] | 378 | |
Jayanth Othayoth | 9a56bfa | 2017-08-26 03:03:47 -0500 | [diff] [blame] | 379 | #matching systemd-coredump core file format. |
Jayanth Othayoth | fa388d0 | 2017-09-03 22:34:14 -0500 | [diff] [blame] | 380 | pid=$(echo $file | awk -F . '{ print $5}') |
Jayanth Othayoth | 47a1416 | 2017-08-09 07:10:39 -0500 | [diff] [blame] | 381 | } |
| 382 | |
Jayanth Othayoth | d891643 | 2017-09-03 23:14:47 -0500 | [diff] [blame^] | 383 | # @brief set elog pid by reading _PID information from the elog d-bus object. |
| 384 | # _PID information is stored elog Additional data field |
| 385 | # Data format "_PID=<pid>" |
| 386 | function set_elog_pid() |
| 387 | { |
| 388 | additional_data=$(busctl get-property xyz.openbmc_project.Logging \ |
| 389 | $optional_path \ |
| 390 | xyz.openbmc_project.Logging.Entry \ |
| 391 | AdditionalData) |
| 392 | |
| 393 | #read _PID data. |
| 394 | if [ ! -z "$additional_data" ]; then |
| 395 | pid=$(echo $additional_data | \ |
| 396 | awk -F _PID= '{ print ($2+0)}') |
| 397 | fi |
| 398 | } |
| 399 | |
Jayanth Othayoth | f1e02d3 | 2017-08-09 07:06:45 -0500 | [diff] [blame] | 400 | # @brief Capture debug data based on the input command array. |
| 401 | # and stores in to global temporary name specific location. |
| 402 | # @param $1 Source array |
| 403 | # @return 0 on success, error code otherwise |
| 404 | function capture_data() |
| 405 | { |
| 406 | source=("$@") |
| 407 | |
| 408 | for ((i=0;i<${#source[@]};i+=1)); do |
| 409 | ${source[i]} |
| 410 | done |
| 411 | |
| 412 | return 0 |
| 413 | } |
| 414 | |
Jayanth Othayoth | c2ece2d | 2017-08-09 06:57:12 -0500 | [diff] [blame] | 415 | # @brief Calculate file or directory compressed size based on input |
| 416 | # and check whether the size in the the allowed size limit. |
| 417 | # Remove the file or directory from the name_dir |
| 418 | # if the check fails. |
| 419 | # @param $1 Source file or directory |
| 420 | # @return 0 on success, error code if size exceeds the limit. |
| 421 | # Limitation: compress and tar will have few bytes size difference |
Jayanth Othayoth | c2ece2d | 2017-08-09 06:57:12 -0500 | [diff] [blame] | 422 | function check_size() |
| 423 | { |
| 424 | source=$1 |
| 425 | |
| 426 | #No size check required incase dump_size is set to unlimited |
| 427 | if [ $dump_size = $UNLIMITED ]; then |
| 428 | return 0 |
| 429 | fi |
| 430 | |
| 431 | #get the file or directory size |
| 432 | if [[ -d $source ]] && [[ -n $source ]]; then |
| 433 | tar -cf "$source.tar" -C \ |
| 434 | $(dirname "$source") $(basename "$source") |
| 435 | size=$(stat -c%s "$source.tar") |
| 436 | rm "$source.tar" |
| 437 | else |
| 438 | size=$(stat -c%s "$source") |
| 439 | fi |
| 440 | |
| 441 | if [ $((size + cur_dump_size)) -gt $dump_size ]; then |
| 442 | #Exceed the allowed limit, |
| 443 | #tar and compress the files and check the size |
| 444 | tar -Jcf "$name_dir.tar.xz" -C \ |
| 445 | $(dirname "$name_dir") $(basename "$name_dir") |
| 446 | size=$(stat -c%s "$name_dir.tar.xz") |
| 447 | if [ $size -gt $dump_size ]; then |
| 448 | #Remove the the specific data from the name_dir and contniue |
| 449 | rm "$source" "$name_dir.tar.xz" |
| 450 | return $RESOURCE_UNAVAILABLE |
| 451 | else |
| 452 | rm "$name_dir.tar.xz" |
| 453 | fi |
| 454 | fi |
| 455 | |
| 456 | #Remove the compressed file from the name directory |
| 457 | cur_dump_size=$((size + cur_dump_size)) |
| 458 | return $SUCCESS |
| 459 | } |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 460 | |
Jayanth Othayoth | e20d5e0 | 2017-08-09 06:48:45 -0500 | [diff] [blame] | 461 | # @brief Initial version of the summary log |
| 462 | init_summary() |
| 463 | { |
| 464 | log_summary "Name: $name.tar.xz" |
| 465 | log_summary "Epochtime: $EPOCHTIME" |
| 466 | log_summary "ID: $dump_id" |
| 467 | log_summary "Type: $dump_type" |
Jayanth Othayoth | 2b05adc | 2017-09-03 22:15:01 -0500 | [diff] [blame] | 468 | log_summary "Optional path: $optional_path" |
Jayanth Othayoth | e20d5e0 | 2017-08-09 06:48:45 -0500 | [diff] [blame] | 469 | } |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 470 | |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 471 | # @brief Check the validity of user inputs and initialize global |
| 472 | # variables. Create directory for temporary data collection |
| 473 | # @return 0 on success, error code otherwise |
| 474 | |
| 475 | function initialize() |
| 476 | { |
| 477 | #Dump file name |
| 478 | if [ -z $name ]; then |
| 479 | name=$"obmcdump_"$dump_id"_$EPOCHTIME" |
| 480 | fi |
| 481 | |
| 482 | #Create temporary data directory. |
| 483 | mkdir -p "$TMP_DIR/$name" |
| 484 | if [ $? -ne 0 ]; then |
| 485 | echo "Error: Failed to create the temporary directory." |
| 486 | return $RESOURCE_UNAVAILABLE; |
| 487 | fi |
| 488 | |
| 489 | #name directory |
| 490 | name_dir="$TMP_DIR/$name" |
| 491 | |
| 492 | #dreport log file |
| 493 | dreport_log="$name_dir/$DREPORT_LOG" |
| 494 | |
| 495 | #summary log file |
| 496 | summary_log="$name_dir/$SUMMARY_LOG" |
| 497 | |
| 498 | #Type |
Jayanth Othayoth | d891643 | 2017-09-03 23:14:47 -0500 | [diff] [blame^] | 499 | if [[ !($dump_type = $TYPE_USER || \ |
| 500 | $dump_type = $TYPE_CORE || \ |
| 501 | $dump_type = $TYPE_ELOG) ]]; then |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 502 | log_error "Invalid -type, Only summary log is available" |
| 503 | dump_type=$SUMMARY_DUMP |
| 504 | fi |
| 505 | |
| 506 | #Size |
| 507 | #Check the input is integer. |
| 508 | if [ "$dump_size" -eq "$dump_size" ] 2>/dev/null; then |
| 509 | #Converts in to bytes. |
| 510 | dump_size="$((dump_size * 1024))" |
| 511 | else |
| 512 | dump_size=$UNLIMITED |
| 513 | fi |
| 514 | |
| 515 | return $SUCCESS |
| 516 | } |
| 517 | |
Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 518 | # @brief Packaging the dump and transferring to dump location. |
| 519 | function package() |
| 520 | { |
| 521 | mkdir -p "$dump_dir" |
| 522 | if [ $? -ne 0 ]; then |
| 523 | log_error "Could not create the destination directory $dump_dir" |
| 524 | dest_dir=$TMP_DIR |
| 525 | fi |
| 526 | |
Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 527 | #tar and compress the files. |
Jayanth Othayoth | 8d0446e | 2017-08-09 07:26:45 -0500 | [diff] [blame] | 528 | tar -Jcf "$name_dir.tar.xz" -C \ |
| 529 | $(dirname "$name_dir") $(basename "$name_dir") |
| 530 | |
| 531 | if [ $? -ne 0 ]; then |
Jayanth Othayoth | ea4b5e5 | 2017-08-09 07:31:24 -0500 | [diff] [blame] | 532 | echo $($TIME_STAMP) "Could not create the compressed tar file" |
Jayanth Othayoth | 8d0446e | 2017-08-09 07:26:45 -0500 | [diff] [blame] | 533 | rm -r "$name_dir" |
| 534 | return $INTERNAL_FAILURE |
| 535 | fi |
Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 536 | |
| 537 | #remove the temporary name specific directory |
| 538 | rm -r "$name_dir" |
| 539 | |
Jayanth Othayoth | ea4b5e5 | 2017-08-09 07:31:24 -0500 | [diff] [blame] | 540 | echo $($TIME_STAMP) "Report is available in $dump_dir" |
Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 541 | |
| 542 | if [ "$TMP_DIR" == "$dump_dir" ]; then |
Jayanth Othayoth | 8d0446e | 2017-08-09 07:26:45 -0500 | [diff] [blame] | 543 | return $SUCCESS |
Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 544 | fi |
| 545 | |
| 546 | #copy the compressed tar file into the destination |
Jayanth Othayoth | 8d0446e | 2017-08-09 07:26:45 -0500 | [diff] [blame] | 547 | cp "$name_dir.tar.xz" "$dump_dir" |
Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 548 | if [ $? -ne 0 ]; then |
Jayanth Othayoth | 8d0446e | 2017-08-09 07:26:45 -0500 | [diff] [blame] | 549 | echo "Failed to copy the $name_dir.tar.xz to $dump_dir" |
| 550 | rm "$name_dir.tar.xz" |
| 551 | return $INTERNAL_FAILURE |
Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 552 | fi |
Jayanth Othayoth | 8d0446e | 2017-08-09 07:26:45 -0500 | [diff] [blame] | 553 | |
| 554 | #Remove the temporary copy of the file |
| 555 | rm "$name_dir.tar.xz" |
Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 556 | } |
Jayanth Othayoth | ea4b5e5 | 2017-08-09 07:31:24 -0500 | [diff] [blame] | 557 | |
Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 558 | # @brief log the error message |
| 559 | # @param error message |
| 560 | function log_error() |
| 561 | { |
Jayanth Othayoth | ea4b5e5 | 2017-08-09 07:31:24 -0500 | [diff] [blame] | 562 | echo $($TIME_STAMP) "ERROR: $@" >> $dreport_log |
Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 563 | if ((quiet != TRUE)); then |
Jayanth Othayoth | ea4b5e5 | 2017-08-09 07:31:24 -0500 | [diff] [blame] | 564 | echo $($TIME_STAMP) "ERROR: $@" >&2 |
Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 565 | fi |
| 566 | } |
| 567 | |
| 568 | # @brief log warning message |
| 569 | # @param warning message |
| 570 | function log_warning() |
| 571 | { |
| 572 | if ((verbose == TRUE)); then |
Jayanth Othayoth | ea4b5e5 | 2017-08-09 07:31:24 -0500 | [diff] [blame] | 573 | echo $($TIME_STAMP) "WARNING: $@" >> $dreport_log |
Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 574 | if ((quiet != TRUE)); then |
Jayanth Othayoth | ea4b5e5 | 2017-08-09 07:31:24 -0500 | [diff] [blame] | 575 | echo $($TIME_STAMP) "WARNING: $@" >&2 |
Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 576 | fi |
| 577 | fi |
| 578 | } |
| 579 | |
| 580 | # @brief log info message |
| 581 | # @param info message |
| 582 | function log_info() |
| 583 | { |
| 584 | if ((verbose == TRUE)); then |
Jayanth Othayoth | ea4b5e5 | 2017-08-09 07:31:24 -0500 | [diff] [blame] | 585 | echo $($TIME_STAMP) "INFO: $@" >> $dreport_log |
Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 586 | if ((quiet != TRUE)); then |
Jayanth Othayoth | ea4b5e5 | 2017-08-09 07:31:24 -0500 | [diff] [blame] | 587 | echo $($TIME_STAMP) "INFO: $@" >&1 |
Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 588 | fi |
| 589 | fi |
| 590 | } |
| 591 | |
| 592 | # @brief log summary message |
| 593 | # @param message |
| 594 | function log_summary() |
| 595 | { |
Jayanth Othayoth | ea4b5e5 | 2017-08-09 07:31:24 -0500 | [diff] [blame] | 596 | echo $($TIME_STAMP) "$@" >> $summary_log |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 597 | if ((quiet != TRUE)); then |
Jayanth Othayoth | ea4b5e5 | 2017-08-09 07:31:24 -0500 | [diff] [blame] | 598 | echo $($TIME_STAMP) "$@" >&1 |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 599 | fi |
Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 600 | } |
| 601 | |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 602 | # @brief Main function |
| 603 | function main() |
| 604 | { |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 605 | #initialize the global variables and |
| 606 | #create temporary storage locations |
| 607 | initialize |
| 608 | result=$? |
| 609 | if [[ ${result} -ne $SUCCESS ]]; then |
Jayanth Othayoth | ea4b5e5 | 2017-08-09 07:31:24 -0500 | [diff] [blame] | 610 | echo $($TIME_STAMP) "Error: Failed to initialize, Exiting" |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 611 | exit; |
| 612 | fi |
Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 613 | |
Jayanth Othayoth | e20d5e0 | 2017-08-09 06:48:45 -0500 | [diff] [blame] | 614 | #Initilize the summary log |
| 615 | init_summary |
| 616 | |
Jayanth Othayoth | acf46e3 | 2017-08-09 07:15:18 -0500 | [diff] [blame] | 617 | #collect data based on the type. |
| 618 | collect_data |
Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 619 | |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 620 | package #package the dump |
Jayanth Othayoth | 8d0446e | 2017-08-09 07:26:45 -0500 | [diff] [blame] | 621 | result=$? |
| 622 | if [[ ${result} -ne $SUCCESS ]]; then |
Jayanth Othayoth | ea4b5e5 | 2017-08-09 07:31:24 -0500 | [diff] [blame] | 623 | echo $($TIME_STAMP) "Error: Failed to package, Exiting" |
Jayanth Othayoth | 8d0446e | 2017-08-09 07:26:45 -0500 | [diff] [blame] | 624 | else |
Jayanth Othayoth | ea4b5e5 | 2017-08-09 07:31:24 -0500 | [diff] [blame] | 625 | echo $($TIME_STAMP) "Sucessfully completed" |
Jayanth Othayoth | 8d0446e | 2017-08-09 07:26:45 -0500 | [diff] [blame] | 626 | exit; |
| 627 | fi |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 628 | } |
| 629 | |
Jayanth Othayoth | 2b05adc | 2017-09-03 22:15:01 -0500 | [diff] [blame] | 630 | TEMP=`getopt -o n:d:i:t:s:p:vVqh \ |
| 631 | --long name:,dir:,dumpid:,type:,size:,path:,verbose,version,quiet,help \ |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 632 | -- "$@"` |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 633 | |
| 634 | if [ $? -ne 0 ] |
| 635 | then |
| 636 | echo "Error: Invalid options" |
| 637 | exit 1 |
| 638 | fi |
| 639 | |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 640 | eval set -- "$TEMP" |
| 641 | |
| 642 | while [[ $# -gt 1 ]]; do |
| 643 | key="$1" |
| 644 | case $key in |
| 645 | -n|--name) |
| 646 | name=$2 |
| 647 | shift 2;; |
| 648 | -d|--dir) |
Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 649 | dump_dir=$2 |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 650 | shift 2;; |
| 651 | -i|--dumpid) |
| 652 | dump_id=$2 |
| 653 | shift 2;; |
| 654 | -t|--type) |
| 655 | dump_type=$2 |
| 656 | shift 2;; |
| 657 | -s|--size) |
| 658 | dump_size=$2 |
| 659 | shift 2;; |
Jayanth Othayoth | 2b05adc | 2017-09-03 22:15:01 -0500 | [diff] [blame] | 660 | -p|--path) |
| 661 | optional_path=$2 |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 662 | shift 2;; |
| 663 | -v|—-verbose) |
| 664 | verbose=$TRUE |
| 665 | shift;; |
| 666 | -V|--version) |
| 667 | shift;; |
| 668 | -q|—-quiet) |
| 669 | quiet=$TRUE |
| 670 | shift;; |
| 671 | -h|--help) |
| 672 | echo "$help" |
| 673 | exit;; |
| 674 | *) # unknown option |
Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 675 | log_error "Unknown argument: $1" |
| 676 | log_info "$help" |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 677 | exit 1;; |
| 678 | esac |
Jayanth Othayoth | 9e95f4b | 2017-07-24 06:42:24 -0500 | [diff] [blame] | 679 | done |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 680 | |
| 681 | main #main program |
| 682 | exit $? |