Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nut-upsmon broken - tmpfs configuration files not created #6644

Closed
petvoigt opened this issue Aug 1, 2018 · 21 comments
Closed

nut-upsmon broken - tmpfs configuration files not created #6644

petvoigt opened this issue Aug 1, 2018 · 21 comments

Comments

@petvoigt
Copy link

petvoigt commented Aug 1, 2018

Maintainer: @cshoredaniel
Environment: AMD GX-412TC SOC, PC Engines APU2, OpenWrt 18.06.0, x86_64)

Description:
Today I performed a sysupgrade via LUCI webGUI from 17.01.4 to 18.06.0.

Installed NUT packages:

nut - 2.7.4-4
nut-common - 2.7.4-4
nut-upsc - 2.7.4-4
nut-upslog - 2.7.4-4
nut-upsmon - 2.7.4-4

I have configured NUT as slave to monitor a master in my LAN, e.g. MODE=netclient in /etc/nut.conf.

/etc/init.d//etc/init.d/nut-monitor cannot be started as it complains about missing /var/etc/nut directory and files in it.
I can manually configure and start nut-upsmon:

mkdir /var/etc/nut && echo "MODE=netclient" > /var/etc/nut/nut.conf
/etc/init.d//etc/init.d/nut-monitor enable
/etc/init.d//etc/init.d/nut-monitor start

As a partial workaround I added to start_service() of /etc/init.d//etc/init.d/nut-monitor the following code:

mkdir -p /var/etc/nut
chmod -R 750 /var/etc/nut

This partial workaround preserves the subdirectory "nut" under /var/etc during a reboot. However, nut-upsmon does not start automatically during boot.

@danielfdickinson
Copy link
Contributor

Will look into it; it was working for me, but I don't remember if I did upsmon standalone...will review. I may have had a PR for that that I closed.

@danielfdickinson
Copy link
Contributor

@petvoigt For your work around you also need chown nut:nut /var/etc/nut

It looks like I missed submitting the fix for upsmon on it's own. Will create a PR and and backport PR and we'll see if the backport gets accepted for 18.06.

@danielfdickinson
Copy link
Contributor

danielfdickinson commented Aug 2, 2018

@petvoigt Also, after the chmod of '$UPSMON_C' and '/var/etc/nut.conf' you need

    chown ${runas:-root}:$(id -gn ${runas:-root}) $UPSMON_C                                    
    chown ${runas:-root}:$(id -gn ${runas:-root}) /var/etc/nut/nut.conf

I know how I missed it now...didn't test upsmon alone and the way I was using nut-server it took care of this business.
Also can you verify nut user and group were created? (opkg should have taken care of that).

@danielfdickinson
Copy link
Contributor

Will test the above PR and submit.

@petvoigt
Copy link
Author

petvoigt commented Aug 2, 2018

@cshoredaniel, ah, sounds good. Thank your for your quick reply and many thanks for the quickly updated NUT documentation under https://openwrt.org/docs/guide-user/services/ups/software.nut.

UID nut and GID nut are present on my system:

id nut
uid=113(nut) gid=113(nut) groups=113(nut),113(nut)

However, I dont' know what to do with $UPSMON_C - in fact it is empty. I tried:

echo $UPSMON_C

@petvoigt petvoigt closed this as completed Aug 2, 2018
@petvoigt petvoigt reopened this Aug 2, 2018
@petvoigt
Copy link
Author

petvoigt commented Aug 2, 2018

Accidently closed issue (wrong button), reopened immetiately.

@danielfdickinson
Copy link
Contributor

@petvoigt Sorry for not being clear: that needs to be in the initscript

@petvoigt
Copy link
Author

petvoigt commented Aug 2, 2018

@cshoredaniel, thanks for clarifying.

After the above mentioned changes to /etc/init.d/nut-monitor I further have to do after every reboot:

chown nut:nut /var/etc/nut
chown nut:nut /var/etc/nut/nut.conf
/etc/init.d/nut-monitor start

Could you please tell me, which NUT packages are affected by your patch and could you please report here, when I can update the affected packages to test your changes?

@danielfdickinson
Copy link
Contributor

danielfdickinson commented Aug 2, 2018

Only package nut-upsmon will be affected and it's just updating the initscript appropriately

Ah, I caught /var/etc/nut in my PR, will check nut.conf.

And I wouldn't count on updates to the packages a downloads.openwrt.com until the .1 release (but that probably won't be that long as there are already errata being corrected in core too).

@danielfdickinson
Copy link
Contributor

I'll post the final initscript revision here (after testing) so you can at least have that on-router until the updated package is available.

@petvoigt
Copy link
Author

petvoigt commented Aug 2, 2018

Thanks a lot. I will keep this issue open until I have tested at least the updated initscript.

@danielfdickinson
Copy link
Contributor

