blob: 24f5a12263989bad75082d0ddbd781d621f6f045 [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"
Alexander Filippov35981582023-01-12 19:42:30 +030017 rm -f "$name_dir/$file_name"
Jayanth Othayoth9d817ba2017-10-15 05:24:39 -050018 return 1
19 fi
20
21 if check_size "$name_dir/$file_name"; then
22 log_info "Collected $desc"
23 else
24 log_warning "Skipping $desc"
25 fi
26}
27
28# @brief Copy the file or directory into the dreport packaging,
29# if it is in the user allowed dump size limit.
30# @param $1 Copy file or directory name.
31# @param $2 Plugin description used for logging.
32function add_copy_file()
33{
34 file_name="$1"
35 desc="$2"
36
Jayanth Othayoth4d9b3a72022-02-17 04:29:55 -060037 cp -Lr $file_name $name_dir
Jayanth Othayoth9d817ba2017-10-15 05:24:39 -050038 if [ $? -ne 0 ]; then
39 log_error "Failed to copy $desc $file_name"
Alexander Filippov35981582023-01-12 19:42:30 +030040 rm -fr "$name_dir/$file_name"
Jayanth Othayoth9d817ba2017-10-15 05:24:39 -050041 return $RESOURCE_UNAVAILABLE
42 fi
43 if check_size "$name_dir/$(basename "$file_name")"; then
44 log_info "Copied $desc $file_name"
45 return $SUCCESS
46 else
Jayanth Othayoth9d817ba2017-10-15 05:24:39 -050047 log_warning "Skipping copy $desc $file_name"
Jayanth Othayotha96d0b22022-02-17 00:50:28 -060048 return $RESOURCE_UNAVAILABLE
Jayanth Othayoth9d817ba2017-10-15 05:24:39 -050049 fi
50}
Marri Devender Rao5ba71762019-06-06 07:32:39 -050051# @brief Copy the symbolic link file to the dreport packaging,
52# if it is in the user allowed dump size limit.
53# @param $1 symbolic link file name
54# @param $2 Plugin description used for logging.
55function add_copy_sym_link_file()
56{
57 file_name="$1"
58 desc="$2"
59
60 cp $file_name $name_dir
61 if [ $? -ne 0 ]; then
62 log_error "Failed to copy $desc $file_name"
Alexander Filippov35981582023-01-12 19:42:30 +030063 rm -fr "$name_dir/$file_name"
Marri Devender Rao5ba71762019-06-06 07:32:39 -050064 return $RESOURCE_UNAVAILABLE
65 fi
66 if check_size "$name_dir/$(basename "$file_name")"; then
67 log_info "Copied $desc $file_name"
68 return $SUCCESS
69 else
70 log_warning "Skipping copy $desc $file_name"
71 return $RESOURCE_UNAVAILABLE
72 fi
73}
Jayanth Othayoth9d817ba2017-10-15 05:24:39 -050074
75# @brief Calculate file or directory compressed size based on input
76# and check whether the size in the allowed size limit.
77# Remove the file or directory from the name_dir
78# if the check fails.
79# @param $1 Source file or directory
80# @return 0 on success, error code if size exceeds the limit.
81# Limitation: compress and tar will have few bytes size difference
82function check_size()
83{
84 source=$1
85
Gunnar Millsb223b662018-04-08 15:02:06 -050086 #No size check required in case dump_size is set to unlimited
Jayanth Othayoth9d817ba2017-10-15 05:24:39 -050087 if [ $dump_size = $UNLIMITED ]; then
88 return 0
89 fi
90
91 #get the file or directory size
92 if [[ -d $source ]] && [[ -n $source ]]; then
93 tar -cf "$source.tar" -C \
Patrick Williams9d26e4f2022-12-08 06:46:44 -060094 $(dirname "$source") $(basename "$source")
Jayanth Othayoth9d817ba2017-10-15 05:24:39 -050095 size=$(stat -c%s "$source.tar")
96 rm "$source.tar"
97 else
98 size=$(stat -c%s "$source")
99 fi
100
101 if [ $((size + cur_dump_size)) -gt $dump_size ]; then
102 #Exceed the allowed limit,
103 #tar and compress the files and check the size
104 tar -Jcf "$name_dir.tar.xz" -C \
Patrick Williams9d26e4f2022-12-08 06:46:44 -0600105 $(dirname "$name_dir") $(basename "$name_dir")
Jayanth Othayoth9d817ba2017-10-15 05:24:39 -0500106 size=$(stat -c%s "$name_dir.tar.xz")
107 if [ $size -gt $dump_size ]; then
108 #Remove the the specific data from the name_dir and continue
109 rm "$source" "$name_dir.tar.xz"
110 return $RESOURCE_UNAVAILABLE
111 else
112 rm "$name_dir.tar.xz"
113 fi
114 fi
115
116 cur_dump_size=$((size + cur_dump_size))
117 return $SUCCESS
118}
119
120# @brief log the error message
121# @param error message
122function log_error()
123{
Patrick Williams9d26e4f2022-12-08 06:46:44 -0600124 echo $($TIME_STAMP) "ERROR: $*" >> $dreport_log
125 if ((quiet != TRUE)); then
126 echo $($TIME_STAMP) "ERROR: $*" >&2
127 fi
Jayanth Othayoth9d817ba2017-10-15 05:24:39 -0500128}
129
130# @brief log warning message
131# @param warning message
132function log_warning()
133{
134 if ((verbose == TRUE)); then
Dhruvaraj Subhashchandranfdc0c3a2020-09-14 01:38:27 -0500135 echo $($TIME_STAMP) "WARNING: $*" >> $dreport_log
Jayanth Othayoth9d817ba2017-10-15 05:24:39 -0500136 if ((quiet != TRUE)); then
Dhruvaraj Subhashchandranfdc0c3a2020-09-14 01:38:27 -0500137 echo $($TIME_STAMP) "WARNING: $*" >&2
Jayanth Othayoth9d817ba2017-10-15 05:24:39 -0500138 fi
139 fi
140}
141
142# @brief log info message
143# @param info message
144function log_info()
145{
146 if ((verbose == TRUE)); then
Dhruvaraj Subhashchandranfdc0c3a2020-09-14 01:38:27 -0500147 echo $($TIME_STAMP) "INFO: $*" >> $dreport_log
Jayanth Othayoth9d817ba2017-10-15 05:24:39 -0500148 if ((quiet != TRUE)); then
Dhruvaraj Subhashchandranfdc0c3a2020-09-14 01:38:27 -0500149 echo $($TIME_STAMP) "INFO: $*" >&1
Jayanth Othayoth9d817ba2017-10-15 05:24:39 -0500150 fi
151 fi
152}
153
154# @brief log summary message
155# @param message
156function log_summary()
157{
Dhruvaraj Subhashchandranfdc0c3a2020-09-14 01:38:27 -0500158 echo $($TIME_STAMP) "$*" >> $summary_log
Jayanth Othayoth9d817ba2017-10-15 05:24:39 -0500159 if ((quiet != TRUE)); then
Dhruvaraj Subhashchandranfdc0c3a2020-09-14 01:38:27 -0500160 echo $($TIME_STAMP) "$*" >&1
Jayanth Othayoth9d817ba2017-10-15 05:24:39 -0500161 fi
162}