blob: d3aff639497db3a0ceb554f9e7f51e4735e9a860 [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
Jayanth Othayoth4d9b3a72022-02-17 04:29:55 -060036 cp -Lr $file_name $name_dir
Jayanth Othayoth9d817ba2017-10-15 05:24:39 -050037 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
Jayanth Othayoth9d817ba2017-10-15 05:24:39 -050045 log_warning "Skipping copy $desc $file_name"
Jayanth Othayotha96d0b22022-02-17 00:50:28 -060046 return $RESOURCE_UNAVAILABLE
Jayanth Othayoth9d817ba2017-10-15 05:24:39 -050047 fi
48}
Marri Devender Rao5ba71762019-06-06 07:32:39 -050049# @brief Copy the symbolic link file to the dreport packaging,
50# if it is in the user allowed dump size limit.
51# @param $1 symbolic link file name
52# @param $2 Plugin description used for logging.
53function add_copy_sym_link_file()
54{
55 file_name="$1"
56 desc="$2"
57
58 cp $file_name $name_dir
59 if [ $? -ne 0 ]; then
60 log_error "Failed to copy $desc $file_name"
61 return $RESOURCE_UNAVAILABLE
62 fi
63 if check_size "$name_dir/$(basename "$file_name")"; then
64 log_info "Copied $desc $file_name"
65 return $SUCCESS
66 else
67 log_warning "Skipping copy $desc $file_name"
68 return $RESOURCE_UNAVAILABLE
69 fi
70}
Jayanth Othayoth9d817ba2017-10-15 05:24:39 -050071
72# @brief Calculate file or directory compressed size based on input
73# and check whether the size in the allowed size limit.
74# Remove the file or directory from the name_dir
75# if the check fails.
76# @param $1 Source file or directory
77# @return 0 on success, error code if size exceeds the limit.
78# Limitation: compress and tar will have few bytes size difference
79function check_size()
80{
81 source=$1
82
Gunnar Millsb223b662018-04-08 15:02:06 -050083 #No size check required in case dump_size is set to unlimited
Jayanth Othayoth9d817ba2017-10-15 05:24:39 -050084 if [ $dump_size = $UNLIMITED ]; then
85 return 0
86 fi
87
88 #get the file or directory size
89 if [[ -d $source ]] && [[ -n $source ]]; then
90 tar -cf "$source.tar" -C \
91 $(dirname "$source") $(basename "$source")
92 size=$(stat -c%s "$source.tar")
93 rm "$source.tar"
94 else
95 size=$(stat -c%s "$source")
96 fi
97
98 if [ $((size + cur_dump_size)) -gt $dump_size ]; then
99 #Exceed the allowed limit,
100 #tar and compress the files and check the size
101 tar -Jcf "$name_dir.tar.xz" -C \
102 $(dirname "$name_dir") $(basename "$name_dir")
103 size=$(stat -c%s "$name_dir.tar.xz")
104 if [ $size -gt $dump_size ]; then
105 #Remove the the specific data from the name_dir and continue
106 rm "$source" "$name_dir.tar.xz"
107 return $RESOURCE_UNAVAILABLE
108 else
109 rm "$name_dir.tar.xz"
110 fi
111 fi
112
113 cur_dump_size=$((size + cur_dump_size))
114 return $SUCCESS
115}
116
117# @brief log the error message
118# @param error message
119function log_error()
120{
Dhruvaraj Subhashchandranfdc0c3a2020-09-14 01:38:27 -0500121 echo $($TIME_STAMP) "ERROR: $*" >> $dreport_log
Jayanth Othayoth9d817ba2017-10-15 05:24:39 -0500122 if ((quiet != TRUE)); then
Dhruvaraj Subhashchandranfdc0c3a2020-09-14 01:38:27 -0500123 echo $($TIME_STAMP) "ERROR: $*" >&2
Jayanth Othayoth9d817ba2017-10-15 05:24:39 -0500124 fi
125}
126
127# @brief log warning message
128# @param warning message
129function log_warning()
130{
131 if ((verbose == TRUE)); then
Dhruvaraj Subhashchandranfdc0c3a2020-09-14 01:38:27 -0500132 echo $($TIME_STAMP) "WARNING: $*" >> $dreport_log
Jayanth Othayoth9d817ba2017-10-15 05:24:39 -0500133 if ((quiet != TRUE)); then
Dhruvaraj Subhashchandranfdc0c3a2020-09-14 01:38:27 -0500134 echo $($TIME_STAMP) "WARNING: $*" >&2
Jayanth Othayoth9d817ba2017-10-15 05:24:39 -0500135 fi
136 fi
137}
138
139# @brief log info message
140# @param info message
141function log_info()
142{
143 if ((verbose == TRUE)); then
Dhruvaraj Subhashchandranfdc0c3a2020-09-14 01:38:27 -0500144 echo $($TIME_STAMP) "INFO: $*" >> $dreport_log
Jayanth Othayoth9d817ba2017-10-15 05:24:39 -0500145 if ((quiet != TRUE)); then
Dhruvaraj Subhashchandranfdc0c3a2020-09-14 01:38:27 -0500146 echo $($TIME_STAMP) "INFO: $*" >&1
Jayanth Othayoth9d817ba2017-10-15 05:24:39 -0500147 fi
148 fi
149}
150
151# @brief log summary message
152# @param message
153function log_summary()
154{
Dhruvaraj Subhashchandranfdc0c3a2020-09-14 01:38:27 -0500155 echo $($TIME_STAMP) "$*" >> $summary_log
Jayanth Othayoth9d817ba2017-10-15 05:24:39 -0500156 if ((quiet != TRUE)); then
Dhruvaraj Subhashchandranfdc0c3a2020-09-14 01:38:27 -0500157 echo $($TIME_STAMP) "$*" >&1
Jayanth Othayoth9d817ba2017-10-15 05:24:39 -0500158 fi
159}