Merge branch 'collectd-4.4'
[collectd.git] / src / load.c
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  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25
26 #ifdef HAVE_SYS_LOADAVG_H
27 #include <sys/loadavg.h>
28 #endif
29
30 #if HAVE_STATGRAB_H
31 # include <statgrab.h>
32 #endif
33
34 #ifdef HAVE_GETLOADAVG
35 #if !defined(LOADAVG_1MIN) || !defined(LOADAVG_5MIN) || !defined(LOADAVG_15MIN)
36 #define LOADAVG_1MIN  0
37 #define LOADAVG_5MIN  1
38 #define LOADAVG_15MIN 2
39 #endif
40 #endif /* defined(HAVE_GETLOADAVG) */
41
42 static void load_submit (gauge_t snum, gauge_t mnum, gauge_t lnum)
43 {
44         value_t values[3];
45         value_list_t vl = VALUE_LIST_INIT;
46
47         values[0].gauge = snum;
48         values[1].gauge = mnum;
49         values[2].gauge = lnum;
50
51         vl.values = values;
52         vl.values_len = STATIC_ARRAY_SIZE (values);
53         vl.time = time (NULL);
54         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
55         sstrncpy (vl.plugin, "load", sizeof (vl.plugin));
56         sstrncpy (vl.type, "load", sizeof (vl.type));
57
58         plugin_dispatch_values (&vl);
59 }
60
61 static int load_read (void)
62 {
63 #if defined(HAVE_GETLOADAVG)
64         double load[3];
65
66         if (getloadavg (load, 3) == 3)
67                 load_submit (load[LOADAVG_1MIN], load[LOADAVG_5MIN], load[LOADAVG_15MIN]);
68         else
69         {
70                 char errbuf[1024];
71                 WARNING ("load: getloadavg failed: %s",
72                                 sstrerror (errno, errbuf, sizeof (errbuf)));
73         }
74 /* #endif HAVE_GETLOADAVG */
75
76 #elif defined(KERNEL_LINUX)
77         gauge_t snum, mnum, lnum;
78         FILE *loadavg;
79         char buffer[16];
80
81         char *fields[8];
82         int numfields;
83         
84         if ((loadavg = fopen ("/proc/loadavg", "r")) == NULL)
85         {
86                 char errbuf[1024];
87                 WARNING ("load: fopen: %s",
88                                 sstrerror (errno, errbuf, sizeof (errbuf)));
89                 return (-1);
90         }
91
92         if (fgets (buffer, 16, loadavg) == NULL)
93         {
94                 char errbuf[1024];
95                 WARNING ("load: fgets: %s",
96                                 sstrerror (errno, errbuf, sizeof (errbuf)));
97                 fclose (loadavg);
98                 return (-1);
99         }
100
101         if (fclose (loadavg))
102         {
103                 char errbuf[1024];
104                 WARNING ("load: fclose: %s",
105                                 sstrerror (errno, errbuf, sizeof (errbuf)));
106         }
107
108         numfields = strsplit (buffer, fields, 8);
109
110         if (numfields < 3)
111                 return (-1);
112
113         snum = atof (fields[0]);
114         mnum = atof (fields[1]);
115         lnum = atof (fields[2]);
116
117         load_submit (snum, mnum, lnum);
118 /* #endif KERNEL_LINUX */
119
120 #elif HAVE_LIBSTATGRAB
121         gauge_t snum, mnum, lnum;
122         sg_load_stats *ls;
123
124         if ((ls = sg_get_load_stats ()) == NULL)
125                 return;
126
127         snum = ls->min1;
128         mnum = ls->min5;
129         lnum = ls->min15;
130
131         load_submit (snum, mnum, lnum);
132 /* #endif HAVE_LIBSTATGRAB */
133
134 #else
135 # error "No applicable input method."
136 #endif
137
138         return (0);
139 }
140
141 void module_register (void)
142 {
143         plugin_register_read ("load", load_read);
144 } /* void module_register */