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;
59 void load_write (char *host, char *inst, char *val)
61 rrd_update_file (host, load_file, val, ds_def, ds_num);
65 void load_submit (double snum, double mnum, double lnum)
69 if (snprintf (buf, BUFSIZE, "%u:%.2f:%.2f:%.2f", (unsigned int) curtime,
70 snum, mnum, lnum) >= BUFSIZE)
73 plugin_submit (MODULE_NAME, "-", buf);
79 #if defined(HAVE_GETLOADAVG)
82 if (getloadavg (load, 3) == 3)
83 load_submit (load[LOADAVG_1MIN], load[LOADAVG_5MIN], load[LOADAVG_15MIN]);
85 syslog (LOG_WARNING, "load: getloadavg failed: %s", strerror (errno));
86 /* #endif HAVE_GETLOADAVG */
88 #elif defined(KERNEL_LINUX)
89 double snum, mnum, lnum;
96 if ((loadavg = fopen ("/proc/loadavg", "r")) == NULL)
98 syslog (LOG_WARNING, "load: fopen: %s", strerror (errno));
102 if (fgets (buffer, 16, loadavg) == NULL)
104 syslog (LOG_WARNING, "load: fgets: %s", strerror (errno));
108 if (fclose (loadavg))
109 syslog (LOG_WARNING, "load: fclose: %s", strerror (errno));
111 numfields = strsplit (buffer, fields, 8);
116 snum = atof (fields[0]);
117 mnum = atof (fields[1]);
118 lnum = atof (fields[2]);
120 load_submit (snum, mnum, lnum);
121 /* #endif KERNEL_LINUX */
123 #elif defined(HAVE_LIBSTATGRAB)
124 double snum, mnum, lnum;
127 if ((ls = sg_get_load_stats ()) == NULL)
134 load_submit (snum, mnum, lnum);
135 #endif /* HAVE_LIBSTATGRAB */
138 void module_register (void)
140 plugin_register (MODULE_NAME, load_init, load_read, load_write);
144 #endif /* COLLECT_LOAD */