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