Added auto-suspend, added wol proxy for ollama
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
#!/bin/bash
|
||||
|
||||
IDLE_TIMEOUT_MINUTES=20
|
||||
|
||||
# Check 1: Any logged-in users?
|
||||
if who | grep -qv "tty"; then
|
||||
echo "Active user sessions found. Skipping suspend."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Check 2: CPU idle percentage
|
||||
CPU_IDLE=$(mpstat 1 1 | tail -n 1 | awk '/:/ {print $12}' | sed 's/,/./g')
|
||||
CPU_THRESHOLD=90
|
||||
|
||||
if (( $(echo "$CPU_IDLE < $CPU_THRESHOLD" | bc -l) )); then
|
||||
echo "CPU too active. Skipping suspend."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if ! command -v nvidia-smi &> /dev/null; then
|
||||
echo "nvidia-smi not found, skipping GPU check"
|
||||
else
|
||||
# Check 3: Is GPU idle?
|
||||
GPU_UTIL=$(nvidia-smi --query-gpu=utilization.gpu --format=csv,noheader,nounits | head -n 1)
|
||||
GPU_THRESHOLD=10
|
||||
|
||||
if (( GPU_UTIL > GPU_THRESHOLD )); then
|
||||
echo "GPU is busy (utilization=${GPU_UTIL}%). Skipping suspend."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Check 4: Is OLLAMA active?
|
||||
OLLAMA_ACTIVE=$(nvidia-smi --query-compute-apps=name,used_memory --format=csv,noheader,nounits | grep ollama | wc -l)
|
||||
if (( OLLAMA_ACTIVE > 0 )); then
|
||||
echo "OLLMAMA is active. Skipping suspend."
|
||||
exit 0
|
||||
fi
|
||||
fi
|
||||
|
||||
echo "System idle. Suspending..."
|
||||
shutdown now
|
||||
@@ -38,11 +38,13 @@ fw_allowed_ports:
|
||||
- { rule: "allow", port: "16443", proto: "tcp", from: "10.19.4.0/24" } # Kubernetes API (local network)
|
||||
- { rule: "allow", port: "139", proto: "tcp", from: "10.19.4.0/24" } # Samba (local network only)
|
||||
- { rule: "allow", port: "445", proto: "tcp", from: "10.19.4.0/24" } # Samba (local network only)
|
||||
- { rule: "allow", port: "11434", proto: "tcp", from: "10.19.4.0/24" } # Ollama (local network only)
|
||||
- { rule: "allow", port: "443", proto: "tcp", from: "10.5.5.2/32" } # HTTPS (Contabo VPN)
|
||||
- { rule: "allow", port: "53", proto: "udp", from: "10.19.4.0/24" } # DNS (local network)
|
||||
- { rule: "allow", port: "53", proto: "udp", from: "10.5.5.2/32" } # DNS (Contabo VPN)
|
||||
|
||||
ufw_outgoing_traffic:
|
||||
- 9 # For Wake-on-LAN
|
||||
- 22 # SSH
|
||||
- 53 # DNS
|
||||
- 80 # HTTP
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
- name: Setup Auto Suspend
|
||||
hosts: auto_suspend
|
||||
|
||||
tasks:
|
||||
- name: Setup Auto Suspend
|
||||
ansible.builtin.include_tasks:
|
||||
file: tasks/auto_suspend.yml
|
||||
@@ -0,0 +1,129 @@
|
||||
- name: Install dependencies
|
||||
become: true
|
||||
ansible.builtin.package:
|
||||
name: "{{ item }}"
|
||||
state: present
|
||||
loop:
|
||||
- sysstat
|
||||
- bc
|
||||
|
||||
- name: Enable sysstat service
|
||||
become: true
|
||||
ansible.builtin.systemd:
|
||||
name: sysstat
|
||||
enabled: true
|
||||
state: started
|
||||
|
||||
- name: Read existing GRUB_CMDLINE_LINUX
|
||||
ansible.builtin.shell: set -o pipefail && grep '^GRUB_CMDLINE_LINUX=' /etc/default/grub | cut -d= -f2- | tr -d '"'
|
||||
register: grub_line
|
||||
changed_when: false
|
||||
failed_when: grub_line.stdout == ""
|
||||
|
||||
- name: Set fact for existing_cmdline
|
||||
ansible.builtin.set_fact:
|
||||
existing_cmdline: "{{ grub_line.stdout }}"
|
||||
|
||||
- name: Set fedora grub file locations
|
||||
ansible.builtin.set_fact:
|
||||
grub_config_path_bios: /boot/grub2/grub.cfg
|
||||
when: ansible_distribution == "Fedora" and ansible_os_family == "RedHat"
|
||||
|
||||
- name: Show current GRUB_CMDLINE_LINUX
|
||||
ansible.builtin.debug:
|
||||
msg: "Current GRUB_CMDLINE_LINUX: {{ existing_cmdline }}"
|
||||
|
||||
- name: Updated GRUB_CMDLINE_LINUX
|
||||
ansible.builtin.debug:
|
||||
msg: "Current GRUB_CMDLINE_LINUX: {{ existing_cmdline | regex_replace(' *mem_sleep_default=\\S+', '') }} mem_sleep_default=deep"
|
||||
|
||||
- name: Ensure mem_sleep_default=deep is set in GRUB_CMDLINE_LINUX
|
||||
become: true
|
||||
vars:
|
||||
grub_cmdline_linux_line: >-
|
||||
GRUB_CMDLINE_LINUX="{{ existing_cmdline | regex_replace(' *mem_sleep_default=\\S+', '') }} mem_sleep_default=deep"
|
||||
ansible.builtin.lineinfile:
|
||||
path: /etc/default/grub
|
||||
regexp: '^GRUB_CMDLINE_LINUX='
|
||||
line: "{{ grub_cmdline_linux_line }}"
|
||||
|
||||
- name: Generate grub config for BIOS
|
||||
become: true
|
||||
changed_when: false
|
||||
ansible.builtin.command: grub2-mkconfig -o {{ grub_config_path_bios }}
|
||||
|
||||
- name: Create idle check and suspend script
|
||||
become: true
|
||||
ansible.builtin.copy:
|
||||
dest: /usr/local/bin/auto_suspend_script.sh
|
||||
mode: '0755'
|
||||
owner: root
|
||||
group: root
|
||||
content: "{{ lookup('file', 'scripts/auto_suspend.sh') }}"
|
||||
|
||||
- name: Create systemd service for auto suspend
|
||||
become: true
|
||||
ansible.builtin.copy:
|
||||
dest: /etc/systemd/system/auto-suspend.service
|
||||
content: |
|
||||
[Unit]
|
||||
Description=Check system idleness and suspend if idle
|
||||
After=multi-user.target
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
ExecStart=/usr/local/bin/auto_suspend_script.sh
|
||||
owner: root
|
||||
group: root
|
||||
mode: '0644'
|
||||
|
||||
- name: Create sleep.conf.d folder if it doesn't exist
|
||||
become: true
|
||||
ansible.builtin.file:
|
||||
path: /etc/systemd/sleep.conf.d
|
||||
state: directory
|
||||
owner: root
|
||||
group: root
|
||||
mode: '0755'
|
||||
|
||||
- name: Create sleep.conf.d for deep sleep
|
||||
become: true
|
||||
ansible.builtin.copy:
|
||||
dest: /etc/systemd/sleep.conf.d/mem-deep.conf
|
||||
content: |
|
||||
[Sleep]
|
||||
MemorySleepMode=deep
|
||||
owner: root
|
||||
group: root
|
||||
mode: '0644'
|
||||
|
||||
- name: Create systemd timer for auto suspend
|
||||
become: true
|
||||
ansible.builtin.copy:
|
||||
dest: /etc/systemd/system/auto-suspend.timer
|
||||
content: |
|
||||
[Unit]
|
||||
Description=Run idle suspend check every 10 minutes
|
||||
|
||||
[Timer]
|
||||
OnBootSec=10min
|
||||
OnUnitActiveSec=10min
|
||||
Unit=auto-suspend.service
|
||||
|
||||
[Install]
|
||||
WantedBy=timers.target
|
||||
owner: root
|
||||
group: root
|
||||
mode: '0644'
|
||||
|
||||
- name: Reload systemd daemon
|
||||
become: true
|
||||
ansible.builtin.systemd:
|
||||
daemon_reload: true
|
||||
|
||||
- name: Enable and start auto-suspend timer
|
||||
become: true
|
||||
ansible.builtin.systemd:
|
||||
name: auto-suspend.timer
|
||||
enabled: true
|
||||
state: started
|
||||
Reference in New Issue
Block a user