little cpu fixes
This commit is contained in:
parent
d59aa0527a
commit
37d466103d
@ -162,6 +162,21 @@ update_installation() {
|
|||||||
generate_service >/lib/systemd/system/iptag.service
|
generate_service >/lib/systemd/system/iptag.service
|
||||||
msg_ok "Updated service file"
|
msg_ok "Updated service file"
|
||||||
|
|
||||||
|
msg_info "Creating manual run command"
|
||||||
|
cat <<'EOF' >/usr/local/bin/iptag-run
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
CONFIG_FILE="/opt/iptag/iptag.conf"
|
||||||
|
SCRIPT_FILE="/opt/iptag/iptag"
|
||||||
|
if [[ ! -f "$SCRIPT_FILE" ]]; then
|
||||||
|
echo "❌ Main script not found: $SCRIPT_FILE"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
export FORCE_SINGLE_RUN=true
|
||||||
|
exec "$SCRIPT_FILE"
|
||||||
|
EOF
|
||||||
|
chmod +x /usr/local/bin/iptag-run
|
||||||
|
msg_ok "Created iptag-run executable - You can execute this manually by entering “iptag-run” in the Proxmox host, so the script is executed by hand."
|
||||||
|
|
||||||
msg_info "Restarting service"
|
msg_info "Restarting service"
|
||||||
systemctl daemon-reload &>/dev/null
|
systemctl daemon-reload &>/dev/null
|
||||||
systemctl enable -q --now iptag.service &>/dev/null
|
systemctl enable -q --now iptag.service &>/dev/null
|
||||||
@ -195,7 +210,7 @@ FORCE_UPDATE_INTERVAL=7200
|
|||||||
|
|
||||||
# Performance optimizations
|
# Performance optimizations
|
||||||
VM_IP_CACHE_TTL=300
|
VM_IP_CACHE_TTL=300
|
||||||
MAX_PARALLEL_VM_CHECKS=2
|
MAX_PARALLEL_VM_CHECKS=1
|
||||||
|
|
||||||
# LXC performance optimizations
|
# LXC performance optimizations
|
||||||
LXC_IP_CACHE_TTL=300
|
LXC_IP_CACHE_TTL=300
|
||||||
@ -206,6 +221,7 @@ LXC_BATCH_SIZE=3
|
|||||||
LXC_STATUS_CACHE_TTL=300
|
LXC_STATUS_CACHE_TTL=300
|
||||||
LXC_AGGRESSIVE_CACHING=true
|
LXC_AGGRESSIVE_CACHING=true
|
||||||
LXC_SKIP_SLOW_METHODS=true
|
LXC_SKIP_SLOW_METHODS=true
|
||||||
|
LXC_ALLOW_FORCED_COMMANDS=false
|
||||||
|
|
||||||
# Debug settings (set to true to enable debugging)
|
# Debug settings (set to true to enable debugging)
|
||||||
DEBUG=false
|
DEBUG=false
|
||||||
@ -576,7 +592,9 @@ update_tags() {
|
|||||||
|
|
||||||
if [[ "$type" == "lxc" ]]; then
|
if [[ "$type" == "lxc" ]]; then
|
||||||
current_ips_full=$(get_lxc_ips "${vmid}")
|
current_ips_full=$(get_lxc_ips "${vmid}")
|
||||||
local current_tags_raw=$(pct config "${vmid}" 2>/dev/null | grep tags | awk '{print $2}')
|
while IFS= read -r line; do
|
||||||
|
[[ "$line" == tags:* ]] && current_tags_raw="${line#tags: }" && break
|
||||||
|
done < <(pct config "$vmid" 2>/dev/null)
|
||||||
else
|
else
|
||||||
current_ips_full=$(get_vm_ips "${vmid}")
|
current_ips_full=$(get_vm_ips "${vmid}")
|
||||||
local vm_config="/etc/pve/qemu-server/${vmid}.conf"
|
local vm_config="/etc/pve/qemu-server/${vmid}.conf"
|
||||||
@ -789,7 +807,10 @@ check_status_changed() {
|
|||||||
check() {
|
check() {
|
||||||
local current_time changes_detected=false
|
local current_time changes_detected=false
|
||||||
current_time=$(date +%s)
|
current_time=$(date +%s)
|
||||||
|
|
||||||
|
local update_lxc=false
|
||||||
|
local update_vm=false
|
||||||
|
|
||||||
# Periodic cache cleanup (every 10 minutes)
|
# Periodic cache cleanup (every 10 minutes)
|
||||||
local time_since_last_cleanup=$((current_time - ${last_cleanup_time:-0}))
|
local time_since_last_cleanup=$((current_time - ${last_cleanup_time:-0}))
|
||||||
if [[ $time_since_last_cleanup -ge 600 ]]; then
|
if [[ $time_since_last_cleanup -ge 600 ]]; then
|
||||||
@ -801,60 +822,56 @@ check() {
|
|||||||
# Check LXC status
|
# Check LXC status
|
||||||
local time_since_last_lxc_check=$((current_time - last_lxc_status_check_time))
|
local time_since_last_lxc_check=$((current_time - last_lxc_status_check_time))
|
||||||
if [[ "${LXC_STATUS_CHECK_INTERVAL:-60}" -gt 0 ]] && \
|
if [[ "${LXC_STATUS_CHECK_INTERVAL:-60}" -gt 0 ]] && \
|
||||||
[[ "${time_since_last_lxc_check}" -ge "${LXC_STATUS_CHECK_INTERVAL:-60}" ]]; then
|
[[ "$time_since_last_lxc_check" -ge "${LXC_STATUS_CHECK_INTERVAL:-60}" ]]; then
|
||||||
last_lxc_status_check_time=${current_time}
|
last_lxc_status_check_time=$current_time
|
||||||
if check_status_changed "lxc"; then
|
if check_status_changed "lxc"; then
|
||||||
changes_detected=true
|
update_lxc=true
|
||||||
log_warning "LXC status changes detected, updating tags"
|
log_warning "LXC status changes detected"
|
||||||
update_all_tags "lxc"
|
|
||||||
last_update_lxc_time=${current_time}
|
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Check VM status
|
# Check VM status
|
||||||
local time_since_last_vm_check=$((current_time - last_vm_status_check_time))
|
local time_since_last_vm_check=$((current_time - last_vm_status_check_time))
|
||||||
if [[ "${VM_STATUS_CHECK_INTERVAL:-60}" -gt 0 ]] && \
|
if [[ "${VM_STATUS_CHECK_INTERVAL:-60}" -gt 0 ]] && \
|
||||||
[[ "${time_since_last_vm_check}" -ge "${VM_STATUS_CHECK_INTERVAL:-60}" ]]; then
|
[[ "$time_since_last_vm_check" -ge "${VM_STATUS_CHECK_INTERVAL:-60}" ]]; then
|
||||||
last_vm_status_check_time=${current_time}
|
last_vm_status_check_time=$current_time
|
||||||
if check_status_changed "vm"; then
|
if check_status_changed "vm"; then
|
||||||
changes_detected=true
|
update_vm=true
|
||||||
log_warning "VM status changes detected, updating tags"
|
log_warning "VM status changes detected"
|
||||||
update_all_tags "vm"
|
|
||||||
last_update_vm_time=${current_time}
|
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Check network interface changes
|
# Check network interface changes
|
||||||
local time_since_last_fw_check=$((current_time - last_fw_net_interface_check_time))
|
local time_since_last_fw_check=$((current_time - last_fw_net_interface_check_time))
|
||||||
if [[ "${FW_NET_INTERFACE_CHECK_INTERVAL:-60}" -gt 0 ]] && \
|
if [[ "${FW_NET_INTERFACE_CHECK_INTERVAL:-60}" -gt 0 ]] && \
|
||||||
[[ "${time_since_last_fw_check}" -ge "${FW_NET_INTERFACE_CHECK_INTERVAL:-60}" ]]; then
|
[[ "$time_since_last_fw_check" -ge "${FW_NET_INTERFACE_CHECK_INTERVAL:-60}" ]]; then
|
||||||
last_fw_net_interface_check_time=${current_time}
|
last_fw_net_interface_check_time=$current_time
|
||||||
if check_status_changed "fw"; then
|
if check_status_changed "fw"; then
|
||||||
changes_detected=true
|
update_lxc=true
|
||||||
log_warning "Network interface changes detected, updating all tags"
|
update_vm=true
|
||||||
update_all_tags "lxc"
|
log_warning "Network interface changes detected"
|
||||||
update_all_tags "vm"
|
|
||||||
last_update_lxc_time=${current_time}
|
|
||||||
last_update_vm_time=${current_time}
|
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Force update if needed
|
# Force update if interval exceeded
|
||||||
for type in "lxc" "vm"; do
|
for type in "lxc" "vm"; do
|
||||||
local last_update_var="last_update_${type}_time"
|
local last_update_var="last_update_${type}_time"
|
||||||
local time_since_last_update=$((current_time - ${!last_update_var}))
|
local time_since_last_update=$((current_time - ${!last_update_var}))
|
||||||
if [[ ${time_since_last_update} -ge ${FORCE_UPDATE_INTERVAL:-1800} ]]; then
|
if [[ $time_since_last_update -ge ${FORCE_UPDATE_INTERVAL:-1800} ]]; then
|
||||||
changes_detected=true
|
|
||||||
local minutes=$((${FORCE_UPDATE_INTERVAL:-1800} / 60))
|
|
||||||
if [[ "$type" == "lxc" ]]; then
|
if [[ "$type" == "lxc" ]]; then
|
||||||
log_info "Scheduled LXC update (every ${minutes} minutes)"
|
update_lxc=true
|
||||||
|
log_info "Scheduled LXC update (every $((FORCE_UPDATE_INTERVAL / 60)) minutes)"
|
||||||
else
|
else
|
||||||
log_info "Scheduled VM update (every ${minutes} minutes)"
|
update_vm=true
|
||||||
|
log_info "Scheduled VM update (every $((FORCE_UPDATE_INTERVAL / 60)) minutes)"
|
||||||
fi
|
fi
|
||||||
update_all_tags "$type"
|
|
||||||
eval "${last_update_var}=${current_time}"
|
eval "${last_update_var}=${current_time}"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
# Final execution
|
||||||
|
$update_lxc && update_all_tags "lxc"
|
||||||
|
$update_vm && update_all_tags "vm"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Initialize time variables
|
# Initialize time variables
|
||||||
@ -872,13 +889,19 @@ main() {
|
|||||||
echo -e "${BLUE}ℹ${NC} Tag format: ${WHITE}${TAG_FORMAT:-$DEFAULT_TAG_FORMAT}${NC}"
|
echo -e "${BLUE}ℹ${NC} Tag format: ${WHITE}${TAG_FORMAT:-$DEFAULT_TAG_FORMAT}${NC}"
|
||||||
echo -e "${BLUE}ℹ${NC} Allowed CIDRs: ${WHITE}${CIDR_LIST[*]}${NC}"
|
echo -e "${BLUE}ℹ${NC} Allowed CIDRs: ${WHITE}${CIDR_LIST[*]}${NC}"
|
||||||
echo -e "${PURPLE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}\n"
|
echo -e "${PURPLE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}\n"
|
||||||
|
|
||||||
|
if [[ "$FORCE_SINGLE_RUN" == "true" ]]; then
|
||||||
|
check
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
while true; do
|
while true; do
|
||||||
check
|
check
|
||||||
sleep "${LOOP_INTERVAL:-300}"
|
sleep "${LOOP_INTERVAL:-300}"
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# Cache cleanup function
|
# Cache cleanup function
|
||||||
cleanup_vm_cache() {
|
cleanup_vm_cache() {
|
||||||
local cache_dir="/tmp"
|
local cache_dir="/tmp"
|
||||||
@ -1001,7 +1024,7 @@ process_vms_parallel() {
|
|||||||
# Parallel LXC processing function
|
# Parallel LXC processing function
|
||||||
process_lxc_parallel() {
|
process_lxc_parallel() {
|
||||||
local lxc_list=("$@")
|
local lxc_list=("$@")
|
||||||
local max_parallel=${MAX_PARALLEL_LXC_CHECKS:-7}
|
local max_parallel=${MAX_PARALLEL_LXC_CHECKS:-2}
|
||||||
local batch_size=${LXC_BATCH_SIZE:-20}
|
local batch_size=${LXC_BATCH_SIZE:-20}
|
||||||
local job_count=0
|
local job_count=0
|
||||||
local pids=()
|
local pids=()
|
||||||
@ -1177,7 +1200,7 @@ get_lxc_ips() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# Fallback: always do lxc-attach/pct exec with timeout if nothing found
|
# Fallback: always do lxc-attach/pct exec with timeout if nothing found
|
||||||
if [[ -z "$ips" ]]; then
|
if [[ -z "$ips" && "${LXC_ALLOW_FORCED_COMMANDS:-true}" == "true" ]]; then
|
||||||
debug_log "lxc $vmid: trying fallback lxc-attach (forced)"
|
debug_log "lxc $vmid: trying fallback lxc-attach (forced)"
|
||||||
local attach_ip=""
|
local attach_ip=""
|
||||||
attach_ip=$(timeout 7s lxc-attach -n "$vmid" -- ip -4 addr show 2>/dev/null | grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}' | grep -v '127.0.0.1' | head -1)
|
attach_ip=$(timeout 7s lxc-attach -n "$vmid" -- ip -4 addr show 2>/dev/null | grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}' | grep -v '127.0.0.1' | head -1)
|
||||||
@ -1192,7 +1215,7 @@ get_lxc_ips() {
|
|||||||
method_used="lxc_attach_forced"
|
method_used="lxc_attach_forced"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
if [[ -z "$ips" ]]; then
|
if [[ -z "$ips" && "${LXC_ALLOW_FORCED_COMMANDS:-true}" == "true" ]]; then
|
||||||
debug_log "lxc $vmid: trying fallback pct exec (forced)"
|
debug_log "lxc $vmid: trying fallback pct exec (forced)"
|
||||||
local pct_ip=""
|
local pct_ip=""
|
||||||
pct_ip=$(timeout 7s pct exec "$vmid" -- ip -4 addr show 2>/dev/null | grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}' | grep -v '127.0.0.1' | head -1)
|
pct_ip=$(timeout 7s pct exec "$vmid" -- ip -4 addr show 2>/dev/null | grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}' | grep -v '127.0.0.1' | head -1)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user