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