-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathram_memory.sh
executable file
·280 lines (258 loc) · 8.17 KB
/
ram_memory.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env bash
# Script Name: ram_memory.sh
# Description: Checks if the system has enough RAM and displays current RAM usage by top programs.
# Usage: ram_memory.sh [options]
#
# Options:
# -h, --help Display this help message and exit.
# -v, --verbose Enable verbose output.
# -m, --minimum RAM Specify minimum RAM required (in GB). Default is 1 GB.
# -u, --unit UNIT Specify unit for RAM values (GB, MB, KB). Default is GB.
# -t, --top N Display top N processes by RAM usage. Default is 10.
# -l, --log-file FILE Enable logging to specified log file.
# -c, --critical PERCENT Specify critical RAM usage level in percent (e.g., 90).
# -s, --swap Include swap memory in calculations.
# -o, --output FILE Save output to specified file.
# --json Output in JSON format.
# --no-color Disable colored output.
#
# Examples:
# ram_memory.sh --minimum 2 --unit GB --top 5
# ram_memory.sh -v -m 1024 -u MB
# ram_memory.sh --critical 85
set -euo pipefail
# Default configurations
VERBOSE=false
MINIMUM_RAM=1
UNIT="GB"
TOP_N=10
LOG_FILE=""
LOG_ENABLED=false
CRITICAL_LEVEL=0
INCLUDE_SWAP=false
OUTPUT_FILE=""
OUTPUT_JSON=false
NO_COLOR=false
# Function to display usage information
print_usage() {
cat << EOF
Usage: $0 [options]
Options:
-h, --help Display this help message and exit.
-v, --verbose Enable verbose output.
-m, --minimum RAM Specify minimum RAM required (in GB). Default is 1 GB.
-u, --unit UNIT Specify unit for RAM values (GB, MB, KB). Default is GB.
-t, --top N Display top N processes by RAM usage. Default is 10.
-l, --log-file FILE Enable logging to specified log file.
-c, --critical PERCENT Specify critical RAM usage level in percent (e.g., 90).
-s, --swap Include swap memory in calculations.
-o, --output FILE Save output to specified file.
--json Output in JSON format.
--no-color Disable colored output.
Examples:
$0 --minimum 2 --unit GB --top 5
$0 -v -m 1024 -u MB
$0 --critical 85
EOF
}
# Function for logging
log_action() {
local message="$1"
if [[ "$LOG_ENABLED" == true ]]; then
echo "$(date +"%Y-%m-%d %T"): $message" >> "$LOG_FILE"
fi
if [[ "$VERBOSE" == true ]]; then
echo "$message"
fi
}
# Function to convert RAM values
convert_ram() {
local ram_kb="$1"
local unit="$2"
local ram_value
case "$unit" in
GB|gb)
ram_value=$(echo "scale=2; $ram_kb/1024/1024" | bc)
;;
MB|mb)
ram_value=$(echo "scale=2; $ram_kb/1024" | bc)
;;
KB|kb)
ram_value="$ram_kb"
;;
*)
echo "Invalid unit: $unit"
exit 1
;;
esac
echo "$ram_value"
}
# Function to check minimum RAM
check_minimum_ram() {
local total_ram_kb
if ! total_ram_kb=$(grep MemTotal /proc/meminfo | awk '{print $2}'); then
echo "Unable to determine total RAM."
exit 1
fi
if [[ "$INCLUDE_SWAP" == true ]]; then
if swap_total_kb=$(grep SwapTotal /proc/meminfo | awk '{print $2}'); then
total_ram_kb=$((total_ram_kb + swap_total_kb))
fi
fi
local total_ram=$(convert_ram "$total_ram_kb" "$UNIT")
local min_ram_kb
case "$UNIT" in
GB|gb)
min_ram_kb=$(echo "$MINIMUM_RAM * 1024 * 1024" | bc)
;;
MB|mb)
min_ram_kb=$(echo "$MINIMUM_RAM * 1024" | bc)
;;
KB|kb)
min_ram_kb="$MINIMUM_RAM"
;;
esac
if (( total_ram_kb < min_ram_kb )); then
echo "The system doesn't meet the requirements. RAM size must be at least $MINIMUM_RAM $UNIT."
log_action "Insufficient RAM. Required: $MINIMUM_RAM $UNIT, Available: $total_ram $UNIT."
exit 1
else
echo "The system meets the minimum RAM requirements."
log_action "Sufficient RAM. Required: $MINIMUM_RAM $UNIT, Available: $total_ram $UNIT."
fi
}
# Function to display top RAM usage
display_top_ram_usage() {
echo "Top $TOP_N programs by RAM usage:"
ps aux --sort=-%mem | head -n $((TOP_N + 1)) | awk '{printf "%-10s %-8s %-5s %-5s %s\n", $1, $2, $3, $4, $11}' | column -t
log_action "Top RAM consuming processes displayed."
}
# Parse command-line arguments
ARGS=("$@")
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-h|--help)
print_usage
exit 0
;;
-v|--verbose)
VERBOSE=true
shift
;;
-m|--minimum)
if [[ -n "$2" ]]; then
MINIMUM_RAM="$2"
shift 2
else
echo "Error: --minimum requires a value."
exit 1
fi
;;
-u|--unit)
if [[ -n "$2" ]]; then
UNIT="$2"
shift 2
else
echo "Error: --unit requires a value."
exit 1
fi
;;
-t|--top)
if [[ -n "$2" ]]; then
TOP_N="$2"
shift 2
else
echo "Error: --top requires a value."
exit 1
fi
;;
-l|--log-file)
if [[ -n "$2" ]]; then
LOG_FILE="$2"
LOG_ENABLED=true
shift 2
else
echo "Error: --log-file requires a value."
exit 1
fi
;;
-c|--critical)
if [[ -n "$2" ]]; then
CRITICAL_LEVEL="$2"
shift 2
else
echo "Error: --critical requires a value."
exit 1
fi
;;
-s|--swap)
INCLUDE_SWAP=true
shift
;;
-o|--output)
if [[ -n "$2" ]]; then
OUTPUT_FILE="$2"
shift 2
else
echo "Error: --output requires a value."
exit 1
fi
;;
--json)
OUTPUT_JSON=true
shift
;;
--no-color)
NO_COLOR=true
shift
;;
*)
echo "Unknown option: $1"
print_usage
exit 1
;;
esac
done
# Redirect output to file if specified
if [[ -n "$OUTPUT_FILE" ]]; then
exec > >(tee -a "$OUTPUT_FILE")
fi
# Main execution
check_minimum_ram
display_top_ram_usage
# Check critical RAM usage level
if [[ "$CRITICAL_LEVEL" -gt 0 ]]; then
meminfo=$(grep -E 'MemTotal|MemAvailable' /proc/meminfo)
total_ram_kb=$(echo "$meminfo" | grep MemTotal | awk '{print $2}')
mem_available_kb=$(echo "$meminfo" | grep MemAvailable | awk '{print $2}')
mem_used_kb=$((total_ram_kb - mem_available_kb))
mem_used_percent=$((mem_used_kb * 100 / total_ram_kb))
if (( mem_used_percent > CRITICAL_LEVEL )); then
echo "Warning: Memory usage is above critical level ($CRITICAL_LEVEL%)."
log_action "Memory usage critical: $mem_used_percent% used."
fi
fi
# Output in JSON format if requested
if [[ "$OUTPUT_JSON" == true ]]; then
meminfo=$(grep -E 'MemTotal|MemFree|MemAvailable|SwapTotal|SwapFree' /proc/meminfo)
total_ram_kb=$(echo "$meminfo" | grep MemTotal | awk '{print $2}')
mem_free_kb=$(echo "$meminfo" | grep MemFree | awk '{print $2}')
mem_available_kb=$(echo "$meminfo" | grep MemAvailable | awk '{print $2}')
swap_total_kb=$(echo "$meminfo" | grep SwapTotal | awk '{print $2}')
swap_free_kb=$(echo "$meminfo" | grep SwapFree | awk '{print $2}')
total_ram=$(convert_ram "$total_ram_kb" "$UNIT")
mem_free=$(convert_ram "$mem_free_kb" "$UNIT")
mem_available=$(convert_ram "$mem_available_kb" "$UNIT")
swap_total=$(convert_ram "$swap_total_kb" "$UNIT")
swap_free=$(convert_ram "$swap_free_kb" "$UNIT")
cat << EOF
{
"total_ram_$UNIT": "$total_ram",
"mem_free_$UNIT": "$mem_free",
"mem_available_$UNIT": "$mem_available",
"swap_total_$UNIT": "$swap_total",
"swap_free_$UNIT": "$swap_free"
}
EOF
fi