Beispiel #1
0
int forks(int argc, char **argv) {
	FILE *f;
	char buff[256];
	if(argc > 1) {
		if(!strcmp(argv[1], "config")) {
			puts("graph_title Fork rate\n"
				"graph_args --base 1000 -l 0 \n"
				"graph_vlabel forks / ${graph_period}\n"
				"graph_category processes\n"
				"graph_info This graph shows the forking rate (new processes started).\n"
				"forks.label forks\n"
				"forks.type DERIVE\n"
				"forks.min 0\n"
				"forks.max 100000\n"
				"forks.info The number of forks per second.");
			print_warncrit("forks");
			return 0;
		}
		if(!strcmp(argv[1], "autoconf"))
			return autoconf_check_readable(PROC_STAT);
	}
	if(!(f=fopen(PROC_STAT, "r")))
		return fail("cannot open " PROC_STAT);
	while(fgets(buff, 256, f)) {
		if(!strncmp(buff, "processes ", 10)) {
			fclose(f);
			printf("forks.value %s", buff+10);
			return 0;
		}
	}
	fclose(f);
	return fail("no processes line found in " PROC_STAT);
}
Beispiel #2
0
int entropy(int argc, char **argv) {
	FILE *f;
	int entropy;
	if(argc > 1) {
		if(!strcmp(argv[1], "config")) {
			puts("graph_title Available entropy\n"
				"graph_args --base 1000 -l 0\n"
				"graph_vlabel entropy (bytes)\n"
				"graph_scale no\n"
				"graph_category system\n"
				"graph_info This graph shows the amount of entropy available in the system.\n"
				"entropy.label entropy\n"
				"entropy.info The number of random bytes available. This is typically used by cryptographic applications.");
			print_warncrit("entropy");
			return 0;
		}
		if(!strcmp(argv[1], "autoconf"))
			return autoconf_check_readable(ENTROPY_AVAIL);
	}
	if(!(f=fopen(ENTROPY_AVAIL, "r")))
		return fail("cannot open " ENTROPY_AVAIL);
	if(1 != fscanf(f, "%d", &entropy)) {
		fclose(f);
		return fail("cannot read from " ENTROPY_AVAIL);
	}
	fclose(f);
	printf("entropy.value %d\n", entropy);
	return 0;
}
Beispiel #3
0
int open_inodes(int argc, char **argv)
{
	FILE *f;
	int nr, freen;
	if (argc > 1) {
		if (!strcmp(argv[1], "config")) {
			puts("graph_title Inode table usage\n"
			     "graph_args --base 1000 -l 0\n"
			     "graph_vlabel number of open inodes\n"
			     "graph_category system\n"
			     "graph_info This graph monitors the Linux open inode table.\n"
			     "used.label open inodes\n"
			     "used.info The number of currently open inodes.\n"
			     "max.label inode table size\n"
			     "max.info The size of the system inode table. This is dynamically adjusted by the kernel.");
			print_warncrit("used");
			print_warncrit("max");
			return 0;
		}
		if (!strcmp(argv[1], "autoconf"))
			return autoconf_check_readable(FS_INODE_NR);
	}
	if (!(f = fopen(FS_INODE_NR, "r")))
		return fail("cannot open " FS_INODE_NR);
	if (2 != fscanf(f, "%d %d", &nr, &freen)) {
		fclose(f);
		return fail("cannot read from " FS_INODE_NR);
	}
	fclose(f);
	printf("used.value %d\nmax.value %d\n", nr - freen, nr);
	return 0;
}
Beispiel #4
0
int fw_packets(int argc, char **argv) {
	FILE *f;
	char buff[1024], *s;
	if(argc > 1) {
		if(!strcmp(argv[1], "config")) {
			puts("graph_title Firewall Throughput\n"
				"graph_args --base 1000 -l 0\n"
				"graph_vlabel Packets/${graph_period}\n"
				"graph_category network\n"
				"received.label Received\n"
				"received.draw AREA\n"
				"received.type DERIVE\n"
				"received.min 0\n"
				"forwarded.label Forwarded\n"
				"forwarded.draw LINE2\n"
				"forwarded.type DERIVE\n"
				"forwarded.min 0");
			return 0;
		}
		if(!strcmp(argv[1], "autoconf"))
			return autoconf_check_readable(PROC_NET_SNMP);
	}
	if(!(f=fopen(PROC_NET_SNMP, "r")))
		return fail("cannot open " PROC_NET_SNMP);
	while(fgets(buff, 1024, f)) {
		if(!strncmp(buff, "Ip: ", 4) && isdigit(buff[4])) {
			fclose(f);
			if(!(s = strtok(buff+4, " \t")))
				break;
			if(!(s = strtok(NULL, " \t")))
				break;
			if(!(s = strtok(NULL, " \t")))
				break;
			printf("received.value %s\n", s);
			if(!(s = strtok(NULL, " \t")))
				break;
			if(!(s = strtok(NULL, " \t")))
				break;
			if(!(s = strtok(NULL, " \t")))
				break;
			printf("forwarded.value %s\n", s);
			return 0;
		}
	}
	fclose(f);
	return fail("no ip line found in " PROC_NET_SNMP);
}
Beispiel #5
0
int interrupts(int argc, char **argv) {
	FILE *f;
	char buff[256];
	if(argc > 1) {
		if(!strcmp(argv[1], "config")) {
			puts("graph_title Interrupts and context switches\n"
				"graph_args --base 1000 -l 0\n"
				"graph_vlabel interrupts & ctx switches / ${graph_period}\n"
				"graph_category system\n"
				"graph_info This graph shows the number of interrupts and context switches on the system. These are typically high on a busy system.\n"
				"intr.info Interrupts are events that alter sequence of instructions executed by a processor. They can come from either hardware (exceptions, NMI, IRQ) or software.");
			puts("ctx.info A context switch occurs when a multitasking operatings system suspends the currently running process, and starts executing another.\n"
				"intr.label interrupts\n"
				"ctx.label context switches\n"
				"intr.type DERIVE\n"
				"ctx.type DERIVE\n"
				"intr.max 100000\n"
				"ctx.max 100000\n"
				"intr.min 0\n"
				"ctx.min 0");
			print_warncrit("intr");
			print_warncrit("ctx");
			return 0;
		}
		if(!strcmp(argv[1], "autoconf"))
			return autoconf_check_readable(PROC_STAT);
	}
	if(!(f=fopen(PROC_STAT, "r")))
		return fail("cannot open " PROC_STAT);
	while(fgets(buff, 256, f)) {
		if(!strncmp(buff, "intr ", 5)) {
			buff[5 + strcspn(buff + 5, " \t\n")] = '\0';
			printf("intr.value %s\n", buff+5);
		} else if(!strncmp(buff, "ctxt ", 5)) {
			buff[5 + strcspn(buff + 5, " \t\n")] = '\0';
			printf("ctx.value %s\n", buff+5);
		}
	}
	fclose(f);
	return 0;
}
Beispiel #6
0
int open_files(int argc, char **argv) {
	FILE *f;
	int alloc, freeh, avail;
	if(argc > 1) {
		if(!strcmp(argv[1], "config")) {
			if(!(f=fopen(FS_FILE_NR, "r")))
				return fail("cannot open " FS_FILE_NR);
			if(1 != fscanf(f, "%*d %*d %d", &avail)) {
				fclose(f);
				return fail("cannot read from " FS_FILE_NR);
			}
			fclose(f);
			puts("graph_title File table usage\n"
				"graph_args --base 1000 -l 0\n"
				"graph_vlabel number of open files\n"
				"graph_category system\n"
				"graph_info This graph monitors the Linux open files table.\n"
				"used.label open files\n"
				"used.info The number of currently open files.\n"
				"max.label max open files\n"
				"max.info The maximum supported number of open "
					"files. Tune by modifying " FS_FILE_NR
					".");
			printf("used.warning %d\nused.critical %d\n",
					(int)(avail*0.92), (int)(avail*0.98));
			return 0;
		}
		if(!strcmp(argv[1], "autoconf"))
			return autoconf_check_readable(FS_FILE_NR);
	}
	if(!(f=fopen(FS_FILE_NR, "r")))
		return fail("cannot open " FS_FILE_NR);
	if(3 != fscanf(f, "%d %d %d", &alloc, &freeh, &avail)) {
		fclose(f);
		return fail("cannot read from " FS_FILE_NR);
	}
	fclose(f);
	printf("used.value %d\nmax.value %d\n", alloc-freeh, avail);
	return 0;
}
Beispiel #7
0
int if_err_(int argc, char **argv) {
	char *interface;
	size_t interface_len;
	FILE *f;
	char buff[256], *s;
	int i;

	interface = basename(argv[0]);
	if(strncmp(interface, "if_err_", 7) != 0)
		return fail("if_err_ invoked with invalid basename");
	interface += 7;
	interface_len = strlen(interface);

	if(argc > 1) {
		if(!strcmp(argv[1], "autoconf"))
			return autoconf_check_readable(PROC_NET_DEV);
		if(!strcmp(argv[1], "suggest")) {
			if(NULL == (f = fopen(PROC_NET_DEV, "r")))
				return 1;
			while(fgets(buff, 256, f)) {
				for(s=buff;*s == ' ';++s)
					;
				i = 0;
				if(!strncmp(s, "lo:", 3))
					continue;
				if(!strncmp(s, "sit", 3)) {
					for(i=3; xisdigit(s[i]); ++i)
						;
					if(s[i] == ':')
						continue;
				}
				while(s[i] != ':' && s[i] != '\0')
					++i;
				if(s[i] != ':')
					continue; /* a header line */
				s[i] = '\0';
				puts(s);
			}
			fclose(f);
			return 0;
		}
		if(!strcmp(argv[1], "config")) {
			puts("graph_order rcvd trans");
			printf("graph_title %s errors\n", interface);
			puts("graph_args --base 1000\n"
				"graph_vlabel packets in (-) / out (+) per "
					"${graph_period}\n"
				"graph_category network");
			printf("graph_info This graph shows the amount of "
				"errors on the %s network interface.\n",
				interface);
			puts("rcvd.label packets\n"
				"rcvd.type COUNTER\n"
				"rcvd.graph no\n"
				"rcvd.warning 1\n"
				"trans.label packets\n"
				"trans.type COUNTER\n"
				"trans.negative rcvd\n"
				"trans.warning 1");
			print_warncrit("rcvd");
			print_warncrit("trans");
			return 0;
		}
	}
	if(NULL == (f = fopen(PROC_NET_DEV, "r")))
		return 1;
	while(fgets(buff, 256, f)) {
		for(s=buff;*s == ' ';++s)
			;
		if(0 != strncmp(s, interface, interface_len))
			continue;
		s += interface_len;
		if(*s != ':')
			continue;
		++s;

		while(*s == ' ')
			++s;

		for(i=1;i<3;++i) {
			while(xisdigit(*s))
				++s;
			while(xisspace(*s))
				++s;
		}
		for(i=0;xisdigit(s[i]);++i)
			;
		printf("rcvd.value ");
		fwrite(s, 1, i, stdout);
		putchar('\n');
		s += i;
		while(xisspace(*s))
			++s;

		for(i=4;i<11;++i) {
			while(xisdigit(*s))
				++s;
			while(xisspace(*s))
				++s;
		}
		for(i=0;xisdigit(s[i]);++i)
			;
		printf("trans.value ");
		fwrite(s, 1, i, stdout);
		putchar('\n');
	}
	fclose(f);
	return 0;
}
Beispiel #8
0
int cpu(int argc, char **argv) {
    FILE *f;
    char buff[256], *s;
    int ncpu=0, extinfo=0, scaleto100=0, hz;
    if(argc > 1) {
        if(!strcmp(argv[1], "config")) {
            s = getenv("scaleto100");
            if(s && !strcmp(s, "yes"))
                scaleto100=1;

            if(!(f=fopen(PROC_STAT, "r")))
                return fail("cannot open " PROC_STAT);
            while(fgets(buff, 256, f)) {
                if(!strncmp(buff, "cpu", 3)) {
                    if(isdigit(buff[3]))
                        ncpu++;
                    if(buff[3] == ' ') {
                        s = strtok(buff+4, " \t");
                        for(extinfo=1; strtok(NULL, " \t"); extinfo++)
                            ;
                    }
                }
            }
            fclose(f);

            if(ncpu < 1 || extinfo < 4)
                return fail("cannot parse " PROC_STAT);

            puts("graph_title CPU usage");
            if(extinfo >= 7)
                puts("graph_order system user nice idle iowait irq softirq");
            else
                puts("graph_order system user nice idle");
            if(scaleto100)
                puts("graph_args --base 1000 -r --lower-limit 0 --upper-limit 100");
            else
                printf("graph_args --base 1000 -r --lower-limit 0 --upper-limit %d\n", 100 * ncpu);
            puts("graph_vlabel %\n"
                 "graph_scale no\n"
                 "graph_info This graph shows how CPU time is spent.\n"
                 "graph_category system\n"
                 "graph_period second\n"
                 "system.label system\n"
                 "system.draw AREA");
            printf("system.max %d\n", 100 * ncpu);
            puts("system.min 0\n"
                 "system.type DERIVE");
            printf("system.warning %d\n", SYSWARNING * ncpu);
            printf("system.critical %d\n", SYSCRITICAL * ncpu);
            puts("system.info CPU time spent by the kernel in system activities\n"
                 "user.label user\n"
                 "user.draw STACK\n"
                 "user.min 0");
            printf("user.max %d\n", 100 * ncpu);
            printf("user.warning %d\n", USRWARNING * ncpu);
            puts("user.type DERIVE\n"
                 "user.info CPU time spent by normal programs and daemons\n"
                 "nice.label nice\n"
                 "nice.draw STACK\n"
                 "nice.min 0");
            printf("nice.max %d\n", 100 * ncpu);
            puts("nice.type DERIVE\n"
                 "nice.info CPU time spent by nice(1)d programs\n"
                 "idle.label idle\n"
                 "idle.draw STACK\n"
                 "idle.min 0");
            printf("idle.max %d\n", 100 * ncpu);
            puts("idle.type DERIVE\n"
                 "idle.info Idle CPU time");
            if(scaleto100)
                printf("system.cdef system,%d,/\n"
                       "user.cdef user,%d,/\n"
                       "nice.cdef nice,%d,/\n"
                       "idle.cdef idle,%d,/\n", ncpu, ncpu, ncpu, ncpu);
            if(extinfo >= 7) {
                puts("iowait.label iowait\n"
                     "iowait.draw STACK\n"
                     "iowait.min 0");
                printf("iowait.max %d\n", 100 * ncpu);
                puts("iowait.type DERIVE\n"
                     "iowait.info CPU time spent waiting for I/O operations to finish\n"
                     "irq.label irq\n"
                     "irq.draw STACK\n"
                     "irq.min 0");
                printf("irq.max %d\n", 100 * ncpu);
                puts("irq.type DERIVE\n"
                     "irq.info CPU time spent handling interrupts\n"
                     "softirq.label softirq\n"
                     "softirq.draw STACK\n"
                     "softirq.min 0");
                printf("softirq.max %d\n", 100 * ncpu);
                puts("softirq.type DERIVE\n"
                     "softirq.info CPU time spent handling \"batched\" interrupts");
                if(scaleto100)
                    printf("iowait.cdef iowait,%d,/\n"
                           "irq.cdef irq,%d,/\n"
                           "softirq.cdef softirq,%d,/\n", ncpu, ncpu, ncpu);
            }
            if(extinfo >= 8) {
                puts("steal.label steal\n"
                     "steal.draw STACK\n"
                     "steal.min 0");
                printf("steal.max %d\n", 100 * ncpu);
                puts("steal.type DERIVE\n"
                     "steal.info The time that a virtual CPU had runnable tasks, but the virtual CPU itself was not running");
                if(scaleto100)
                    printf("steal.cdef steal,%d,/\n", ncpu);
            }
            if(extinfo >= 9) {
                puts("guest.label guest\n"
                     "guest.draw STACK\n"
                     "guest.min 0");
                printf("guest.max %d\n", 100 * ncpu);
                puts("guest.type DERIVE\n"
                     "guest.info The time spent running a virtual CPU for guest operating systems under the control of the Linux kernel.");
                if(scaleto100)
                    printf("guest.cdef guest,%d,/\n", ncpu);
            }
            return 0;
        }
        if(!strcmp(argv[1], "autoconf"))
            return autoconf_check_readable(PROC_STAT);
    }
    if(!(f=fopen(PROC_STAT, "r")))
        return fail("cannot open " PROC_STAT);
    hz = getenvint("HZ", 100);
    while(fgets(buff, 256, f)) {
        if(!strncmp(buff, "cpu ", 4)) {
            fclose(f);
            if(!(s = strtok(buff+4, " \t")))
                break;
            print_stat_value("user", s, hz);
            if(!(s = strtok(NULL, " \t")))
                break;
            print_stat_value("nice", s, hz);
            if(!(s = strtok(NULL, " \t")))
                break;
            print_stat_value("system", s, hz);
            if(!(s = strtok(NULL, " \t")))
                break;
            print_stat_value("idle", s, hz);
            if(!(s = strtok(NULL, " \t")))
                return 0;
            print_stat_value("iowait", s, hz);
            if(!(s = strtok(NULL, " \t")))
                return 0;
            print_stat_value("irq", s, hz);
            if(!(s = strtok(NULL, " \t")))
                return 0;
            print_stat_value("softirq", s, hz);
            if(!(s = strtok(NULL, " \t")))
                return 0;
            print_stat_value("steal", s, hz);
            if(!(s = strtok(NULL, " \t")))
                return 0;
            print_stat_value("guest", s, hz);
            return 0;
        }
    }
    fclose(f);
    return fail("no cpu line found in " PROC_STAT);
}