2 * collectd - src/load.c
3 * Copyright (C) 2005-2008 Florian octo Forster
4 * Copyright (C) 2009 Manuel Sanmartin
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at verplant.org>
24 #define _DEFAULT_SOURCE
31 #ifdef HAVE_SYS_LOADAVG_H
32 #include <sys/loadavg.h>
36 # include <statgrab.h>
39 #ifdef HAVE_GETLOADAVG
40 #if !defined(LOADAVG_1MIN) || !defined(LOADAVG_5MIN) || !defined(LOADAVG_15MIN)
41 #define LOADAVG_1MIN 0
42 #define LOADAVG_5MIN 1
43 #define LOADAVG_15MIN 2
45 #endif /* defined(HAVE_GETLOADAVG) */
48 # include <sys/proc.h> /* AIX 5 */
49 # include <sys/protosw.h>
50 # include <libperfstat.h>
51 #endif /* HAVE_PERFSTAT */
53 static void load_submit (gauge_t snum, gauge_t mnum, gauge_t lnum)
56 value_list_t vl = VALUE_LIST_INIT;
58 values[0].gauge = snum;
59 values[1].gauge = mnum;
60 values[2].gauge = lnum;
63 vl.values_len = STATIC_ARRAY_SIZE (values);
64 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
65 sstrncpy (vl.plugin, "load", sizeof (vl.plugin));
66 sstrncpy (vl.type, "load", sizeof (vl.type));
68 plugin_dispatch_values (&vl);
71 static int load_read (void)
73 #if defined(HAVE_GETLOADAVG)
76 if (getloadavg (load, 3) == 3)
77 load_submit (load[LOADAVG_1MIN], load[LOADAVG_5MIN], load[LOADAVG_15MIN]);
81 WARNING ("load: getloadavg failed: %s",
82 sstrerror (errno, errbuf, sizeof (errbuf)));
84 /* #endif HAVE_GETLOADAVG */
86 #elif defined(KERNEL_LINUX)
87 gauge_t snum, mnum, lnum;
94 if ((loadavg = fopen ("/proc/loadavg", "r")) == NULL)
97 WARNING ("load: fopen: %s",
98 sstrerror (errno, errbuf, sizeof (errbuf)));
102 if (fgets (buffer, 16, loadavg) == NULL)
105 WARNING ("load: fgets: %s",
106 sstrerror (errno, errbuf, sizeof (errbuf)));
111 if (fclose (loadavg))
114 WARNING ("load: fclose: %s",
115 sstrerror (errno, errbuf, sizeof (errbuf)));
118 numfields = strsplit (buffer, fields, 8);
123 snum = atof (fields[0]);
124 mnum = atof (fields[1]);
125 lnum = atof (fields[2]);
127 load_submit (snum, mnum, lnum);
128 /* #endif KERNEL_LINUX */
130 #elif HAVE_LIBSTATGRAB
131 gauge_t snum, mnum, lnum;
134 if ((ls = sg_get_load_stats ()) == NULL)
141 load_submit (snum, mnum, lnum);
142 /* #endif HAVE_LIBSTATGRAB */
145 gauge_t snum, mnum, lnum;
146 perfstat_cpu_total_t cputotal;
148 if (perfstat_cpu_total(NULL, &cputotal, sizeof(perfstat_cpu_total_t), 1) < 0)
151 WARNING ("load: perfstat_cpu : %s",
152 sstrerror (errno, errbuf, sizeof (errbuf)));
156 snum = (float)cputotal.loadavg[0]/(float)(1<<SBITS);
157 mnum = (float)cputotal.loadavg[1]/(float)(1<<SBITS);
158 lnum = (float)cputotal.loadavg[2]/(float)(1<<SBITS);
160 load_submit (snum, mnum, lnum);
161 /* #endif HAVE_PERFSTAT */
164 # error "No applicable input method."
170 void module_register (void)
172 plugin_register_read ("load", load_read);
173 } /* void module_register */