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