2 * collectd - src/load.c
3 * Copyright (C) 2005-2008 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>
28 #ifdef HAVE_SYS_LOADAVG_H
29 #include <sys/loadavg.h>
33 # include <statgrab.h>
36 #ifdef HAVE_GETLOADAVG
37 #if !defined(LOADAVG_1MIN) || !defined(LOADAVG_5MIN) || !defined(LOADAVG_15MIN)
38 #define LOADAVG_1MIN 0
39 #define LOADAVG_5MIN 1
40 #define LOADAVG_15MIN 2
42 #endif /* defined(HAVE_GETLOADAVG) */
44 static void load_submit (gauge_t snum, gauge_t mnum, gauge_t lnum)
47 value_list_t vl = VALUE_LIST_INIT;
49 values[0].gauge = snum;
50 values[1].gauge = mnum;
51 values[2].gauge = lnum;
54 vl.values_len = STATIC_ARRAY_SIZE (values);
55 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
56 sstrncpy (vl.plugin, "load", sizeof (vl.plugin));
57 sstrncpy (vl.type, "load", sizeof (vl.type));
59 plugin_dispatch_values (&vl);
62 static int load_read (void)
64 #if defined(HAVE_GETLOADAVG)
67 if (getloadavg (load, 3) == 3)
68 load_submit (load[LOADAVG_1MIN], load[LOADAVG_5MIN], load[LOADAVG_15MIN]);
72 WARNING ("load: getloadavg failed: %s",
73 sstrerror (errno, errbuf, sizeof (errbuf)));
75 /* #endif HAVE_GETLOADAVG */
77 #elif defined(KERNEL_LINUX)
78 gauge_t snum, mnum, lnum;
85 if ((loadavg = fopen ("/proc/loadavg", "r")) == NULL)
88 WARNING ("load: fopen: %s",
89 sstrerror (errno, errbuf, sizeof (errbuf)));
93 if (fgets (buffer, 16, loadavg) == NULL)
96 WARNING ("load: fgets: %s",
97 sstrerror (errno, errbuf, sizeof (errbuf)));
102 if (fclose (loadavg))
105 WARNING ("load: fclose: %s",
106 sstrerror (errno, errbuf, sizeof (errbuf)));
109 numfields = strsplit (buffer, fields, 8);
114 snum = atof (fields[0]);
115 mnum = atof (fields[1]);
116 lnum = atof (fields[2]);
118 load_submit (snum, mnum, lnum);
119 /* #endif KERNEL_LINUX */
121 #elif HAVE_LIBSTATGRAB
122 gauge_t snum, mnum, lnum;
125 if ((ls = sg_get_load_stats ()) == NULL)
132 load_submit (snum, mnum, lnum);
133 /* #endif HAVE_LIBSTATGRAB */
136 # error "No applicable input method."
142 void module_register (void)
144 plugin_register_read ("load", load_read);
145 } /* void module_register */