Bumped version to 3.11.6; Updated ChangeLog.
[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 char *load_file = "load.rrd";
48
49 static char *ds_def[] =
50 {
51         "DS:shortterm:GAUGE:"COLLECTD_HEARTBEAT":0:100",
52         "DS:midterm:GAUGE:"COLLECTD_HEARTBEAT":0:100",
53         "DS:longterm:GAUGE:"COLLECTD_HEARTBEAT":0:100",
54         NULL
55 };
56 static int ds_num = 3;
57
58 static void load_init (void)
59 {
60         return;
61 }
62
63 static void load_write (char *host, char *inst, char *val)
64 {
65         rrd_update_file (host, load_file, val, ds_def, ds_num);
66 }
67
68 #if LOAD_HAVE_READ
69 #define BUFSIZE 256
70 static void load_submit (double snum, double mnum, double lnum)
71 {
72         char buf[BUFSIZE];
73
74         if (snprintf (buf, BUFSIZE, "%u:%.2f:%.2f:%.2f", (unsigned int) curtime,
75                                 snum, mnum, lnum) >= BUFSIZE)
76                 return;
77
78         plugin_submit (MODULE_NAME, "-", buf);
79 }
80 #undef BUFSIZE
81
82 static void load_read (void)
83 {
84 #if defined(HAVE_GETLOADAVG)
85         double load[3];
86
87         if (getloadavg (load, 3) == 3)
88                 load_submit (load[LOADAVG_1MIN], load[LOADAVG_5MIN], load[LOADAVG_15MIN]);
89         else
90                 syslog (LOG_WARNING, "load: getloadavg failed: %s", strerror (errno));
91 /* #endif HAVE_GETLOADAVG */
92
93 #elif defined(KERNEL_LINUX)
94         double snum, mnum, lnum;
95         FILE *loadavg;
96         char buffer[16];
97
98         char *fields[8];
99         int numfields;
100         
101         if ((loadavg = fopen ("/proc/loadavg", "r")) == NULL)
102         {
103                 syslog (LOG_WARNING, "load: fopen: %s", strerror (errno));
104                 return;
105         }
106
107         if (fgets (buffer, 16, loadavg) == NULL)
108         {
109                 syslog (LOG_WARNING, "load: fgets: %s", strerror (errno));
110                 fclose (loadavg);
111                 return;
112         }
113
114         if (fclose (loadavg))
115                 syslog (LOG_WARNING, "load: fclose: %s", strerror (errno));
116
117         numfields = strsplit (buffer, fields, 8);
118
119         if (numfields < 3)
120                 return;
121
122         snum = atof (fields[0]);
123         mnum = atof (fields[1]);
124         lnum = atof (fields[2]);
125
126         load_submit (snum, mnum, lnum);
127 /* #endif KERNEL_LINUX */
128
129 #elif defined(HAVE_LIBSTATGRAB)
130         double snum, mnum, lnum;
131         sg_load_stats *ls;
132
133         if ((ls = sg_get_load_stats ()) == NULL)
134                 return;
135
136         snum = ls->min1;
137         mnum = ls->min5;
138         lnum = ls->min15;
139
140         load_submit (snum, mnum, lnum);
141 #endif /* HAVE_LIBSTATGRAB */
142 }
143 #else
144 # define load_read NULL
145 #endif /* LOAD_HAVE_READ */
146
147 void module_register (void)
148 {
149         plugin_register (MODULE_NAME, load_init, load_read, load_write);
150 }
151
152 #undef MODULE_NAME