summaryrefslogtreecommitdiffstats
path: root/net/nut/files/nut-sched.default
blob: e0636b4ee937f17075a5474050f6d9490fec5364 (plain)
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
68
69
#!/bin/sh

# In recent (relevant) versions of shellcheck busybox is a valid shell type
# shellcheck shell=busybox

# uci-defaults script to setup upsmon notifications using upssched
# applied during install or on first boot after install, if setting on install
# fails. Expects nut-notify-exec to also be run in uci-defaults.

# IPKG_INSTROOT is intentionally only set when building an image and
# is intentionally empty on a live OpenWrt device

# Shellcheck source paths intentionally point to the location of files of
# the scripts in the development environment (where shellcheck is used), not
# on the live OpenWrt device.

# This script lives in the nut-upssched package, which is independent of the
# nut-sendmail-notify package in which nut-sendmail-notify.default lives
# In the nut-upssched package, this script lives in
# /etc/uci-defaults/80-nut-sched

# The separate packages limit the opportunities for code-sharing across the
# scripts.

# Only run this uci-defaults script on a live OpenWrt device
[ -z "${IPKG_INSTROOT}" ] || exit 0

# shellcheck source=net/nut/files/functions.sh.functions
. /lib/functions.sh || {
	logger -s -t nut-sched "FATAL: Unable to source 'functions.sh'" || true
	exit 1
}

# Flags for existing notifycmd
SKIP_ADD_NOTIFYCMD="false"

# Load and process nut_monitor (upsmon) configuration
config_load nut_monitor || {
	logger -s -t nut-sched "FATAL: loading 'nut_monitor' failed" || true
	exit 1
}

config_get val "upsmon" notifycmd
# shellcheck disable=SC2154
if [ -n "${val}" ]; then
	SKIP_ADD_NOTIFYCMD="true"
fi

# If notification command is not set, set it
if [ "${SKIP_ADD_NOTIFYCMD}" = "false" ]; then
	# Ensure upsmon type section with the name upsmon exists
	uci set "nut_monitor.upsmon=upsmon" || {
		logger -s -t nut-sched "Failed to ensure an upsmon section named upsmon exists" || true
		exit 1
	}

	uci set "nut_monitor.upsmon.notifycmd=/usr/sbin/upssched" || {
		logger -s -t nut-sched "Failed to set notifycmd to upssched" || true
		uci revert nut_monitor || true
		exit 1
	}

	uci commit nut_monitor || {
		logger -s -t nut-sched "Failed to commit changes to nut_monitor" || true
		exit 1
	}
fi

exit 0