-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasy_mem_watch.sh
181 lines (147 loc) · 6.11 KB
/
easy_mem_watch.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
#!/bin/bash
# Version: 1.0.12
# Function to display error and exit
error_exit() {
echo "Error: $1"
exit 1
}
bash <(curl -s https://raw.githubusercontent.com/Dr0ff/Useful-scripts/refs/heads/main/tt.logo.sh)
echo -e "\e[33m"
echo "/////////////////////////////////////////////////////////////////////////////////////////////"
echo "// //"
echo -e "// \e[0mThis script will help you to configure and install the memory usage monitor \e[33m //"
echo -e "// \e[0mfor your validator node, just follow few simple steps bellow \e[33m //"
echo "// //"
echo "/////////////////////////////////////////////////////////////////////////////////////////////"
echo " "
echo " "
echo " "
echo -e "\e[32m Now we need to find the process to monitor the memory consumption\e[0m"
echo ""
# Function to prompt for manual or search input
prompt_process_name() {
while true; do
echo -e "\e[33mEnter the process name or part of it (e.g xpl ju juno ...)\e[0m"
read -p "" PROCESS_PART
if [[ -z "$PROCESS_PART" ]] || ! [[ "$PROCESS_PART" =~ ^[a-zA-Z0-9._-]+$ ]]; then
echo -e "\e[31mInvalid input. Please try again.\e[0m\n"
else
MATCHING_PROCESSES=$(ps -eo comm | grep -i "$PROCESS_PART" | sort | uniq)
if [[ -z "$MATCHING_PROCESSES" ]]; then
echo -e "\e[31mNo matching processes found. Please try again.\e[0m\n"
else
# Display the matching processes
echo -e "\n\e[33mMATCHING PROCESSES:\e[0m"
PROCESS_LIST=()
i=1
while IFS= read -r process; do
echo "$i. $process"
PROCESS_LIST+=("$process")
((i++))
done <<< "$MATCHING_PROCESSES"
while true; do
echo -e "\n\e[33mChoose a process\e[0m"
read -p "" SELECTION
if [[ "$SELECTION" =~ ^[0-9]+$ ]]; then
if [[ "$SELECTION" -ge 1 && "$SELECTION" -le "${#PROCESS_LIST[@]}" ]]; then
SELECTED_PROCESS="${PROCESS_LIST[$SELECTION - 1]}"
echo -e "\n\e[33mPROCESS NAME SELECTED: \e[32m$SELECTED_PROCESS\e[0m\n"
break 2
else
echo -e "\n\e[31mInvalid selection. Please try again.\e[0m"
fi
else
echo -e "\n\e[31mInvalid input. Please enter a number.\e[0m"
fi
done
fi
fi
done
}
# Function to prompt for directory
prompt_directory() {
while true; do
echo -e "\n\e[32mWe need to find the directory to save this script (e.g. .xpla or .juno...)\e[0m"
echo "Searching for matching directories..."
# Выполним команду поиска с фильтрацией точки в имени и исключением текущей директории
DIRECTORIES=$(find . -maxdepth 1 -type d -name ".${SELECTED_PROCESS:0:4}*" 2>/dev/null | sed 's/^\.\///g')
if [[ -z "$DIRECTORIES" ]]; then
echo -e "\n\e[31mNo matching directories found.\n\e[33mLooks like the directory doesn't exist.\e[0m"
echo -e "\n\e[33mPlease check the installation directory and try again\nExiting...\e[0m"
exit 1
else
echo -e "\n\e[33mMATCHING DIRECTORIES:\e[0m"
i=1
DIRECTORY_LIST=()
for DIR in $DIRECTORIES; do
echo "$i. $DIR"
DIRECTORY_LIST+=("$DIR")
((i++))
done
echo -e "\n\e[33mEnter the number corresponding to the directory:\e[0m"
read -p "" DIR_SELECTION
if [[ "$DIR_SELECTION" -ge 1 && "$DIR_SELECTION" -le "${#DIRECTORY_LIST[@]}" ]]; then
SELECTED_DIRECTORY="${DIRECTORY_LIST[$DIR_SELECTION - 1]}"
break
else
echo -e "\n\e[31mError: Invalid selection. Please try again.\e[0m"
fi
fi
done
echo -e "\n\e[33mDIRECTORY SELECTED: \e[32m$SELECTED_DIRECTORY\e[0m"
}
# Main script logic
prompt_process_name
prompt_directory
# Prompt for memory limit
while true; do
echo -e "\n\e[33mEnter the memory limit in kilobytes (e.g., 3000000 for 3 GB)\e[0m"
read -p "" MEMORY_LIMIT
if [[ "$MEMORY_LIMIT" =~ ^[0-9]+$ ]]; then
MEMORY_LIMIT_GB=$(echo "scale=2; $MEMORY_LIMIT / (1024 * 1024)" | bc)
echo ""
# printf "\033[33mMemory limit set to: %.2f GB.\033[0m\n" "$MEMORY_LIMIT_GB"
echo -e "\n\e[33mMemory limit set to:\e[32m $MEMORY_LIMIT_GB GB.\e[0m"
break
else
echo -e "\n\e[31mInvalid input. Please enter a numeric value.\e[0m"
fi
done
# Print the selected directory
echo -e "\n\e[33mSELECTED DIRECTORY:\e[32m '$SELECTED_DIRECTORY'.\n\n\e[33mProceeding with the next steps....\e[0m"
# Form the path to save the script
SCRIPT_PATH="$SELECTED_DIRECTORY/memory_check.sh"
# Create the main script (overwrites the existing file)
cat <<EOF > "$SCRIPT_PATH"
#!/bin/bash
# Version: 1.0.12
# Process name to monitor
PROCESS_NAME="$SELECTED_PROCESS"
# Memory limit in kilobytes
MEMORY_LIMIT_MB="$MEMORY_LIMIT"
# Check if the process is running
PROCESS_COUNT=\$(pgrep -c "\$PROCESS_NAME")
if [[ "\$PROCESS_COUNT" -eq 0 ]]; then
echo -e "\$(date): Process \$PROCESS_NAME is not running. Skipping memory check.\n" >> /var/log/memory_check.log
exit 0
fi
# Get the memory usage of the process (in KB)
MEMORY_USAGE=\$(ps --no-headers -o rss -p \$(pgrep "\$PROCESS_NAME") | awk '{sum+=\$1} END {print sum}')
# Check if the limit is exceeded
if [[ "\$MEMORY_USAGE" -gt "\$MEMORY_LIMIT_MB" ]]; then
echo -e "\$(date): Memory limit \$MEMORY_USAGE KB exceeded for \$PROCESS_NAME. Restarting service...\n" >> /var/log/memory_check.log
# Get the service name and restart it
SERVICE_NAME=\$(cat /proc/\$(pidof "\$PROCESS_NAME")/cgroup | sed -n 's|.*/\([^.]*\)\.service|\1|p')
if systemctl restart "\$SERVICE_NAME"; then
echo -e "\$(date): Service \$SERVICE_NAME restarted successfully.\n" >> /var/log/mem_watch.log
else
echo -e "\$(date): Failed to restart \$SERVICE_NAME.\n" >> /var/log/mem_watch.log
fi
fi
EOF
# Make the new script executable
#chmod +x "$SCRIPT_PATH"
# Information about the created script
echo -e "\n\e[33mScript created and saved in\e[32m $SCRIPT_PATH\e[0m"
echo -e "\n\e[33mLogs are stored in\e[32m /var/log/mem_watch.log\e[0m"
echo -e "\e[33mYou can check logs by using\e[32m cat /var/log/mem_watch.log\e[0m\n"