solaris-fixes branch: Applied the swap-patch by Christophe Kalt.
[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                 return;
111         }
112
113         if (fclose (loadavg))
114                 syslog (LOG_WARNING, "load: fclose: %s", strerror (errno));
115
116         numfields = strsplit (buffer, fields, 8);
117
118         if (numfields < 3)
119                 return;
120
121         snum = atof (fields[0]);
122         mnum = atof (fields[1]);
123         lnum = atof (fields[2]);
124
125         load_submit (snum, mnum, lnum);
126 /* #endif KERNEL_LINUX */
127
128 #elif defined(HAVE_LIBSTATGRAB)
129         double snum, mnum, lnum;
130         sg_load_stats *ls;
131
132         if ((ls = sg_get_load_stats ()) == NULL)
133                 return;
134
135         snum = ls->min1;
136         mnum = ls->min5;
137         lnum = ls->min15;
138
139         load_submit (snum, mnum, lnum);
140 #endif /* HAVE_LIBSTATGRAB */
141 }
142 #else
143 # define load_read NULL
144 #endif /* LOAD_HAVE_READ */
145
146 void module_register (void)
147 {
148         plugin_register (MODULE_NAME, load_init, load_read, load_write);
149 }
150
151 #undef MODULE_NAME