Shutdown dependent on network activity ?

Status
Not open for further replies.

Milhouse

Guru
Joined
Jun 1, 2011
Messages
564
Do you not need a clause before to check whether it is running in the background like so?

Need? No. Good to have? Depends.

With sasidle, there is no harm running it as background daemon at boot, and then a second version interactively so there was no reason to prevent this situation from arising. Not sure if the same is true of ethidle, but if running a second interactive version causes a problem then sure, add additional checks to prevent it (assuming it's likely to happen at all, once you've stopped testing).

Also how do you start stop the daemon after it has booted?

If you look at the original sasidle post, there are two scripts - the service control script (item #2, that is run from /etc/local/rc.d/sasidled once the system is booted - write it to /conf/base/etc/local/rc.d/sasidled for non-volatile storage) and the daemon itself (item #3, which you already have as ethidle.sh - this can be anywhere you like, just reference it from the service control script). Add the additional config entries to rc.conf, then use your service control script to stop/start/restart the daemon (if enabled, it will auto-start during boot). Eg to stop the daemon, execute "/etc/local/rc.d/ethidled stop".

You'll need to change the dependencies in the service script so that it starts once the network is up, and once snmpd is started - the first four comment lines are not comments, but instructions to FreeNAS to ensure boot scripts are started in the right sequence). You probably require the following as ethidled has different dependencies to sasidled:

Code:
# PROVIDE: ethidled
# REQUIRE: NETWORKING DAEMON snmpd
# BEFORE: LOGIN
# KEYWORD: shutdown
 

kaos

Cadet
Joined
Mar 31, 2012
Messages
1
Brilliant, looks like it is all working thanks for your help

could you post both of your scripts please.

i've tried to get them working on my own, but what happens is that the deamon starts the script and then freenas is not able to finish the boot process. therefore some of the other deamons are not loaded and i have no access through ssh or the web interface.

regards

k
 

Milhouse

Guru
Joined
Jun 1, 2011
Messages
564
I've got the same problem.
The script works flawlessly, but the daemon starts before snmpd and the script is executed in the main line;
shouldn't it run in the background?
I've also added the lines to rc.conf as said in the SASIDLE thread

What version of FreeNAS? Not sure why the daemon would run before snmpd, when it has a dependency on snmpd.

As for the called script being run in the foreground, that's because you need to modify ethidle so that it backgrounds itself - the original OP didn't need this functionality in the end so it was never added (exercise left for the student :)).

Essentially, your ethidle should become as follows (note addition of lines 4 through 12):

Code:
#!/bin/sh
#set -xv

# Being called by rc
if [ ! -z $RC_PID ]; then
	if [ -z $PROCMAIN ]; then
		export PROCMAIN=YES;
		/bin/sh $0 ${ALL_ARGS} | logger -i -t ethidle &
		exit 0
	fi
	echo $$ >/var/run/ethidled.pid
fi

TMPFILE=/tmp/ifoctets.dat

SNMP_COMMUNITY=public
SNMP_HOST=localhost
SNMP_INDEX=1
SNMP_GET="/usr/local/bin/snmpget -c $SNMP_COMMUNITY -v 2c -Ov $SNMP_HOST ifInOctets.$SNMP_INDEX ifOutOctets.$SNMP_INDEX"

TIMEOUT=10
TIMEDELAY=60

THRESHOLD_IN=20240
THRESHOLD_OUT=25048

echo "Count    In Delta    Out Delta"

COUNT=$TIMEOUT
OCT_IN2=0
OCT_OUT2=0

while [ true ]; do
	let OCT_IN1=OCT_IN2 >/dev/null
	let OCT_OUT1=OCT_OUT2 >/dev/null

	$SNMP_GET | tr -s '\n' ' ' > $TMPFILE
	read C1 OCT_IN2 C2 OCT_OUT2 < $TMPFILE

	let OCT_IN=OCT_IN2-OCT_IN1 >/dev/null
	let OCT_OUT=OCT_OUT2-OCT_OUT1 >/dev/null

#	if [ ${OCT_IN-0} -le 0 -a ${OCT_OUT-0} -le 0 ]; then
	if [ ${OCT_IN-0} -lt $THRESHOLD_IN -a ${OCT_OUT-0} -lt $THRESHOLD_OUT ]; then
		let COUNT=COUNT-1 >/dev/null
	else
		COUNT=${TIMEOUT}
	fi

	printf " %.3d  %11d  %11d\n" $COUNT $OCT_IN $OCT_OUT

	[ $COUNT -le 0 ] && break || sleep $TIMEDELAY
done

echo "SHUTTING DOWN SERVER"
shutdown -p now


You may want to quieten the script (ie. suppress printf statement) as all output will now appear in /var/log/messages.
 

qlcnnsamsg

Cadet
Joined
May 24, 2012
Messages
2
Thank you for your quick reply!
I'm pretty new on freebsd, so my scripting knowledge is really bad.
As soon as I get home I'll check my freenas version. I can also try to post the boot log if it helps.
Anyway I'm quite sure to have the last stable version (8.0......)
I've noticed that whatever I put in the 'REQUIRE:' section (I've tried with the names of other daemons), the daemon always starts in the same position.
Rcoder problem?

So, my freenas build is "FreeNAS-8.0.4-RELEASE-p1-x86 (11059)"
Is the boot log /var/log/messages?
 

tommie_fox

Cadet
Joined
Sep 5, 2012
Messages
1
Hi all,

Im a newb to Freenas8 and scripts but have been using Freenas 7 for a few years.
Last month I lost a 1TB disk from my RAID1 so decided it was a good time to upgrade to Freenas 8.2.0. I am using an old machine to do some testing which is going well and I think I will be using this in production as it has a few more HD bays. Problem is this machine is noisier and consumes more power.
I stumbled across this thread and seems EXACTLY what i need however I just cannot get the script to work !!! Its probably me.......

So I have copied the code and saved it in my datastore (mnt/DataTest) but when I try to test from shell I get the below (and attached) error:

screenie1.jpg

[root@freenas ~]# sh /mnt/DataTest/shutdown.sh
: not found
: not found
: not found
: not found
: not found
: not found
sec between polls: 30
Count In Delta Out Delta
: not found
: not found
/mnt/DataTest/shutdown.sh: 48: Syntax error: end of file unexpected (expecting "
then")
[root@freenas ~]#

Here is the script I am using:

Code:
#!/bin/sh
#set -xv

TMPFILE=/tmp/ifoctets.dat

SNMP_COMMUNITY=public
SNMP_HOST=localhost
SNMP_INDEX=1
SNMP_GET="/usr/local/bin/snmpget -c $SNMP_COMMUNITY -v 2c -Ov $SNMP_HOST ifInOctets.$SNMP_INDEX ifOutOctets.$SNMP_INDEX"

TIMEOUT=2
TIMEDELAY=30

THRESHOLD_IN=20480
THRESHOLD_OUT=10240


echo "Delay between polls: $TIMEDELAY sec" 
echo "Count    In Delta    Out Delta"

COUNT=$TIMEOUT
OCT_IN2=0
OCT_OUT2=0

while [ true ]; do
	let OCT_IN1=OCT_IN2 >/dev/null
	let OCT_OUT1=OCT_OUT2 >/dev/null

	$SNMP_GET | tr -s '\n' ' ' > $TMPFILE
	read C1 OCT_IN2 C2 OCT_OUT2 < $TMPFILE

	let OCT_IN=OCT_IN2-OCT_IN1 >/dev/null
	let OCT_OUT=OCT_OUT2-OCT_OUT1 >/dev/null

#	if [ ${OCT_IN-0} -le 0 -a ${OUT_OUT-0} -le 0 ]; then
	if [ ${OCT_IN-0} -lt $THRESHOLD_IN -a ${OUT_OUT-0} -lt $THRESHOLD_OUT ]; then
		let COUNT=COUNT-1 >/dev/null
	else
		COUNT=${TIMEOUT}
	fi

	printf " %.3d  %11d  %11d\n" $COUNT $OCT_IN $OCT_OUT

	[ $COUNT -le 0 ] && break || sleep $TIMEDELAY
done

echo -e "Inbound $THRESHOLD_IN and outbound $THRESHOLD_OUT thresholds have been reached $TIMEOUT times. \nSystem is shutting down."
#shutdown -p now

Please can someone help ?

Many thanks

Tom
 

Milhouse

Guru
Joined
Jun 1, 2011
Messages
564
Have you created the file in Windows format? Make sure you have created the file with Linux line endings.

Get the free version of an editor like TextPad that will allow you to open and save files with Linux (or Windows, or Mac) line endings.
 

Ismael Duarte

Contributor
Joined
Jun 13, 2011
Messages
154
I had this working perfectly, I've upgraded to 8.3b2 and does not work anymore.
Here's some erros when activating snmp
Sep 11 09:46:32 MyFreeNas notifier: Stopping bsnmpd.
Sep 11 09:46:38 MyFreeNas notifier: [: missing ]
Sep 11 09:46:38 MyFreeNas notifier: bsnmpd not running? (check /var/run/snmpd.pid).
Sep 11 09:46:38 MyFreeNas notifier: Starting bsnmpd.
Sep 11 09:46:38 MyFreeNas snmpd[3898]: disk_OS_get_disks: device 'cd0' not in device list
Sep 11 09:46:38 MyFreeNas snmpd[3898]: disk_OS_get_disks: device 'da0' not in device list
Sep 11 09:46:38 MyFreeNas snmpd[3898]: disk_OS_get_disks: device 'ada2' not in device list
Sep 11 09:46:38 MyFreeNas snmpd[3898]: disk_OS_get_disks: device 'ada1' not in device list
Sep 11 09:46:38 MyFreeNas snmpd[3898]: disk_OS_get_disks: device 'ada0' not in device list
Sep 11 09:46:38 MyFreeNas snmpd[3898]: hrPrinterTable: printcap entry for has errors, skipping
Sep 11 09:46:38 MyFreeNas snmpd[3898]: hrSWInstalledTable: stat("/var/db/pkg") failed: No such file or directory

Any help ?
 

babaroga

Explorer
Joined
Jul 22, 2012
Messages
50
Hello,

I have upgraded my system from Beta1 and have the same problem.

I have raised support ticket #1801, so hopefully someone will look into it.
 

Milhouse

Guru
Joined
Jun 1, 2011
Messages
564
I'm afraid I'm a bit behind the times with the latest version and keep meaning to try 8.2 or even one of the 8.3 betas - maybe later this week, but it does look like a problem somewhere with the snmp service on which this script is dependent.
 

madmax

Explorer
Joined
Aug 31, 2012
Messages
64
I had this working perfectly, I've upgraded to 8.3b2 and does not work anymore.
Here's some erros when activating snmp


Any help ?

I'm getting same errors as you posted but the script service and script works. It collects traffic and meets the threshold and then shut's down.

I'm running 8.3 alpha r12434 nightly build.
 

Ismael Duarte

Contributor
Joined
Jun 13, 2011
Messages
154
I'm getting same errors as you posted but the script service and script works. It collects traffic and meets the threshold and then shut's down.

I'm running 8.3 alpha r12434 nightly build.

The script works?
I've the same problem since I've upgraded to B2, Now with rc1 still does not work. Does not counts the trafic any more, always 0!
I really need this back to work!
Thanks
 

Child

Dabbler
Joined
Sep 25, 2011
Messages
44
Hm ... I can verify the 0-issue. I've the same behavior after upgrading from 8.2.0 to 8.3.0-RC. Seems as if something changed with SNMP ? Anyone?
 

Milhouse

Guru
Joined
Jun 1, 2011
Messages
564
Sorry for the delay in replying, as I've only just got around to testing anything later than 8.0.2! :)

I've now installed 8.3.0-RC1 and certainly, the SNMP service startup needs some attention - and there is an outstanding ticket for that - so hopefully it will get looked at before release.

However, despite the bsnmpd "noise" at startup (for most users noise is all it is, and can be ignored) the shutdown script should be working as it is possible to query the SNMP network counters.

Code:
[root@freenas3] ~# uname -a
FreeBSD freenas3.local 8.3-RELEASE-p4 FreeBSD 8.3-RELEASE-p4 #0 r241385M: Tue Oct  9 16:12:04 PDT 2012     root@build.ixsystems.com:/usr/home/jpaetzel/8.3.0/os-base/amd64/usr/home/jpaetzel/8.3.0/FreeBSD/src/sys/FREENAS.amd64  amd64

[root@freenas3] ~# tail -10 /var/log/messages
Oct 23 00:54:54 freenas3 notifier: bsnmpd not running? (check /var/run/snmpd.pid).
Oct 23 00:54:54 freenas3 notifier: Starting bsnmpd.
Oct 23 00:54:54 freenas3 snmpd[12582]: disk_OS_get_disks: device 'da1' not in device list
Oct 23 00:54:54 freenas3 snmpd[12582]: disk_OS_get_disks: device 'da0' not in device list
Oct 23 00:54:54 freenas3 snmpd[12582]: disk_OS_get_disks: device 'ada3' not in device list
Oct 23 00:54:54 freenas3 snmpd[12582]: disk_OS_get_disks: device 'ada2' not in device list
Oct 23 00:54:54 freenas3 snmpd[12582]: disk_OS_get_disks: device 'ada1' not in device list
Oct 23 00:54:54 freenas3 snmpd[12582]: disk_OS_get_disks: device 'ada0' not in device list
Oct 23 00:54:54 freenas3 snmpd[12582]: hrPrinterTable: printcap entry for <noname?> has errors, skipping
Oct 23 00:54:54 freenas3 snmpd[12582]: hrSWInstalledTable: stat("/var/db/pkg") failed: No such file or directory

[root@freenas3] ~# snmpwalk -c public -v 2c localhost ifName
IF-MIB::ifName.1 = STRING: em0
IF-MIB::ifName.2 = STRING: usbus0
IF-MIB::ifName.3 = STRING: usbus1
IF-MIB::ifName.4 = STRING: usbus2
IF-MIB::ifName.5 = STRING: usbus3
IF-MIB::ifName.6 = STRING: usbus4
IF-MIB::ifName.7 = STRING: usbus5
IF-MIB::ifName.8 = STRING: lo0
IF-MIB::ifName.9 = STRING: bridge0
IF-MIB::ifName.10 = STRING: epair0a

[root@freenas3] ~# snmpget -c public -v 2c -Ov localhost ifInOctets.1 ifOutOctets.1
Counter32: 75716715
Counter32: 517214952

# Waited a few seconds for network traffic to increment the counters...

[root@freenas3] ~# snmpget -c public -v 2c -Ov localhost ifInOctets.1 ifOutOctets.1
Counter32: 75742123
Counter32: 517227410


Anyone that is having a problem with the script not reliably detecting an idle network should look at how much data is being transferred over their network interface - it may not be reaching the idle state. I've got a MySQL instance running in a jail, and it seems that 8.3.0-RC1 is generating more "idle" network traffic than 8.0.2 (maybe because of the bridged network?), so perhaps users with a jail will need to tweak the IN/OUT threshold values accordingly (ie. bump them up a bit).
 

Milhouse

Guru
Joined
Jun 1, 2011
Messages
564
Hm ... I can verify the 0-issue. I've the same behavior after upgrading from 8.2.0 to 8.3.0-RC. Seems as if something changed with SNMP ? Anyone?

Change of interface index, maybe? What output do you get with "snmpwalk -c public -v 2c localhost ifName", what is the name of your interface (and do you have the corresponding index configured in your script?)
 
Status
Not open for further replies.
Top