Automatically disconnecting disabled/asleep AFP sessions.

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:
- Issue the serveradmin command 'getConnectedUsers' to the AFP service to build up an array of all current AFP sessions
- Check each session, and build up an array of all current disabled/asleep sessions
- Check each disabled/asleep session and build up an array of all that have been idle over a certain period of time
- Issue the serveradmin command 'disconnectUsers' to the AFP service for each such session
- 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....
