blob: 21eba109484a17b70acfdc5f895597d9eb703df1 [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 Othayoth7b774872017-07-26 05:02:53 -050066
Jayanth Othayothe20d5e02017-08-09 06:48:45 -050067# @brief Initial version of the summary log
68init_summary()
69{
70 log_summary "Name: $name.tar.xz"
71 log_summary "Epochtime: $EPOCHTIME"
72 log_summary "ID: $dump_id"
73 log_summary "Type: $dump_type"
74 log_summary "Optional file: $optional_file"
75}
Jayanth Othayoth7b774872017-07-26 05:02:53 -050076
Jayanth Othayoth230e9a32017-08-09 06:39:59 -050077# @brief Check the validity of user inputs and initialize global
78# variables. Create directory for temporary data collection
79# @return 0 on success, error code otherwise
80
81function initialize()
82{
83 #Dump file name
84 if [ -z $name ]; then
85 name=$"obmcdump_"$dump_id"_$EPOCHTIME"
86 fi
87
88 #Create temporary data directory.
89 mkdir -p "$TMP_DIR/$name"
90 if [ $? -ne 0 ]; then
91 echo "Error: Failed to create the temporary directory."
92 return $RESOURCE_UNAVAILABLE;
93 fi
94
95 #name directory
96 name_dir="$TMP_DIR/$name"
97
98 #dreport log file
99 dreport_log="$name_dir/$DREPORT_LOG"
100
101 #summary log file
102 summary_log="$name_dir/$SUMMARY_LOG"
103
104 #Type
105 if [[ !($dump_type = $TYPE_USER || $dump_type = $TYPE_CORE) ]]; then
106 log_error "Invalid -type, Only summary log is available"
107 dump_type=$SUMMARY_DUMP
108 fi
109
110 #Size
111 #Check the input is integer.
112 if [ "$dump_size" -eq "$dump_size" ] 2>/dev/null; then
113 #Converts in to bytes.
114 dump_size="$((dump_size * 1024))"
115 else
116 dump_size=$UNLIMITED
117 fi
118
119 return $SUCCESS
120}
121
Jayanth Othayothff0699d2017-07-26 07:53:03 -0500122# @brief Packaging the dump and transferring to dump location.
123function package()
124{
125 mkdir -p "$dump_dir"
126 if [ $? -ne 0 ]; then
127 log_error "Could not create the destination directory $dump_dir"
128 dest_dir=$TMP_DIR
129 fi
130
131 #TODO openbmc/openbmc#1506 Enable file level compression.
132 #tar and compress the files.
133 tar_file="$name_dir.tar.xz"
134 tar -Jcf "$tar_file" -C "$TMP_DIR" "$name"
135
136 #remove the temporary name specific directory
137 rm -r "$name_dir"
138
139 #check the file size is in the allowed limit
140 if [ $(stat -c%s "$tar_file") -gt $dump_size ]; then
141 echo "File size exceeds the limit allowed"
142 rm -rf "$TMP_DIR"
143 exit 1
144 #TODO openbmc/openbmc#1506 Revisit the error handling
145 fi
146
147 echo "Report is available in $dump_dir"
148
149 if [ "$TMP_DIR" == "$dump_dir" ]; then
150 return
151 fi
152
153 #copy the compressed tar file into the destination
154 cp "$tar_file" "$dump_dir"
155 if [ $? -ne 0 ]; then
156 echo "Failed to copy the $tar_file to $dump_dir"
157 return
158 else
159 rm -rf "$TMP_DIR"
160 fi
161}
Jayanth Othayoth6d3ee1c2017-07-26 05:18:31 -0500162# @brief log the error message
163# @param error message
164function log_error()
165{
Jayanth Othayoth230e9a32017-08-09 06:39:59 -0500166 echo "ERROR: $@" >> $dreport_log
Jayanth Othayoth6d3ee1c2017-07-26 05:18:31 -0500167 if ((quiet != TRUE)); then
168 echo "ERROR: $@" >&2
169 fi
170}
171
172# @brief log warning message
173# @param warning message
174function log_warning()
175{
176 if ((verbose == TRUE)); then
Jayanth Othayoth230e9a32017-08-09 06:39:59 -0500177 echo "WARNING: $@" >> $dreport_log
Jayanth Othayoth6d3ee1c2017-07-26 05:18:31 -0500178 if ((quiet != TRUE)); then
179 echo "WARNING: $@" >&2
180 fi
181 fi
182}
183
184# @brief log info message
185# @param info message
186function log_info()
187{
188 if ((verbose == TRUE)); then
Jayanth Othayoth230e9a32017-08-09 06:39:59 -0500189 echo "INFO: $@" >> $dreport_log
Jayanth Othayoth6d3ee1c2017-07-26 05:18:31 -0500190 if ((quiet != TRUE)); then
191 echo "INFO: $@" >&1
192 fi
193 fi
194}
195
196# @brief log summary message
197# @param message
198function log_summary()
199{
Jayanth Othayoth230e9a32017-08-09 06:39:59 -0500200 echo "$@" >> $summary_log
201 if ((quiet != TRUE)); then
Jayanth Othayoth6d3ee1c2017-07-26 05:18:31 -0500202 echo "$@" >&1
Jayanth Othayoth230e9a32017-08-09 06:39:59 -0500203 fi
Jayanth Othayoth6d3ee1c2017-07-26 05:18:31 -0500204}
205
Jayanth Othayoth7b774872017-07-26 05:02:53 -0500206# @brief Main function
207function main()
208{
Jayanth Othayoth230e9a32017-08-09 06:39:59 -0500209 #initialize the global variables and
210 #create temporary storage locations
211 initialize
212 result=$?
213 if [[ ${result} -ne $SUCCESS ]]; then
214 echo $(date -u)" Error: Failed to initialize, Exiting"
215 exit;
216 fi
Jayanth Othayoth6d3ee1c2017-07-26 05:18:31 -0500217
Jayanth Othayothe20d5e02017-08-09 06:48:45 -0500218 #Initilize the summary log
219 init_summary
220
Jayanth Othayoth230e9a32017-08-09 06:39:59 -0500221 #TODO Add Dump report generating script.
Jayanth Othayothff0699d2017-07-26 07:53:03 -0500222
Jayanth Othayoth230e9a32017-08-09 06:39:59 -0500223 package #package the dump
Jayanth Othayoth7b774872017-07-26 05:02:53 -0500224}
225
226TEMP=`getopt -o n:d:i:t:s:f:vVqh \
227 --long name:,dir:,dumpid:,type:,size:,file:,verbose,version,quiet,help \
228 -- "$@"`
Jayanth Othayoth230e9a32017-08-09 06:39:59 -0500229
230if [ $? -ne 0 ]
231then
232 echo "Error: Invalid options"
233 exit 1
234fi
235
Jayanth Othayoth7b774872017-07-26 05:02:53 -0500236eval set -- "$TEMP"
237
238while [[ $# -gt 1 ]]; do
239 key="$1"
240 case $key in
241 -n|--name)
242 name=$2
243 shift 2;;
244 -d|--dir)
Jayanth Othayothff0699d2017-07-26 07:53:03 -0500245 dump_dir=$2
Jayanth Othayoth7b774872017-07-26 05:02:53 -0500246 shift 2;;
247 -i|--dumpid)
248 dump_id=$2
249 shift 2;;
250 -t|--type)
251 dump_type=$2
252 shift 2;;
253 -s|--size)
254 dump_size=$2
255 shift 2;;
256 -f|--file)
Jayanth Othayoth230e9a32017-08-09 06:39:59 -0500257 optional_file=$2
Jayanth Othayoth7b774872017-07-26 05:02:53 -0500258 shift 2;;
259 -v|—-verbose)
260 verbose=$TRUE
261 shift;;
262 -V|--version)
263 shift;;
264 -q|—-quiet)
265 quiet=$TRUE
266 shift;;
267 -h|--help)
268 echo "$help"
269 exit;;
270 *) # unknown option
Jayanth Othayoth6d3ee1c2017-07-26 05:18:31 -0500271 log_error "Unknown argument: $1"
272 log_info "$help"
Jayanth Othayoth7b774872017-07-26 05:02:53 -0500273 exit 1;;
274 esac
Jayanth Othayoth9e95f4b2017-07-24 06:42:24 -0500275done
Jayanth Othayoth7b774872017-07-26 05:02:53 -0500276
277main #main program
278exit $?