Skip to content

Commit

Permalink
earlyoom: Add vlab-earlyoom-static and update notify script
Browse files Browse the repository at this point in the history
  • Loading branch information
taoky committed Oct 8, 2024
1 parent d01f888 commit 0bf569b
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 4 deletions.
11 changes: 11 additions & 0 deletions vlab-earlyoom-static/DEBIAN/control
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Package: vlab-earlyoom-static
Version: 1.1-5
Section: base
Priority: optional
Architecture: amd64
Recommends: zenity
Provides: earlyoom
Conflicts: earlyoom, vlab-earlyoom
Replaces: earlyoom, vlab-earlyoom
Maintainer: USTC Vlab <[email protected]>
Description: Vlab earlyoom configurations for interactive responsiveness, with static earlyoom binary
4 changes: 4 additions & 0 deletions vlab-earlyoom-static/DEBIAN/postinst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh

echo "Enabling and starting vlab-earlyoom.service..."
systemctl enable --now vlab-earlyoom.service || true
19 changes: 19 additions & 0 deletions vlab-earlyoom-static/DEBIAN/preinst
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh
set -e

PACKAGE=vlab-earlyoom

case "$1" in
install)
systemctl unmask earlyoom.service || true
;;
upgrade|abort-upgrade)
systemctl unmask earlyoom.service || true
;;
*)
echo "preinst called with unknown argument \`$1'" >&2
exit 0
;;
esac
#DEBHELPER#
exit 0
2 changes: 2 additions & 0 deletions vlab-earlyoom-static/etc/default/earlyoom
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Options to pass to earlyoom
EARLYOOM_ARGS="-r 60 -m 5,4 -s 5 --avoid sshd -N /etc/vlab/earlyoom-notif.sh"
35 changes: 35 additions & 0 deletions vlab-earlyoom-static/etc/vlab/earlyoom-notif.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash

MSG="由于内存不足,earlyoom 已结束进程 $EARLYOOM_NAME 以保证用户界面流畅。如果多次看到此消息,请联系管理员添加内存。"

RATELIMIT="/tmp/earlyoom-ratelimit"

(
flock -x 200 -w 2
if [ -f "$RATELIMIT" ]; then
CURRENT=$(date +%s)
FILE=$(stat -c %Y "$RATELIMIT")

DIFF=$((CURRENT-FILE))
# 10 seconds rate-limit
if [ "$DIFF" -lt 10 ]; then
echo "Rate-limited earlyoom notification"
exit 0
fi
fi
touch "$RATELIMIT"
) 200>"$RATELIMIT"

wall "$MSG"
date >> /tmp/earlyoom-record
if ! command -v zenity &> /dev/null; then
exit 0
fi

if id "ubuntu" &>/dev/null; then
USER="ubuntu"
else
USER="vlab"
fi

DISPLAY=:0 sudo -u "$USER" zenity --error --text "$MSG" &
17 changes: 17 additions & 0 deletions vlab-earlyoom-static/lib/systemd/system/vlab-earlyoom.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[Unit]
Description=Early OOM Daemon in Vlab environment
Documentation=man:earlyoom(1) https://github.com/rfjakob/earlyoom

[Service]
EnvironmentFile=-/etc/default/earlyoom
ExecStart=/usr/bin/earlyoom $EARLYOOM_ARGS
# earlyoom never exits on it's own, so have systemd
# restart it should it get killed for some reason.
Restart=always
# set memory limits and max tasks number
TasksMax=10
MemoryMax=50M

[Install]
WantedBy=multi-user.target
Alias=earlyoom.service
Binary file added vlab-earlyoom-static/usr/bin/earlyoom
Binary file not shown.
6 changes: 3 additions & 3 deletions vlab-earlyoom/DEBIAN/control
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Package: vlab-earlyoom
Version: 1.1-4
Version: 1.1-5
Section: base
Priority: optional
Architecture: all
Recommends: zenity
Provides: earlyoom
Conflicts: earlyoom
Replaces: earlyoom
Conflicts: earlyoom, vlab-earlyoom-static
Replaces: earlyoom, vlab-earlyoom-static
Maintainer: USTC Vlab <[email protected]>
Description: Vlab earlyoom configurations for interactive responsiveness
12 changes: 11 additions & 1 deletion vlab-earlyoom/etc/vlab/earlyoom-notif.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,14 @@ RATELIMIT="/tmp/earlyoom-ratelimit"

wall "$MSG"
date >> /tmp/earlyoom-record
DISPLAY=:0 sudo -u ubuntu zenity --error --text "$MSG" &
if ! command -v zenity &> /dev/null; then
exit 0
fi

if id "ubuntu" &>/dev/null; then
USER="ubuntu"
else
USER="vlab"
fi

DISPLAY=:0 sudo -u "$USER" zenity --error --text "$MSG" &

0 comments on commit 0bf569b

Please sign in to comment.