If you could leave it until PR is merged that would be best! That way others can find it.

@petvoigt
Copy link
Author

petvoigt commented Aug 2, 2018

OK, if I understand correctly, this will be after testing the .1 release.

@danielfdickinson
Copy link
Contributor

Oh, no worries, it'll autoclose when the PR is merged due to commit message.

@petvoigt
Copy link
Author

petvoigt commented Aug 2, 2018

OK, did not know this;-)

@danielfdickinson
Copy link
Contributor

Ok, put the script below in /etc/init.d/nut-monitor on the device and you ought to be good on reboot.

#!/bin/sh /etc/rc.common

START=60
USE_PROCD=1
UPSMON_C=/var/etc/nut/upsmon.conf

nut_upsmon_conf() {
	local cfg="$1"

	echo "# Config file automatically generated from UCI config" > $UPSMON_C

	config_get runas "$cfg" runas "nut"
	[ -n "$runas" ] && echo "RUN_AS_USER $runas" >> $UPSMON_C

	config_get val "$cfg" minsupplies 1
	echo "MINSUPPLIES $val" >> $UPSMON_C

	config_get val "$cfg" shutdowncmd "/sbin/halt"
	echo "SHUTDOWNCMD \"$val\"" >> $UPSMON_C

	config_get val "$cfg" notifycmd
	[ -n "$val" ] && echo "NOTIFYCMD \"$val\"" >> $UPSMON_C

	config_get val "$cfg" pollfreq 5
	echo "POLLFREQ $val" >> $UPSMON_C

	config_get val "$cfg" pollfreqalert 5
	echo "POLLFREQALERT $val" >> $UPSMON_C

	config_get val "$cfg" hostsync 15
	echo "HOSTSYNC $val" >> $UPSMON_C

	config_get val "$cfg" deadtime 15
	echo "DEADTIME $val" >> $UPSMON_C

	config_get val "$cfg" powerdownflag /var/run/killpower
	echo "POWERDOWNFLAG $val" >> $UPSMON_C

	config_get val "$cfg" onlinemsg
	[ -n "$val" ] && echo "NOTIFYMSG ONLINE \"$val\"" >> $UPSMON_C
	config_get val "$cfg" onbattmsg
	[ -n "$val" ] && echo "NOTIFYMSG ONBATT \"$val\"" >> $UPSMON_C
	config_get val "$cfg" lowbattmsg
	[ -n "$val" ] && echo "NOTIFYMSG LOWBATT \"$val\"" >> $UPSMON_C
	config_get val "$cfg" fsdmsg
	[ -n "$val" ] && echo "NOTIFYMSG FSD \"$val\"" >> $UPSMON_C
	config_get val "$cfg" commokmsg
	[ -n "$val" ] && echo "NOTIFYMSG COMMOK \"$val\"" >> $UPSMON_C
	config_get val "$cfg" commbadmsg
	[ -n "$val" ] && echo "NOTIFYMSG COMMBAD \"$val\"" >> $UPSMON_C
	config_get val "$cfg" shutdownmsg
	[ -n "$val" ] && echo "NOTIFYMSG SHUTDOWN \"$val\"" >> $UPSMON_C
	config_get val "$cfg" replbattmsg
	[ -n "$val" ] && echo "NOTIFYMSG REPLBATT \"$val\"" >> $UPSMON_C
	config_get val "$cfg" nocommmsg
	[ -n "$val" ] && echo "NOTIFYMSG NOCOMM \"$val\"" >> $UPSMON_C
	config_get val "$cfg" noparentmsg
	[ -n "$val" ] && echo "NOTIFYMSG NOPARENT \"$val\"" >> $UPSMON_C

	notifylist() {
		local value="$1"
		append optval "$value" "+"
	}

	setnotify() {
		local cfg="$1"
		local optname="$2"
		local optval
		config_list_foreach "$cfg" "$optname" notifylist
		if [ -z "$optval" ]; then
			# If no list fallback to trying option, fallback to default
			config_get optval "$cfg" "$optname" "$default"
			if [ -n "$optval" ]; then
				echo "$optval"
			else
				# No default, so do the NUT default
				echo "SYSLOG"
			fi
		else
			echo "$optval"
		fi
        }

	local default optval
	val=""
	config_list_foreach "$cfg" defaultnotify notifylist
	default="$optval"
	echo "NOTIFYFLAG ONLINE $(setnotify "$cfg" onlinenotify)" >> $UPSMON_C
	echo "NOTIFYFLAG ONBATT $(setnotify "$cfg" onbattnotify)" >> $UPSMON_C
	echo "NOTIFYFLAG LOWBATT $(setnotify "$cfg" lowbatnotify)" >> $UPSMON_C
	echo "NOTIFYFLAG FSD $(setnotify "$cfg" fsdnotify)" >> $UPSMON_C
	echo "NOTIFYFLAG COMMOK $(setnotify "$cfg" commoknotify)" >> $UPSMON_C
	echo "NOTIFYFLAG COMMBAD $(setnotify "$cfg" commbadnotify)" >> $UPSMON_C
	echo "NOTIFYFLAG SHUTDOWN $(setnotify "$cfg" shutdownnotify)" >> $UPSMON_C
	echo "NOTIFYFLAG REPLBATT $(setnotify "$cfg" repolbattnotify)" >> $UPSMON_C
	echo "NOTIFYFLAG NOCOMM $(setnotify "$cfg" nocommnotify)" >> $UPSMON_C
	echo "NOTIFYFLAG NOPARENT $(setnotify "$cfg" noparentnotify)" >> $UPSMON_C

	config_get val "$cfg" rbwarntime 43200
	echo "RBWARNTIME $val" >> $UPSMON_C

	config_get val "$cfg" nocommwarntime 300
	echo "NOCOMMWARNTIME $val" >> $UPSMON_C

	config_get val "$cfg" finaldelay 5
	echo "FINALDELAY $val" >> $UPSMON_C

	config_get val "$cfg" certpath
	if [ -n "$val" ]; then echo "CERTPATH $val" >> $UPSMON_C; fi

	config_get_bool val "$cfg" certverify 0
	if [ -n "$val" ]; then echo "CERTVERIFY $val" >> $UPSMON_C; fi

	config_get_bool val "$cfg" forcessl 0
	if [ -n "$val" ]; then echo "FORCESSL $val" >> $UPSMON_C; fi
}

