/*- * Copyright (c) 2006 Niki Denev , * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* * $Id: qstat-gid.c,v 1.14 2006/11/23 09:53:05 tormentor Exp $ * (c) 2003-2005 by Nikolay Denev * Qstat-Gid - extended per GID queue stats * * gcc -Wall -O2 qstat-gid.c -o qstat-gid * install -o qmailq -g $support -m 4110 qstat-gid /usr/local/bin/qstat-gid * */ #include #include #include #include #include #include #include #include #include #include #include #define MAX_UID 65535 #define MAX_PATH 256 #define MSG_QUEUE "/var/qmail/queue" int main() { static char qq[] = MSG_QUEUE; static char grp_unknown[] = "*unknown*"; int i; char md[MAX_PATH]; unsigned int gid_queue[MAX_UID]; unsigned int gid_todo[MAX_UID]; unsigned int queue = 0; unsigned int todo = 0; char *gname; DIR *dir; struct dirent *mf; struct stat mfs; struct group *grp; bzero(gid_queue, sizeof(unsigned int) * MAX_UID); bzero(gid_todo, sizeof(unsigned int) * MAX_UID); for (i = 0; i <= 22; i++) { snprintf(md, MAX_PATH, "%s/mess/%d", qq, i); if ((dir = opendir(md)) == NULL) { fprintf(stderr, "%s : %s\n", strerror(errno), md); exit(-1); } if ((chdir(md) == -1)) { fprintf(stderr, "%s : %s\n", strerror(errno), md); exit(-1); } while ((mf = readdir(dir)) > 0) { if (mf->d_name[0] == '.') continue; if (stat(mf->d_name, &mfs) < 0) continue; gid_queue[(int)mfs.st_gid]++; queue++; } closedir(dir); } snprintf(md, MAX_PATH, "%s/todo", qq); if ((dir = opendir(md)) == NULL) { fprintf(stderr, "%s : %s\n", strerror(errno), md); exit(-1); } if ((chdir(md) == -1)) { fprintf(stderr, "%s : %s\n", strerror(errno), md); exit(-1); } while ((mf = readdir(dir)) > 0) { if (mf->d_name[0] == '.') continue; if (stat(mf->d_name, &mfs) < 0) continue; gid_todo[(int)mfs.st_gid]++; todo++; } closedir(dir); printf(" $Id: qstat-gid.c,v 1.14 2006/11/23 09:53:05 tormentor Exp $\n"); printf("+---------------------+-------+---------+---------+---------+\n"); printf("| Group | Gid | Queue | To-do | Total |\n"); printf("+---------------------+-------+---------+---------+---------+\n"); for (i = 0; i < MAX_UID; i++) { if ((gid_queue[i] == 0) && (gid_todo[i] == 0)) continue; if ((grp = getgrgid(i))) { gname = grp->gr_name; } else { gname = grp_unknown; } printf("| %19s | %5d | %7d | %7d | %7d |\n", gname, i, gid_queue[i], gid_todo[i], gid_queue[i] + gid_todo[i] ); } printf("+---------------------+-------+---------+---------+---------+\n"); printf("| TOTALS | %7d | %7d | %7d |\n", queue, todo, queue + todo); printf("+-----------------------------+---------+---------+---------+\n"); return(0); }