⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mmstat.c

📁 mmstatd包含一个 C库和服务器
💻 C
字号:
/* $Id: mmstat.c,v 1.4 2003/01/09 08:07:08 mmondor Exp $ *//* * Copyright (C) 2002-2003, Matthew Mondor * 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. All advertising materials mentioning features or use of this software *    must display the following acknowledgement: *      This product includes software written by Matthew Mondor. * 4. The name of Matthew Mondor may not be used to endorse or promote *    products derived from this software without specific prior written *    permission. * * THIS SOFTWARE IS PROVIDED BY MATTHEW MONDOR ``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 MATTHEW MONDOR 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. *//* HEADERS */#include <sys/types.h>#include <stdlib.h>#include <stdio.h>#include <time.h>#include <mmtypes.h>#include <mmstat.h>#include <mmstring.h>#include <mmlist.h>MMCOPYRIGHT("@(#) Copyright (c) 2002-2003\n\\tMatthew Mondor. All rights reserved.\n");MMRCSID("$Id: mmstat.c,v 1.4 2003/01/09 08:07:08 mmondor Exp $");/* STRUCTURES */struct entnode {    node nod;    MMSTATENT ent;};/* PROTOTYPES */int main(int, char **);static void usage(void);static int stat_report(int, char **);static int stat_hreport(int, char **);static int stat_reset(int, char **);static int stat_update(int, char **);static int stat_delete(int, char **);static int stat_rotate(int, char **);static int compar_entnode(const void *, const void *);/* FUNCTIONS */intmain(int argc, char **argv){    int ret = -1;    if (argc >= 2) {	if (!mm_stricmp(argv[1], "report")) ret = stat_report(argc, argv);	else if (!mm_stricmp(argv[1], "hreport"))	    ret = stat_hreport(argc, argv);	else if (!mm_stricmp(argv[1], "reset")) ret = stat_reset(argc, argv);	else if (!mm_stricmp(argv[1], "update")) ret = stat_update(argc, argv);	else if (!mm_stricmp(argv[1], "delete")) ret = stat_delete(argc, argv);	else if (!mm_stricmp(argv[1], "rotate")) ret = stat_rotate(argc, argv);	else usage();    } else usage();    return (ret);}staticvoid usage(void){    fprintf(stderr, "\nUsage:\n");    fprintf(stderr, "mmstat [h]report [<key|pattern>]\n");    fprintf(stderr,	    "mmstat reset <p|v[a]> <key|pattern>%%<value> \[<key|pattern>%%<value> ...]\n");    fprintf(stderr,	    "mmstat update <p|v[a]> <key|pattern>%%<value> \[<key|pattern>%%<value> ...]\n");    fprintf(stderr, "mmstat delete <key|pattern> [<key|pattern> ...]\n");    fprintf(stderr, "mmstat rotate <pattern> <prefix>\n\n");}staticint stat_report(int argc, char **argv){    list *lst;    struct entnode *entnod;    MMSTATRES *res;    MMSTATENT *ent;    int ret = 0;    char key[KEY_SIZE];    if (argc == 2 || argc == 3) {	mm_memclr(key, KEY_SIZE);	if (argc == 3) mm_strncpy(key, argv[2], KEY_SIZE - 1);	if ((res = mmstat_report(key))) {	    /* Cache all results as fast as possible to free the librarian */	    if ((lst = openlist(malloc, free, sizeof(struct entnode), 8192,			    0))) {		while ((ent = mmstat_nextres(res))) {		    if ((entnod = (struct entnode *)allocnode(lst, FALSE))) {			mm_memcpy(&entnod->ent, ent, sizeof(MMSTATENT));			APPENDNODE(lst, (node *)entnod);		    } else {			lst = closelist(lst);			break;		    }		}	    }	    mmstat_freeres(res);	    if (lst) {		/* Now output list of results to stdout, which could be piped		 * and processed at an arbitrary rate without hogging the		 * librarian.		 */		for (entnod = (struct entnode *)lst->top; entnod;			entnod = (struct entnode *)entnod->nod.next) {		    ent = &entnod->ent;		    printf("%c %d %ld %ld %lld %s\n",			   ent->persistant ? 'p' : 'v', ent->uid,			   (long)ent->created, (long)ent->modified,			   ent->value, ent->key);		}		closelist(lst);	    } else {		printf("Error allocating memory for results\n");		ret = -1;	    }	} else {	    printf("Error connecting to mmstatd\n");	    ret = -1;	}    } else {	usage();	ret = -1;    }    return (ret);}staticint stat_hreport(int argc, char **argv){    list *lst;    struct entnode *entnod;    MMSTATRES *res;    MMSTATENT *ent;    int ret = 0;    char key[KEY_SIZE];    if (argc == 2 || argc == 3) {	mm_memclr(key, KEY_SIZE);	if (argc == 3) mm_strncpy(key, argv[2], KEY_SIZE - 1);	if ((res = mmstat_report(key))) {	    /* Cache all results as fast as possible to free the librarian */	    if ((lst = openlist(malloc, free, sizeof(struct entnode), 8192,			    0))) {		while ((ent = mmstat_nextres(res))) {		    if ((entnod = (struct entnode *)allocnode(lst, FALSE))) {			mm_memcpy(&entnod->ent, ent, sizeof(MMSTATENT));			APPENDNODE(lst, (node *)entnod);		    } else {			lst = closelist(lst);			break;		    }		}	    }	    mmstat_freeres(res);	    if (lst) {		/* Now output list of results to stdout, which could be piped		 * and processed at an arbitrary rate without hogging the		 * librarian.		 */		/* We want to sort the results by key name; We thus need to		 * setup an index array.		 */		struct entnode **index;		struct tm modifiedt;		char modified[15];		if ((index = malloc(sizeof(struct entnode *) *				(lst->nodes + 1))) != NULL) {		    int i;		    for (entnod = (struct entnode *)lst->top, i = 0; entnod;			    entnod = (struct entnode *)entnod->nod.next)			index[i++] = entnod;		    index[i] = NULL;		    qsort(index, lst->nodes, sizeof(struct entnode *),			    compar_entnode);		    for (i = 0; index[i] != NULL; i++) {			ent = &(index[i])->ent;			gmtime_r(&ent->modified, &modifiedt);			strftime(modified, 14, "%y%m%d.%H%M%S", &modifiedt);			printf("%10lld %s %s\n", ent->value, modified,				ent->key);		    }		    free(index);		}		closelist(lst);	    } else {		printf("Error allocating memory for results\n");		ret = -1;	    }	} else {	    printf("Error connecting to mmstatd\n");	    ret = -1;	}    } else {	usage();	ret = -1;    }    return (ret);}staticint stat_reset(int argc, char **argv){    MMSTAT mms;    int i, ret = 0;    char *arg, *args[3];    bool persistant = FALSE, autoflush = FALSE, ok = TRUE;    if (argc > 3) {	if (mm_strchr(argv[2], 'p')) persistant = TRUE;	if (mm_strchr(argv[2], 'v')) persistant = FALSE;	if (mm_strchr(argv[2], 'a')) autoflush = TRUE;	if (mmstat_init(&mms, persistant, autoflush)) {	    mmstat_transact(&mms, TRUE);	    for (i = 3; i < argc; i++) {		arg = argv[i];		if (mm_strspl(args, arg, 2, '%') == 2)		    mmstat(&mms, STAT_RESET, atol(args[1]), "%s", args[0]);	    }	    if (!mmstat_transact(&mms, FALSE)) {		printf("Error sending request to mmstatd\n");		ret = -1;	    }	} else {	    printf("Error opening mmstatd handler\n");	    ret = -1;	}    } else	ok = FALSE;    if (!ok) {	usage();	ret = -1;    }    return (ret);}static intstat_update(int argc, char **argv){    MMSTAT mms;    int i, ret = 0;    char *arg, *args[3];    bool persistant = FALSE, autoflush = FALSE, ok = TRUE;    if (argc > 3) {	if (mm_strchr(argv[2], 'p')) persistant = TRUE;	if (mm_strchr(argv[2], 'v')) persistant = FALSE;	if (mm_strchr(argv[2], 'a')) autoflush = TRUE;	if (mmstat_init(&mms, persistant, TRUE)) {	    mmstat_transact(&mms, TRUE);	    for (i = 3; i < argc; i++) {		arg = argv[i];		if (mm_strspl(args, arg, 2, '%') == 2)		    mmstat(&mms, STAT_UPDATE, atol(args[1]), "%s", args[0]);	    }	    if (!mmstat_transact(&mms, FALSE)) {		printf("Error sending request to mmstatd\n");		ret = -1;	    }	} else {	    printf("Error opening mmstatd handler\n");	    ret = -1;	}    } else	ok = FALSE;    if (!ok) {	usage();	ret = -1;    }    return (ret);}static intstat_delete(int argc, char **argv){    MMSTAT mms;    int i, ret = 0;    if (argc > 2) {	if (mmstat_init(&mms, TRUE, TRUE)) {	    mmstat_transact(&mms, TRUE);	    for (i = 2; i < argc; i++)		mmstat(&mms, STAT_DELETE, 0, "%s", argv[i]);	    if (!mmstat_transact(&mms, FALSE)) {		printf("Error sending request to mmstatd\n");		ret = -1;	    }	} else {	    printf("Error opening mmstatd handler\n");	    ret = -1;	}	exit(0);    } else {	usage();	ret = -1;    }    return (ret);}static intstat_rotate(int argc, char **argv){    int ret = 0;    if (argc == 4) {	if (!mmstat_rotate(argv[2], argv[3])) {	    printf("Error sending request to mmstatd\n");	    ret = -1;	}    } else {	usage();	ret = -1;    }    return (ret);}static intcompar_entnode(const void *from, const void *to){    const struct entnode **f = (const struct entnode **)from,	**t = (const struct entnode **)to;    return (mm_strcmp((*f)->ent.key, (*t)->ent.key));}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -