From 1ef35c5e38f2b78d6bddf922b0ee3d4e1c05b5db Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Thu, 17 Mar 2005 16:40:36 +0000 Subject: CGI: load average --- lavg/Makefile | 7 ++++++ lavg/lavg.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 lavg/Makefile create mode 100644 lavg/lavg.c diff --git a/lavg/Makefile b/lavg/Makefile new file mode 100644 index 0000000..9bdb443 --- /dev/null +++ b/lavg/Makefile @@ -0,0 +1,7 @@ +# $Id$ + +PROG= lavg +CFLAGS+= -Wall +NOMAN= + +.include diff --git a/lavg/lavg.c b/lavg/lavg.c new file mode 100644 index 0000000..b587d57 --- /dev/null +++ b/lavg/lavg.c @@ -0,0 +1,70 @@ +/* $Id$ */ +/* + * Copyright (c) 2005 demon + * + * 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 + +#define THRESHOLD 1 + +static const struct { + char *name; + char *color; +} status[] = { + {"green", "#008B00"}, + {"yellow", "#CDCD00"}, + {"orange", "#FFA500"}, + {"red", "#FF0000"} +}; + +#define NELEM 3 + +int +main(void) +{ + double avg[NELEM]; + int i, n; + + printf("Content-Type: text/html\n\n\n"); + + if (getloadavg(avg, NELEM) == -1) + printf("no load average information available\n"); + else { + n = 0; + for (i = 0; i < NELEM; i++) { + if (avg[i] > THRESHOLD) + n++; + } + + printf("\nload %.2f %s\n\n\n" + "load averages:", avg[0], status[n].name); + for (i = 0; i < NELEM; 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); +} -- cgit v1.2.3