blob: 65ad2822d498637be255b0ea32d0ee1824c5e41a [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.
Jayanth Othayoth230e9a32017-08-09 06:39:59 -050017 Default output directory is /tmp
Jayanth Othayoth9e95f4b2017-07-24 06:42:24 -050018 -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
Jayanth Othayoth230e9a32017-08-09 06:39:59 -050040declare -r UNLIMITED="unlimited"
41declare -r SUMMARY_DUMP="summary"
42declare -r TYPE_USER="user"
43declare -r TYPE_CORE="core"
Jayanth Othayoth6d3ee1c2017-07-26 05:18:31 -050044declare -r SUMMARY_LOG="summary.log"
45declare -r DREPORT_LOG="dreport.log"
Jayanth Othayoth230e9a32017-08-09 06:39:59 -050046declare -r TMP_DIR="/tmp"
47declare -r EPOCHTIME=$(date +"%s")
48
49#Error Codes
50declare -r SUCCESS="0"
51declare -r INTERNAL_FAILURE="1"
52declare -r RESOURCE_UNAVAILABLE="2"
Jayanth Othayoth7b774872017-07-26 05:02:53 -050053
54#VARIABLES
Jayanth Othayoth230e9a32017-08-09 06:39:59 -050055declare -x name=""
Jayanth Othayothff0699d2017-07-26 07:53:03 -050056declare -x dump_dir="/tmp"
Jayanth Othayoth230e9a32017-08-09 06:39:59 -050057declare -x dump_id="00000000"
58declare -x dump_type=$TYPE_USER
Jayanth Othayoth7b774872017-07-26 05:02:53 -050059declare -x verbose=$FALSE
60declare -x quiet=$FALSE
Jayanth Othayoth230e9a32017-08-09 06:39:59 -050061declare -x dump_size="unlimited"
62declare -x name_dir=""
63declare -x optional_file=""
64declare -x dreport_log=""
65declare -x summary_log=""
Jayanth Othayothc2ece2d2017-08-09 06:57:12 -050066declare -x cur_dump_size=0
67
68# @brief Calculate file or directory compressed size based on input
69# and check whether the size in the the allowed size limit.
70# Remove the file or directory from the name_dir
71# if the check fails.
72# @param $1 Source file or directory
73# @return 0 on success, error code if size exceeds the limit.
74# Limitation: compress and tar will have few bytes size difference
75# between tar and compression approch.
76function check_size()
77{
78 source=$1
79
80 #No size check required incase dump_size is set to unlimited
81 if [ $dump_size = $UNLIMITED ]; then
82 return 0
83 fi
84
85 #get the file or directory size
86 if [[ -d $source ]] && [[ -n $source ]]; then
87 tar -cf "$source.tar" -C \
88 $(dirname "$source") $(basename "$source")
89 size=$(stat -c%s "$source.tar")
90 rm "$source.tar"
91 else
92 size=$(stat -c%s "$source")
93 fi
94
95 if [ $((size + cur_dump_size)) -gt $dump_size ]; then
96 #Exceed the allowed limit,
97 #tar and compress the files and check the size
98 tar -Jcf "$name_dir.tar.xz" -C \
99 $(dirname "$name_dir") $(basename "$name_dir")
100 size=$(stat -c%s "$name_dir.tar.xz")
101 if [ $size -gt $dump_size ]; then
102 #Remove the the specific data from the name_dir and contniue
103 rm "$source" "$name_dir.tar.xz"
104 return $RESOURCE_UNAVAILABLE
105 else
106 rm "$name_dir.tar.xz"
107 fi
108 fi
109
110 #Remove the compressed file from the name directory
111 cur_dump_size=$((size + cur_dump_size))
112 return $SUCCESS
113}
Jayanth Othayoth7b774872017-07-26 05:02:53 -0500114
Jayanth Othayothe20d5e02017-08-09 06:48:45 -0500115# @brief Initial version of the summary log
116init_summary()
117{
118 log_summary "Name: $name.tar.xz"
119 log_summary "Epochtime: $EPOCHTIME"
120 log_summary "ID: $dump_id"
121 log_summary "Type: $dump_type"
122 log_summary "Optional file: $optional_file"
123}
Jayanth Othayoth7b774872017-07-26 05:02:53 -0500124
Jayanth Othayoth230e9a32017-08-09 06:39:59 -0500125# @brief Check the validity of user inputs and initialize global
126# variables. Create directory for temporary data collection
127# @return 0 on success, error code otherwise
128
129function initialize()
130{
131 #Dump file name
132 if [ -z $name ]; then
133 name=$"obmcdump_"$dump_id"_$EPOCHTIME"
134 fi
135
136 #Create temporary data directory.
137 mkdir -p "$TMP_DIR/$name"
138 if [ $? -ne 0 ]; then
139 echo "Error: Failed to create the temporary directory."
140 return $RESOURCE_UNAVAILABLE;
141 fi
142
143 #name directory
144 name_dir="$TMP_DIR/$name"
145
146 #dreport log file
147 dreport_log="$name_dir/$DREPORT_LOG"
148
149 #summary log file
150 summary_log="$name_dir/$SUMMARY_LOG"
151
152 #Type
153 if [[ !($dump_type = $TYPE_USER || $dump_type = $TYPE_CORE) ]]; then
154 log_error "Invalid -type, Only summary log is available"
155 dump_type=$SUMMARY_DUMP
156 fi
157
158 #Size
159 #Check the input is integer.
160 if [ "$dump_size" -eq "$dump_size" ] 2>/dev/null; then
161 #Converts in to bytes.
162 dump_size="$((dump_size * 1024))"
163 else
164 dump_size=$UNLIMITED
165 fi
166
167 return $SUCCESS
168}
169
Jayanth Othayothff0699d2017-07-26 07:53:03 -0500170# @brief Packaging the dump and transferring to dump location.
171function package()
172{
173 mkdir -p "$dump_dir"
174 if [ $? -ne 0 ]; then
175 log_error "Could not create the destination directory $dump_dir"
176 dest_dir=$TMP_DIR
177 fi
178
179 #TODO openbmc/openbmc#1506 Enable file level compression.
180 #tar and compress the files.
181 tar_file="$name_dir.tar.xz"
182 tar -Jcf "$tar_file" -C "$TMP_DIR" "$name"
183
184 #remove the temporary name specific directory
185 rm -r "$name_dir"
186
187 #check the file size is in the allowed limit
188 if [ $(stat -c%s "$tar_file") -gt $dump_size ]; then
189 echo "File size exceeds the limit allowed"
190 rm -rf "$TMP_DIR"
191 exit 1
192 #TODO openbmc/openbmc#1506 Revisit the error handling
193 fi
194
195 echo "Report is available in $dump_dir"
196
197 if [ "$TMP_DIR" == "$dump_dir" ]; then
198 return
199 fi
200
201 #copy the compressed tar file into the destination
202 cp "$tar_file" "$dump_dir"
203 if [ $? -ne 0 ]; then
204 echo "Failed to copy the $tar_file to $dump_dir"
205 return
206 else
207 rm -rf "$TMP_DIR"
208 fi
209}
Jayanth Othayoth6d3ee1c2017-07-26 05:18:31 -0500210# @brief log the error message
211# @param error message
212function log_error()
213{
Jayanth Othayoth230e9a32017-08-09 06:39:59 -0500214 echo "ERROR: $@" >> $dreport_log
Jayanth Othayoth6d3ee1c2017-07-26 05:18:31 -0500215 if ((quiet != TRUE)); then
216 echo "ERROR: $@" >&2
217 fi
218}
219
220# @brief log warning message
221# @param warning message
222function log_warning()
223{
224 if ((verbose == TRUE)); then
Jayanth Othayoth230e9a32017-08-09 06:39:59 -0500225 echo "WARNING: $@" >> $dreport_log
Jayanth Othayoth6d3ee1c2017-07-26 05:18:31 -0500226 if ((quiet != TRUE)); then
227 echo "WARNING: $@" >&2
228 fi
229 fi
230}
231
232# @brief log info message
233# @param info message
234function log_info()
235{
236 if ((verbose == TRUE)); then
Jayanth Othayoth230e9a32017-08-09 06:39:59 -0500237 echo "INFO: $@" >> $dreport_log
Jayanth Othayoth6d3ee1c2017-07-26 05:18:31 -0500238 if ((quiet != TRUE)); then
239 echo "INFO: $@" >&1
240 fi
241 fi
242}
243
244# @brief log summary message
245# @param message
246function log_summary()
247{
Jayanth Othayoth230e9a32017-08-09 06:39:59 -0500248 echo "$@" >> $summary_log
249 if ((quiet != TRUE)); then
Jayanth Othayoth6d3ee1c2017-07-26 05:18:31 -0500250 echo "$@" >&1
Jayanth Othayoth230e9a32017-08-09 06:39:59 -0500251 fi
Jayanth Othayoth6d3ee1c2017-07-26 05:18:31 -0500252}
253
Jayanth Othayoth7b774872017-07-26 05:02:53 -0500254# @brief Main function
255function main()
256{
Jayanth Othayoth230e9a32017-08-09 06:39:59 -0500257 #initialize the global variables and
258 #create temporary storage locations
259 initialize
260 result=$?
261 if [[ ${result} -ne $SUCCESS ]]; then
262 echo $(date -u)" Error: Failed to initialize, Exiting"
263 exit;
264 fi
Jayanth Othayoth6d3ee1c2017-07-26 05:18:31 -0500265
Jayanth Othayothe20d5e02017-08-09 06:48:45 -0500266 #Initilize the summary log
267 init_summary
268
Jayanth Othayoth230e9a32017-08-09 06:39:59 -0500269 #TODO Add Dump report generating script.
Jayanth Othayothff0699d2017-07-26 07:53:03 -0500270
Jayanth Othayoth230e9a32017-08-09 06:39:59 -0500271 package #package the dump
Jayanth Othayoth7b774872017-07-26 05:02:53 -0500272}
273
274TEMP=`getopt -o n:d:i:t:s:f:vVqh \
275 --long name:,dir:,dumpid:,type:,size:,file:,verbose,version,quiet,help \
276 -- "$@"`
Jayanth Othayoth230e9a32017-08-09 06:39:59 -0500277
278if [ $? -ne 0 ]
279then
280 echo "Error: Invalid options"
281 exit 1
282fi
283
Jayanth Othayoth7b774872017-07-26 05:02:53 -0500284eval set -- "$TEMP"
285
286while [[ $# -gt 1 ]]; do
287 key="$1"
288 case $key in
289 -n|--name)
290 name=$2
291 shift 2;;
292 -d|--dir)
Jayanth Othayothff0699d2017-07-26 07:53:03 -0500293 dump_dir=$2
Jayanth Othayoth7b774872017-07-26 05:02:53 -0500294 shift 2;;
295 -i|--dumpid)
296 dump_id=$2
297 shift 2;;
298 -t|--type)
299 dump_type=$2
300 shift 2;;
301 -s|--size)
302 dump_size=$2
303 shift 2;;
304 -f|--file)
Jayanth Othayoth230e9a32017-08-09 06:39:59 -0500305 optional_file=$2
Jayanth Othayoth7b774872017-07-26 05:02:53 -0500306 shift 2;;
307 -v|—-verbose)
308 verbose=$TRUE
309 shift;;
310 -V|--version)
311 shift;;
312 -q|—-quiet)
313 quiet=$TRUE
314 shift;;
315 -h|--help)
316 echo "$help"
317 exit;;
318 *) # unknown option
Jayanth Othayoth6d3ee1c2017-07-26 05:18:31 -0500319 log_error "Unknown argument: $1"
320 log_info "$help"
Jayanth Othayoth7b774872017-07-26 05:02:53 -0500321 exit 1;;
322 esac
Jayanth Othayoth9e95f4b2017-07-24 06:42:24 -0500323done
Jayanth Othayoth7b774872017-07-26 05:02:53 -0500324
325main #main program
326exit $?