1 /**
2 * collectd - src/load.c
3 * Copyright (C) 2005-2007 Florian octo Forster
4 *
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.
8 *
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.
13 *
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
17 *
18 * Authors:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
26 #ifdef HAVE_SYS_LOADAVG_H
27 #include <sys/loadavg.h>
28 #endif
30 #ifdef HAVE_GETLOADAVG
31 #if !defined(LOADAVG_1MIN) || !defined(LOADAVG_5MIN) || !defined(LOADAVG_15MIN)
32 #define LOADAVG_1MIN 0
33 #define LOADAVG_5MIN 1
34 #define LOADAVG_15MIN 2
35 #endif
36 #endif /* defined(HAVE_GETLOADAVG) */
38 static void load_submit (gauge_t snum, gauge_t mnum, gauge_t lnum)
39 {
40 value_t values[3];
41 value_list_t vl = VALUE_LIST_INIT;
43 values[0].gauge = snum;
44 values[1].gauge = mnum;
45 values[2].gauge = lnum;
47 vl.values = values;
48 vl.values_len = STATIC_ARRAY_SIZE (values);
49 vl.time = time (NULL);
50 strcpy (vl.host, hostname_g);
51 strcpy (vl.plugin, "load");
53 plugin_dispatch_values ("load", &vl);
54 }
56 static int load_read (void)
57 {
58 #if defined(HAVE_GETLOADAVG)
59 double load[3];
61 if (getloadavg (load, 3) == 3)
62 load_submit (load[LOADAVG_1MIN], load[LOADAVG_5MIN], load[LOADAVG_15MIN]);
63 else
64 {
65 char errbuf[1024];
66 WARNING ("load: getloadavg failed: %s",
67 sstrerror (errno, errbuf, sizeof (errbuf)));
68 }
69 /* #endif HAVE_GETLOADAVG */
71 #elif defined(KERNEL_LINUX)
72 gauge_t snum, mnum, lnum;
73 FILE *loadavg;
74 char buffer[16];
76 char *fields[8];
77 int numfields;
79 if ((loadavg = fopen ("/proc/loadavg", "r")) == NULL)
80 {
81 char errbuf[1024];
82 WARNING ("load: fopen: %s",
83 sstrerror (errno, errbuf, sizeof (errbuf)));
84 return (-1);
85 }
87 if (fgets (buffer, 16, loadavg) == NULL)
88 {
89 char errbuf[1024];
90 WARNING ("load: fgets: %s",
91 sstrerror (errno, errbuf, sizeof (errbuf)));
92 fclose (loadavg);
93 return (-1);
94 }
96 if (fclose (loadavg))
97 {
98 char errbuf[1024];
99 WARNING ("load: fclose: %s",
100 sstrerror (errno, errbuf, sizeof (errbuf)));
101 }
103 numfields = strsplit (buffer, fields, 8);
105 if (numfields < 3)
106 return (-1);
108 snum = atof (fields[0]);
109 mnum = atof (fields[1]);
110 lnum = atof (fields[2]);
112 load_submit (snum, mnum, lnum);
113 /* #endif KERNEL_LINUX */
115 #elif defined(HAVE_LIBSTATGRAB)
116 gauge_t snum, mnum, lnum;
117 sg_load_stats *ls;
119 if ((ls = sg_get_load_stats ()) == NULL)
120 return;
122 snum = ls->min1;
123 mnum = ls->min5;
124 lnum = ls->min15;
126 load_submit (snum, mnum, lnum);
127 /* #endif HAVE_LIBSTATGRAB */
129 #else
130 # error "No applicable input method."
131 #endif
133 return (0);
134 }
136 void module_register (void)
137 {
138 plugin_register_read ("load", load_read);
139 } /* void module_register */