blob: d576a2644b05172c97462ed0db1681aeada3f06e [file] [log] [blame]
Jayanth Othayoth9e95f4b2017-07-24 06:42:24 -05001#! /bin/bash
2
3help=$"
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
11usage: dreport [OPTION]
12
13Options:
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 Othayoth7b774872017-07-26 05:02:53 -050037#CONSTANTS
38declare -r TRUE=1
39declare -r FALSE=0
40declare -r USERINITIATED_TYPE=0
41declare -r APPLICATIONCORED_TYPE=1
42declare -r DUMP_MAX_SIZE=500 #in KB
Jayanth Othayoth6d3ee1c2017-07-26 05:18:31 -050043declare -r SUMMARY_LOG="summary.log"
44declare -r DREPORT_LOG="dreport.log"
45declare -r TMP_DIR="/tmp/dreport"
Jayanth Othayoth7b774872017-07-26 05:02:53 -050046
47#VARIABLES
48declare -x name=$"obmcdump_00000000_$(date +"%s")"
Jayanth Othayothff0699d2017-07-26 07:53:03 -050049declare -x dump_dir="/tmp"
Jayanth Othayoth6d3ee1c2017-07-26 05:18:31 -050050declare -x dump_id=0
Jayanth Othayoth7b774872017-07-26 05:02:53 -050051declare -x dump_type=$USERINITIATED_TYPE
52declare -x verbose=$FALSE
53declare -x quiet=$FALSE
54declare -x dump_size=$DUMP_MAX_SIZE
Jayanth Othayothff0699d2017-07-26 07:53:03 -050055declare -x name_dir="$TMP_DIR/$name"
Jayanth Othayoth7b774872017-07-26 05:02:53 -050056
57# PACKAGE VERSION
58PACKAGE_VERSION="0.0.1"
59
Jayanth Othayothff0699d2017-07-26 07:53:03 -050060# @brief Packaging the dump and transferring to dump location.
61function package()
62{
63 mkdir -p "$dump_dir"
64 if [ $? -ne 0 ]; then
65 log_error "Could not create the destination directory $dump_dir"
66 dest_dir=$TMP_DIR
67 fi
68
69 #TODO openbmc/openbmc#1506 Enable file level compression.
70 #tar and compress the files.
71 tar_file="$name_dir.tar.xz"
72 tar -Jcf "$tar_file" -C "$TMP_DIR" "$name"
73
74 #remove the temporary name specific directory
75 rm -r "$name_dir"
76
77 #check the file size is in the allowed limit
78 if [ $(stat -c%s "$tar_file") -gt $dump_size ]; then
79 echo "File size exceeds the limit allowed"
80 rm -rf "$TMP_DIR"
81 exit 1
82 #TODO openbmc/openbmc#1506 Revisit the error handling
83 fi
84
85 echo "Report is available in $dump_dir"
86
87 if [ "$TMP_DIR" == "$dump_dir" ]; then
88 return
89 fi
90
91 #copy the compressed tar file into the destination
92 cp "$tar_file" "$dump_dir"
93 if [ $? -ne 0 ]; then
94 echo "Failed to copy the $tar_file to $dump_dir"
95 return
96 else
97 rm -rf "$TMP_DIR"
98 fi
99}
Jayanth Othayoth6d3ee1c2017-07-26 05:18:31 -0500100# @brief log the error message
101# @param error message
102function log_error()
103{
104 echo "ERROR: $@" >>"$name_dir/$DREPORT_LOG"
105 if ((quiet != TRUE)); then
106 echo "ERROR: $@" >&2
107 fi
108}
109
110# @brief log warning message
111# @param warning message
112function log_warning()
113{
114 if ((verbose == TRUE)); then
115 echo "WARNING: $@" >>"$name_dir/$DREPORT_LOG"
116 if ((quiet != TRUE)); then
117 echo "WARNING: $@" >&2
118 fi
119 fi
120}
121
122# @brief log info message
123# @param info message
124function log_info()
125{
126 if ((verbose == TRUE)); then
127 echo "INFO: $@" >>"$name_dir/$DREPORT_LOG"
128 if ((quiet != TRUE)); then
129 echo "INFO: $@" >&1
130 fi
131 fi
132}
133
134# @brief log summary message
135# @param message
136function log_summary()
137{
138 echo "$@" >> "$name_dir/$SUMMARY_LOG"
139 if ((quiet != TRUE)); then
140 echo "$@" >&1
141 fi
142}
143
Jayanth Othayoth7b774872017-07-26 05:02:53 -0500144# @brief Main function
145function main()
146{
Jayanth Othayothff0699d2017-07-26 07:53:03 -0500147 mkdir -p "$TMP_DIR/$name"
Jayanth Othayoth6d3ee1c2017-07-26 05:18:31 -0500148 if [ $? -ne 0 ]; then
149 log_error "Failed to create the temporary directory."
150 exit;
151 fi
152
153 log_summary "Version: $PACKAGE_VERSION"
Jayanth Othayothff0699d2017-07-26 07:53:03 -0500154
155 #TODO Add Dump report generating script.
156
157 package #package the dump
Jayanth Othayoth7b774872017-07-26 05:02:53 -0500158}
159
160TEMP=`getopt -o n:d:i:t:s:f:vVqh \
161 --long name:,dir:,dumpid:,type:,size:,file:,verbose,version,quiet,help \
162 -- "$@"`
163eval set -- "$TEMP"
164
165while [[ $# -gt 1 ]]; do
166 key="$1"
167 case $key in
168 -n|--name)
169 name=$2
170 shift 2;;
171 -d|--dir)
Jayanth Othayothff0699d2017-07-26 07:53:03 -0500172 dump_dir=$2
Jayanth Othayoth7b774872017-07-26 05:02:53 -0500173 shift 2;;
174 -i|--dumpid)
175 dump_id=$2
176 shift 2;;
177 -t|--type)
178 dump_type=$2
179 shift 2;;
180 -s|--size)
181 dump_size=$2
182 shift 2;;
183 -f|--file)
184 dump_file=$2
185 shift 2;;
186 -v|—-verbose)
187 verbose=$TRUE
188 shift;;
189 -V|--version)
190 shift;;
191 -q|—-quiet)
192 quiet=$TRUE
193 shift;;
194 -h|--help)
195 echo "$help"
196 exit;;
197 *) # unknown option
Jayanth Othayoth6d3ee1c2017-07-26 05:18:31 -0500198 log_error "Unknown argument: $1"
199 log_info "$help"
Jayanth Othayoth7b774872017-07-26 05:02:53 -0500200 exit 1;;
201 esac
Jayanth Othayoth9e95f4b2017-07-24 06:42:24 -0500202done
Jayanth Othayoth7b774872017-07-26 05:02:53 -0500203
204main #main program
205exit $?