/* $Id$ * $URL$ * * plugin for load average * * Copyright (C) 2003 Michael Reinelt * Copyright (C) 2004 The LCD4Linux Team * * This file is part of LCD4Linux. * * LCD4Linux is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * LCD4Linux is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * */ /* * exported functions: * * int plugin_init_loadavg (void) * adds functions for load average * */ #include "config.h" #include #include #include #include #include #include #include #include #include #include "debug.h" #include "plugin.h" #ifndef HAVE_GETLOADAVG static int fd = -2; int getloadavg(double loadavg[], int nelem) { char buf[65], *p; ssize_t nread; int i; if (fd == -2) fd = open("/proc/loadavg", O_RDONLY); if (fd < 0) return -1; lseek(fd, 0, SEEK_SET); nread = read(fd, buf, sizeof buf - 1); if (nread < 0) return -1; buf[nread - 1] = '\0'; if (nelem > 3) nelem = 3; p = buf; for (i = 0; i < nelem; ++i) { char *endp; loadavg[i] = strtod(p, &endp); if (endp == NULL || endp == p) /* This should not happen. The format of /proc/loadavg must have changed. Don't return with what we have, signal an error. */ return -1; p = endp; } return i; } #endif static void my_loadavg(RESULT * result, RESULT * arg1) { static int nelem = -1; int index, age; static double loadavg[3]; static struct timeval last_value; struct timeval now; gettimeofday(&now, NULL); age = (now.tv_sec - last_value.tv_sec) * 1000 + (now.tv_usec - last_value.tv_usec) / 1000; /* reread every 10 msec only */ if (nelem == -1 || age == 0 || age > 10) { nelem = getloadavg(loadavg, 3); if (nelem < 0) { error("getloadavg() failed!"); SetResult(&result, R_STRING, ""); return; } last_value = now; } index = R2N(arg1); if (index < 1 || index > nelem) { error("loadavg(%d): index out of range!", index); SetResult(&result, R_STRING, ""); return; } SetResult(&result, R_NUMBER, &(loadavg[index - 1])); return; } int plugin_init_loadavg(void) { AddFunction("loadavg", 1, my_loadavg); return 0; } void plugin_exit_loadavg(void) { #ifndef HAVE_GETLOADAVG if (fd > 0) close(fd); fd = -2; #endif } f47b31e8cd56108f3f10711f8&showmsg=1&follow=1'>Expand)AuthorFilesLines 2021-04-30Rename a few satellites with two names and update frequenciesMauro Carvalho Chehab2-283/+182 2021-04-30Update frequencies from LyngsatMauro Carvalho Chehab188-12304/+12666 >Christoph Pfister11-8/+50 :19 +0000'>2004-06-02[lcd4linux @ 2004-06-02 09:41:19 by reinelt]reinelt18-114/+211 2004-06-02[lcd4linux @ 2004-06-02 05:56:25 by reinelt]reinelt1-2/+6 2004-06-02[lcd4linux @ 2004-06-02 05:35:55 by reinelt]reinelt1-0/+20 2004-06-02[lcd4linux @ 2004-06-02 05:27:59 by reinelt]reinelt26-0/+2405 2004-06-02[lcd4linux @ 2004-06-02 05:14:16 by reinelt]reinelt2-17/+31 2004-06-01[lcd4linux @ 2004-06-01 06:45:28 by reinelt]reinelt18-71/+241 2004-06-01[lcd4linux @ 2004-06-01 06:04:25 by reinelt]reinelt2-25/+25 2004-05-31[lcd4linux @ 2004-05-31 21:23:16 by reinelt]reinelt1-37/+49 2004-05-31[lcd4linux @ 2004-05-31 21:05:13 by reinelt]reinelt5-77/+111 2004-05-31[lcd4linux @ 2004-05-31 16:39:05 by reinelt]reinelt11-180/+535 2004-05-31[lcd4linux @ 2004-05-31 06:27:34 by reinelt]reinelt1-0/+40 2004-05-31[lcd4linux @ 2004-05-31 06:24:42 by reinelt]reinelt2-7/+29 2004-05-31[lcd4linux @ 2004-05-31 05:38:02 by reinelt]reinelt8-42/+94 2004-05-31[lcd4linux @ 2004-05-31 01:31:01 by andy-b]andy-b1-10/+12 2004-05-30[lcd4linux @ 2004-05-30 08:25:50 by reinelt]reinelt2-30/+92 2004-05-29[lcd4linux @ 2004-05-29 23:30:20 by reinelt]reinelt1-1/+9 2004-05-29[lcd4linux @ 2004-05-29 15:53:28 by reinelt]reinelt3-9/+25 2004-05-29[lcd4linux @ 2004-05-29 01:07:56 by reinelt]reinelt2-21/+25 2004-05-29[lcd4linux @ 2004-05-29 00:27:14 by reinelt]reinelt8-11/+214 2004-05-28[lcd4linux @ 2004-05-28 14:38:10 by reinelt]reinelt2-1/+41