2 * collectd - src/load.c
3 * Copyright (C) 2005-2007 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
26 #ifdef HAVE_SYS_LOADAVG_H
27 #include <sys/loadavg.h>
31 # include <statgrab.h>
34 #ifdef HAVE_GETLOADAVG
35 #if !defined(LOADAVG_1MIN) || !defined(LOADAVG_5MIN) || !defined(LOADAVG_15MIN)
36 #define LOADAVG_1MIN 0
37 #define LOADAVG_5MIN 1
38 #define LOADAVG_15MIN 2
40 #endif /* defined(HAVE_GETLOADAVG) */
42 static void load_submit (gauge_t snum, gauge_t mnum, gauge_t lnum)
45 value_list_t vl = VALUE_LIST_INIT;
47 values[0].gauge = snum;
48 values[1].gauge = mnum;
49 values[2].gauge = lnum;
52 vl.values_len = STATIC_ARRAY_SIZE (values);
53 vl.time = time (NULL);
54 strcpy (vl.host, hostname_g);
55 strcpy (vl.plugin, "load");
57 plugin_dispatch_values ("load", &vl);
60 static int load_read (void)
62 #if defined(HAVE_GETLOADAVG)
65 if (getloadavg (load, 3) == 3)
66 load_submit (load[LOADAVG_1MIN], load[LOADAVG_5MIN], load[LOADAVG_15MIN]);
70 WARNING ("load: getloadavg failed: %s",
71 sstrerror (errno, errbuf, sizeof (errbuf)));
73 /* #endif HAVE_GETLOADAVG */
75 #elif defined(KERNEL_LINUX)
76 gauge_t snum, mnum, lnum;
83 if ((loadavg = fopen ("/proc/loadavg", "r")) == NULL)
86 WARNING ("load: fopen: %s",
87 sstrerror (errno, errbuf, sizeof (errbuf)));
91 if (fgets (buffer, 16, loadavg) == NULL)
94 WARNING ("load: fgets: %s",
95 sstrerror (errno, errbuf, sizeof (errbuf)));
100 if (fclose (loadavg))
103 WARNING ("load: fclose: %s",
104 sstrerror (errno, errbuf, sizeof (errbuf)));
107 numfields = strsplit (buffer, fields, 8);
112 snum = atof (fields[0]);
113 mnum = atof (fields[1]);
114 lnum = atof (fields[2]);
116 load_submit (snum, mnum, lnum);
117 /* #endif KERNEL_LINUX */
119 #elif HAVE_LIBSTATGRAB
120 gauge_t snum, mnum, lnum;
123 if ((ls = sg_get_load_stats ()) == NULL)
130 load_submit (snum, mnum, lnum);
131 /* #endif HAVE_LIBSTATGRAB */
134 # error "No applicable input method."
140 void module_register (void)
142 plugin_register_read ("load", load_read);
143 } /* void module_register */