summaryrefslogtreecommitdiff
path: root/lavg/lavg.c
blob: d9420020b22443a3a2688d48fc604da2b7790543 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* $Id$ */
/*
 * Copyright (c) 2005 Dimitri Sokolyuk <demon@vhost.dyndns.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef lint
static const char rcsid[] = "$Id$";
#endif /* not lint */

#include <sys/param.h>
#include <sys/sysctl.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>

static const struct {
	char *name;
	char *color;
} status[] = {
	{"low",      "#00A650"},
	{"guarded",  "#1B62B7"},
	{"elevated", "#FEDD03"},
	{"high",     "#F57215"}
	{"severe",   "#ED192D"}
};

int	factor[3] = { 1, 1, 2 };

int
ncpu(void)
{
	int mib[2], n;
	size_t len;

	mib[0] = CTL_HW;
	mib[1] = HW_NCPU;
	len = sizeof(n);

	if (sysctl(mib, 2, &n, &len, NULL, 0) == -1)
		err(1, "sysctl");

	return n;
}

int
main(void)
{
	double avg[3];
	int threshold, i, n;

	threshold = ncpu();
	
	printf("Content-Type: text/html\n\n<html>\n");

	if (getloadavg(avg, 3) == -1)
		printf("no load average information available\n");
	else {
		for (n = 0, i = 0; i < 3; i++) {
			if (avg[i] > threshold)
				n += factor[i];
		}

		printf("<head>\n<title>load %.2f %s</title>\n</head>\n",
		    avg[0], status[n].name);
		printf("<body>\ncpu number: %d<br>\nload averages:", threshold);
		for (i = 0; i < 3; i++) {
			if (i > 0)
				printf(",");
			printf(" %.2f", avg[i]);
		}

		printf("<br>\nload status: <font color=\"%s\">%s</font>\n"
				"</body>\n", status[n].color, status[n].name);
	}
	printf("</html>\n");

	exit(0);
}