xref: /openbmc/phosphor-debug-collector/tools/dreport.d/include.d/functions (revision 3598158b90a69177f25f2ff6aab05c385c8fc987)
1 #!/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.
8 function 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         rm -f "$name_dir/$file_name"
18         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.
32 function add_copy_file()
33 {
34     file_name="$1"
35     desc="$2"
36 
37     cp -Lr $file_name $name_dir
38     if [ $? -ne 0 ]; then
39         log_error "Failed to copy $desc $file_name"
40         rm -fr "$name_dir/$file_name"
41         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
47         log_warning "Skipping copy $desc $file_name"
48         return $RESOURCE_UNAVAILABLE
49     fi
50 }
51 # @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.
55 function 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"
63         rm -fr "$name_dir/$file_name"
64         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 }
74 
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
82 function check_size()
83 {
84     source=$1
85 
86     #No size check required in case dump_size is set to unlimited
87     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 \
94             $(dirname "$source") $(basename "$source")
95         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 \
105             $(dirname "$name_dir") $(basename "$name_dir")
106         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
122 function log_error()
123 {
124     echo $($TIME_STAMP) "ERROR: $*" >> $dreport_log
125     if ((quiet != TRUE)); then
126         echo $($TIME_STAMP) "ERROR: $*" >&2
127     fi
128 }
129 
130 # @brief log warning message
131 # @param warning message
132 function log_warning()
133 {
134     if ((verbose == TRUE)); then
135         echo $($TIME_STAMP) "WARNING: $*" >> $dreport_log
136         if ((quiet != TRUE)); then
137             echo $($TIME_STAMP) "WARNING: $*" >&2
138         fi
139     fi
140 }
141 
142 # @brief log info message
143 # @param info message
144 function log_info()
145 {
146     if ((verbose == TRUE)); then
147         echo $($TIME_STAMP) "INFO: $*" >> $dreport_log
148         if ((quiet != TRUE)); then
149             echo $($TIME_STAMP) "INFO: $*" >&1
150         fi
151     fi
152 }
153 
154 # @brief log summary message
155 # @param message
156 function log_summary()
157 {
158     echo $($TIME_STAMP) "$*" >> $summary_log
159     if ((quiet != TRUE)); then
160         echo $($TIME_STAMP) "$*" >&1
161     fi
162 }
163