Tree wide: Don't set vl.host to hostname_g in plugin code.
[collectd.git] / src / uptime.c
1 /**
2  * collectd - src/uptime.c
3  * Copyright (C) 2009   Marco Chiappero
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  *   Marco Chiappero <marco at absence.it>
20  **/
21
22 #include "collectd.h"
23
24 #include "common.h"
25 #include "plugin.h"
26
27 #if KERNEL_LINUX
28 # define STAT_FILE "/proc/stat"
29 /* Using /proc filesystem to retrieve the boot time, Linux only. */
30 /* #endif KERNEL_LINUX */
31
32 #elif HAVE_LIBKSTAT
33 /* Using kstats chain to retrieve the boot time on Solaris / OpenSolaris systems */
34 /* #endif HAVE_LIBKSTAT */
35
36 #elif HAVE_SYS_SYSCTL_H
37 # include <sys/sysctl.h>
38 /* Using sysctl interface to retrieve the boot time on *BSD / Darwin / OS X systems */
39 /* #endif HAVE_SYS_SYSCTL_H */
40
41 #elif HAVE_PERFSTAT
42 # include <sys/protosw.h>
43 # include <libperfstat.h>
44 /* Using perfstat_cpu_total to retrive the boot time in AIX */
45 /* #endif HAVE_PERFSTAT */
46
47 #else
48 # error "No applicable input method."
49 #endif
50
51 /*
52  * Global variables
53  */
54 /* boottime always used, no OS distinction */
55 static time_t boottime;
56
57 #if HAVE_LIBKSTAT
58 extern kstat_ctl_t *kc;
59 #endif /* #endif HAVE_LIBKSTAT */
60
61 static void uptime_submit (gauge_t value)
62 {
63         value_list_t vl = VALUE_LIST_INIT;
64
65         vl.values = &(value_t) { .gauge = value };
66         vl.values_len = 1;
67
68         sstrncpy (vl.plugin, "uptime", sizeof (vl.plugin));
69         sstrncpy (vl.type, "uptime", sizeof (vl.type));
70
71         plugin_dispatch_values (&vl);
72 }
73
74 static int uptime_init (void) /* {{{ */
75 {
76         /*
77          * On most unix systems the uptime is calculated by looking at the boot
78          * time (stored in unix time, since epoch) and the current one. We are
79          * going to do the same, reading the boot time value while executing
80          * the uptime_init function (there is no need to read, every time the
81          * plugin_read is called, a value that won't change). However, since
82          * uptime_init is run only once, if the function fails in retrieving
83          * the boot time, the plugin is unregistered and there is no chance to
84          * try again later. Nevertheless, this is very unlikely to happen.
85          */
86
87 #if KERNEL_LINUX
88         unsigned long starttime;
89         char buffer[1024];
90         int ret;
91         FILE *fh;
92
93         ret = 0;
94
95         fh = fopen (STAT_FILE, "r");
96
97         if (fh == NULL)
98         {
99                 char errbuf[1024];
100                 ERROR ("uptime plugin: Cannot open "STAT_FILE": %s",
101                         sstrerror (errno, errbuf, sizeof (errbuf)));
102                 return (-1);
103         }
104
105         while (fgets (buffer, 1024, fh) != NULL)
106         {
107                 /* look for the btime string and read the value */
108                 ret = sscanf (buffer, "btime %lu", &starttime);
109                 /* avoid further loops if btime has been found and read
110                  * correctly (hopefully) */
111                 if (ret == 1)
112                         break;
113         }
114
115         fclose (fh);
116
117         /* loop done, check if no value has been found/read */
118         if (ret != 1)
119         {
120                 ERROR ("uptime plugin: No value read from "STAT_FILE"");
121                 return (-1);
122         }
123
124         boottime = (time_t) starttime;
125
126         if (boottime == 0)
127         {
128                 ERROR ("uptime plugin: btime read from "STAT_FILE", "
129                                 "but `boottime' is zero!");
130                 return (-1);
131         }
132 /* #endif KERNEL_LINUX */
133
134 #elif HAVE_LIBKSTAT
135         kstat_t *ksp;
136         kstat_named_t *knp;
137
138         ksp = NULL;
139         knp = NULL;
140
141         /* kstats chain already opened by update_kstat (using *kc), verify everything went fine. */
142         if (kc == NULL)
143         {
144                 ERROR ("uptime plugin: kstat chain control structure not available.");
145                 return (-1);
146         }
147
148         ksp = kstat_lookup (kc, "unix", 0, "system_misc");
149         if (ksp == NULL)
150         {
151                 ERROR ("uptime plugin: Cannot find unix:0:system_misc kstat.");
152                 return (-1);
153         }
154
155         if (kstat_read (kc, ksp, NULL) < 0)
156         {
157                 ERROR ("uptime plugin: kstat_read failed.");
158                 return (-1);
159         }
160
161         knp = (kstat_named_t *) kstat_data_lookup (ksp, "boot_time");
162         if (knp == NULL)
163         {
164                 ERROR ("uptime plugin: kstat_data_lookup (boot_time) failed.");
165                 return (-1);
166         }
167
168         boottime = (time_t) knp->value.ui32;
169
170         if (boottime == 0)
171         {
172                 ERROR ("uptime plugin: kstat_data_lookup returned success, "
173                         "but `boottime' is zero!");
174                 return (-1);
175         }
176 /* #endif HAVE_LIBKSTAT */
177
178 # elif HAVE_SYS_SYSCTL_H
179         struct timeval boottv = { 0 };
180         size_t boottv_len;
181         int status;
182
183         int mib[] = { CTL_KERN, KERN_BOOTTIME };
184
185         boottv_len = sizeof (boottv);
186
187         status = sysctl (mib, STATIC_ARRAY_SIZE (mib), &boottv, &boottv_len,
188                         /* new_value = */ NULL, /* new_length = */ 0);
189         if (status != 0)
190         {
191                 char errbuf[1024];
192                 ERROR ("uptime plugin: No value read from sysctl interface: %s",
193                         sstrerror (errno, errbuf, sizeof (errbuf)));
194                 return (-1);
195         }
196
197         boottime = boottv.tv_sec;
198
199         if (boottime == 0)
200         {
201                 ERROR ("uptime plugin: sysctl(3) returned success, "
202                                 "but `boottime' is zero!");
203                 return (-1);
204         }
205 /* #endif HAVE_SYS_SYSCTL_H */
206
207 #elif HAVE_PERFSTAT
208         int status;
209         perfstat_cpu_total_t cputotal;
210         int hertz;
211
212         status = perfstat_cpu_total(NULL, &cputotal,
213                 sizeof(perfstat_cpu_total_t), 1);
214         if (status < 0)
215         {
216                 char errbuf[1024];
217                 ERROR ("uptime plugin: perfstat_cpu_total: %s",
218                         sstrerror (errno, errbuf, sizeof (errbuf)));
219                 return (-1);
220         }
221
222         hertz = sysconf(_SC_CLK_TCK);
223         if (hertz <= 0)
224                 hertz = HZ;
225
226         boottime = time(NULL) - cputotal.lbolt / hertz;
227 #endif /* HAVE_PERFSTAT */
228
229         return (0);
230 } /* }}} int uptime_init */
231
232 static int uptime_read (void)
233 {
234         gauge_t uptime;
235         time_t elapsed;
236
237         /* calculate the amount of time elapsed since boot, AKA uptime */
238         elapsed = time (NULL) - boottime;
239
240         uptime = (gauge_t) elapsed;
241
242         uptime_submit (uptime);
243
244         return (0);
245 }
246
247 void module_register (void)
248 {
249         plugin_register_init ("uptime", uptime_init);
250         plugin_register_read ("uptime", uptime_read);
251 } /* void module_register */