mirror of
https://github.com/kuhyx/scripts.git
synced 2026-07-04 14:43:08 +02:00
feat: show pc warn
This commit is contained in:
parent
805b5ccdbe
commit
5befc3835f
@ -75,6 +75,12 @@ interval=10
|
||||
color=#FFFFFF
|
||||
|
||||
|
||||
[pc_startup]
|
||||
command=~/.config/i3blocks/pc_startup_status.sh
|
||||
interval=30
|
||||
color=#FFFFFF
|
||||
|
||||
|
||||
[time]
|
||||
command=echo " $(date '+%Y-%m-%d %H:%M')" # for time (Font Awesome icon)
|
||||
interval=1
|
||||
|
||||
68
i3-configuration/i3blocks/pc_startup_status.sh
Executable file
68
i3-configuration/i3blocks/pc_startup_status.sh
Executable file
@ -0,0 +1,68 @@
|
||||
#!/bin/bash
|
||||
# PC Startup Monitor status script for i3blocks
|
||||
# Shows compact startup compliance status in the status bar
|
||||
|
||||
# Function to check if today is a monitored day
|
||||
is_monitored_day() {
|
||||
local day_of_week=$(date +%u)
|
||||
if [[ "$day_of_week" == "1" ]] || [[ "$day_of_week" == "5" ]] || [[ "$day_of_week" == "6" ]] || [[ "$day_of_week" == "7" ]]; then
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to check if current time is in window
|
||||
is_current_time_in_window() {
|
||||
local current_hour=$(date +%H)
|
||||
local current_hour_num=$((10#$current_hour))
|
||||
if [[ $current_hour_num -ge 5 ]] && [[ $current_hour_num -lt 8 ]]; then
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to check if PC was booted in window today
|
||||
was_booted_in_window_today() {
|
||||
local today=$(date +%Y-%m-%d)
|
||||
local uptime_seconds=$(awk '{print int($1)}' /proc/uptime 2>/dev/null || echo "0")
|
||||
local boot_time=$(date -d "@$(($(date +%s) - uptime_seconds))" +"%Y-%m-%d %H:%M:%S")
|
||||
local boot_date=$(echo "$boot_time" | cut -d' ' -f1)
|
||||
|
||||
if [[ "$boot_date" != "$today" ]]; then
|
||||
return 1
|
||||
fi
|
||||
|
||||
local boot_hour=$(echo "$boot_time" | cut -d' ' -f2 | cut -d':' -f1)
|
||||
local boot_hour_num=$((10#$boot_hour))
|
||||
|
||||
if [[ $boot_hour_num -ge 5 ]] && [[ $boot_hour_num -lt 8 ]]; then
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Main logic
|
||||
if ! is_monitored_day; then
|
||||
# Not a monitored day
|
||||
echo "PC:skip"
|
||||
echo
|
||||
echo "#888888" # Gray
|
||||
elif is_current_time_in_window; then
|
||||
# Currently in the window - all good
|
||||
echo "PC:live"
|
||||
echo
|
||||
echo "#00FF00" # Green
|
||||
elif was_booted_in_window_today; then
|
||||
# Was booted in window today - compliant
|
||||
echo "PC:ok"
|
||||
echo
|
||||
echo "#00FF00" # Green
|
||||
else
|
||||
# Was NOT booted in window today - non-compliant
|
||||
echo "PC:warn"
|
||||
echo
|
||||
echo "#FF0000" # Red
|
||||
fi
|
||||
278
scripts/pc_startup_visual_status.sh
Executable file
278
scripts/pc_startup_visual_status.sh
Executable file
@ -0,0 +1,278 @@
|
||||
#!/bin/bash
|
||||
# Visual PC Startup Monitor Status Display
|
||||
# Shows a nice visual representation of the monitoring status and schedule
|
||||
|
||||
# Color codes for visual display
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
PURPLE='\033[0;35m'
|
||||
CYAN='\033[0;36m'
|
||||
WHITE='\033[1;37m'
|
||||
GRAY='\033[0;37m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
# Unicode symbols for visual elements
|
||||
CHECK="✓"
|
||||
CROSS="✗"
|
||||
WARNING="⚠️"
|
||||
CLOCK="🕐"
|
||||
CALENDAR="📅"
|
||||
COMPUTER="💻"
|
||||
BELL="🔔"
|
||||
|
||||
# Function to draw a box around text
|
||||
draw_box() {
|
||||
local text="$1"
|
||||
local width=${#text}
|
||||
local padding=2
|
||||
local total_width=$((width + padding * 2))
|
||||
|
||||
# Top border
|
||||
printf "┌"
|
||||
printf "─%.0s" $(seq 1 $total_width)
|
||||
printf "┐\n"
|
||||
|
||||
# Content with padding
|
||||
printf "│%*s%s%*s│\n" $padding "" "$text" $padding ""
|
||||
|
||||
# Bottom border
|
||||
printf "└"
|
||||
printf "─%.0s" $(seq 1 $total_width)
|
||||
printf "┘\n"
|
||||
}
|
||||
|
||||
# Function to show current day status
|
||||
show_day_status() {
|
||||
local day_of_week=$(date +%u)
|
||||
local day_name=$(date +%A)
|
||||
local today=$(date +%Y-%m-%d)
|
||||
|
||||
printf "${BLUE}${CALENDAR} Day Status${NC}\n"
|
||||
printf "═══════════════\n"
|
||||
|
||||
# Show all days with status
|
||||
local days=("Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday" "Sunday")
|
||||
local monitored=(1 0 0 0 1 1 1) # 1=monitored, 0=not monitored
|
||||
|
||||
for i in {0..6}; do
|
||||
local day_num=$((i + 1))
|
||||
if [[ $day_num -eq 7 ]]; then day_num=0; fi # Sunday is 0 in some contexts
|
||||
|
||||
if [[ ${monitored[$i]} -eq 1 ]]; then
|
||||
if [[ $day_of_week -eq $((i + 1)) ]] || [[ $day_of_week -eq 7 && $i -eq 6 ]]; then
|
||||
printf "${GREEN}${CHECK} ${days[$i]} (TODAY - MONITORED)${NC}\n"
|
||||
else
|
||||
printf "${CYAN}${CHECK} ${days[$i]} (monitored)${NC}\n"
|
||||
fi
|
||||
else
|
||||
if [[ $day_of_week -eq $((i + 1)) ]]; then
|
||||
printf "${GRAY}○ ${days[$i]} (TODAY - not monitored)${NC}\n"
|
||||
else
|
||||
printf "${GRAY}○ ${days[$i]}${NC}\n"
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
printf "\n"
|
||||
}
|
||||
|
||||
# Function to show time window status
|
||||
show_time_status() {
|
||||
local current_hour=$(date +%H)
|
||||
local current_minute=$(date +%M)
|
||||
local current_hour_num=$((10#$current_hour))
|
||||
|
||||
printf "${YELLOW}${CLOCK} Time Window Status${NC}\n"
|
||||
printf "═══════════════════════\n"
|
||||
|
||||
# Show 24-hour timeline with window highlighted
|
||||
printf "Timeline (24-hour format):\n"
|
||||
printf "00 01 02 03 04 "
|
||||
printf "${GREEN}05 06 07${NC} "
|
||||
printf "08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23\n"
|
||||
printf " "
|
||||
printf "${GREEN}▲─────▲${NC}\n"
|
||||
printf " "
|
||||
printf "${GREEN}Expected Window${NC}\n"
|
||||
|
||||
# Current time indicator
|
||||
printf "\nCurrent time: ${WHITE}%02d:%s${NC}\n" $current_hour_num "$current_minute"
|
||||
|
||||
if [[ $current_hour_num -ge 5 && $current_hour_num -lt 8 ]]; then
|
||||
printf "Status: ${GREEN}${CHECK} Within expected window (5AM-8AM)${NC}\n"
|
||||
else
|
||||
printf "Status: ${YELLOW}○ Outside expected window${NC}\n"
|
||||
fi
|
||||
|
||||
printf "\n"
|
||||
}
|
||||
|
||||
# Function to show boot time analysis
|
||||
show_boot_analysis() {
|
||||
printf "${PURPLE}${COMPUTER} Boot Time Analysis${NC}\n"
|
||||
printf "═══════════════════════\n"
|
||||
|
||||
# Get boot time
|
||||
local uptime_seconds=$(awk '{print int($1)}' /proc/uptime 2>/dev/null || echo "0")
|
||||
local boot_time=$(date -d "@$(($(date +%s) - uptime_seconds))" +"%Y-%m-%d %H:%M:%S")
|
||||
local boot_date=$(echo "$boot_time" | cut -d' ' -f1)
|
||||
local boot_time_only=$(echo "$boot_time" | cut -d' ' -f2)
|
||||
local boot_hour=$(echo "$boot_time_only" | cut -d':' -f1)
|
||||
local boot_hour_num=$((10#$boot_hour))
|
||||
local today=$(date +%Y-%m-%d)
|
||||
|
||||
printf "System boot time: ${WHITE}$boot_time${NC}\n"
|
||||
|
||||
if [[ "$boot_date" == "$today" ]]; then
|
||||
printf "Boot date: ${GREEN}${CHECK} Today${NC}\n"
|
||||
|
||||
if [[ $boot_hour_num -ge 5 && $boot_hour_num -lt 8 ]]; then
|
||||
printf "Boot window: ${GREEN}${CHECK} Within expected window (5AM-8AM)${NC}\n"
|
||||
printf "Status: ${GREEN}${CHECK} COMPLIANT${NC}\n"
|
||||
else
|
||||
printf "Boot window: ${RED}${CROSS} Outside expected window${NC}\n"
|
||||
printf "Status: ${RED}${WARNING} NON-COMPLIANT${NC}\n"
|
||||
fi
|
||||
else
|
||||
printf "Boot date: ${YELLOW}○ Not today ($boot_date)${NC}\n"
|
||||
printf "Status: ${YELLOW}○ System was not booted today${NC}\n"
|
||||
fi
|
||||
|
||||
printf "\n"
|
||||
}
|
||||
|
||||
# Function to show monitoring system status
|
||||
show_system_status() {
|
||||
printf "${CYAN}${BELL} Monitoring System${NC}\n"
|
||||
printf "═══════════════════════\n"
|
||||
|
||||
# Check if timer exists and is enabled
|
||||
if systemctl is-enabled pc-startup-monitor.timer &>/dev/null; then
|
||||
printf "Service: ${GREEN}${CHECK} ENABLED${NC}\n"
|
||||
|
||||
if systemctl is-active pc-startup-monitor.timer &>/dev/null; then
|
||||
printf "Timer: ${GREEN}${CHECK} ACTIVE${NC}\n"
|
||||
else
|
||||
printf "Timer: ${RED}${CROSS} INACTIVE${NC}\n"
|
||||
fi
|
||||
|
||||
# Show next check time
|
||||
local next_check=$(systemctl list-timers pc-startup-monitor.timer --no-pager 2>/dev/null | grep pc-startup-monitor | awk '{print $1, $2, $3}' || echo "Not scheduled")
|
||||
printf "Next check: ${WHITE}$next_check${NC}\n"
|
||||
|
||||
else
|
||||
printf "Service: ${RED}${CROSS} NOT ENABLED${NC}\n"
|
||||
printf "Timer: ${RED}${CROSS} NOT ACTIVE${NC}\n"
|
||||
fi
|
||||
|
||||
printf "\n"
|
||||
}
|
||||
|
||||
# Function to show overall compliance status
|
||||
show_compliance_overview() {
|
||||
local day_of_week=$(date +%u)
|
||||
local current_hour=$(date +%H)
|
||||
local current_hour_num=$((10#$current_hour))
|
||||
|
||||
# Check if today is monitored
|
||||
local is_monitored=false
|
||||
if [[ "$day_of_week" == "1" ]] || [[ "$day_of_week" == "5" ]] || [[ "$day_of_week" == "6" ]] || [[ "$day_of_week" == "7" ]]; then
|
||||
is_monitored=true
|
||||
fi
|
||||
|
||||
printf "${WHITE}"
|
||||
draw_box "COMPLIANCE OVERVIEW"
|
||||
printf "${NC}\n"
|
||||
|
||||
if [[ "$is_monitored" == true ]]; then
|
||||
printf "Today: ${GREEN}${CHECK} Monitored day${NC}\n"
|
||||
|
||||
# Check current compliance
|
||||
if [[ $current_hour_num -ge 5 && $current_hour_num -lt 8 ]]; then
|
||||
printf "Current status: ${GREEN}${CHECK} PC is on during expected window${NC}\n"
|
||||
printf "Action needed: ${GREEN}None - currently compliant${NC}\n"
|
||||
else
|
||||
# Check if booted in window
|
||||
local uptime_seconds=$(awk '{print int($1)}' /proc/uptime 2>/dev/null || echo "0")
|
||||
local boot_time=$(date -d "@$(($(date +%s) - uptime_seconds))" +"%Y-%m-%d %H:%M:%S")
|
||||
local boot_date=$(echo "$boot_time" | cut -d' ' -f1)
|
||||
local boot_hour=$(echo "$boot_time" | cut -d' ' -f2 | cut -d':' -f1)
|
||||
local boot_hour_num=$((10#$boot_hour))
|
||||
local today=$(date +%Y-%m-%d)
|
||||
|
||||
if [[ "$boot_date" == "$today" ]] && [[ $boot_hour_num -ge 5 && $boot_hour_num -lt 8 ]]; then
|
||||
printf "Current status: ${GREEN}${CHECK} PC was booted in expected window${NC}\n"
|
||||
printf "Action needed: ${GREEN}None - compliant${NC}\n"
|
||||
else
|
||||
printf "Current status: ${RED}${WARNING} PC was NOT booted in expected window${NC}\n"
|
||||
printf "Action needed: ${YELLOW}Warning will be shown at 8:30 AM${NC}\n"
|
||||
fi
|
||||
fi
|
||||
else
|
||||
printf "Today: ${GRAY}○ Not a monitored day${NC}\n"
|
||||
printf "Current status: ${GRAY}No monitoring required${NC}\n"
|
||||
printf "Action needed: ${GRAY}None${NC}\n"
|
||||
fi
|
||||
|
||||
printf "\n"
|
||||
}
|
||||
|
||||
# Function to show recent activity
|
||||
show_recent_activity() {
|
||||
printf "${GRAY}📋 Recent Activity${NC}\n"
|
||||
printf "════════════════\n"
|
||||
|
||||
# Show last 5 log entries
|
||||
local logs=$(journalctl -t pc-startup-monitor --no-pager -n 5 --output=short 2>/dev/null || echo "No logs found")
|
||||
|
||||
if [[ "$logs" == "No logs found" ]]; then
|
||||
printf "${GRAY}No recent monitoring activity${NC}\n"
|
||||
else
|
||||
echo "$logs" | while IFS= read -r line; do
|
||||
if [[ $line == *"WARNING"* ]]; then
|
||||
printf "${RED}$line${NC}\n"
|
||||
elif [[ $line == *"compliance OK"* ]]; then
|
||||
printf "${GREEN}$line${NC}\n"
|
||||
else
|
||||
printf "${GRAY}$line${NC}\n"
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
||||
printf "\n"
|
||||
}
|
||||
|
||||
# Main display function
|
||||
main() {
|
||||
clear
|
||||
|
||||
# Header
|
||||
printf "${BLUE}"
|
||||
draw_box "PC STARTUP MONITOR - VISUAL STATUS"
|
||||
printf "${NC}\n\n"
|
||||
|
||||
printf "${WHITE}Current Date/Time: $(date)${NC}\n"
|
||||
printf "${WHITE}System Uptime: $(uptime -p)${NC}\n\n"
|
||||
|
||||
# Show all status sections
|
||||
show_day_status
|
||||
show_time_status
|
||||
show_boot_analysis
|
||||
show_system_status
|
||||
show_compliance_overview
|
||||
show_recent_activity
|
||||
|
||||
# Footer with commands
|
||||
printf "${BLUE}═══════════════════════════════════════════════════════════════${NC}\n"
|
||||
printf "${WHITE}Commands:${NC}\n"
|
||||
printf " ${CYAN}sudo pc-startup-monitor-manager.sh status${NC} - Show system status\n"
|
||||
printf " ${CYAN}sudo pc-startup-monitor-manager.sh test${NC} - Test monitor now\n"
|
||||
printf " ${CYAN}sudo pc-startup-monitor-manager.sh logs${NC} - View detailed logs\n"
|
||||
printf " ${CYAN}$0${NC} - Show this visual status\n"
|
||||
printf "${BLUE}═══════════════════════════════════════════════════════════════${NC}\n"
|
||||
}
|
||||
|
||||
# Run main function
|
||||
main "$@"
|
||||
534
scripts/setup_pc_startup_monitor.sh
Executable file
534
scripts/setup_pc_startup_monitor.sh
Executable file
@ -0,0 +1,534 @@
|
||||
#!/bin/bash
|
||||
# Script to monitor PC startup times on specific days
|
||||
# Checks if PC was turned on between 5AM-8AM on Monday, Friday, Saturday, Sunday
|
||||
# Handles sudo privileges automatically
|
||||
|
||||
set -e # Exit on any error
|
||||
|
||||
echo "PC Startup Time Monitor for Arch Linux"
|
||||
echo "======================================"
|
||||
echo "Current Date: $(date)"
|
||||
echo "User: ${SUDO_USER:-$USER}"
|
||||
|
||||
# Function to check and request sudo privileges
|
||||
check_sudo() {
|
||||
if [[ $EUID -ne 0 ]]; then
|
||||
echo "This script requires sudo privileges to access system logs and create services."
|
||||
echo "Requesting sudo access..."
|
||||
exec sudo "$0" "$@"
|
||||
fi
|
||||
}
|
||||
|
||||
# Get the actual user (even when running with sudo)
|
||||
if [[ -n "$SUDO_USER" ]]; then
|
||||
ACTUAL_USER="$SUDO_USER"
|
||||
USER_HOME="/home/$SUDO_USER"
|
||||
else
|
||||
ACTUAL_USER="$USER"
|
||||
USER_HOME="$HOME"
|
||||
fi
|
||||
|
||||
echo "Target user: $ACTUAL_USER"
|
||||
echo "User home: $USER_HOME"
|
||||
|
||||
# Function to check if today is a monitored day
|
||||
is_monitored_day() {
|
||||
local day_of_week=$(date +%u) # 1=Monday, 7=Sunday
|
||||
|
||||
# Check if today is Monday (1), Friday (5), Saturday (6), or Sunday (7)
|
||||
if [[ "$day_of_week" == "1" ]] || [[ "$day_of_week" == "5" ]] || [[ "$day_of_week" == "6" ]] || [[ "$day_of_week" == "7" ]]; then
|
||||
return 0 # Yes, it's a monitored day
|
||||
else
|
||||
return 1 # No, it's not a monitored day
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to check if current time is between 5AM and 8AM
|
||||
is_current_time_in_window() {
|
||||
local current_hour=$(date +%H)
|
||||
local current_hour_num=$((10#$current_hour)) # Convert to decimal to avoid octal issues
|
||||
|
||||
if [[ $current_hour_num -ge 5 ]] && [[ $current_hour_num -lt 8 ]]; then
|
||||
return 0 # Yes, current time is in the 5AM-8AM window
|
||||
else
|
||||
return 1 # No, current time is outside the window
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to check if PC was booted between 5AM-8AM today
|
||||
was_booted_in_window_today() {
|
||||
local today=$(date +%Y-%m-%d)
|
||||
local boot_time=""
|
||||
|
||||
# Get the last boot time using multiple methods for reliability
|
||||
if command -v uptime &>/dev/null; then
|
||||
# Method 1: Calculate boot time from uptime
|
||||
local uptime_seconds=$(awk '{print int($1)}' /proc/uptime 2>/dev/null || echo "0")
|
||||
if [[ $uptime_seconds -gt 0 ]]; then
|
||||
boot_time=$(date -d "@$(($(date +%s) - uptime_seconds))" +"%Y-%m-%d %H:%M:%S")
|
||||
fi
|
||||
fi
|
||||
|
||||
# Method 2: Use systemd if available (fallback)
|
||||
if [[ -z "$boot_time" ]] && command -v systemctl &>/dev/null; then
|
||||
boot_time=$(systemd-analyze | grep "Startup finished" | sed -n 's/.*finished in .* = \(.*\)$/\1/p' 2>/dev/null || echo "")
|
||||
if [[ -n "$boot_time" ]]; then
|
||||
# This gives us relative time, need to calculate absolute time
|
||||
local current_time=$(date +%s)
|
||||
local uptime_sec=$(awk '{print int($1)}' /proc/uptime 2>/dev/null || echo "0")
|
||||
boot_time=$(date -d "@$((current_time - uptime_sec))" +"%Y-%m-%d %H:%M:%S")
|
||||
fi
|
||||
fi
|
||||
|
||||
# Method 3: Use who -b (fallback)
|
||||
if [[ -z "$boot_time" ]] && command -v who &>/dev/null; then
|
||||
boot_time=$(who -b | awk '{print $3, $4}' 2>/dev/null || echo "")
|
||||
if [[ -n "$boot_time" ]]; then
|
||||
boot_time="$today $boot_time"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Method 4: Use /proc/uptime as final fallback
|
||||
if [[ -z "$boot_time" ]]; then
|
||||
local uptime_seconds=$(awk '{print int($1)}' /proc/uptime 2>/dev/null || echo "0")
|
||||
boot_time=$(date -d "@$(($(date +%s) - uptime_seconds))" +"%Y-%m-%d %H:%M:%S")
|
||||
fi
|
||||
|
||||
echo "Boot time detected: $boot_time"
|
||||
|
||||
# Check if boot time is from today
|
||||
local boot_date=$(echo "$boot_time" | cut -d' ' -f1)
|
||||
if [[ "$boot_date" != "$today" ]]; then
|
||||
echo "PC was not booted today (boot date: $boot_date, today: $today)"
|
||||
return 1 # Not booted today
|
||||
fi
|
||||
|
||||
# Extract hour from boot time
|
||||
local boot_hour=$(echo "$boot_time" | cut -d' ' -f2 | cut -d':' -f1)
|
||||
local boot_hour_num=$((10#$boot_hour)) # Convert to decimal
|
||||
|
||||
echo "Boot hour: $boot_hour_num"
|
||||
|
||||
# Check if boot time was between 5AM (5) and 8AM (7, since we want before 8AM)
|
||||
if [[ $boot_hour_num -ge 5 ]] && [[ $boot_hour_num -lt 8 ]]; then
|
||||
echo "PC was booted in the expected window (5AM-8AM)"
|
||||
return 0 # Yes, booted in window
|
||||
else
|
||||
echo "PC was NOT booted in the expected window (5AM-8AM)"
|
||||
return 1 # No, not booted in window
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to show notification/warning
|
||||
show_startup_warning() {
|
||||
local day_name=$(date +%A)
|
||||
local current_time=$(date +"%H:%M")
|
||||
local today=$(date +%Y-%m-%d)
|
||||
|
||||
echo ""
|
||||
echo "⚠️ PC STARTUP TIME WARNING"
|
||||
echo "=========================="
|
||||
echo "Date: $today ($day_name)"
|
||||
echo "Current time: $current_time"
|
||||
echo ""
|
||||
echo "This PC was expected to be turned on between 5:00 AM and 8:00 AM today,"
|
||||
echo "but it was not turned on during that time window."
|
||||
echo ""
|
||||
echo "Expected: Monday, Friday, Saturday, Sunday between 5:00-8:00 AM"
|
||||
echo "Actual: PC was turned on outside the expected window"
|
||||
echo ""
|
||||
|
||||
# Log the warning
|
||||
logger -t pc-startup-monitor "WARNING: PC was not turned on during expected window (5AM-8AM) on $day_name $today"
|
||||
|
||||
# Try to show desktop notification if possible
|
||||
if command -v notify-send &>/dev/null && [[ -n "$DISPLAY" ]]; then
|
||||
if [[ $EUID -eq 0 ]]; then
|
||||
# Running as root, send notification as user
|
||||
sudo -u "$ACTUAL_USER" DISPLAY="$DISPLAY" notify-send "PC Startup Warning" "PC was not turned on between 5AM-8AM as expected on $day_name" --urgency=normal --expire-time=10000 2>/dev/null || true
|
||||
else
|
||||
notify-send "PC Startup Warning" "PC was not turned on between 5AM-8AM as expected on $day_name" --urgency=normal --expire-time=10000 2>/dev/null || true
|
||||
fi
|
||||
fi
|
||||
|
||||
echo "This warning has been logged to the system journal."
|
||||
echo "You can view startup logs with: journalctl -t pc-startup-monitor"
|
||||
echo ""
|
||||
}
|
||||
|
||||
# Function to create the monitoring service
|
||||
create_monitoring_service() {
|
||||
echo ""
|
||||
echo "1. Creating PC Startup Monitor Service..."
|
||||
echo "======================================="
|
||||
|
||||
local service_file="/etc/systemd/system/pc-startup-monitor.service"
|
||||
|
||||
cat > "$service_file" << 'EOF'
|
||||
[Unit]
|
||||
Description=PC Startup Time Monitor
|
||||
After=multi-user.target
|
||||
Wants=network.target
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
ExecStart=/usr/local/bin/pc-startup-check.sh
|
||||
StandardOutput=journal
|
||||
StandardError=journal
|
||||
RemainAfterExit=true
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
EOF
|
||||
|
||||
echo "✓ Created monitoring service: $service_file"
|
||||
}
|
||||
|
||||
# Function to create the monitoring timer
|
||||
create_monitoring_timer() {
|
||||
echo ""
|
||||
echo "2. Creating PC Startup Monitor Timer..."
|
||||
echo "====================================="
|
||||
|
||||
local timer_file="/etc/systemd/system/pc-startup-monitor.timer"
|
||||
|
||||
cat > "$timer_file" << 'EOF'
|
||||
[Unit]
|
||||
Description=Timer for PC startup monitoring
|
||||
Requires=pc-startup-monitor.service
|
||||
|
||||
[Timer]
|
||||
OnCalendar=*-*-* 08:30:00
|
||||
Persistent=false
|
||||
AccuracySec=1m
|
||||
|
||||
[Install]
|
||||
WantedBy=timers.target
|
||||
EOF
|
||||
|
||||
echo "✓ Created monitoring timer: $timer_file"
|
||||
}
|
||||
|
||||
# Function to create the main monitoring script
|
||||
create_monitoring_script() {
|
||||
echo ""
|
||||
echo "3. Creating PC Startup Monitor Script..."
|
||||
echo "======================================"
|
||||
|
||||
local script_file="/usr/local/bin/pc-startup-check.sh"
|
||||
|
||||
cat > "$script_file" << 'EOF'
|
||||
#!/bin/bash
|
||||
# PC Startup Time Monitor Check Script
|
||||
# Monitors if PC was turned on during expected hours on specific days
|
||||
|
||||
# Function to check if today is a monitored day
|
||||
is_monitored_day() {
|
||||
local day_of_week=$(date +%u) # 1=Monday, 7=Sunday
|
||||
|
||||
# Check if today is Monday (1), Friday (5), Saturday (6), or Sunday (7)
|
||||
if [[ "$day_of_week" == "1" ]] || [[ "$day_of_week" == "5" ]] || [[ "$day_of_week" == "6" ]] || [[ "$day_of_week" == "7" ]]; then
|
||||
return 0 # Yes, it's a monitored day
|
||||
else
|
||||
return 1 # No, it's not a monitored day
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to check if current time is between 5AM and 8AM
|
||||
is_current_time_in_window() {
|
||||
local current_hour=$(date +%H)
|
||||
local current_hour_num=$((10#$current_hour))
|
||||
|
||||
if [[ $current_hour_num -ge 5 ]] && [[ $current_hour_num -lt 8 ]]; then
|
||||
return 0 # Yes, current time is in the 5AM-8AM window
|
||||
else
|
||||
return 1 # No, current time is outside the window
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to check if PC was booted between 5AM-8AM today
|
||||
was_booted_in_window_today() {
|
||||
local today=$(date +%Y-%m-%d)
|
||||
|
||||
# Calculate boot time from uptime
|
||||
local uptime_seconds=$(awk '{print int($1)}' /proc/uptime 2>/dev/null || echo "0")
|
||||
local boot_time=$(date -d "@$(($(date +%s) - uptime_seconds))" +"%Y-%m-%d %H:%M:%S")
|
||||
|
||||
# Check if boot time is from today
|
||||
local boot_date=$(echo "$boot_time" | cut -d' ' -f1)
|
||||
if [[ "$boot_date" != "$today" ]]; then
|
||||
return 1 # Not booted today
|
||||
fi
|
||||
|
||||
# Extract hour from boot time
|
||||
local boot_hour=$(echo "$boot_time" | cut -d' ' -f2 | cut -d':' -f1)
|
||||
local boot_hour_num=$((10#$boot_hour))
|
||||
|
||||
# Check if boot time was between 5AM and 8AM
|
||||
if [[ $boot_hour_num -ge 5 ]] && [[ $boot_hour_num -lt 8 ]]; then
|
||||
return 0 # Yes, booted in window
|
||||
else
|
||||
return 1 # No, not booted in window
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to show notification/warning
|
||||
show_startup_warning() {
|
||||
local day_name=$(date +%A)
|
||||
local current_time=$(date +"%H:%M")
|
||||
local today=$(date +%Y-%m-%d)
|
||||
|
||||
echo "⚠️ PC STARTUP TIME WARNING"
|
||||
echo "Date: $today ($day_name)"
|
||||
echo "Current time: $current_time"
|
||||
echo "This PC was expected to be turned on between 5:00 AM and 8:00 AM today, but was not."
|
||||
|
||||
# Log the warning
|
||||
logger -t pc-startup-monitor "WARNING: PC was not turned on during expected window (5AM-8AM) on $day_name $today"
|
||||
}
|
||||
|
||||
# Main logic
|
||||
echo "$(date): PC Startup Monitor Check"
|
||||
logger -t pc-startup-monitor "Running startup time check at $(date)"
|
||||
|
||||
# Step 0: Check if today is a monitored day
|
||||
if ! is_monitored_day; then
|
||||
day_name=$(date +%A)
|
||||
echo "$(date): Today is $day_name - not a monitored day. Skipping check."
|
||||
logger -t pc-startup-monitor "Skipping check - today ($day_name) is not a monitored day"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Step 1 & 2: Check if current time is between 5AM and 8AM
|
||||
if is_current_time_in_window; then
|
||||
echo "$(date): Current time is within 5AM-8AM window. No action needed."
|
||||
logger -t pc-startup-monitor "Current time is within monitored window (5AM-8AM) - no action needed"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Step 4: Check if PC was turned on between 5AM-8AM today
|
||||
if was_booted_in_window_today; then
|
||||
echo "$(date): PC was booted in expected window (5AM-8AM). All good."
|
||||
logger -t pc-startup-monitor "PC was booted in expected window (5AM-8AM) - compliance OK"
|
||||
else
|
||||
echo "$(date): PC was NOT booted in expected window (5AM-8AM). Showing warning."
|
||||
show_startup_warning
|
||||
fi
|
||||
EOF
|
||||
|
||||
chmod +x "$script_file"
|
||||
echo "✓ Created monitoring script: $script_file"
|
||||
}
|
||||
|
||||
# Function to create management script
|
||||
create_management_script() {
|
||||
echo ""
|
||||
echo "4. Creating Management Script..."
|
||||
echo "=============================="
|
||||
|
||||
local script_file="/usr/local/bin/pc-startup-monitor-manager.sh"
|
||||
|
||||
cat > "$script_file" << 'EOF'
|
||||
#!/bin/bash
|
||||
# PC Startup Monitor Manager
|
||||
# Provides easy management of the PC startup monitoring feature
|
||||
|
||||
TIMER_NAME="pc-startup-monitor.timer"
|
||||
SERVICE_NAME="pc-startup-monitor.service"
|
||||
|
||||
show_status() {
|
||||
echo "PC Startup Monitor Status"
|
||||
echo "========================"
|
||||
|
||||
if systemctl is-enabled "$TIMER_NAME" &>/dev/null; then
|
||||
echo "Status: ENABLED"
|
||||
if systemctl is-active "$TIMER_NAME" &>/dev/null; then
|
||||
echo "Timer: ACTIVE"
|
||||
else
|
||||
echo "Timer: INACTIVE"
|
||||
fi
|
||||
else
|
||||
echo "Status: NOT ENABLED"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "Next check scheduled:"
|
||||
systemctl list-timers "$TIMER_NAME" --no-pager 2>/dev/null | grep "$TIMER_NAME" || echo "Timer not active"
|
||||
|
||||
echo ""
|
||||
echo "Recent logs:"
|
||||
journalctl -t pc-startup-monitor --no-pager -n 10 2>/dev/null || echo "No recent logs"
|
||||
}
|
||||
|
||||
test_now() {
|
||||
echo "Running startup monitor check now..."
|
||||
/usr/local/bin/pc-startup-check.sh
|
||||
}
|
||||
|
||||
case "$1" in
|
||||
"status")
|
||||
show_status
|
||||
;;
|
||||
"logs")
|
||||
echo "PC Startup Monitor Logs"
|
||||
echo "======================"
|
||||
journalctl -t pc-startup-monitor --no-pager -n 30
|
||||
;;
|
||||
"test")
|
||||
test_now
|
||||
;;
|
||||
*)
|
||||
echo "PC Startup Monitor Manager"
|
||||
echo "Usage: $0 {status|logs|test}"
|
||||
echo ""
|
||||
echo "Commands:"
|
||||
echo " status - Show current status and next check time"
|
||||
echo " logs - Show recent monitoring logs"
|
||||
echo " test - Run a startup check now (for testing)"
|
||||
echo ""
|
||||
show_status
|
||||
;;
|
||||
esac
|
||||
EOF
|
||||
|
||||
chmod +x "$script_file"
|
||||
echo "✓ Created management script: $script_file"
|
||||
}
|
||||
|
||||
# Function to enable the services
|
||||
enable_services() {
|
||||
echo ""
|
||||
echo "5. Enabling PC Startup Monitor..."
|
||||
echo "==============================="
|
||||
|
||||
# Reload systemd daemon
|
||||
systemctl daemon-reload
|
||||
echo "✓ Reloaded systemd daemon"
|
||||
|
||||
# Enable and start the timer
|
||||
systemctl enable pc-startup-monitor.timer
|
||||
echo "✓ Enabled pc-startup-monitor timer"
|
||||
|
||||
systemctl start pc-startup-monitor.timer
|
||||
echo "✓ Started pc-startup-monitor timer"
|
||||
}
|
||||
|
||||
# Function to test the setup
|
||||
test_setup() {
|
||||
echo ""
|
||||
echo "6. Testing Setup..."
|
||||
echo "=================="
|
||||
|
||||
echo "Service files:"
|
||||
if [[ -f "/etc/systemd/system/pc-startup-monitor.service" ]]; then
|
||||
echo "✓ Service file exists"
|
||||
else
|
||||
echo "✗ Service file missing"
|
||||
fi
|
||||
|
||||
if [[ -f "/etc/systemd/system/pc-startup-monitor.timer" ]]; then
|
||||
echo "✓ Timer file exists"
|
||||
else
|
||||
echo "✗ Timer file missing"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "Timer status:"
|
||||
if systemctl is-enabled pc-startup-monitor.timer &>/dev/null; then
|
||||
echo "✓ Timer is enabled"
|
||||
else
|
||||
echo "✗ Timer is not enabled"
|
||||
fi
|
||||
|
||||
if systemctl is-active pc-startup-monitor.timer &>/dev/null; then
|
||||
echo "✓ Timer is active"
|
||||
else
|
||||
echo "✗ Timer is not active"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "Testing current logic:"
|
||||
/usr/local/bin/pc-startup-check.sh
|
||||
}
|
||||
|
||||
# Function to show final instructions
|
||||
show_instructions() {
|
||||
echo ""
|
||||
echo "=========================================="
|
||||
echo "PC Startup Monitor Setup Complete"
|
||||
echo "=========================================="
|
||||
echo "Summary:"
|
||||
echo "✓ Monitoring service created (/etc/systemd/system/pc-startup-monitor.service)"
|
||||
echo "✓ Monitoring timer created (/etc/systemd/system/pc-startup-monitor.timer)"
|
||||
echo "✓ Monitor script created (/usr/local/bin/pc-startup-check.sh)"
|
||||
echo "✓ Management script created (/usr/local/bin/pc-startup-monitor-manager.sh)"
|
||||
echo "✓ Timer enabled and started"
|
||||
echo ""
|
||||
echo "How it works:"
|
||||
echo "• Monitors PC startup times on Monday, Friday, Saturday, Sunday"
|
||||
echo "• Expects PC to be turned on between 5:00 AM - 8:00 AM"
|
||||
echo "• Checks daily at 8:30 AM if PC was turned on in expected window"
|
||||
echo "• Shows warning if PC was not turned on during expected time"
|
||||
echo ""
|
||||
echo "Management commands:"
|
||||
echo " sudo pc-startup-monitor-manager.sh status - Check status"
|
||||
echo " sudo pc-startup-monitor-manager.sh logs - View monitor logs"
|
||||
echo " sudo pc-startup-monitor-manager.sh test - Test monitor now"
|
||||
echo ""
|
||||
echo "Next check: Tomorrow at 8:30 AM (if it's a monitored day)"
|
||||
echo ""
|
||||
}
|
||||
|
||||
# Function to prompt for confirmation
|
||||
confirm_setup() {
|
||||
echo ""
|
||||
echo "PC Startup Monitor Setup"
|
||||
echo "======================="
|
||||
echo "This will set up monitoring for PC startup times."
|
||||
echo ""
|
||||
echo "Monitoring schedule:"
|
||||
echo "- Days: Monday, Friday, Saturday, Sunday"
|
||||
echo "- Expected startup time: 5:00 AM - 8:00 AM"
|
||||
echo "- Check time: 8:30 AM daily"
|
||||
echo "- Action: Show warning if PC wasn't started in expected window"
|
||||
echo ""
|
||||
read -p "Do you want to proceed? (y/N): " confirm
|
||||
|
||||
case "$confirm" in
|
||||
[yY]|[yY][eE][sS])
|
||||
echo "Proceeding with setup..."
|
||||
return 0
|
||||
;;
|
||||
*)
|
||||
echo "Setup cancelled."
|
||||
exit 0
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
# Main execution flow
|
||||
main() {
|
||||
# Check for sudo privileges
|
||||
check_sudo "$@"
|
||||
|
||||
# Confirm setup
|
||||
confirm_setup
|
||||
|
||||
# Create all components
|
||||
create_monitoring_service
|
||||
create_monitoring_timer
|
||||
create_monitoring_script
|
||||
create_management_script
|
||||
|
||||
# Enable services
|
||||
enable_services
|
||||
|
||||
# Test setup
|
||||
test_setup
|
||||
|
||||
# Show instructions
|
||||
show_instructions
|
||||
}
|
||||
|
||||
# Run main function
|
||||
main "$@"
|
||||
Loading…
Reference in New Issue
Block a user