Code

Merge remote-tracking branch 'github/pr/387'
[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  **/
24 #define _BSD_SOURCE
26 #include "collectd.h"
27 #include "common.h"
28 #include "plugin.h"
30 #ifdef HAVE_SYS_LOADAVG_H
31 #include <sys/loadavg.h>
32 #endif
34 #if HAVE_STATGRAB_H
35 # include <statgrab.h>
36 #endif
38 #ifdef HAVE_GETLOADAVG
39 #if !defined(LOADAVG_1MIN) || !defined(LOADAVG_5MIN) || !defined(LOADAVG_15MIN)
40 #define LOADAVG_1MIN  0
41 #define LOADAVG_5MIN  1
42 #define LOADAVG_15MIN 2
43 #endif
44 #endif /* defined(HAVE_GETLOADAVG) */
46 #ifdef HAVE_PERFSTAT
47 # include <sys/proc.h> /* AIX 5 */
48 # include <sys/protosw.h>
49 # include <libperfstat.h>
50 #endif /* HAVE_PERFSTAT */
52 static void load_submit (gauge_t snum, gauge_t mnum, gauge_t lnum)
53 {
54         value_t values[3];
55         value_list_t vl = VALUE_LIST_INIT;
57         values[0].gauge = snum;
58         values[1].gauge = mnum;
59         values[2].gauge = lnum;
61         vl.values = values;
62         vl.values_len = STATIC_ARRAY_SIZE (values);
63         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
64         sstrncpy (vl.plugin, "load", sizeof (vl.plugin));
65         sstrncpy (vl.type, "load", sizeof (vl.type));
67         plugin_dispatch_values (&vl);
68 }
70 static int load_read (void)
71 {
72 #if defined(HAVE_GETLOADAVG)
73         double load[3];
75         if (getloadavg (load, 3) == 3)
76                 load_submit (load[LOADAVG_1MIN], load[LOADAVG_5MIN], load[LOADAVG_15MIN]);
77         else
78         {
79                 char errbuf[1024];
80                 WARNING ("load: getloadavg failed: %s",
81                                 sstrerror (errno, errbuf, sizeof (errbuf)));
82         }
83 /* #endif HAVE_GETLOADAVG */
85 #elif defined(KERNEL_LINUX)
86         gauge_t snum, mnum, lnum;
87         FILE *loadavg;
88         char buffer[16];
90         char *fields[8];
91         int numfields;
93         if ((loadavg = fopen ("/proc/loadavg", "r")) == NULL)
94         {
95                 char errbuf[1024];
96                 WARNING ("load: fopen: %s",
97                                 sstrerror (errno, errbuf, sizeof (errbuf)));
98                 return (-1);
99         }
101         if (fgets (buffer, 16, loadavg) == NULL)
102         {
103                 char errbuf[1024];
104                 WARNING ("load: fgets: %s",
105                                 sstrerror (errno, errbuf, sizeof (errbuf)));
106                 fclose (loadavg);
107                 return (-1);
108         }
110         if (fclose (loadavg))
111         {
112                 char errbuf[1024];
113                 WARNING ("load: fclose: %s",
114                                 sstrerror (errno, errbuf, sizeof (errbuf)));
115         }
117         numfields = strsplit (buffer, fields, 8);
119         if (numfields < 3)
120                 return (-1);
122         snum = atof (fields[0]);
123         mnum = atof (fields[1]);
124         lnum = atof (fields[2]);
126         load_submit (snum, mnum, lnum);
127 /* #endif KERNEL_LINUX */
129 #elif HAVE_LIBSTATGRAB
130         gauge_t snum, mnum, lnum;
131         sg_load_stats *ls;
133         if ((ls = sg_get_load_stats ()) == NULL)
134                 return;
136         snum = ls->min1;
137         mnum = ls->min5;
138         lnum = ls->min15;
140         load_submit (snum, mnum, lnum);
141 /* #endif HAVE_LIBSTATGRAB */
143 #elif HAVE_PERFSTAT
144         gauge_t snum, mnum, lnum;
145         perfstat_cpu_total_t cputotal;
147         if (perfstat_cpu_total(NULL,  &cputotal, sizeof(perfstat_cpu_total_t), 1) < 0)
148         {
149                 char errbuf[1024];
150                 WARNING ("load: perfstat_cpu : %s",
151                                 sstrerror (errno, errbuf, sizeof (errbuf)));
152                 return (-1);
153         }
155         snum = (float)cputotal.loadavg[0]/(float)(1<<SBITS);
156         mnum = (float)cputotal.loadavg[1]/(float)(1<<SBITS);
157         lnum = (float)cputotal.loadavg[2]/(float)(1<<SBITS);
159         load_submit (snum, mnum, lnum);
160 /* #endif HAVE_PERFSTAT */
162 #else
163 # error "No applicable input method."
164 #endif
166         return (0);
169 void module_register (void)
171         plugin_register_read ("load", load_read);
172 } /* void module_register */