nut_upsmon_add() {
	local cfg="$1"
	local type="$2"
	local upsname
	local hostname
	local port
	local powervalue
	local username
	local password
	local system

	# if UPSMON_C is a symlink we're only doing generated config
	[ -L $UPSMON_C ] && {
		rm -f $UPSMON_C
		nut_upsmon_conf ""
	}

	config_get upsname "$cfg" upsname
	config_get hostname "$cfg" hostname localhost
	config_get port "$cfg" port
	config_get powervalue "$cfg" powervalue 1
	config_get username "$cfg" username
	config_get password "$cfg" password
	system="$upsname@$hostname"
	if [ -n "$port" ]; then
	    system="$system:$port";
	fi
	echo "MONITOR $system $powervalue $username $password $type" >> $UPSMON_C
}

start_service() {
	mkdir -p "$(dirname "$UPSMON_C")"
	chmod 750 "$(dirname "$UPSMON_C")"

	config_load nut_monitor

	config_foreach nut_upsmon_conf upsmon
	config_foreach nut_upsmon_add master master
	config_foreach nut_upsmon_add slave slave

	[ -z "$(cat /var/etc/nut/nut.conf)" ] && echo "MODE=netclient" >>/var/etc/nut/nut.conf

	chmod 640 $UPSMON_C
	chmod 640 /var/etc/nut/nut.conf

	chown ${runas:-root}:$(id -gn ${runas:-root}) /var/etc/nut
	chown ${runas:-root}:$(id -gn ${runas:-root}) /var/etc/nut/nut.conf
	chown ${runas:-root}:$(id -gn ${runas:-root}) $UPSMON_C

	[ -d /var/run/nut ] || {
		mkdir -m 0750 -p /var/run/nut
		chown ${runas:-root}:$(id -gn ${runas:-root}) /var/run/nut
	}

	exec $DEBUG /usr/sbin/upsmon $UPSMON_OPTIONS
}

stop_service() {
	exec /usr/sbin/upsmon -c stop
}

reload_service() {
	exec /usr/sbin/upsmon -c reload
}

@stintel stintel closed this as completed in 98fdf16 Aug 2, 2018
stintel pushed a commit to stintel/openwrt-packages that referenced this issue Aug 2, 2018
nut-monitor failed to create required dir /var/etc/nut, as
well as failing to set appropriate user on the directory and
conf files.  Fixing this closes
openwrt#6644

Signed-off-by: Daniel F. Dickinson <cshored@thecshore.com>
(cherry picked from commit 98fdf16)
@petvoigt
Copy link
Author

petvoigt commented Aug 2, 2018

Script runs like a charm, thanks a lot.

@danielfdickinson
Copy link
Contributor

Excellent! And thank you for your helping get this fixed!

@danielfdickinson
Copy link
Contributor

danielfdickinson commented Aug 8, 2018 via email

@petvoigt
Copy link
Author

petvoigt commented Aug 9, 2018 via email

@danielfdickinson
Copy link
Contributor

@petvoigt FYI I've posted PR: #7638 which ought to do must of what's now in master except the USB hotplug bits (as that's a new feature IMO). If you can, please test and comment!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants