Contribute  :  Advanced Search  :  Directory  :  Forum  :  FAQ's  :  My Downloads  :  Links  :  Polls  
AFP548 Changing the world one server at a time.
Welcome to AFP548
Thursday, July 29 2010 @ 09:31 am MDT
   

Automatically disconnecting disabled/asleep AFP sessions.

Articles

So a few people on the macos-x-server mailing list noticed that recent versions of OS X Server seem to be building up large numbers of Disabled/Asleep connections on their AFP servers, even with appropriate disconnection settings when users don't have files open.

 I'm even seeing this for servers where AFP session caching has been disabled, and this is as good a time as any to again point readers towards Apple's article on Understanding the "disconnect when idle" feature for AFP connections.

 I've been doing a little bit of work lately on scripts that parse out the result of serveradmin commands, so I thought I'd post up a script I put together that illustrates how you can do this with PHP command line scripting.  

Read on to download the script and read a brief description of how it works. 



Basically the script works like this:

  1. Issue the serveradmin command 'getConnectedUsers' to the AFP service to build up an array of all current AFP sessions
  2. Check each session, and build up an array of all current disabled/asleep sessions
  3. Check each disabled/asleep session and build up an array of all that have been idle over a certain period of time
  4. Issue the serveradmin command 'disconnectUsers' to the AFP service for each such session
  5. Print the username and IP to the screen of each disconnected session

 

There are really only four parameters to configure at the start of the script, and hopefully the in-line comments as shown below explain how they work.

// The disconection threshold sets the idle time threshold for disconnections.
// Any connection that has been idle for a time greater than this will be disconnected.
// This is in seconds, so for one hour, set it to 3600, 24 hours, set it to 86400, etc.

$disconnection_threshold = 86400;

// The disconnection message sets the message that users will be sent when being disocnnected.
// Leave it empty to not send a message.

$disconnection_message = "";

// The disconnection time sets the amount of time users will be given before being disconnected.
// Leave it at 0 to disconnect immediately.

$disconnection_time = 0;
 // The report_disconnection_details flag sets whether this script reports the details of disconnected sessions
// when finished. You probably want to set this to 0 if you're scheduling the script.

$report_disconnection_details = 1;

 

After finishing this script I realised that a better general framework may have been to use the -xml output option of serveradmin instead, and then use an XML parser to pull out the info, but in this simple case it's easy enough to use preg_replace to pull out the relevant data.

 

When you download the script, make sure the permissions are such that it is executable, and then you should be able to simply run it from the command line on your AFP server. It's probably a good idea to open up the GUI of Server Admin to examine your current connections, and then refresh the display after executing the script, just to make sure things are working happily for you.

 As always, if you follow this link to download the script and run it without testing it out, it may do anything, up to and including anything as disastrous as Australia not actually crushing all their opposition in the ICC Cricket World Cup 2007 or new episodes of Heroes being delayed by another couple of months....

Story Options

Advertising

Automatically disconnecting disabled/asleep AFP sessions. | 10 comments | Create New Account
The following comments are owned by whomever posted them. This site is not responsible for what they say.
Automatically disconnecting disabled/asleep AFP sessions.
Authored by: Anonymous on Wednesday, April 18 2007 @ 06:19 am MDT
[iamleeg here...didn't log in though...]

so what impact does terminating these sessions have? Are they actually stale and never going to be used again, or will their existence save some time when one of those machines next goes to connect? Or is there a security risk in having an idle connection hijacked?
Automatically disconnecting disabled/asleep AFP sessions.
Authored by: Anonymous on Wednesday, April 18 2007 @ 01:59 pm MDT
Great script!

However....

"Heroes being delayed by another couple of months...."

That's not funny at all!

Thanks,
David
http://www.FloridaPets.org
Automatically disconnecting disabled/asleep AFP sessions.
Authored by: beauh on Monday, June 04 2007 @ 07:22 pm MDT
Nice script, very useful. The first thing I noticed is that some attributes aren't getting stored as I would expect (i.e. trying to apply the disconnect to specific users, or specific IP). Easy enough fix, but worth mentioning. Simply add the line:

$value = preg_replace('/"/', '', $value);

on line 62, before $connections[$index][$attribute] = ...

that way, you don't have to test for quotations in the stored array value. ( $connection['name'] == "username" ) vs ( $connection['name'] == '"username"' ).

- b-
Automatically disconnecting disabled/asleep AFP sessions.
Authored by: Anonymous on Tuesday, October 02 2007 @ 04:01 pm MDT
Thanks for sharing this great script! When I execute manually from the cmd line, it works like a charm! I would like this to execute automatically on a given interval because I have many issues with ghost connections on my server. I attempted to make a cron job to execute this command every half hour as root, however while the cron job itself runs, I get the following error from the script's execution:

sh: line 1: serveradmin: command not found
Problem executing serveradmin command afp:command = getConnectedUsers, return status was: 127

As indicated earlier, the script runs fine when executed from command line. Any clues or suggestions?
Automatically disconnecting disabled/asleep AFP sessions.
Authored by: rsaeks on Thursday, February 28 2008 @ 10:10 am MST
Thanks for the script. It worked great on 10.4.9, but seems to be broken in 10.4.11. I've run it both though the launchd job I created and manually. They both return this:

sh: -c: line 1: syntax error near unexpected token `newline'
sh: -c: line 1: `/usr/sbin/serveradmin command << '
Problem executing /usr/sbin/serveradmin command << , return status was: 2

Any ideas?