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 verplant.org>
22 * Manuel Sanmartin
23 * Vedran Bartonicek <vbartoni at gmail.com>
24 **/
26 #define _BSD_SOURCE
28 #include "collectd.h"
29 #include "common.h"
30 #include "plugin.h"
32 #include <unistd.h>
34 #ifdef HAVE_SYS_LOADAVG_H
35 #include <sys/loadavg.h>
36 #endif
38 #if HAVE_STATGRAB_H
39 # include <statgrab.h>
40 #endif
42 #ifdef HAVE_GETLOADAVG
43 #if !defined(LOADAVG_1MIN) || !defined(LOADAVG_5MIN) || !defined(LOADAVG_15MIN)
44 #define LOADAVG_1MIN 0
45 #define LOADAVG_5MIN 1
46 #define LOADAVG_15MIN 2
47 #endif
48 #endif /* defined(HAVE_GETLOADAVG) */
50 #ifdef HAVE_PERFSTAT
51 # include <sys/proc.h> /* AIX 5 */
52 # include <sys/protosw.h>
53 # include <libperfstat.h>
54 #endif /* HAVE_PERFSTAT */
56 static _Bool report_relative_load = 0;
58 static const char *config_keys[] =
59 {
60 "ReportRelative"
61 };
62 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
64 static int load_config (const char *key, const char *value)
65 {
66 if (strcasecmp (key, "ReportRelative") == 0)
67 #ifdef _SC_NPROCESSORS_ONLN
68 report_relative_load = IS_TRUE (value) ? 1 : 0;
69 #else
70 WARNING ("load plugin: The \"ReportRelative\" configuration "
71 "is not available, because I can't determine the "
72 "number of CPUS on this system. Sorry.");
73 #endif
74 return (-1);
76 }
77 static void load_submit (gauge_t snum, gauge_t mnum, gauge_t lnum)
78 {
79 value_t values[3];
80 value_list_t vl = VALUE_LIST_INIT;
81 int cores = 0;
82 char errbuf[1024];
84 #ifdef _SC_NPROCESSORS_ONLN
85 if (report_relative_load) {
86 if ((cores = sysconf(_SC_NPROCESSORS_ONLN)) < 1) {
87 WARNING ("load: sysconf failed : %s",
88 sstrerror (errno, errbuf, sizeof (errbuf)));
89 }
90 }
91 #endif
92 if (cores > 0) {
93 snum /= cores;
94 mnum /= cores;
95 lnum /= cores;
96 }
98 values[0].gauge = snum;
99 values[1].gauge = mnum;
100 values[2].gauge = lnum;
102 vl.values = values;
103 vl.values_len = STATIC_ARRAY_SIZE (values);
105 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
106 sstrncpy (vl.plugin, "load", sizeof (vl.plugin));
107 sstrncpy (vl.type, "load", sizeof (vl.type));
109 if (cores > 0) {
110 sstrncpy(vl.type_instance, "relative",
111 sizeof (vl.type_instance));
112 }
114 plugin_dispatch_values (&vl);
115 }
117 static int load_read (void)
118 {
119 #if defined(HAVE_GETLOADAVG)
120 double load[3];
122 if (getloadavg (load, 3) == 3)
123 load_submit (load[LOADAVG_1MIN], load[LOADAVG_5MIN], load[LOADAVG_15MIN]);
124 else
125 {
126 char errbuf[1024];
127 WARNING ("load: getloadavg failed: %s",
128 sstrerror (errno, errbuf, sizeof (errbuf)));
129 }
130 /* #endif HAVE_GETLOADAVG */
132 #elif defined(KERNEL_LINUX)
133 gauge_t snum, mnum, lnum;
134 FILE *loadavg;
135 char buffer[16];
137 char *fields[8];
138 int numfields;
140 if ((loadavg = fopen ("/proc/loadavg", "r")) == NULL)
141 {
142 char errbuf[1024];
143 WARNING ("load: fopen: %s",
144 sstrerror (errno, errbuf, sizeof (errbuf)));
145 return (-1);
146 }
148 if (fgets (buffer, 16, loadavg) == NULL)
149 {
150 char errbuf[1024];
151 WARNING ("load: fgets: %s",
152 sstrerror (errno, errbuf, sizeof (errbuf)));
153 fclose (loadavg);
154 return (-1);
155 }
157 if (fclose (loadavg))
158 {
159 char errbuf[1024];
160 WARNING ("load: fclose: %s",
161 sstrerror (errno, errbuf, sizeof (errbuf)));
162 }
164 numfields = strsplit (buffer, fields, 8);
166 if (numfields < 3)
167 return (-1);
169 snum = atof (fields[0]);
170 mnum = atof (fields[1]);
171 lnum = atof (fields[2]);
173 load_submit(snum, mnum, lnum);
174 /* #endif KERNEL_LINUX */
176 #elif HAVE_LIBSTATGRAB
177 gauge_t snum, mnum, lnum;
178 sg_load_stats *ls;
180 if ((ls = sg_get_load_stats ()) == NULL)
181 return;
183 snum = ls->min1;
184 mnum = ls->min5;
185 lnum = ls->min15;
186 load_submit(snum, mnum, lnum);
187 /* #endif HAVE_LIBSTATGRAB */
189 #elif HAVE_PERFSTAT
190 gauge_t snum, mnum, lnum;
191 perfstat_cpu_total_t cputotal;
193 if (perfstat_cpu_total(NULL, &cputotal, sizeof(perfstat_cpu_total_t), 1) < 0)
194 {
195 char errbuf[1024];
196 WARNING ("load: perfstat_cpu : %s",
197 sstrerror (errno, errbuf, sizeof (errbuf)));
198 return (-1);
199 }
201 snum = (float)cputotal.loadavg[0]/(float)(1<<SBITS);
202 mnum = (float)cputotal.loadavg[1]/(float)(1<<SBITS);
203 lnum = (float)cputotal.loadavg[2]/(float)(1<<SBITS);
204 load_submit(snum, mnum, lnum);
205 /* #endif HAVE_PERFSTAT */
207 #else
208 # error "No applicable input method."
209 #endif
211 return (0);
212 }
214 void module_register (void)
215 {
216 plugin_register_config ("load", load_config, config_keys, config_keys_num);
217 plugin_register_read ("load", load_read);
218 } /* void module_register */