Test to test the number of hard links

Issue #1074 resolved
Lutz Mader created an issue

Hello Tildeslash.

In some directories used to exchange data to many directories are created sometimes and I get the error "Too many links".

Monit support some file and directory attribute test like timestamps and file size but does not support a test of the used hard links. Monit can not find problems based on the used hard links therefore.

In some filesystems the max number of hard links is very large, but in some it is not. Only 32767 hard links are supported. Therefore the number of directories in a directory is limited also. Based on the tests in "src/validate.c", a test similar to the "st_size" test to test the "st_nlink" attribute seems to be useful too, the data is already available in "stat_buf" but not used.

From my point of view, a test of the number of hard links resp. the number of directories is useful for directories (and files).

A suggestion only,
Lutz

p.s.
OS FS max hard links
AIX jfs 32767
Linux ext3 65000
Linux ext4 65000
Linux vxfs 32767
MacOS hfs large
MacOS apfs large

Comments (10)

  1. Lutz Mader reporter

    Hello Tildeslash,
    based on my suggested enhancement request I add the following additional test to monit.

    NLINK TEST
    
    The nlink statement may be used in a check file, fifo or directory
    service entry. If specified in the control file, Monit will
    compute the number of hard links for a file, fifo or directory.
    
    Testing a specific value:
    
     IF NLINK [[operator] value] THEN action
    
    Testing nlink changes:
    
     IF CHANGED NLINK THEN action
    
    operator is a choice of "<", ">", "!=", "==" in C notation,
    "GT", "LT", "EQ", "NE" in shell sh notation and "GREATER",
    "LESS", "EQUAL", "NOTEQUAL" in human readable form (if not
    specified, default is EQUAL).
    
    value is a watermark.
    
    action is a choice of "ALERT", "RESTART", "START", "STOP",
    "EXEC" or "UNMONITOR".
    
    For example to send an alert if the used number of hard links in a
    directory is too large:
    
     check directory upload with path /data/upload
           if nlink > 32700 then alert
    

    I will spend some time for testing now, but in general the changes are working on Linux and MacOS.

    With regards,
    Lutz

    p.s.

    I pushed the changes to my repository, see https://bitbucket.org/lutzmad/monit/branch/issue_1074

  2. Tildeslash repo owner

    Thanks Lutz, the changes looks good.

    I’d suggest to change the “nlink” to either just “links” or “hardlinks”, to match the “ls” output. Although there is already the “LINK” keyword used in the network interface context, i think the difference will be clear int he context.

    The directory link count is a special case though: it corresponds to the number of subdirectories (minus 2). Although the “ls” also uses a term “link”, and it’d be clear for the person, who understands the difference, it may be probably more user friendly, if the “nlink” directory test would be replaced by “subdirectories” count test?

  3. Lutz Mader reporter

    Thanks for suggestions.

    it may be probably more user friendly, if the “nlink” directory test would be replaced by “subdirectories” count test?

    In MacOS the number of hard links, "st_nlinks" is the number of all files and folder in a folder. Unfortunately hfs/apfs does not handle hard/soft links like the most Unix/Linux file systems.

    I’d suggest to change the “nlink” to either just “links” or “hardlinks”, to match the “ls” output.

    I will change "nlink(s)" to "hardlink(s)".

    Additional folder checks like the "number of files" or the "number of folder" ("subdirectories") are "nice to have" too. But this data is not available in the stats data like "st_nlinks" or "st_size" (for files).

    We will see, I will be back soon,
    Lutz

  4. Lutz Mader reporter

    Hello,
    the pull request #124 "Additional hardlink test for check file, fifo or directory service entries" was added.

    My suggestion for adding the hardlink test enhancement,
    Lutz

  5. Tildeslash repo owner

    fixed Issue #1074: Added a hardlink test to file, directory and fifo checks. Thanks to Lutz Mader. Example: check directory upload with path /data/upload if hardlink > 32000 then alert

    → <<cset cd05118f9ba7>>

  6. Log in to comment