-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrlgl.sh
More file actions
executable file
·308 lines (258 loc) · 9.96 KB
/
Copy pathrlgl.sh
File metadata and controls
executable file
·308 lines (258 loc) · 9.96 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/bin/bash
# Colors
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # reset
# Function to show usage
show_usage() {
echo "Usage: $0 [OPTIONS]"
echo "Options:"
echo " -c, --config FILE Specify config file (default: config.yaml)"
echo " -h, --help Show this help message"
echo ""
echo "Example: $0 -c /path/to/custom-config.yaml"
}
# Parse command line arguments
CONFIG_FILE_ARG=""
while [[ $# -gt 0 ]]; do
case $1 in
-c|--config)
CONFIG_FILE_ARG="$2"
shift 2
;;
-h|--help)
show_usage
exit 0
;;
*)
echo "Unknown option: $1"
show_usage
exit 1
;;
esac
done
# Get the script directory
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Set config file path
if [[ -n "$CONFIG_FILE_ARG" ]]; then
# Use absolute path if provided, or relative to current directory
if [[ "$CONFIG_FILE_ARG" = /* ]]; then
CONFIG_FILE="$CONFIG_FILE_ARG"
else
CONFIG_FILE="$(pwd)/$CONFIG_FILE_ARG"
fi
else
# Use default config file in script directory
CONFIG_FILE="$SCRIPT_DIR/config.yaml"
fi
# Check if config file exists
if [ ! -f "$CONFIG_FILE" ]; then
echo "Error: Config file not found at $CONFIG_FILE"
exit 1
fi
echo "Using config file: $CONFIG_FILE"
sleep 2 # Brief pause to show the config file being used
start_time=$(date +%s%3N) # milliseconds since epoch
config_reload_count=0
# Variables to track previous state to avoid unnecessary redraws
previous_complete_display=""
# Variables for polling intervals
declare -ga section_check_poll_intervals=()
declare -ga check_last_run_times=()
declare -ga check_last_results=() # Cache results between polling intervals
# Get initial config file modification time
get_config_mtime() {
if [[ -f "$CONFIG_FILE" ]]; then
stat -c %Y "$CONFIG_FILE" 2>/dev/null || stat -f %m "$CONFIG_FILE" 2>/dev/null
else
echo "0"
fi
}
config_mtime=$(get_config_mtime)
# Cleanup function
cleanup() {
clear
tput cnorm
exit
}
trap cleanup INT # cleanup on Ctrl+C
tput civis # hide cursor
# Parse configuration file
parse_config() {
# Initialize arrays for sections
declare -ga section_titles=()
declare -ga section_check_names=()
declare -ga section_check_commands=()
section_check_poll_intervals=()
check_last_run_times=()
# Parse global poll_interval if exists
global_poll_interval=$(grep "^[[:space:]]*poll_interval:" "$1" | head -1 | sed 's/.*poll_interval:[[:space:]]*\([0-9.]*\).*/\1/')
if [[ -z "$global_poll_interval" || ! "$global_poll_interval" =~ ^[0-9]+\.?[0-9]*$ ]]; then
global_poll_interval=0.5 # default fallback
fi
# Get section starting lines (lines with "- title:")
section_lines=$(grep -n "^[[:space:]]*-[[:space:]]*title:" "$1" | cut -d':' -f1)
for section_line in $section_lines; do
# Extract title
title=$(sed -n "${section_line}s/.*title:[[:space:]]*\"*\([^\"]*\)\"*.*/\1/p" "$1")
section_titles+=("$title")
# Find checks for this section
section_names=()
section_commands=()
section_intervals=()
# Find the next section start or end of file
next_section_line=$(echo "$section_lines" | awk -v current="$section_line" '$1 > current {print $1; exit}')
if [[ -z "$next_section_line" ]]; then
next_section_line=$(wc -l < "$1")
((next_section_line++))
fi
# Find check entries between this section and the next
check_lines=$(sed -n "${section_line},${next_section_line}p" "$1" | grep -n "^[[:space:]]*-[[:space:]]*name:" | cut -d':' -f1)
for relative_line in $check_lines; do
absolute_line=$((section_line + relative_line - 1))
name=$(sed -n "${absolute_line}s/.*name:[[:space:]]*\"*\([^\"]*\)\"*.*/\1/p" "$1")
# Look for command line in the next few lines
cmd_line=$((absolute_line + 1))
command=$(sed -n "${cmd_line}s/.*command:[[:space:]]*\"*\([^\"]*\)\"*.*/\1/p" "$1")
# Look for poll_interval in the next few lines after name
poll_interval=""
for ((offset=1; offset<=3; offset++)); do
check_line=$((absolute_line + offset))
if [[ $check_line -lt $next_section_line ]]; then
interval_line=$(sed -n "${check_line}p" "$1")
if [[ "$interval_line" =~ poll_interval:[[:space:]]*([0-9.]+) ]]; then
poll_interval="${BASH_REMATCH[1]}"
break
fi
fi
done
# Use global interval if no specific interval found
if [[ -z "$poll_interval" ]]; then
poll_interval="$global_poll_interval"
fi
if [[ -n "$name" && -n "$command" ]]; then
section_names+=("$name")
section_commands+=("$command")
section_intervals+=("$poll_interval")
check_last_run_times+=(0) # Initialize last run time
check_last_results+=(1) # Initialize with failure state
fi
done
# Store arrays as strings (bash limitation workaround)
section_check_names+=("$(printf "%s\n" "${section_names[@]}")")
section_check_commands+=("$(printf "%s\n" "${section_commands[@]}")")
section_check_poll_intervals+=("$(printf "%s\n" "${section_intervals[@]}")")
done
}
# Function to reload configuration
reload_config() {
parse_config "$CONFIG_FILE"
((config_reload_count++))
config_mtime=$(get_config_mtime)
# Reset previous state to force redraw
previous_complete_display=""
}
# Check if config file has been modified
check_config_changed() {
local current_mtime
current_mtime=$(get_config_mtime)
if [[ "$current_mtime" != "$config_mtime" ]]; then
reload_config
fi
}
# Load the configuration
parse_config "$CONFIG_FILE"
# Function to update complete display only if changed
update_complete_display() {
local new_complete_display="$1"
if [[ "$new_complete_display" != "$previous_complete_display" ]]; then
clear
echo -ne "$new_complete_display"
previous_complete_display="$new_complete_display"
fi
}
# Counter for config check frequency (check every 10 iterations = ~5 seconds)
config_check_counter=0
check_index=0 # Global check index counter
while true; do
# Check for config file changes every 10 iterations (every ~5 seconds)
if [[ $((config_check_counter % 10)) -eq 0 ]]; then
check_config_changed
fi
((config_check_counter++))
# Build complete display
display_content=""
check_index=0 # Reset check index for this iteration
# Process each section
for i in "${!section_titles[@]}"; do
# Add section title
title="${section_titles[i]}"
title_length=${#title}
box_width=$((title_length + 4)) # 2 spaces on each side
# Create top border
display_content+="┌"
for ((j=0; j<box_width; j++)); do
display_content+="─"
done
display_content+="┐\n"
# Create title line
display_content+="│ ${YELLOW}${title}${NC} │\n"
# Create bottom border
display_content+="└"
for ((j=0; j<box_width; j++)); do
display_content+="─"
done
display_content+="┘\n\n"
# Get checks for this section
IFS=$'\n' read -d '' -r -a names <<< "${section_check_names[i]}" || true
IFS=$'\n' read -d '' -r -a commands <<< "${section_check_commands[i]}" || true
IFS=$'\n' read -d '' -r -a intervals <<< "${section_check_poll_intervals[i]}" || true
# Process checks for this section
for j in "${!names[@]}"; do
if [[ -n "${names[j]}" && -n "${commands[j]}" ]]; then
current_time=$(date +%s%3N) # milliseconds
# Convert poll interval to milliseconds using bash arithmetic
poll_interval_ms=$(printf "%.0f" $(echo "${intervals[j]} * 1000" | awk '{print $1 * 1000}'))
# Initialize cached result if not exists
if [[ -z "${check_last_results[check_index]:-}" ]]; then
check_last_results[check_index]=1 # Default to failure
fi
# Check if enough time has passed since last run
time_since_last=$((current_time - ${check_last_run_times[check_index]}))
if [[ $time_since_last -ge $poll_interval_ms ]]; then
# Execute the command and cache the result
(cd "$SCRIPT_DIR" && bash -c "${commands[j]}")
result=$?
check_last_run_times[check_index]=$current_time
check_last_results[check_index]=$result
else
# Use cached result
result=${check_last_results[check_index]}
fi
if [[ $result -eq 0 ]]; then
display_content+="${GREEN}■ ${names[j]}${NC} (${intervals[j]}s)\n"
else
display_content+="${RED}■ ${names[j]}${NC} (${intervals[j]}s)\n"
fi
((check_index++))
fi
done
# Add spacing between sections
if [[ $i -lt $((${#section_titles[@]} - 1)) ]]; then
display_content+="\n"
fi
done
# Calculate elapsed time
now=$(date +%s%3N)
elapsed=$((now - start_time))
sec=$((elapsed / 1000))
min=$((sec / 60))
sec=$((sec % 60))
# Add timer and global interval info to the complete display
display_content+="\n\n${YELLOW}$(printf "%02d:%02d" "$min" "$sec")${NC} | Global: ${global_poll_interval}s\n"
# Update complete display at once
update_complete_display "${display_content}"
# Sleep before next update (use a shorter base interval for responsiveness)
sleep 0.1
done