summaryrefslogtreecommitdiffstats
path: root/net/nut/files/nut-notify-exec.default
blob: 242066b1cc82dae51313b0d1c264d5bba33cf0bb (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
70
71
72
73
74
75
76
77
78
#!/bin/sh

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

# uci-defaults script to set notify flags to enable execution of notifycmd
# (set separate scripts) unless the user has configured that notifications
# should be ignored.

# 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

# In the nut-upssched package, this script lives in
# /etc/uci-defaults/70-notify-exec
# In the nut-sendmail-notify package, this script lives in
# /etc/uci-defaults/71-notify-exec
# The difference in names is required to avoid conflicting files should both
# packages be installed on the same device.

# 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-notify-exec "FATAL: Unable to source 'functions.sh'." || true
	exit 1
}

# Flags for whether defaultnotify option is set to IGNORE
DEFAULTNOTIFY_IS_IGNORE="false"

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

config_get val "upsmon" defaultnotify
# shellcheck disable=SC2154
if [ "${val}" = "IGNORE" ]; then
	DEFAULTNOTIFY_IS_IGNORE="true"
else
	# Ensure upsmon type section with the name upsmon exists (creating if
	# needed, doing nothing if a upsmon type section named 'upsmon' already
	# exists).
	uci set "nut_monitor.upsmon=upsmon" || {
		logger -s -t nut-notify-exec "Failed to ensure an upsmon section named upsmon exists" || true
		exit 1
	}
fi

# Set default notifications to enable notifycmd unless set to ignore
if [ "${DEFAULTNOTIFY_IS_IGNORE}" = "true" ]; then
	defaultnotify="IGNORE"
# We only support IGNORE, SYSLOG, and EXEC options to defaultnotify
# Since we want logging (SYSLOG) and require EXEC so that /usr/sbin/upssched
# gets called by NUT, we enforce EXEC as well, producing SYSLOG+EXEC, unless
# the user is ignoring all notifications.
else
	defaultnotify="SYSLOG+EXEC"
fi

uci set "nut_monitor.upsmon.defaultnotify"="$defaultnotify" || {
	logger -s -t nut-notify-exec "FATAL: Failed to set 'defaultnotify' for 'upsmon'" || true
	uci revert nut_monitor || true
	exit 1
}

uci commit nut_monitor || {
	logger -s -t nut-notify-exec "ERROR: Failed to commit changes to nut_monitor" || true
	exit 1
}

exit 0