2 * collectd - src/load.c
3 * Copyright (C) 2005,2006 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at verplant.org>
27 #define MODULE_NAME "load"
29 #if defined(HAVE_GETLOADAVG) || defined(KERNEL_LINUX) || defined(HAVE_LIBSTATGRAB)
30 # define LOAD_HAVE_READ 1
32 # define LOAD_HAVE_READ 0
35 #ifdef HAVE_SYS_LOADAVG_H
36 #include <sys/loadavg.h>
39 #ifdef HAVE_GETLOADAVG
40 #if !defined(LOADAVG_1MIN) || !defined(LOADAVG_5MIN) || !defined(LOADAVG_15MIN)
41 #define LOADAVG_1MIN 0
42 #define LOADAVG_5MIN 1
43 #define LOADAVG_15MIN 2
45 #endif /* defined(HAVE_GETLOADAVG) */
47 static data_source_t dsrc[3] =
49 {"shortterm", DS_TYPE_GAUGE, 0.0, 100.0},
50 {"midterm", DS_TYPE_GAUGE, 0.0, 100.0},
51 {"longterm", DS_TYPE_GAUGE, 0.0, 100.0}
54 static data_set_t ds =
60 static void load_submit (double snum, double mnum, double lnum)
65 values[0].gauge = snum;
66 values[1].gauge = mnum;
67 values[2].gauge = lnum;
71 vl.time = time (NULL);
72 /* FIXME: do this globally */
73 if (gethostname (vl.host, sizeof (vl.host)) != 0)
75 syslog (LOG_ERR, "load plugin: gethostname failed: %s",
79 strcpy (vl.plugin, "load");
80 strcpy (vl.plugin_instance, "");
81 strcpy (vl.type_instance, "");
83 plugin_dispatch_values ("load", &vl);
86 static int load_read (void)
88 #if defined(HAVE_GETLOADAVG)
91 if (getloadavg (load, 3) == 3)
92 load_submit (load[LOADAVG_1MIN], load[LOADAVG_5MIN], load[LOADAVG_15MIN]);
94 syslog (LOG_WARNING, "load: getloadavg failed: %s", strerror (errno));
95 /* #endif HAVE_GETLOADAVG */
97 #elif defined(KERNEL_LINUX)
98 double snum, mnum, lnum;
105 if ((loadavg = fopen ("/proc/loadavg", "r")) == NULL)
107 syslog (LOG_WARNING, "load: fopen: %s", strerror (errno));
111 if (fgets (buffer, 16, loadavg) == NULL)
113 syslog (LOG_WARNING, "load: fgets: %s", strerror (errno));
118 if (fclose (loadavg))
119 syslog (LOG_WARNING, "load: fclose: %s", strerror (errno));
121 numfields = strsplit (buffer, fields, 8);
126 snum = atof (fields[0]);
127 mnum = atof (fields[1]);
128 lnum = atof (fields[2]);
130 load_submit (snum, mnum, lnum);
131 /* #endif KERNEL_LINUX */
133 #elif defined(HAVE_LIBSTATGRAB)
134 double snum, mnum, lnum;
137 if ((ls = sg_get_load_stats ()) == NULL)
144 load_submit (snum, mnum, lnum);
145 #endif /* HAVE_LIBSTATGRAB */
149 #endif /* LOAD_HAVE_READ */
151 void module_register (void)
153 plugin_register_data_set (&ds);
155 plugin_register_read ("load", load_read);