qwizzie
Well-known member
You will need to create three seperate shell scripts : hardware.sh, docker.sh (first post) and monitor.sh (second post)
With regards to the first two :
hardware.sh and docker.sh will run every 24 hours and log their output in log files.
hardware.sh also contains a trigger alert, it will visually warn you when dashd Process Memory Reserve exceeds 85%
These two files will not be reboot resistant, they will stop daily logging after a reboot. You can also manually stop them by issuing
ps -aux | grep yourusername and giving a kill proces command. Or just use dashmate stop and sudo reboot to kill them and then start dashmate again.
These two shell scripts need to be run only once and they will then perform daily log operations on the time your started them.
monitor.sh (second post) will show the content of those log files on screen in a nice colorfull overview and can be used as many times as needed.
Afterwards you can also make a selection to show a specific container by pressing y, or just end the script by pressing n.
./monitor.sh is the command that you will keep using after having set this up. Make sure Putty is running as a full window.
Step 1 : nano hardware.sh
Step 2 : add following code for Ubuntu logging (adjust total_ram_gb=32 under section ' # Calculate the percentage of process memory reserve' if you have lower RAM installed)
Ctrl + X (Save? y enter)
Step 3 : chmod +x hardware.sh
Step 4 : ./hardware.sh
Step 5 : nano docker.sh
Step 6 : add following code for Docker containers logging
Ctrl + X (Save? y enter)
Step 7 : chmod +x docker.sh
Step 8 : ./docker.sh
With regards to the first two :
hardware.sh and docker.sh will run every 24 hours and log their output in log files.
hardware.sh also contains a trigger alert, it will visually warn you when dashd Process Memory Reserve exceeds 85%
These two files will not be reboot resistant, they will stop daily logging after a reboot. You can also manually stop them by issuing
ps -aux | grep yourusername and giving a kill proces command. Or just use dashmate stop and sudo reboot to kill them and then start dashmate again.
These two shell scripts need to be run only once and they will then perform daily log operations on the time your started them.
monitor.sh (second post) will show the content of those log files on screen in a nice colorfull overview and can be used as many times as needed.
Afterwards you can also make a selection to show a specific container by pressing y, or just end the script by pressing n.
./monitor.sh is the command that you will keep using after having set this up. Make sure Putty is running as a full window.
Step 1 : nano hardware.sh
Step 2 : add following code for Ubuntu logging (adjust total_ram_gb=32 under section ' # Calculate the percentage of process memory reserve' if you have lower RAM installed)
Bash:
#!/bin/bash
# Set script to exit immediately if any command fails
set -e
# Function to get CPU usage
get_cpu_usage() {
cpu_info=$(top -bn1 | grep "Cpu(s)" || true)
if [ -n "$cpu_info" ]; then
cpu_usage=$(echo "$cpu_info" | awk '{print $2 + $4}')
echo "CPU Usage: ${cpu_usage}%"
else
echo "Error: Unable to retrieve CPU usage."
fi
}
# Function to get memory usage
get_memory_usage() {
memory_info=$(free -m || true)
if [ -n "$memory_info" ]; then
memory_usage=$(echo "$memory_info" | awk '/Mem/ {print $3}')
memory_usage_gb=$(echo "scale=2; $memory_usage / 1024" | bc)
echo "Memory Usage: ${memory_usage_gb}GB"
else
echo "Error: Unable to retrieve memory usage."
fi
}
# Function to get process memory reserve
get_process_memory_reserve() {
process_memory_reserve=$(ps -opid,vsz,cmd -C dashd | awk 'NR>1 {sum += $2} END {print sum}')
process_memory_reserve_gb=$(echo "scale=2; $process_memory_reserve / (1024 * 1024)" | bc)
echo "Process Memory Reserve: ${process_memory_reserve_gb}GB"
# Calculate the percentage of process memory reserve
total_ram_gb=32 # Total RAM in GB, adjust if necessary in case you have lower RAM
process_memory_reserve_percentage=$(echo "scale=2; $process_memory_reserve_gb / $total_ram_gb * 100" | bc)
echo "Percentage: ${process_memory_reserve_percentage}%"
# Check if dashd Process Memory Reserve exceeds 85%
if (( $(echo "$process_memory_reserve_percentage > 85" | bc -l) )); then
echo -e "\e[1;31mAlert: dashd Process Memory Reserve exceeds 85%!\e[0m"
echo -e "\e[1;31mAlert: dashd Process Memory Reserve exceeds 85%!\e[0m" >> hardware.log
# You can add additional actions here, such as sending an email or triggering a notification.
fi
}
# Function to get disk usage
get_disk_usage() {
disk_info=$(df -h / || true)
if [ -n "$disk_info" ]; then
disk_usage=$(echo "$disk_info" | awk '/\// {print $5}')
echo "Disk Usage: ${disk_usage}"
else
echo "Error: Unable to retrieve disk usage."
fi
}
# Function to log data
log_data() {
echo "$(date) - $(get_cpu_usage) - $(get_memory_usage) - $(get_process_memory_reserve) - $(get_disk_usage)"
}
# Main function
main() {
while true; do
log_data >> hardware.log
sleep 24h
done
}
# Execute the main function in the background
main &
# Notify user that the script is running in the background
echo "Script is running in the background. You can close the terminal."
Ctrl + X (Save? y enter)
Step 3 : chmod +x hardware.sh
Step 4 : ./hardware.sh
Step 5 : nano docker.sh
Step 6 : add following code for Docker containers logging
Bash:
#!/bin/bash
# Set script to exit immediately if any command fails
set -e
# Function to log Docker stats for all containers
log_docker_stats() {
docker_stats=$(docker stats --no-stream | sed '1d')
if [ -n "$docker_stats" ]; then
echo "$docker_stats"
else
echo "Error: Unable to retrieve Docker stats for the containers."
fi
}
# Function to log data
log_data() {
# Prepare data with timestamp and Docker stats
echo "$(date)" >> dockerinfo.log # Log the date on the first line
log_docker_stats >> dockerinfo.log # Append Docker stats on the next line
}
# Main function
main() {
while true; do
log_data # Call log_data function
sleep 24h # Sleep for 24 hours
done
}
# Execute the main function in the background
main &
# Notify user that the script is running in the background
echo "Script is running in the background. You can close the terminal."
Ctrl + X (Save? y enter)
Step 7 : chmod +x docker.sh
Step 8 : ./docker.sh
Last edited: