removed
[collectd.git] / src / load.c
1 /**
2  * collectd - src/load.c
3  * Copyright (C) 2005  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 "load.h"
24
25 #if COLLECT_LOAD
26 #define MODULE_NAME "load"
27
28 #include "plugin.h"
29 #include "common.h"
30
31 #ifdef HAVE_SYS_LOADAVG_H
32 #include <sys/loadavg.h>
33 #endif
34
35 #ifdef HAVE_GETLOADAVG
36 #if !defined(LOADAVG_1MIN) || !defined(LOADAVG_5MIN) || !defined(LOADAVG_15MIN)
37 #define LOADAVG_1MIN  0
38 #define LOADAVG_5MIN  1
39 #define LOADAVG_15MIN 2
40 #endif
41 #endif /* defined(HAVE_GETLOADAVG) */
42
43 static char *load_file = "load.rrd";
44
45 static char *ds_def[] =
46 {
47         "DS:shortterm:GAUGE:25:0:100",
48         "DS:midterm:GAUGE:25:0:100",
49         "DS:longterm:GAUGE:25:0:100",
50         NULL
51 };
52 static int ds_num = 3;
53
54 void load_init (void)
55 {
56         return;
57 }
58
59 void load_write (char *host, char *inst, char *val)
60 {
61         rrd_update_file (host, load_file, val, ds_def, ds_num);
62 }
63
64 #define BUFSIZE 256
65 void load_submit (double snum, double mnum, double lnum)
66 {
67         char buf[BUFSIZE];
68
69         if (snprintf (buf, BUFSIZE, "%u:%.2f:%.2f:%.2f", (unsigned int) curtime,
70                                 snum, mnum, lnum) >= BUFSIZE)
71                 return;
72
73         plugin_submit (MODULE_NAME, "-", buf);
74 }
75 #undef BUFSIZE
76
77 void load_read (void)
78 {
79 #if defined(HAVE_GETLOADAVG)
80         double load[3];
81
82         if (getloadavg (load, 3) == 3)
83                 load_submit (load[LOADAVG_1MIN], load[LOADAVG_5MIN], load[LOADAVG_15MIN]);
84         else
85                 syslog (LOG_WARNING, "load: getloadavg failed: %s", strerror (errno));
86 /* #endif HAVE_GETLOADAVG */
87
88 #elif defined(KERNEL_LINUX)
89         double snum, mnum, lnum;
90         FILE *loadavg;
91         char buffer[16];
92
93         char *fields[8];
94         int numfields;
95         
96         if ((loadavg = fopen ("/proc/loadavg", "r")) == NULL)
97         {
98                 syslog (LOG_WARNING, "load: fopen: %s", strerror (errno));
99                 return;
100         }
101
102         if (fgets (buffer, 16, loadavg) == NULL)
103         {
104                 syslog (LOG_WARNING, "load: fgets: %s", strerror (errno));
105                 return;
106         }
107
108         if (fclose (loadavg))
109                 syslog (LOG_WARNING, "load: fclose: %s", strerror (errno));
110
111         numfields = strsplit (buffer, fields, 8);
112
113         if (numfields < 3)
114                 return;
115
116         snum = atof (fields[0]);
117         mnum = atof (fields[1]);
118         lnum = atof (fields[2]);
119
120         load_submit (snum, mnum, lnum);
121 /* #endif KERNEL_LINUX */
122
123 #elif defined(HAVE_LIBSTATGRAB)
124         double snum, mnum, lnum;
125         sg_load_stats *ls;
126
127         if ((ls = sg_get_load_stats ()) == NULL)
128                 return;
129
130         snum = ls->min1;
131         mnum = ls->min5;
132         lnum = ls->min15;
133
134         load_submit (snum, mnum, lnum);
135 #endif /* HAVE_LIBSTATGRAB */
136 }
137
138 void module_register (void)
139 {
140         plugin_register (MODULE_NAME, load_init, load_read, load_write);
141 }
142
143 #undef MODULE_NAME
144 #endif /* COLLECT_LOAD */