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>
30 #ifdef HAVE_SYS_LOADAVG_H
31 #include <sys/loadavg.h>
35 # include <statgrab.h>
38 #ifdef HAVE_GETLOADAVG
39 #if !defined(LOADAVG_1MIN) || !defined(LOADAVG_5MIN) || !defined(LOADAVG_15MIN)
40 #define LOADAVG_1MIN 0
41 #define LOADAVG_5MIN 1
42 #define LOADAVG_15MIN 2
44 #endif /* defined(HAVE_GETLOADAVG) */
47 # include <sys/proc.h> /* AIX 5 */
48 # include <sys/protosw.h>
49 # include <libperfstat.h>
50 #endif /* HAVE_PERFSTAT */
52 static void load_submit (gauge_t snum, gauge_t mnum, gauge_t lnum)
55 value_list_t vl = VALUE_LIST_INIT;
57 values[0].gauge = snum;
58 values[1].gauge = mnum;
59 values[2].gauge = lnum;
62 vl.values_len = STATIC_ARRAY_SIZE (values);
63 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
64 sstrncpy (vl.plugin, "load", sizeof (vl.plugin));
65 sstrncpy (vl.type, "load", sizeof (vl.type));
67 plugin_dispatch_values (&vl);
70 static int load_read (void)
72 #if defined(HAVE_GETLOADAVG)
75 if (getloadavg (load, 3) == 3)
76 load_submit (load[LOADAVG_1MIN], load[LOADAVG_5MIN], load[LOADAVG_15MIN]);
80 WARNING ("load: getloadavg failed: %s",
81 sstrerror (errno, errbuf, sizeof (errbuf)));
83 /* #endif HAVE_GETLOADAVG */
85 #elif defined(KERNEL_LINUX)
86 gauge_t snum, mnum, lnum;
93 if ((loadavg = fopen ("/proc/loadavg", "r")) == NULL)
96 WARNING ("load: fopen: %s",
97 sstrerror (errno, errbuf, sizeof (errbuf)));
101 if (fgets (buffer, 16, loadavg) == NULL)
104 WARNING ("load: fgets: %s",
105 sstrerror (errno, errbuf, sizeof (errbuf)));
110 if (fclose (loadavg))
113 WARNING ("load: fclose: %s",
114 sstrerror (errno, errbuf, sizeof (errbuf)));
117 numfields = strsplit (buffer, fields, 8);
122 snum = atof (fields[0]);
123 mnum = atof (fields[1]);
124 lnum = atof (fields[2]);
126 load_submit (snum, mnum, lnum);
127 /* #endif KERNEL_LINUX */
129 #elif HAVE_LIBSTATGRAB
130 gauge_t snum, mnum, lnum;
133 if ((ls = sg_get_load_stats ()) == NULL)
140 load_submit (snum, mnum, lnum);
141 /* #endif HAVE_LIBSTATGRAB */
144 gauge_t snum, mnum, lnum;
145 perfstat_cpu_total_t cputotal;
147 if (perfstat_cpu_total(NULL, &cputotal, sizeof(perfstat_cpu_total_t), 1) < 0)
150 WARNING ("load: perfstat_cpu : %s",
151 sstrerror (errno, errbuf, sizeof (errbuf)));
155 snum = (float)cputotal.loadavg[0]/(float)(1<<SBITS);
156 mnum = (float)cputotal.loadavg[1]/(float)(1<<SBITS);
157 lnum = (float)cputotal.loadavg[2]/(float)(1<<SBITS);
159 load_submit (snum, mnum, lnum);
160 /* #endif HAVE_PERFSTAT */
163 # error "No applicable input method."
169 void module_register (void)
171 plugin_register_read ("load", load_read);
172 } /* void module_register */