First Commit

This commit is contained in:
KacperLa 2025-11-24 13:17:00 -05:00
parent 51ca26f5ce
commit 7d1ba02c42
8 changed files with 1106 additions and 61 deletions

204
logs.sh Executable file
View file

@ -0,0 +1,204 @@
#!/usr/bin/env bash
#
# Log viewer for Porkbun Dynamic DNS systemd service
#
# This script provides convenient access to service logs via journalctl
set -euo pipefail
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
SERVICE_NAME="porkbun-ddns.service"
TIMER_NAME="porkbun-ddns.timer"
# Check if systemctl is available
if ! command -v systemctl &> /dev/null; then
echo -e "${RED}Error: systemctl not found${NC}"
echo "This script requires systemd."
exit 1
fi
# Check if journalctl is available
if ! command -v journalctl &> /dev/null; then
echo -e "${RED}Error: journalctl not found${NC}"
echo "This script requires journalctl."
exit 1
fi
# Function to show help
show_help() {
cat << EOF
${GREEN}Porkbun DDNS Log Viewer${NC}
Usage: $0 [OPTION]
Options:
-f, --follow Follow logs in real-time (like tail -f)
-n, --lines NUMBER Show last N lines (default: 50)
-t, --today Show logs from today only
-h, --hour Show logs from the last hour
-b, --boot Show logs since last boot
-a, --all Show all logs (no limit)
-s, --status Show service and timer status
-e, --errors Show only error messages
-v, --verbose Show verbose output (all log levels)
--help Show this help message
Examples:
$0 Show last 50 log lines
$0 -f Follow logs in real-time
$0 -n 100 Show last 100 lines
$0 -t Show today's logs
$0 -h Show last hour's logs
$0 -e Show only errors
$0 -s Show service status
EOF
}
# Function to show status
show_status() {
echo -e "${BLUE}=== Timer Status ===${NC}"
systemctl status "$TIMER_NAME" --no-pager -l || true
echo ""
echo -e "${BLUE}=== Service Status ===${NC}"
systemctl status "$SERVICE_NAME" --no-pager -l || true
echo ""
echo -e "${BLUE}=== Next Scheduled Runs ===${NC}"
systemctl list-timers "$TIMER_NAME" --no-pager || true
echo ""
echo -e "${BLUE}=== Recent Runs ===${NC}"
journalctl -u "$SERVICE_NAME" -n 5 --no-pager -o short-precise
}
# Parse command line arguments
FOLLOW=false
LINES=50
SINCE=""
PRIORITY=""
SHOW_ALL=false
SHOW_STATUS=false
if [ $# -eq 0 ]; then
# Default: show last 50 lines
:
else
while [[ $# -gt 0 ]]; do
case $1 in
-f|--follow)
FOLLOW=true
shift
;;
-n|--lines)
LINES="$2"
shift 2
;;
-t|--today)
SINCE="today"
shift
;;
-h|--hour)
SINCE="1 hour ago"
shift
;;
-b|--boot)
SINCE="boot"
shift
;;
-a|--all)
SHOW_ALL=true
shift
;;
-s|--status)
SHOW_STATUS=true
shift
;;
-e|--errors)
PRIORITY="err"
shift
;;
-v|--verbose)
PRIORITY=""
shift
;;
--help)
show_help
exit 0
;;
*)
echo -e "${RED}Error: Unknown option: $1${NC}"
echo "Use --help for usage information"
exit 1
;;
esac
done
fi
# Check if service exists
if ! systemctl list-unit-files | grep -q "$SERVICE_NAME"; then
echo -e "${YELLOW}Warning: $SERVICE_NAME is not installed${NC}"
echo "Install the service first with: ./install.sh"
exit 1
fi
# Show status if requested
if [ "$SHOW_STATUS" = true ]; then
show_status
exit 0
fi
# Build journalctl command
CMD="journalctl -u $SERVICE_NAME"
# Add follow flag
if [ "$FOLLOW" = true ]; then
CMD="$CMD -f"
fi
# Add line limit
if [ "$SHOW_ALL" = false ] && [ "$FOLLOW" = false ]; then
CMD="$CMD -n $LINES"
fi
# Add time filter
if [ -n "$SINCE" ]; then
if [ "$SINCE" = "boot" ]; then
CMD="$CMD -b"
else
CMD="$CMD --since \"$SINCE\""
fi
fi
# Add priority filter
if [ -n "$PRIORITY" ]; then
CMD="$CMD -p $PRIORITY"
fi
# Add no-pager for non-follow mode
if [ "$FOLLOW" = false ]; then
CMD="$CMD --no-pager"
fi
# Show what we're doing
if [ "$FOLLOW" = true ]; then
echo -e "${GREEN}Following logs for $SERVICE_NAME...${NC}"
echo "Press Ctrl+C to stop"
echo ""
elif [ -n "$SINCE" ]; then
echo -e "${GREEN}Showing logs since $SINCE...${NC}"
echo ""
elif [ "$SHOW_ALL" = true ]; then
echo -e "${GREEN}Showing all logs for $SERVICE_NAME...${NC}"
echo ""
else
echo -e "${GREEN}Showing last $LINES log lines for $SERVICE_NAME...${NC}"
echo ""
fi
# Execute the command
eval "$CMD"