1 /**
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
6 *
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.
10 *
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.
15 *
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
19 *
20 * Authors:
21 * Florian octo Forster <octo at collectd.org>
22 * Manuel Sanmartin
23 * Vedran Bartonicek <vbartoni at gmail.com>
24 **/
26 #define _DEFAULT_SOURCE
27 #define _BSD_SOURCE
29 #include "collectd.h"
31 #include "common.h"
32 #include "plugin.h"
34 #include <unistd.h>
36 #ifdef HAVE_SYS_LOADAVG_H
37 #include <sys/loadavg.h>
38 #endif
40 #if HAVE_STATGRAB_H
41 #include <statgrab.h>
42 #endif
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
49 #endif
50 #endif /* defined(HAVE_GETLOADAVG) */
52 #ifdef HAVE_PERFSTAT
53 #include <libperfstat.h>
54 #include <sys/proc.h> /* AIX 5 */
55 #include <sys/protosw.h>
56 #endif /* HAVE_PERFSTAT */
58 static _Bool report_relative_load = 0;
60 static const char *config_keys[] = {"ReportRelative"};
61 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
63 static int load_config(const char *key, const char *value) {
64 if (strcasecmp(key, "ReportRelative") == 0)
65 #ifdef _SC_NPROCESSORS_ONLN
66 report_relative_load = IS_TRUE(value) ? 1 : 0;
67 #else
68 WARNING("load plugin: The \"ReportRelative\" configuration "
69 "is not available, because I can't determine the "
70 "number of CPUS on this system. Sorry.");
71 #endif
72 return (-1);
73 }
74 static void load_submit(gauge_t snum, gauge_t mnum, gauge_t lnum) {
75 int cores = 0;
76 char errbuf[1024];
78 #ifdef _SC_NPROCESSORS_ONLN
79 if (report_relative_load) {
80 if ((cores = sysconf(_SC_NPROCESSORS_ONLN)) < 1) {
81 WARNING("load: sysconf failed : %s",
82 sstrerror(errno, errbuf, sizeof(errbuf)));
83 }
84 }
85 #endif
86 if (cores > 0) {
87 snum /= cores;
88 mnum /= cores;
89 lnum /= cores;
90 }
92 value_list_t vl = VALUE_LIST_INIT;
93 value_t values[] = {
94 {.gauge = snum}, {.gauge = mnum}, {.gauge = lnum},
95 };
97 vl.values = values;
98 vl.values_len = STATIC_ARRAY_SIZE(values);
100 sstrncpy(vl.plugin, "load", sizeof(vl.plugin));
101 sstrncpy(vl.type, "load", sizeof(vl.type));
103 if (cores > 0) {
104 sstrncpy(vl.type_instance, "relative", sizeof(vl.type_instance));
105 }
107 plugin_dispatch_values(&vl);
108 }
110 static int load_read(void) {
111 #if defined(HAVE_GETLOADAVG)
112 double load[3];
114 if (getloadavg(load, 3) == 3)
115 load_submit(load[LOADAVG_1MIN], load[LOADAVG_5MIN], load[LOADAVG_15MIN]);
116 else {
117 char errbuf[1024];
118 WARNING("load: getloadavg failed: %s",
119 sstrerror(errno, errbuf, sizeof(errbuf)));
120 }
121 /* #endif HAVE_GETLOADAVG */
123 #elif defined(KERNEL_LINUX)
124 gauge_t snum, mnum, lnum;
125 FILE *loadavg;
126 char buffer[16];
128 char *fields[8];
129 int numfields;
131 if ((loadavg = fopen("/proc/loadavg", "r")) == NULL) {
132 char errbuf[1024];
133 WARNING("load: fopen: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
134 return (-1);
135 }
137 if (fgets(buffer, 16, loadavg) == NULL) {
138 char errbuf[1024];
139 WARNING("load: fgets: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
140 fclose(loadavg);
141 return (-1);
142 }
144 if (fclose(loadavg)) {
145 char errbuf[1024];
146 WARNING("load: fclose: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
147 }
149 numfields = strsplit(buffer, fields, 8);
151 if (numfields < 3)
152 return (-1);
154 snum = atof(fields[0]);
155 mnum = atof(fields[1]);
156 lnum = atof(fields[2]);
158 load_submit(snum, mnum, lnum);
159 /* #endif KERNEL_LINUX */
161 #elif HAVE_LIBSTATGRAB
162 gauge_t snum, mnum, lnum;
163 sg_load_stats *ls;
165 if ((ls = sg_get_load_stats()) == NULL)
166 return;
168 snum = ls->min1;
169 mnum = ls->min5;
170 lnum = ls->min15;
171 load_submit(snum, mnum, lnum);
172 /* #endif HAVE_LIBSTATGRAB */
174 #elif HAVE_PERFSTAT
175 gauge_t snum, mnum, lnum;
176 perfstat_cpu_total_t cputotal;
178 if (perfstat_cpu_total(NULL, &cputotal, sizeof(perfstat_cpu_total_t), 1) <
179 0) {
180 char errbuf[1024];
181 WARNING("load: perfstat_cpu : %s",
182 sstrerror(errno, errbuf, sizeof(errbuf)));
183 return (-1);
184 }
186 snum = (float)cputotal.loadavg[0] / (float)(1 << SBITS);
187 mnum = (float)cputotal.loadavg[1] / (float)(1 << SBITS);
188 lnum = (float)cputotal.loadavg[2] / (float)(1 << SBITS);
189 load_submit(snum, mnum, lnum);
190 /* #endif HAVE_PERFSTAT */
192 #else
193 #error "No applicable input method."
194 #endif
196 return (0);
197 }
199 void module_register(void) {
200 plugin_register_config("load", load_config, config_keys, config_keys_num);
201 plugin_register_read("load", load_read);
202 } /* void module_register */