views:

313

answers:

4

How can I poll disk activity in Applescript? Check to see if disk X is being read, written, or idle every N seconds and do something.

+1  A: 

In general, polling is less efficient than being notified when something happens. Additionally, if you're checking whether something is reading from a disk, you will probably be accessing said disk yourself, possibly influencing what you're trying to observe.

Since 10.5, OSX includes something called the File System Events framework, which provides course-grained notifications of changes to the file system. The problem in your case is that this is Objective-C only. Apple has some nice documentation about this API.

Fortunately, there is also the call method AppleScript command. This allows you to work with Objective-C objects from within AppleScript. Here's the documentation on that.

I have no experience with either, hence the documentation references. Hopefully, this should get you going.

jackrabbit
A: 

You could run the terminal command iostat periodically. You'd have to parse the results into a form you could digest.

If you know enough about various UNIX command line tools, I'd suggest iostat piping the output to awk or sed to extract just the information you want.

Mike Heinz
A: 

You should really look at Dtrace. It has the ability to do this sort of thing.

#!/usr/sbin/dtrace -s
/*
 * bitesize.d - analyse disk I/O size by process.
 *              Written using DTrace (Solaris 10 build 63).
 *
 * This produces a report for the size of disk events caused by 
 * processes. These are the disk events sent by the block I/O driver.
 *
 * If applications must use the disks, we generally prefer they do so
 * sequentially with large I/O sizes. 
 *
 * 15-Jun-2005, ver 1.00
 *
 * USAGE:   bitesize.d # wait several seconds, then hit Ctrl-C
 *
 * FIELDS:
 *   PID process ID
 *   CMD command and argument list
 *   value size in bytes
 *   count number of I/O operations
 *
 * NOTES: 
 * The application may be requesting smaller sized operations, which
 * are being rounded up to the nearest sector size or UFS block size.
 * To analyse what the application is requesting, DTraceToolkit programs
 * such as Proc/fddist may help.
 *
 * SEE ALSO: seeksize.d, iosnoop
 *
 * Standard Disclaimer: This is freeware, use at your own risk.
 *
 * 31-Mar-2004  Brendan Gregg Created this, build 51.
 * 10-Oct-2004     "      " Rewrote to use the io provider, build 63.
 */

#pragma D option quiet

/*
 * Print header
 */
dtrace:::BEGIN
{
    printf("Sampling... Hit Ctrl-C to end.\n");
}

/*
 * Process io start
 */
io:::start
{
    /* fetch details */
    this->size = args[0]->b_bcount;
    cmd = (string)curpsinfo->pr_psargs;

    /* store details */
    @Size[pid,cmd] = quantize(this->size);
}

/*
 * Print final report
 */
dtrace:::END 
{
    printf("\n%8s  %s\n","PID","CMD");
    printa("%8d  %s\n%@d\n",@Size);
}

From here.

To run use

sudo dtrace -s bitsize.d
Milhous
A: 

As Porkchop D. Clown mentioned, you can use iostat. A command you could use is:

 iostat -c 50 -w 5

Which will run iostat 50 times every 5 seconds.

Darryl Hein