Update 2025-04-13_16:21:50
This commit is contained in:
69
lxc_list_sed.sh
Executable file
69
lxc_list_sed.sh
Executable file
@ -0,0 +1,69 @@
|
||||
#!/bin/bash
|
||||
# =============================================================================
|
||||
# Script Name: lxc_list_sed.sh
|
||||
# Version: 1.10
|
||||
# Description: This script lists all LXC containers, checks their statuses, and
|
||||
# updates the SENDMAILTO field in /etc/logcheck/logcheck.conf for
|
||||
# running containers. It includes error handling and logging.
|
||||
# =============================================================================
|
||||
|
||||
# 01 Required commands
|
||||
REQUIRED_CMDS=("lxc-ls" "lxc-info" "lxc-attach" "lxc-start")
|
||||
|
||||
# 02 Check if required commands are available
|
||||
for CMD in "${REQUIRED_CMDS[@]}"; do
|
||||
if ! command -v $CMD &> /dev/null; then
|
||||
echo "Command $CMD is not installed. Please install it and try again."
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
|
||||
# 03 Function to check if a container is running
|
||||
is_container_running() {
|
||||
local CONTAINER=$1
|
||||
if lxc-info -n "$CONTAINER" | grep -q 'RUNNING'; then
|
||||
echo "$CONTAINER is running."
|
||||
return 0 # Container is running
|
||||
else
|
||||
echo "$CONTAINER is not running or does not exist."
|
||||
return 1 # Container is not running
|
||||
fi
|
||||
}
|
||||
|
||||
# 04 List all running Linux containers
|
||||
list_running_containers() {
|
||||
local CONTAINERS=($(lxc-ls -f | awk '$2 == "RUNNING" {print $1}'))
|
||||
echo ${CONTAINERS[@]}
|
||||
}
|
||||
|
||||
# 05 Update SENDMAILTO in /etc/logcheck/logcheck.conf for running LXC containers
|
||||
update_sendmailto() {
|
||||
local NEW_EMAIL="logcheck+srvlogz@bubuit.net"
|
||||
local RUNNING_CONTAINERS=($@) # Get the list of running containers as arguments
|
||||
|
||||
for CONTAINER in "${RUNNING_CONTAINERS[@]}"; do
|
||||
echo "Attempting to update SENDMAILTO in $CONTAINER"
|
||||
if lxc-attach -n "$CONTAINER" -- bash -c "[ -f /etc/logcheck/logcheck.conf ]"; then
|
||||
if lxc-attach -n "$CONTAINER" -- bash -c "sed -i 's/^SENDMAILTO.*/SENDMAILTO=\"$NEW_EMAIL\"/' /etc/logcheck/logcheck.conf"; then
|
||||
echo "Successfully updated SENDMAILTO in $CONTAINER"
|
||||
else
|
||||
echo "Failed to update SENDMAILTO in $CONTAINER"
|
||||
fi
|
||||
else
|
||||
echo "/etc/logcheck/logcheck.conf does not exist in $CONTAINER"
|
||||
fi
|
||||
sleep 1 # Introduce a sleep to avoid running too fast
|
||||
done
|
||||
}
|
||||
|
||||
# 06 Main script execution
|
||||
RUNNING_CONTAINERS=$(list_running_containers)
|
||||
echo -e "\nRunning Containers: ${RUNNING_CONTAINERS[@]}"
|
||||
echo -ne "\n"
|
||||
|
||||
if [ -n "$RUNNING_CONTAINERS" ]; then
|
||||
update_sendmailto ${RUNNING_CONTAINERS[@]}
|
||||
else
|
||||
echo "No running containers found."
|
||||
fi
|
||||
|
Reference in New Issue
Block a user