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