src/plugin.[ch]: Store the hostname in a global variable to minimize calls to `gethos...
[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; either version 2 of the License, or (at your
8  * option) any later version.
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  **/
22
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26
27 #define MODULE_NAME "load"
28
29 #if defined(HAVE_GETLOADAVG) || defined(KERNEL_LINUX) || defined(HAVE_LIBSTATGRAB)
30 # define LOAD_HAVE_READ 1
31 #else
32 # define LOAD_HAVE_READ 0
33 #endif
34
35 #ifdef HAVE_SYS_LOADAVG_H
36 #include <sys/loadavg.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 static data_source_t dsrc[3] =
48 {
49         {"shortterm", DS_TYPE_GAUGE, 0.0, 100.0},
50         {"midterm",   DS_TYPE_GAUGE, 0.0, 100.0},
51         {"longterm",  DS_TYPE_GAUGE, 0.0, 100.0}
52 };
53
54 static data_set_t ds =
55 {
56         "load", 3, dsrc
57 };
58
59 #if LOAD_HAVE_READ
60 static void load_submit (double snum, double mnum, double lnum)
61 {
62         value_t values[3];
63         value_list_t vl = VALUE_LIST_INIT;
64
65         values[0].gauge = snum;
66         values[1].gauge = mnum;
67         values[2].gauge = lnum;
68
69         vl.values = values;
70         vl.values_len = 3;
71         vl.time = time (NULL);
72         strcpy (vl.host, hostname);
73         strcpy (vl.plugin, "load");
74         strcpy (vl.plugin_instance, "");
75         strcpy (vl.type_instance, "");
76
77         plugin_dispatch_values ("load", &vl);
78 }
79
80 static int load_read (void)
81 {
82 #if defined(HAVE_GETLOADAVG)
83         double load[3];
84
85         if (getloadavg (load, 3) == 3)
86                 load_submit (load[LOADAVG_1MIN], load[LOADAVG_5MIN], load[LOADAVG_15MIN]);
87         else
88                 syslog (LOG_WARNING, "load: getloadavg failed: %s", strerror (errno));
89 /* #endif HAVE_GETLOADAVG */
90
91 #elif defined(KERNEL_LINUX)
92         double snum, mnum, lnum;
93         FILE *loadavg;
94         char buffer[16];
95
96         char *fields[8];
97         int numfields;
98         
99         if ((loadavg = fopen ("/proc/loadavg", "r")) == NULL)
100         {
101                 syslog (LOG_WARNING, "load: fopen: %s", strerror (errno));
102                 return;
103         }
104
105         if (fgets (buffer, 16, loadavg) == NULL)
106         {
107                 syslog (LOG_WARNING, "load: fgets: %s", strerror (errno));
108                 fclose (loadavg);
109                 return;
110         }
111
112         if (fclose (loadavg))
113                 syslog (LOG_WARNING, "load: fclose: %s", strerror (errno));
114
115         numfields = strsplit (buffer, fields, 8);
116
117         if (numfields < 3)
118                 return;
119
120         snum = atof (fields[0]);
121         mnum = atof (fields[1]);
122         lnum = atof (fields[2]);
123
124         load_submit (snum, mnum, lnum);
125 /* #endif KERNEL_LINUX */
126
127 #elif defined(HAVE_LIBSTATGRAB)
128         double snum, mnum, lnum;
129         sg_load_stats *ls;
130
131         if ((ls = sg_get_load_stats ()) == NULL)
132                 return;
133
134         snum = ls->min1;
135         mnum = ls->min5;
136         lnum = ls->min15;
137
138         load_submit (snum, mnum, lnum);
139 #endif /* HAVE_LIBSTATGRAB */
140
141         return (0);
142 }
143 #endif /* LOAD_HAVE_READ */
144
145 void module_register (void)
146 {
147         plugin_register_data_set (&ds);
148 #if LOAD_HAVE_READ
149         plugin_register_read ("load", load_read);
150 #endif
151 }
152
153 #undef MODULE_NAME