LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   monitoring directory space usage (https://www.linuxquestions.org/questions/linux-software-2/monitoring-directory-space-usage-898261/)

unkie888 08-19-2011 06:02 AM

monitoring directory space usage
 
Code:

du
is a great utility but when dealing with very large filesystems it can take a long time to run.

is there a daemon i can run that will monitor disk usage of select directories, so that i can have real-time or near-real-time stats?

tronayne 08-19-2011 07:15 AM

You might want to look at the stat utility, possibly with some of its options (see the manual page).

stat dirname will show you, for example
Code:

stat /spares/gnis
  File: `/spares/gnis'
  Size: 12288          Blocks: 24        IO Block: 4096  directory
Device: 809h/2057d      Inode: 5505025    Links: 2                                               
Access: (0555/dr-xr-xr-x)  Uid: (    0/    root)  Gid: (    0/    root)                           
Access: 2011-08-19 07:56:49.657805813 -0400                                                       
Modify: 2007-12-03 17:28:31.000000000 -0500                                                       
Change: 2011-03-14 16:55:16.443481961 -0400                                                       
 Birth: -

On the other hand, if you look at man 2 stat you might be able to roll-your-own utility customized for your needs; here is an example, named status.c, that may be useful:
Code:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <grp.h>
#include <pwd.h>
#include <time.h>

main    (int argc, char *argv[])
{
        time_t  now;
        struct  group  *grentry, *getgrgid (gid_t);
        struct  passwd  *pwentry, *getpwuid (uid_t);
        struct  stat    status;
        struct  tm      *tstr, *localtime (const time_t *);

        if (argc != 2) {
                (void) fprintf (stderr, "usage:\t%s file\n", argv [0]);
                return (1);
        }
        /*      get stat structure for specified file  */
        if (stat (argv [1], &status) == -1) {
                (void) fprintf (stderr, "%s:\tcan't stat %s\n",
                    argv [0], argv [1]);
                return (2);
        }
        /*      display the type of file        */
        if (status.st_mode & S_IFREG)
                (void) fprintf (stdout, "Regular File:\t");
        else if (status.st_mode & S_IFDIR)
                (void) fprintf (stdout, "Directory:\t");
        else if (status.st_mode & S_IFIFO)
                (void) fprintf (stdout, "FIFO Special File:\t");
        else if (status.st_mode & S_IFCHR)
                (void) fprintf (stdout, "Character Special File:\t");
        else if (status.st_mode & S_IFBLK)
                (void) fprintf (stdout, "Block Special File:\t");
        else if (status.st_mode & S_IFLNK)
                (void) fprintf (stdout, "Symbolic Link:\t");
        else
                (void) fprintf (stdout, "Unknown File:\t");
        (void) fprintf (stdout, "%s\n", argv [1]);
        (void) fprintf (stdout, "Inode:\t\t%d\n", (int) status.st_ino);
        (void) fprintf (stdout, "Mode:\t\t%d\n", (int) status.st_mode);
        (void) fprintf (stdout, "Device:\t\t%d\n", (int) status.st_dev);
        (void) fprintf (stdout, "Blocksize:\t%d\n", (int) status.st_blksize);
        (void) fprintf (stdout, "Links:\t\t%d\n", (int) status.st_nlink);
        (void) fprintf (stdout, "Owner (%d):\t", (int) status.st_uid);
        /*      look up owner in /etc/passwd    */
        if ((pwentry = getpwuid (status.st_uid)) == (struct passwd *) NULL) {
                (void) fprintf (stdout, "not found\n");
        } else {
                (void) fprintf (stdout, "%s\n", pwentry->pw_name);
        }
        /*      look up group name in /etc/group        */
        (void) fprintf (stdout, "Group (%d):\t", (int) status.st_gid);
        if ((grentry = getgrgid (status.st_gid)) == (struct group *) NULL) {
                (void) fprintf (stdout, "no found\n");
        } else {
                (void) fprintf (stdout, "%s\n", grentry->gr_name);
        }
        (void) fprintf (stdout, "Size:\t\t%d\n", (int) status.st_size);
        now = status.st_atime;
        tstr = localtime (&now);
        (void) fprintf (stdout, "atime:\t\t%02d:%02d:%02d %02d/%02d/%02d\t",
            tstr->tm_hour, tstr->tm_min, tstr->tm_sec,
            tstr->tm_mon+1, tstr->tm_mday, tstr->tm_year);
        (void) fprintf (stdout, "(Last Access)\n");
        now = status.st_ctime;
        tstr = localtime (&now);
        (void) fprintf (stdout, "ctime:\t\t%02d:%02d:%02d %02d/%02d/%02d\t",
            tstr->tm_hour, tstr->tm_min, tstr->tm_sec,
            tstr->tm_mon+1, tstr->tm_mday, tstr->tm_year);
        (void) fprintf (stdout, "(Last Data Modification)\n");
        now = status.st_mtime;
        tstr = localtime (&now);
        (void) fprintf (stdout, "mtime:\t\t%02d:%02d:%02d %02d/%02d/%02d\t",
            tstr->tm_hour, tstr->tm_min, tstr->tm_sec,
            tstr->tm_mon+1, tstr->tm_mday, tstr->tm_year);
        (void) fprintf (stdout, "(Last File Status Change)\n");
        return (0);
}

You could save the above as, say, status.c then enter
Code:

make status
to compile it -- execute it with
Code:

status name
where name is a file or directory. And, of course, you can modify the above as you see fit.

You might also consider using the df utility on the disk partition you're concerned about; if you see growth in the partition that would probably be an indication that you need to take some action.

Hope this helps some.


All times are GMT -5. The time now is 06:47 AM.