-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpapercut_check_spool_and_clear.sh
More file actions
67 lines (56 loc) · 2.1 KB
/
papercut_check_spool_and_clear.sh
File metadata and controls
67 lines (56 loc) · 2.1 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
#!/bin/bash
# Script to check and clear PaperCut and CUPS spool space
# This script checks the disk usage of the PaperCut and CUPS spool directories
# and clears the spool space if the usage exceeds a specified threshold.
# Ensure the script is run with sudo privileges
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root. Please use sudo to execute it."
exit 1
fi
# Define the spool directories for PaperCut and CUPS
PAPERCUT_SPOOL_DIR="/var/spool/papercut/"
CUPS_SPOOL_DIR="/var/spool/cups"
# Define the threshold for low disk space (in percentage)
THRESHOLD=90
# Define the log file
LOG_FILE="/var/log/spool_space_check.log"
# Ensure the log file exists and is writable
touch "$LOG_FILE" || { echo "Failed to create log file: $LOG_FILE"; exit 1; }
# Function to log messages
log_message() {
local message
message=$1
echo "$(date '+%Y-%m-%d %H:%M:%S') - $message" | tee -a "$LOG_FILE"
}
# Function to check and clear spool space
check_and_clear_spool() {
local dir
dir=$1
local usage
usage=$(df "$dir" | awk 'NR==2 {print $5}' | sed 's/%//')
log_message "Checking disk usage for $dir..."
if [ "$usage" -ge "$THRESHOLD" ]; then
log_message "Disk usage for $dir is at $usage%, which is above the threshold of $THRESHOLD%."
log_message "Clearing spool space in $dir..."
if rm -rf "${dir:?}"/*; then
log_message "Spool space cleared for $dir."
else
log_message "Failed to clear spool space for $dir."
fi
else
log_message "Disk usage for $dir is at $usage%, which is below the threshold of $THRESHOLD%. No action needed."
fi
}
# Check and clear PaperCut spool space
if [ -d "$PAPERCUT_SPOOL_DIR" ]; then
check_and_clear_spool "$PAPERCUT_SPOOL_DIR"
else
log_message "PaperCut spool directory $PAPERCUT_SPOOL_DIR does not exist. Skipping..."
fi
# Check and clear CUPS spool space
if [ -d "$CUPS_SPOOL_DIR" ]; then
check_and_clear_spool "$CUPS_SPOOL_DIR"
else
log_message "CUPS spool directory $CUPS_SPOOL_DIR does not exist. Skipping..."
fi
log_message "Spool space check and cleanup completed."