/* $Id$ */ /* * Copyright (c) 2005 Dimitri Sokolyuk * * 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 #include #include #include #include #include static const struct { char *name; char *color; } status[] = { {"low", "#00A650"}, {"guarded", "#1B62B7"}, {"elevated", "#FEDD03"}, {"high", "#F57215"}, {"severe", "#ED192D"} }; int factor[3] = { 2, 1, 1 }; 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], threshold; int i, n, cpu; cpu = ncpu(); threshold = log(cpu) + 1; printf("Content-Type: text/html\n\n\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("\n"); printf("load %.2f %s\n", avg[0], status[n].name); printf("\n"); printf("\n"); printf("\n\n"); printf("\n", cpu); printf("\n", threshold); printf("\n"); printf("\n", status[n].color, status[n].name); printf("
cpu%d
threshold%.2f
averages"); for (i = 0; i < 3; i++) { printf("%.2f", avg[i]); if (i < 2) printf(", "); } printf("
status%s
\n\n"); } printf("\n"); exit(0); }