[Xymon] file counts
Richard L. Hamilton
rlhamil2 at gmail.com
Fri Mar 3 19:25:42 CET 2017
The following should count directory entries immediately within a given directory, excluding "." and ".." if present (not all filesystems list those explicitly, although they still otherwise work as expected). If compiled with -DTEST, it also provides a main() function to exercise the file counting function. It compiles without warnings and appears to work as expected on at least OS X 10.11.6 (El Capitan), Ubuntu 16.04.2 LTS (Xenial Xerus), and Solaris 9. I checked man pages for various functions and include files rather than going from memory, to be reasonably sure I was doing everything the proper way, so that it would be as portable as possible. The actual function is int countfiles(const char *dirpath), and returns the number of files, or -1 in event of error (with errno reflecting the error that caused it to fail, whether opening the directory, or occasionally some problem reading the directory). There's probably a thing or two I could have done better (a static const int for -1 rather than two uses of the actual constant might have saved four bytes), but other than that, it's pretty clean, IMO.
#include <stdlib.h>
#include <errno.h>
#include <dirent.h>
#include <string.h>
#ifdef TEST
extern int countfiles(const char *);
#include <stdio.h>
int
main(int argc, char **argv)
{
int arg, count, rc = 0;
for (arg = 1; arg < argc; ++arg) {
if ((count = countfiles(argv[arg])) == -1) {
perror(argv[arg]);
rc = 1;
}
else
(void) printf("%s: %d\n", argv[arg], count);
}
return (rc);
}
#endif
int
countfiles(const char *dirpath)
{
int count = 0, errno_save;
DIR *dirp;
struct dirent *dp;
if ((dirp = opendir(dirpath)) == NULL)
return (-1);
errno = 0; /* because readdir() returns NULL on both EOF and error */
while ((dp = readdir(dirp)) !=NULL) {
if (strcmp(dp->d_name, ".") == 0 ||
strcmp(dp->d_name, "..") == 0)
continue;
++count;
}
if (errno != 0) {
errno_save = errno;
(void) closedir(dirp);
errno = errno_save;
return (-1);
}
(void) closedir(dirp);
return (count);
}
> On Mar 3, 2017, at 09:11, Galen Johnson <Galen.Johnson at sas.com> wrote:
>
> Thanks for the offer, Kenneth. I have a simple script (ls $dir | wc -l) as well but I had thought that it was already a part of the Xymon base. It would be nice to have it as an option to DIR and if I can find the time, I may look into dusting off my C skills and take a stab at it...though using the terms "skills" along side "C" is a stretch for me :-).
>
> =G=
>
> From: Kenneth S. Petersen <ksp at stougaards.dk <mailto:ksp at stougaards.dk>>
> Sent: Friday, March 3, 2017 3:37 AM
> To: Galen Johnson; Galen Johnson
> Cc: xymon at xymon.com <mailto:xymon at xymon.com>
> Subject: SV: [Xymon] file counts
>
> Hi Galen,
> I’m using the count file feature but with a external script on windows along with bbwin 0.13 or 0.12
>
> It a two part script, one cfg file in the bbwin etc folder and one vbs in the bbwin ext folder.
> Then call it from bbwin.cfg
> Actually it’s the bbwin, fsmon.vbs that I have altered so it fits my needs.
>
> Let me know if you want the script.
>
>
>
>
> Med Venlig Hilsen
> Kenneth S. Petersen
> IT Konsulent – Stougaards IT
> Wiuffsvej 3, DK-4540 Fårevejle, Denmark
> Email: info at stougaardsit.dk <mailto:info at stougaardsit.dk> Mob.+45 2163 2325
>
>
> Fra: Xymon [mailto:xymon-bounces at xymon.com <mailto:xymon-bounces at xymon.com>] På vegne af Galen Johnson
> Sendt: 3. marts 2017 01:30
> Til: Galen Johnson <Galen.Johnson at sas.com <mailto:Galen.Johnson at sas.com>>
> Cc: xymon at xymon.com <mailto:xymon at xymon.com>
> Emne: Re: [Xymon] file counts
>
> Apparently, I wasn't the only one with this need. I found a thread from 2011. I really don't want to go that route so consider this a feature request that DIR can support more than just size but file count as well.
>
> =G=
>
> On Thu, Mar 2, 2017 at 2:20 PM, Galen Johnson <Galen.Johnson at sas.com <mailto:Galen.Johnson at sas.com>> wrote:
> Hey,
>
> I could've sworn that Xymon had a way to count the number of files in a particular directory (natively) and you could set up alerts based on number of files. Apparently, I was wrong or I'm not seeing it in the docs. Obviously, I could get clever with file ages but that's just annoying. Am I missing something?
>
> thanks
>
> =G=
>
> _______________________________________________
> Xymon mailing list
> Xymon at xymon.com <mailto:Xymon at xymon.com>
> http://lists.xymon.com/mailman/listinfo/xymon <http://lists.xymon.com/mailman/listinfo/xymon>
>
> _______________________________________________
> Xymon mailing list
> Xymon at xymon.com <mailto:Xymon at xymon.com>
> http://lists.xymon.com/mailman/listinfo/xymon <http://lists.xymon.com/mailman/listinfo/xymon>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.xymon.com/pipermail/xymon/attachments/20170303/fe2c2fdf/attachment.html>
More information about the Xymon
mailing list