blob: a2af4da187cf8097f0221578375ff8978f62bb8e [file] [log] [blame]
Jayanth Othayoth9d817ba2017-10-15 05:24:39 -05001#!/usr/bin/env bash
2
3# @brief Execute the command and save the output into the dreport
4# packaging, if it is in the user allowed dump size limit.
5# @param $1 Command to be executed.
6# @param $2 Save file name.
7# @param $3 Plugin description used for logging.
8function add_cmd_output()
9{
10 command="$1"
11 file_name="$2"
12 desc="$3"
13
14 eval $command >> "$name_dir/$file_name"
15 if [ $? -ne 0 ]; then
16 log_error "Failed to collect $desc"
17 return 1
18 fi
19
20 if check_size "$name_dir/$file_name"; then
21 log_info "Collected $desc"
22 else
23 log_warning "Skipping $desc"
24 fi
25}
26
27# @brief Copy the file or directory into the dreport packaging,
28# if it is in the user allowed dump size limit.
29# @param $1 Copy file or directory name.
30# @param $2 Plugin description used for logging.
31function add_copy_file()
32{
33 file_name="$1"
34 desc="$2"
35
36 cp -r $file_name $name_dir
37 if [ $? -ne 0 ]; then
38 log_error "Failed to copy $desc $file_name"
39 return $RESOURCE_UNAVAILABLE
40 fi
41 if check_size "$name_dir/$(basename "$file_name")"; then
42 log_info "Copied $desc $file_name"
43 return $SUCCESS
44 else
45 return $RESOURCE_UNAVAILABLE
46 log_warning "Skipping copy $desc $file_name"
47 fi
48}
49
50# @brief Calculate file or directory compressed size based on input
51# and check whether the size in the allowed size limit.
52# Remove the file or directory from the name_dir
53# if the check fails.
54# @param $1 Source file or directory
55# @return 0 on success, error code if size exceeds the limit.
56# Limitation: compress and tar will have few bytes size difference
57function check_size()
58{
59 source=$1
60
61 #No size check required incase dump_size is set to unlimited
62 if [ $dump_size = $UNLIMITED ]; then
63 return 0
64 fi
65
66 #get the file or directory size
67 if [[ -d $source ]] && [[ -n $source ]]; then
68 tar -cf "$source.tar" -C \
69 $(dirname "$source") $(basename "$source")
70 size=$(stat -c%s "$source.tar")
71 rm "$source.tar"
72 else
73 size=$(stat -c%s "$source")
74 fi
75
76 if [ $((size + cur_dump_size)) -gt $dump_size ]; then
77 #Exceed the allowed limit,
78 #tar and compress the files and check the size
79 tar -Jcf "$name_dir.tar.xz" -C \
80 $(dirname "$name_dir") $(basename "$name_dir")
81 size=$(stat -c%s "$name_dir.tar.xz")
82 if [ $size -gt $dump_size ]; then
83 #Remove the the specific data from the name_dir and continue
84 rm "$source" "$name_dir.tar.xz"
85 return $RESOURCE_UNAVAILABLE
86 else
87 rm "$name_dir.tar.xz"
88 fi
89 fi
90
91 cur_dump_size=$((size + cur_dump_size))
92 return $SUCCESS
93}
94
95# @brief log the error message
96# @param error message
97function log_error()
98{
99 echo $($TIME_STAMP) "ERROR: $@" >> $dreport_log
100 if ((quiet != TRUE)); then
101 echo $($TIME_STAMP) "ERROR: $@" >&2
102 fi
103}
104
105# @brief log warning message
106# @param warning message
107function log_warning()
108{
109 if ((verbose == TRUE)); then
110 echo $($TIME_STAMP) "WARNING: $@" >> $dreport_log
111 if ((quiet != TRUE)); then
112 echo $($TIME_STAMP) "WARNING: $@" >&2
113 fi
114 fi
115}
116
117# @brief log info message
118# @param info message
119function log_info()
120{
121 if ((verbose == TRUE)); then
122 echo $($TIME_STAMP) "INFO: $@" >> $dreport_log
123 if ((quiet != TRUE)); then
124 echo $($TIME_STAMP) "INFO: $@" >&1
125 fi
126 fi
127}
128
129# @brief log summary message
130# @param message
131function log_summary()
132{
133 echo $($TIME_STAMP) "$@" >> $summary_log
134 if ((quiet != TRUE)); then
135 echo $($TIME_STAMP) "$@" >&1
136 fi
137}