-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate-config.sh
More file actions
133 lines (121 loc) · 4.97 KB
/
Copy pathgenerate-config.sh
File metadata and controls
133 lines (121 loc) · 4.97 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/bin/sh
# generate-config.sh — NUT config file generation helpers.
# Sourced by entrypoint.sh; not executed directly.
# ---------------------------------------------------------------------------
# Config generation helpers
# ---------------------------------------------------------------------------
# If /etc/nut/<name>.user exists, copy it over /etc/nut/<name> and return 0
# (caller skips generation). Return 1 otherwise.
use_user_override() {
[ -e "/etc/nut/$1.user" ] || return 1
if ! cp "/etc/nut/$1.user" "/etc/nut/$1"; then
printf 'level=error msg="failed to apply mounted override; aborting" file=%s.user\n' "$1" >&2
exit 1
fi
printf 'level=info msg="using mounted %s.user"\n' "$1" >&2
}
generate_all_configs() {
# Required variables — fail fast if caller forgot to set them.
: "${UPS_NAME:?generate_all_configs requires UPS_NAME}"
: "${UPS_DRIVER:?generate_all_configs requires UPS_DRIVER}"
: "${UPS_PORT:?generate_all_configs requires UPS_PORT}"
: "${API_USER:?generate_all_configs requires API_USER}"
: "${API_PASSWORD:?generate_all_configs requires API_PASSWORD}"
: "${API_ADDRESS:?generate_all_configs requires API_ADDRESS}"
: "${API_PORT:?generate_all_configs requires API_PORT}"
: "${ADMIN_PASSWORD:?generate_all_configs requires ADMIN_PASSWORD}"
: "${SHUTDOWN_CMD:?generate_all_configs requires SHUTDOWN_CMD}"
: "${POLLFREQ:?generate_all_configs requires POLLFREQ}"
: "${POLLFREQALERT:?generate_all_configs requires POLLFREQALERT}"
: "${DEADTIME:?generate_all_configs requires DEADTIME}"
: "${FINALDELAY:?generate_all_configs requires FINALDELAY}"
: "${HOSTSYNC:?generate_all_configs requires HOSTSYNC}"
: "${NOCOMMWARNTIME:?generate_all_configs requires NOCOMMWARNTIME}"
: "${RBWARNTIME:?generate_all_configs requires RBWARNTIME}"
# --- nut.conf — always generated (MODE is not user-configurable) ---
cat >/etc/nut/nut.conf <<'EOF'
MODE=netserver
EOF
# --- ups.conf — skip if user-mounted ---
if ! use_user_override ups.conf; then
cat >/etc/nut/ups.conf <<UPSEOF
[$UPS_NAME]
desc = "$UPS_DESC"
driver = $UPS_DRIVER
port = $UPS_PORT
UPSEOF
# pollonly is only meaningful for USB HID drivers; other drivers may
# ignore it or warn. Emit it only when applicable.
case "$UPS_DRIVER" in
usbhid-ups | *_usb)
printf ' pollonly\n' >>/etc/nut/ups.conf
;;
esac
# Battery overrides (ignorelb tells NUT to use our thresholds instead of
# hardware).
if [ -n "${LOWBATT_PERCENT:-}" ] || [ -n "${LOWBATT_RUNTIME:-}" ] \
|| [ -n "${CRITBATT_PERCENT:-}" ] || [ -n "${CRITBATT_RUNTIME:-}" ]; then
printf ' ignorelb\n' >>/etc/nut/ups.conf
fi
# Battery override directives — explicit per-variable to avoid eval.
[ -n "${LOWBATT_PERCENT:-}" ] \
&& printf ' override.%s = %s\n' "battery.charge.low" "$LOWBATT_PERCENT" >>/etc/nut/ups.conf
[ -n "${LOWBATT_RUNTIME:-}" ] \
&& printf ' override.%s = %s\n' "battery.runtime.low" "$LOWBATT_RUNTIME" >>/etc/nut/ups.conf
[ -n "${CRITBATT_PERCENT:-}" ] \
&& printf ' override.%s = %s\n' "battery.charge.critical" "$CRITBATT_PERCENT" >>/etc/nut/ups.conf
[ -n "${CRITBATT_RUNTIME:-}" ] \
&& printf ' override.%s = %s\n' "battery.runtime.critical" "$CRITBATT_RUNTIME" >>/etc/nut/ups.conf
if [ -n "${LOWBATT_PERCENT:-}${LOWBATT_RUNTIME:-}${CRITBATT_PERCENT:-}${CRITBATT_RUNTIME:-}" ]; then
printf 'level=info msg="battery thresholds overridden (ignorelb active)" low_pct=%s low_rt=%s crit_pct=%s crit_rt=%s\n' \
"${LOWBATT_PERCENT:-unset}" "${LOWBATT_RUNTIME:-unset}" \
"${CRITBATT_PERCENT:-unset}" "${CRITBATT_RUNTIME:-unset}" >&2
else
printf 'level=info msg="no battery threshold overrides; using UPS hardware defaults"\n' >&2
fi
fi
# --- upsd.conf — skip if user-mounted ---
if ! use_user_override upsd.conf; then
cat >/etc/nut/upsd.conf <<UPSDEOF
LISTEN $API_ADDRESS $API_PORT
UPSDEOF
fi
# --- upsd.users — skip if user-mounted ---
if ! use_user_override upsd.users; then
cat >/etc/nut/upsd.users <<USERSEOF
[admin]
password = "$ADMIN_PASSWORD"
actions = set
actions = fsd
instcmds = all
[$API_USER]
password = "$API_PASSWORD"
upsmon primary
USERSEOF
fi
# --- upsmon.conf — skip if user-mounted ---
if ! use_user_override upsmon.conf; then
cat >/etc/nut/upsmon.conf <<MONEOF
MONITOR $UPS_NAME@127.0.0.1:$API_PORT 1 "$API_USER" "$API_PASSWORD" primary
SHUTDOWNCMD "$SHUTDOWN_CMD"
POWERDOWNFLAG /var/run/nut/killpower
NOTIFYCMD /usr/local/bin/nut-notify.sh
POLLFREQ $POLLFREQ
POLLFREQALERT $POLLFREQALERT
DEADTIME $DEADTIME
FINALDELAY $FINALDELAY
HOSTSYNC $HOSTSYNC
NOCOMMWARNTIME $NOCOMMWARNTIME
RBWARNTIME $RBWARNTIME
NOTIFYFLAG ONLINE SYSLOG+EXEC
NOTIFYFLAG ONBATT SYSLOG+EXEC+WALL
NOTIFYFLAG LOWBATT SYSLOG+EXEC+WALL
NOTIFYFLAG FSD SYSLOG+EXEC+WALL
NOTIFYFLAG COMMOK SYSLOG+EXEC
NOTIFYFLAG COMMBAD SYSLOG+EXEC
NOTIFYFLAG SHUTDOWN SYSLOG+EXEC+WALL
NOTIFYFLAG REPLBATT SYSLOG+EXEC
NOTIFYFLAG NOCOMM SYSLOG+EXEC
MONEOF
fi
}