Merge pull request #1596 from rubenk/fix-a-few-more-prototypes
[collectd.git] / src / load.c
1 /**
2  * collectd - src/load.c
3  * Copyright (C) 2005-2008  Florian octo Forster
4  * Copyright (C) 2009       Manuel Sanmartin
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; only version 2 of the License is applicable.
9  *
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.
14  *
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
18  *
19  * Authors:
20  *   Florian octo Forster <octo at verplant.org>
21  *   Manuel Sanmartin
22  **/
23
24 #define _DEFAULT_SOURCE
25 #define _BSD_SOURCE
26
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
30
31 #ifdef HAVE_SYS_LOADAVG_H
32 #include <sys/loadavg.h>
33 #endif
34
35 #if HAVE_STATGRAB_H
36 # include <statgrab.h>
37 #endif
38
39 #ifdef HAVE_GETLOADAVG
40 #if !defined(LOADAVG_1MIN) || !defined(LOADAVG_5MIN) || !defined(LOADAVG_15MIN)
41 #define LOADAVG_1MIN  0
42 #define LOADAVG_5MIN  1
43 #define LOADAVG_15MIN 2
44 #endif
45 #endif /* defined(HAVE_GETLOADAVG) */
46
47 #ifdef HAVE_PERFSTAT
48 # include <sys/proc.h> /* AIX 5 */
49 # include <sys/protosw.h>
50 # include <libperfstat.h>
51 #endif /* HAVE_PERFSTAT */
52
53 static void load_submit (gauge_t snum, gauge_t mnum, gauge_t lnum)
54 {
55         value_t values[3];
56         value_list_t vl = VALUE_LIST_INIT;
57
58         values[0].gauge = snum;
59         values[1].gauge = mnum;
60         values[2].gauge = lnum;
61
62         vl.values = values;
63         vl.values_len = STATIC_ARRAY_SIZE (values);
64         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
65         sstrncpy (vl.plugin, "load", sizeof (vl.plugin));
66         sstrncpy (vl.type, "load", sizeof (vl.type));
67
68         plugin_dispatch_values (&vl);
69 }
70
71 static int load_read (void)
72 {
73 #if defined(HAVE_GETLOADAVG)
74         double load[3];
75
76         if (getloadavg (load, 3) == 3)
77                 load_submit (load[LOADAVG_1MIN], load[LOADAVG_5MIN], load[LOADAVG_15MIN]);
78         else
79         {
80                 char errbuf[1024];
81                 WARNING ("load: getloadavg failed: %s",
82                                 sstrerror (errno, errbuf, sizeof (errbuf)));
83         }
84 /* #endif HAVE_GETLOADAVG */
85
86 #elif defined(KERNEL_LINUX)
87         gauge_t snum, mnum, lnum;
88         FILE *loadavg;
89         char buffer[16];
90
91         char *fields[8];
92         int numfields;
93
94         if ((loadavg = fopen ("/proc/loadavg", "r")) == NULL)
95         {
96                 char errbuf[1024];
97                 WARNING ("load: fopen: %s",
98                                 sstrerror (errno, errbuf, sizeof (errbuf)));
99                 return (-1);
100         }
101
102         if (fgets (buffer, 16, loadavg) == NULL)
103         {
104                 char errbuf[1024];
105                 WARNING ("load: fgets: %s",
106                                 sstrerror (errno, errbuf, sizeof (errbuf)));
107                 fclose (loadavg);
108                 return (-1);
109         }
110
111         if (fclose (loadavg))
112         {
113                 char errbuf[1024];
114                 WARNING ("load: fclose: %s",
115                                 sstrerror (errno, errbuf, sizeof (errbuf)));
116         }
117
118         numfields = strsplit (buffer, fields, 8);
119
120         if (numfields < 3)
121                 return (-1);
122
123         snum = atof (fields[0]);
124         mnum = atof (fields[1]);
125         lnum = atof (fields[2]);
126
127         load_submit (snum, mnum, lnum);
128 /* #endif KERNEL_LINUX */
129
130 #elif HAVE_LIBSTATGRAB
131         gauge_t snum, mnum, lnum;
132         sg_load_stats *ls;
133
134         if ((ls = sg_get_load_stats ()) == NULL)
135                 return;
136
137         snum = ls->min1;
138         mnum = ls->min5;
139         lnum = ls->min15;
140
141         load_submit (snum, mnum, lnum);
142 /* #endif HAVE_LIBSTATGRAB */
143
144 #elif HAVE_PERFSTAT
145         gauge_t snum, mnum, lnum;
146         perfstat_cpu_total_t cputotal;
147
148         if (perfstat_cpu_total(NULL,  &cputotal, sizeof(perfstat_cpu_total_t), 1) < 0)
149         {
150                 char errbuf[1024];
151                 WARNING ("load: perfstat_cpu : %s",
152                                 sstrerror (errno, errbuf, sizeof (errbuf)));
153                 return (-1);
154         }
155
156         snum = (float)cputotal.loadavg[0]/(float)(1<<SBITS);
157         mnum = (float)cputotal.loadavg[1]/(float)(1<<SBITS);
158         lnum = (float)cputotal.loadavg[2]/(float)(1<<SBITS);
159
160         load_submit (snum, mnum, lnum);
161 /* #endif HAVE_PERFSTAT */
162
163 #else
164 # error "No applicable input method."
165 #endif
166
167         return (0);
168 }
169
170 void module_register (void)
171 {
172         plugin_register_read ("load", load_read);
173 } /* void module_register */