2 * collectd - src/load.c
3 * Copyright (C) 2005 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; either version 2 of the License, or (at your
8 * option) any later version.
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>
26 #define MODULE_NAME "load"
31 #ifdef HAVE_SYS_LOADAVG_H
32 #include <sys/loadavg.h>
35 #ifdef HAVE_GETLOADAVG
36 #if !defined(LOADAVG_1MIN) || !defined(LOADAVG_5MIN) || !defined(LOADAVG_15MIN)
37 #define LOADAVG_1MIN 0
38 #define LOADAVG_5MIN 1
39 #define LOADAVG_15MIN 2
41 #endif /* defined(HAVE_GETLOADAVG) */
43 static char *load_file = "load.rrd";
45 static char *ds_def[] =
47 "DS:shortterm:GAUGE:25:0:100",
48 "DS:midterm:GAUGE:25:0:100",
49 "DS:longterm:GAUGE:25:0:100",
52 static int ds_num = 3;
54 extern time_t curtime;
61 void load_write (char *host, char *inst, char *val)
63 rrd_update_file (host, load_file, val, ds_def, ds_num);
67 void load_submit (double snum, double mnum, double lnum)
71 if (snprintf (buf, BUFSIZE, "%u:%.2f:%.2f:%.2f", (unsigned int) curtime,
72 snum, mnum, lnum) >= BUFSIZE)
75 plugin_submit (MODULE_NAME, "-", buf);
81 #if defined(HAVE_GETLOADAVG)
84 if (getloadavg (load, 3) == 3)
85 load_submit (load[LOADAVG_1MIN], load[LOADAVG_5MIN], load[LOADAVG_15MIN]);
87 syslog (LOG_WARNING, "load: getloadavg failed: %s", strerror (errno));
88 /* #endif HAVE_GETLOADAVG */
90 #elif defined(KERNEL_LINUX)
91 double snum, mnum, lnum;
98 if ((loadavg = fopen ("/proc/loadavg", "r")) == NULL)
100 syslog (LOG_WARNING, "load: fopen: %s", strerror (errno));
104 if (fgets (buffer, 16, loadavg) == NULL)
106 syslog (LOG_WARNING, "load: fgets: %s", strerror (errno));
110 if (fclose (loadavg))
111 syslog (LOG_WARNING, "load: fclose: %s", strerror (errno));
113 numfields = strsplit (buffer, fields, 8);
118 snum = atof (fields[0]);
119 mnum = atof (fields[1]);
120 lnum = atof (fields[2]);
122 load_submit (snum, mnum, lnum);
123 /* #endif KERNEL_LINUX */
125 #elif defined(HAVE_LIBSTATGRAB)
126 double snum, mnum, lnum;
129 if ((ls = sg_get_load_stats ()) == NULL)
136 load_submit (snum, mnum, lnum);
137 #endif /* HAVE_LIBSTATGRAB */
140 void module_register (void)
142 plugin_register (MODULE_NAME, load_init, load_read, load_write);
146 #endif /* COLLECT_LOAD */