Let plugin_dispatch_values() set value_list.time in case of 'now'.
[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         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
54         sstrncpy (vl.plugin, "load", sizeof (vl.plugin));
55         sstrncpy (vl.type, "load", sizeof (vl.type));
56
57         plugin_dispatch_values (&vl);
58 }
59
60 static int load_read (void)
61 {
62 #if defined(HAVE_GETLOADAVG)
63         double load[3];
64
65         if (getloadavg (load, 3) == 3)
66                 load_submit (load[LOADAVG_1MIN], load[LOADAVG_5MIN], load[LOADAVG_15MIN]);
67         else
68         {
69                 char errbuf[1024];
70                 WARNING ("load: getloadavg failed: %s",
71                                 sstrerror (errno, errbuf, sizeof (errbuf)));
72         }
73 /* #endif HAVE_GETLOADAVG */
74
75 #elif defined(KERNEL_LINUX)
76         gauge_t snum, mnum, lnum;
77         FILE *loadavg;
78         char buffer[16];
79
80         char *fields[8];
81         int numfields;
82         
83         if ((loadavg = fopen ("/proc/loadavg", "r")) == NULL)
84         {
85                 char errbuf[1024];
86                 WARNING ("load: fopen: %s",
87                                 sstrerror (errno, errbuf, sizeof (errbuf)));
88                 return (-1);
89         }
90
91         if (fgets (buffer, 16, loadavg) == NULL)
92         {
93                 char errbuf[1024];
94                 WARNING ("load: fgets: %s",
95                                 sstrerror (errno, errbuf, sizeof (errbuf)));
96                 fclose (loadavg);
97                 return (-1);
98         }
99
100         if (fclose (loadavg))
101         {
102                 char errbuf[1024];
103                 WARNING ("load: fclose: %s",
104                                 sstrerror (errno, errbuf, sizeof (errbuf)));
105         }
106
107         numfields = strsplit (buffer, fields, 8);
108
109         if (numfields < 3)
110                 return (-1);
111
112         snum = atof (fields[0]);
113         mnum = atof (fields[1]);
114         lnum = atof (fields[2]);
115
116         load_submit (snum, mnum, lnum);
117 /* #endif KERNEL_LINUX */
118
119 #elif HAVE_LIBSTATGRAB
120         gauge_t snum, mnum, lnum;
121         sg_load_stats *ls;
122
123         if ((ls = sg_get_load_stats ()) == NULL)
124                 return;
125
126         snum = ls->min1;
127         mnum = ls->min5;
128         lnum = ls->min15;
129
130         load_submit (snum, mnum, lnum);
131 /* #endif HAVE_LIBSTATGRAB */
132
133 #else
134 # error "No applicable input method."
135 #endif
136
137         return (0);
138 }
139
140 void module_register (void)
141 {
142         plugin_register_read ("load", load_read);
143 } /* void module_register */