1 /**
2 * collectd - src/load.c
3 * Copyright (C) 2005-2008 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 #define _BSD_SOURCE
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
28 #ifdef HAVE_SYS_LOADAVG_H
29 #include <sys/loadavg.h>
30 #endif
32 #if HAVE_STATGRAB_H
33 # include <statgrab.h>
34 #endif
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
41 #endif
42 #endif /* defined(HAVE_GETLOADAVG) */
44 static void load_submit (gauge_t snum, gauge_t mnum, gauge_t lnum)
45 {
46 value_t values[3];
47 value_list_t vl = VALUE_LIST_INIT;
49 values[0].gauge = snum;
50 values[1].gauge = mnum;
51 values[2].gauge = lnum;
53 vl.values = values;
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);
60 }
62 static int load_read (void)
63 {
64 #if defined(HAVE_GETLOADAVG)
65 double load[3];
67 if (getloadavg (load, 3) == 3)
68 load_submit (load[LOADAVG_1MIN], load[LOADAVG_5MIN], load[LOADAVG_15MIN]);
69 else
70 {
71 char errbuf[1024];
72 WARNING ("load: getloadavg failed: %s",
73 sstrerror (errno, errbuf, sizeof (errbuf)));
74 }
75 /* #endif HAVE_GETLOADAVG */
77 #elif defined(KERNEL_LINUX)
78 gauge_t snum, mnum, lnum;
79 FILE *loadavg;
80 char buffer[16];
82 char *fields[8];
83 int numfields;
85 if ((loadavg = fopen ("/proc/loadavg", "r")) == NULL)
86 {
87 char errbuf[1024];
88 WARNING ("load: fopen: %s",
89 sstrerror (errno, errbuf, sizeof (errbuf)));
90 return (-1);
91 }
93 if (fgets (buffer, 16, loadavg) == NULL)
94 {
95 char errbuf[1024];
96 WARNING ("load: fgets: %s",
97 sstrerror (errno, errbuf, sizeof (errbuf)));
98 fclose (loadavg);
99 return (-1);
100 }
102 if (fclose (loadavg))
103 {
104 char errbuf[1024];
105 WARNING ("load: fclose: %s",
106 sstrerror (errno, errbuf, sizeof (errbuf)));
107 }
109 numfields = strsplit (buffer, fields, 8);
111 if (numfields < 3)
112 return (-1);
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;
123 sg_load_stats *ls;
125 if ((ls = sg_get_load_stats ()) == NULL)
126 return;
128 snum = ls->min1;
129 mnum = ls->min5;
130 lnum = ls->min15;
132 load_submit (snum, mnum, lnum);
133 /* #endif HAVE_LIBSTATGRAB */
135 #else
136 # error "No applicable input method."
137 #endif
139 return (0);
140 }
142 void module_register (void)
143 {
144 plugin_register_read ("load", load_read);
145 } /* void module_register */