/* $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 static const struct { char *name; char *color; } status[] = { {"green", "#008B00"}, {"yellow", "#CDCD00"}, {"orange", "#FFA500"}, {"red", "#FF0000"} }; 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 theshold, i, n; threshold = ncpu(); printf("Content-Type: text/html\n\n\n"); if (getloadavg(avg, 3) == -1) printf("no load average information available\n"); else { n = 0; for (i = 0; i < 3; i++) { if (avg[i] > threshold) n++; } printf("\nload %.2f %s\n\n", avg[0], status[n].name); printf("\ncpu number: %d
\nload averages:", threshold); for (i = 0; i < 3; i++) { if (i > 0) printf(","); printf(" %.2f", avg[i]); } printf("
\nload status: %s\n" "\n", status[n].color, status[n].name); } printf("\n"); exit(0); }