2 * collectd - src/load.c
3 * Copyright (C) 2005-2008 Florian octo Forster
4 * Copyright (C) 2009 Manuel Sanmartin
5 * Copyright (C) 2013 Vedran Bartonicek
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; only version 2 of the License is applicable.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 * Florian octo Forster <octo at collectd.org>
23 * Vedran Bartonicek <vbartoni at gmail.com>
26 #define _DEFAULT_SOURCE
36 #ifdef HAVE_SYS_LOADAVG_H
37 #include <sys/loadavg.h>
41 # include <statgrab.h>
44 #ifdef HAVE_GETLOADAVG
45 #if !defined(LOADAVG_1MIN) || !defined(LOADAVG_5MIN) || !defined(LOADAVG_15MIN)
46 #define LOADAVG_1MIN 0
47 #define LOADAVG_5MIN 1
48 #define LOADAVG_15MIN 2
50 #endif /* defined(HAVE_GETLOADAVG) */
53 # include <sys/proc.h> /* AIX 5 */
54 # include <sys/protosw.h>
55 # include <libperfstat.h>
56 #endif /* HAVE_PERFSTAT */
58 static _Bool report_relative_load = 0;
60 static const char *config_keys[] =
64 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
66 static int load_config (const char *key, const char *value)
68 if (strcasecmp (key, "ReportRelative") == 0)
69 #ifdef _SC_NPROCESSORS_ONLN
70 report_relative_load = IS_TRUE (value) ? 1 : 0;
72 WARNING ("load plugin: The \"ReportRelative\" configuration "
73 "is not available, because I can't determine the "
74 "number of CPUS on this system. Sorry.");
79 static void load_submit (gauge_t snum, gauge_t mnum, gauge_t lnum)
82 value_list_t vl = VALUE_LIST_INIT;
86 #ifdef _SC_NPROCESSORS_ONLN
87 if (report_relative_load) {
88 if ((cores = sysconf(_SC_NPROCESSORS_ONLN)) < 1) {
89 WARNING ("load: sysconf failed : %s",
90 sstrerror (errno, errbuf, sizeof (errbuf)));
100 values[0].gauge = snum;
101 values[1].gauge = mnum;
102 values[2].gauge = lnum;
105 vl.values_len = STATIC_ARRAY_SIZE (values);
107 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
108 sstrncpy (vl.plugin, "load", sizeof (vl.plugin));
109 sstrncpy (vl.type, "load", sizeof (vl.type));
112 sstrncpy(vl.type_instance, "relative",
113 sizeof (vl.type_instance));
116 plugin_dispatch_values (&vl);
119 static int load_read (void)
121 #if defined(HAVE_GETLOADAVG)
124 if (getloadavg (load, 3) == 3)
125 load_submit (load[LOADAVG_1MIN], load[LOADAVG_5MIN], load[LOADAVG_15MIN]);
129 WARNING ("load: getloadavg failed: %s",
130 sstrerror (errno, errbuf, sizeof (errbuf)));
132 /* #endif HAVE_GETLOADAVG */
134 #elif defined(KERNEL_LINUX)
135 gauge_t snum, mnum, lnum;
142 if ((loadavg = fopen ("/proc/loadavg", "r")) == NULL)
145 WARNING ("load: fopen: %s",
146 sstrerror (errno, errbuf, sizeof (errbuf)));
150 if (fgets (buffer, 16, loadavg) == NULL)
153 WARNING ("load: fgets: %s",
154 sstrerror (errno, errbuf, sizeof (errbuf)));
159 if (fclose (loadavg))
162 WARNING ("load: fclose: %s",
163 sstrerror (errno, errbuf, sizeof (errbuf)));
166 numfields = strsplit (buffer, fields, 8);
171 snum = atof (fields[0]);
172 mnum = atof (fields[1]);
173 lnum = atof (fields[2]);
175 load_submit(snum, mnum, lnum);
176 /* #endif KERNEL_LINUX */
178 #elif HAVE_LIBSTATGRAB
179 gauge_t snum, mnum, lnum;
182 if ((ls = sg_get_load_stats ()) == NULL)
188 load_submit(snum, mnum, lnum);
189 /* #endif HAVE_LIBSTATGRAB */
192 gauge_t snum, mnum, lnum;
193 perfstat_cpu_total_t cputotal;
195 if (perfstat_cpu_total(NULL, &cputotal, sizeof(perfstat_cpu_total_t), 1) < 0)
198 WARNING ("load: perfstat_cpu : %s",
199 sstrerror (errno, errbuf, sizeof (errbuf)));
203 snum = (float)cputotal.loadavg[0]/(float)(1<<SBITS);
204 mnum = (float)cputotal.loadavg[1]/(float)(1<<SBITS);
205 lnum = (float)cputotal.loadavg[2]/(float)(1<<SBITS);
206 load_submit(snum, mnum, lnum);
207 /* #endif HAVE_PERFSTAT */
210 # error "No applicable input method."
216 void module_register (void)
218 plugin_register_config ("load", load_config, config_keys, config_keys_num);
219 plugin_register_read ("load", load_read);
220 } /* void module_register */