b92e54f53b60380b77df26f7e1225a571f36c4d3
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 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
26 #define MODULE_NAME "load"
28 #if defined(HAVE_GETLOADAVG) || defined(KERNEL_LINUX) || defined(HAVE_LIBSTATGRAB)
29 # define LOAD_HAVE_READ 1
30 #else
31 # define LOAD_HAVE_READ 0
32 #endif
34 #ifdef HAVE_SYS_LOADAVG_H
35 #include <sys/loadavg.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 static data_source_t dsrc[3] =
47 {
48 {"shortterm", DS_TYPE_GAUGE, 0.0, 100.0},
49 {"midterm", DS_TYPE_GAUGE, 0.0, 100.0},
50 {"longterm", DS_TYPE_GAUGE, 0.0, 100.0}
51 };
53 static data_set_t ds =
54 {
55 "load", 3, dsrc
56 };
58 #if LOAD_HAVE_READ
59 static void load_submit (gauge_t snum, gauge_t mnum, gauge_t lnum)
60 {
61 value_t values[3];
62 value_list_t vl = VALUE_LIST_INIT;
64 values[0].gauge = snum;
65 values[1].gauge = mnum;
66 values[2].gauge = lnum;
68 vl.values = values;
69 vl.values_len = STATIC_ARRAY_SIZE (values);
70 vl.time = time (NULL);
71 strcpy (vl.host, hostname_g);
72 strcpy (vl.plugin, "load");
74 plugin_dispatch_values ("load", &vl);
75 }
77 static int load_read (void)
78 {
79 #if defined(HAVE_GETLOADAVG)
80 double load[3];
82 if (getloadavg (load, 3) == 3)
83 load_submit (load[LOADAVG_1MIN], load[LOADAVG_5MIN], load[LOADAVG_15MIN]);
84 else
85 {
86 char errbuf[1024];
87 WARNING ("load: getloadavg failed: %s",
88 sstrerror (errno, errbuf, sizeof (errbuf)));
89 }
90 /* #endif HAVE_GETLOADAVG */
92 #elif defined(KERNEL_LINUX)
93 gauge_t snum, mnum, lnum;
94 FILE *loadavg;
95 char buffer[16];
97 char *fields[8];
98 int numfields;
100 if ((loadavg = fopen ("/proc/loadavg", "r")) == NULL)
101 {
102 char errbuf[1024];
103 WARNING ("load: fopen: %s",
104 sstrerror (errno, errbuf, sizeof (errbuf)));
105 return;
106 }
108 if (fgets (buffer, 16, loadavg) == NULL)
109 {
110 char errbuf[1024];
111 WARNING ("load: fgets: %s",
112 sstrerror (errno, errbuf, sizeof (errbuf)));
113 fclose (loadavg);
114 return;
115 }
117 if (fclose (loadavg))
118 {
119 char errbuf[1024];
120 WARNING ("load: fclose: %s",
121 sstrerror (errno, errbuf, sizeof (errbuf)));
122 }
124 numfields = strsplit (buffer, fields, 8);
126 if (numfields < 3)
127 return;
129 snum = atof (fields[0]);
130 mnum = atof (fields[1]);
131 lnum = atof (fields[2]);
133 load_submit (snum, mnum, lnum);
134 /* #endif KERNEL_LINUX */
136 #elif defined(HAVE_LIBSTATGRAB)
137 gauge_t snum, mnum, lnum;
138 sg_load_stats *ls;
140 if ((ls = sg_get_load_stats ()) == NULL)
141 return;
143 snum = ls->min1;
144 mnum = ls->min5;
145 lnum = ls->min15;
147 load_submit (snum, mnum, lnum);
148 #endif /* HAVE_LIBSTATGRAB */
150 return (0);
151 }
152 #endif /* LOAD_HAVE_READ */
154 void module_register (void)
155 {
156 plugin_register_data_set (&ds);
157 #if LOAD_HAVE_READ
158 plugin_register_read ("load", load_read);
159 #endif
160 }
162 #undef MODULE_NAME