blob: f04100ff32e35509927a2ceafcd9b1ea2a6be2cc [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
67# PACKAGE VERSION
68PACKAGE_VERSION="0.0.1"
69
Jayanth Othayoth230e9a32017-08-09 06:39:59 -050070# @brief Check the validity of user inputs and initialize global
71# variables. Create directory for temporary data collection
72# @return 0 on success, error code otherwise
73
74function initialize()
75{
76 #Dump file name
77 if [ -z $name ]; then
78 name=$"obmcdump_"$dump_id"_$EPOCHTIME"
79 fi
80
81 #Create temporary data directory.
82 mkdir -p "$TMP_DIR/$name"
83 if [ $? -ne 0 ]; then
84 echo "Error: Failed to create the temporary directory."
85 return $RESOURCE_UNAVAILABLE;
86 fi
87
88 #name directory
89 name_dir="$TMP_DIR/$name"
90
91 #dreport log file
92 dreport_log="$name_dir/$DREPORT_LOG"
93
94 #summary log file
95 summary_log="$name_dir/$SUMMARY_LOG"
96
97 #Type
98 if [[ !($dump_type = $TYPE_USER || $dump_type = $TYPE_CORE) ]]; then
99 log_error "Invalid -type, Only summary log is available"
100 dump_type=$SUMMARY_DUMP
101 fi
102
103 #Size
104 #Check the input is integer.
105 if [ "$dump_size" -eq "$dump_size" ] 2>/dev/null; then
106 #Converts in to bytes.
107 dump_size="$((dump_size * 1024))"
108 else
109 dump_size=$UNLIMITED
110 fi
111
112 return $SUCCESS
113}
114
Jayanth Othayothff0699d2017-07-26 07:53:03 -0500115# @brief Packaging the dump and transferring to dump location.
116function package()
117{
118 mkdir -p "$dump_dir"
119 if [ $? -ne 0 ]; then
120 log_error "Could not create the destination directory $dump_dir"
121 dest_dir=$TMP_DIR
122 fi
123
124 #TODO openbmc/openbmc#1506 Enable file level compression.
125 #tar and compress the files.
126 tar_file="$name_dir.tar.xz"
127 tar -Jcf "$tar_file" -C "$TMP_DIR" "$name"
128
129 #remove the temporary name specific directory
130 rm -r "$name_dir"
131
132 #check the file size is in the allowed limit
133 if [ $(stat -c%s "$tar_file") -gt $dump_size ]; then
134 echo "File size exceeds the limit allowed"
135 rm -rf "$TMP_DIR"
136 exit 1
137 #TODO openbmc/openbmc#1506 Revisit the error handling
138 fi
139
140 echo "Report is available in $dump_dir"
141
142 if [ "$TMP_DIR" == "$dump_dir" ]; then
143 return
144 fi
145
146 #copy the compressed tar file into the destination
147 cp "$tar_file" "$dump_dir"
148 if [ $? -ne 0 ]; then
149 echo "Failed to copy the $tar_file to $dump_dir"
150 return
151 else
152 rm -rf "$TMP_DIR"
153 fi
154}
Jayanth Othayoth6d3ee1c2017-07-26 05:18:31 -0500155# @brief log the error message
156# @param error message
157function log_error()
158{
Jayanth Othayoth230e9a32017-08-09 06:39:59 -0500159 echo "ERROR: $@" >> $dreport_log
Jayanth Othayoth6d3ee1c2017-07-26 05:18:31 -0500160 if ((quiet != TRUE)); then
161 echo "ERROR: $@" >&2
162 fi
163}
164
165# @brief log warning message
166# @param warning message
167function log_warning()
168{
169 if ((verbose == TRUE)); then
Jayanth Othayoth230e9a32017-08-09 06:39:59 -0500170 echo "WARNING: $@" >> $dreport_log
Jayanth Othayoth6d3ee1c2017-07-26 05:18:31 -0500171 if ((quiet != TRUE)); then
172 echo "WARNING: $@" >&2
173 fi
174 fi
175}
176
177# @brief log info message
178# @param info message
179function log_info()
180{
181 if ((verbose == TRUE)); then
Jayanth Othayoth230e9a32017-08-09 06:39:59 -0500182 echo "INFO: $@" >> $dreport_log
Jayanth Othayoth6d3ee1c2017-07-26 05:18:31 -0500183 if ((quiet != TRUE)); then
184 echo "INFO: $@" >&1
185 fi
186 fi
187}
188
189# @brief log summary message
190# @param message
191function log_summary()
192{
Jayanth Othayoth230e9a32017-08-09 06:39:59 -0500193 echo "$@" >> $summary_log
194 if ((quiet != TRUE)); then
Jayanth Othayoth6d3ee1c2017-07-26 05:18:31 -0500195 echo "$@" >&1
Jayanth Othayoth230e9a32017-08-09 06:39:59 -0500196 fi
Jayanth Othayoth6d3ee1c2017-07-26 05:18:31 -0500197}
198
Jayanth Othayoth7b774872017-07-26 05:02:53 -0500199# @brief Main function
200function main()
201{
Jayanth Othayoth230e9a32017-08-09 06:39:59 -0500202 #initialize the global variables and
203 #create temporary storage locations
204 initialize
205 result=$?
206 if [[ ${result} -ne $SUCCESS ]]; then
207 echo $(date -u)" Error: Failed to initialize, Exiting"
208 exit;
209 fi
Jayanth Othayoth6d3ee1c2017-07-26 05:18:31 -0500210
Jayanth Othayoth230e9a32017-08-09 06:39:59 -0500211 #TODO Add Dump report generating script.
Jayanth Othayothff0699d2017-07-26 07:53:03 -0500212
Jayanth Othayoth230e9a32017-08-09 06:39:59 -0500213 package #package the dump
Jayanth Othayoth7b774872017-07-26 05:02:53 -0500214}
215
216TEMP=`getopt -o n:d:i:t:s:f:vVqh \
217 --long name:,dir:,dumpid:,type:,size:,file:,verbose,version,quiet,help \
218 -- "$@"`
Jayanth Othayoth230e9a32017-08-09 06:39:59 -0500219
220if [ $? -ne 0 ]
221then
222 echo "Error: Invalid options"
223 exit 1
224fi
225
Jayanth Othayoth7b774872017-07-26 05:02:53 -0500226eval set -- "$TEMP"
227
228while [[ $# -gt 1 ]]; do
229 key="$1"
230 case $key in
231 -n|--name)
232 name=$2
233 shift 2;;
234 -d|--dir)
Jayanth Othayothff0699d2017-07-26 07:53:03 -0500235 dump_dir=$2
Jayanth Othayoth7b774872017-07-26 05:02:53 -0500236 shift 2;;
237 -i|--dumpid)
238 dump_id=$2
239 shift 2;;
240 -t|--type)
241 dump_type=$2
242 shift 2;;
243 -s|--size)
244 dump_size=$2
245 shift 2;;
246 -f|--file)
Jayanth Othayoth230e9a32017-08-09 06:39:59 -0500247 optional_file=$2
Jayanth Othayoth7b774872017-07-26 05:02:53 -0500248 shift 2;;
249 -v|—-verbose)
250 verbose=$TRUE
251 shift;;
252 -V|--version)
253 shift;;
254 -q|—-quiet)
255 quiet=$TRUE
256 shift;;
257 -h|--help)
258 echo "$help"
259 exit;;
260 *) # unknown option
Jayanth Othayoth6d3ee1c2017-07-26 05:18:31 -0500261 log_error "Unknown argument: $1"
262 log_info "$help"
Jayanth Othayoth7b774872017-07-26 05:02:53 -0500263 exit 1;;
264 esac
Jayanth Othayoth9e95f4b2017-07-24 06:42:24 -0500265done
Jayanth Othayoth7b774872017-07-26 05:02:53 -0500266
267main #main program
268exit $?