McMahon has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks...

Googling and CPAN turned up not much, so I wonder if this is possible: is there some way of measuring the extent of disk fragmentation on Windows machines using Perl?

I realize that measuring fragmentation is troublesome-- I'm just looking for a place to start-- hopefully with Perl.

-Chris
  • Comment on measure Win32 disk fragmentation with Perl?

Replies are listed 'Best First'.
Re: measure Win32 disk fragmentation with Perl?
by meredith (Friar) on Apr 26, 2004 at 18:43 UTC

    AFAIK, there are no modules that will do that for either FAT or NTFS volumes. I thought maybe the Disk Defragmenter in Windows 2000 might be available over WMI (Windows Management Instrumentation), but that proved false. If you have a Windows Server 2003 machine, this info may be useful: Win32_Volume Class. Other than that, I don't see any automated options for you. Sorry -- I tried. :)



    mhoward - at - hattmoward.org
      Other places to look (if absolutely needed) include SNMP .. or raw Win32 API ...

      Or switch to Linux and stop worrying about fragmentation :)

        Or switch to Linux and stop worrying about fragmentation :)

        To be fair, NTFS is pretty good at handling fragmentation. It's that old simple-stupid FAT fs you have to worry about. No sane admin should be running a server using primarily FAT. Few desktop users running 2k or XP should need it, too.

        ----
        : () { :|:& };:

        Note: All code is untested, unless otherwise stated

Re: measure Win32 disk fragmentation with Perl?
by paulbort (Hermit) on Apr 26, 2004 at 19:15 UTC
    Assuming you are at at least Windows 2000, SysInternals has a sample defrag with API explanation at their site.

    They tend to have lots of good low-level Win32 hacking tools.

    --
    Spring: Forces, Coiled Again!
      Thanks paulbort!

      SysInternals has a tool called "contig" that was close enough that with a little Perl, I could make it Do The Right Thing.

      contig reports the number of fragments that comprise each file in a directory. It's normal output was surprisingly unreliable-- it skipped files apparently randomly sometimes-- but the verbose output seems to be more consistent. Also, it doesn't run continuously. In order to get a picture of fragmentation over time, the user has to run contig manually over and over.

      So I wrote a little controller script in Perl that runs contig every 5 seconds in verbose/recursive/analyze mode, parses the output, and writes the interesting parts to an output file. Here's the script:

      use warnings; use strict; open OUT, ">out.txt"; while (1) { $| = 1; my @out = qx/contig.exe -a -s -v *"/; foreach my $line(@out) { if ($line =~ "is in") { print OUT $line; } } sleep 5; print OUT "\n";


      I was pleasantly surprised to find that @out actually contained what I wanted it to. Made parsing the output really easy.
Re: measure Win32 disk fragmentation with Perl?
by CountZero (Bishop) on Apr 26, 2004 at 19:01 UTC
    I haven't looked at this for years, but if the Windows file-system is not fundamentally different from what it was in the good'ol days of floppy disks and DOS, you need to do a low level read of the sectors where the directory and file structure is kept and then for each file traverse the linked lists which map the clusters where the data is stored and somehow calculate the amount of dispersion of the files over the disk.

    All pretty ugly stuff at a low level and most certainly ideal to totally mess up your disks.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: measure Win32 disk fragmentation with Perl?
by jayrom (Pilgrim) on Apr 26, 2004 at 19:02 UTC
    I am not sure this is possible, but here's a useful site for using Perl on win32. Check out the Administrator's Handbook. jayrom