2 * collectd - src/uptime.c
3 * Copyright (C) 2009 Marco Chiappero
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.
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.
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
19 * Marco Chiappero <marco at absence.it>
28 # define STAT_FILE "/proc/stat"
29 /* Using /proc filesystem to retrieve the boot time, Linux only. */
30 /* #endif KERNEL_LINUX */
33 /* Using kstats chain to retrieve the boot time on Solaris / OpenSolaris systems */
34 /* #endif HAVE_LIBKSTAT */
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 */
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 */
48 # error "No applicable input method."
54 /* boottime always used, no OS distinction */
55 static time_t boottime;
58 extern kstat_ctl_t *kc;
59 #endif /* #endif HAVE_LIBKSTAT */
61 static void uptime_submit (gauge_t uptime)
64 value_list_t vl = VALUE_LIST_INIT;
66 values[0].gauge = uptime;
71 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
72 sstrncpy (vl.plugin, "uptime", sizeof (vl.plugin));
73 sstrncpy (vl.type, "uptime", sizeof (vl.type));
75 plugin_dispatch_values (&vl);
78 static int uptime_init (void) /* {{{ */
81 * On most unix systems the uptime is calculated by looking at the boot
82 * time (stored in unix time, since epoch) and the current one. We are
83 * going to do the same, reading the boot time value while executing
84 * the uptime_init function (there is no need to read, every time the
85 * plugin_read is called, a value that won't change). However, since
86 * uptime_init is run only once, if the function fails in retrieving
87 * the boot time, the plugin is unregistered and there is no chance to
88 * try again later. Nevertheless, this is very unlikely to happen.
92 unsigned long starttime;
99 fh = fopen (STAT_FILE, "r");
104 ERROR ("uptime plugin: Cannot open "STAT_FILE": %s",
105 sstrerror (errno, errbuf, sizeof (errbuf)));
109 while (fgets (buffer, 1024, fh) != NULL)
111 /* look for the btime string and read the value */
112 ret = sscanf (buffer, "btime %lu", &starttime);
113 /* avoid further loops if btime has been found and read
114 * correctly (hopefully) */
121 /* loop done, check if no value has been found/read */
124 ERROR ("uptime plugin: No value read from "STAT_FILE"");
128 boottime = (time_t) starttime;
132 ERROR ("uptime plugin: btime read from "STAT_FILE", "
133 "but `boottime' is zero!");
136 /* #endif KERNEL_LINUX */
145 /* kstats chain already opened by update_kstat (using *kc), verify everything went fine. */
148 ERROR ("uptime plugin: kstat chain control structure not available.");
152 ksp = kstat_lookup (kc, "unix", 0, "system_misc");
155 ERROR ("uptime plugin: Cannot find unix:0:system_misc kstat.");
159 if (kstat_read (kc, ksp, NULL) < 0)
161 ERROR ("uptime plugin: kstat_read failed.");
165 knp = (kstat_named_t *) kstat_data_lookup (ksp, "boot_time");
168 ERROR ("uptime plugin: kstat_data_lookup (boot_time) failed.");
172 boottime = (time_t) knp->value.ui32;
176 ERROR ("uptime plugin: kstat_data_lookup returned success, "
177 "but `boottime' is zero!");
180 /* #endif HAVE_LIBKSTAT */
182 # elif HAVE_SYS_SYSCTL_H
183 struct timeval boottv = { 0 };
187 int mib[] = { CTL_KERN, KERN_BOOTTIME };
189 boottv_len = sizeof (boottv);
191 status = sysctl (mib, STATIC_ARRAY_SIZE (mib), &boottv, &boottv_len,
192 /* new_value = */ NULL, /* new_length = */ 0);
196 ERROR ("uptime plugin: No value read from sysctl interface: %s",
197 sstrerror (errno, errbuf, sizeof (errbuf)));
201 boottime = boottv.tv_sec;
205 ERROR ("uptime plugin: sysctl(3) returned success, "
206 "but `boottime' is zero!");
209 /* #endif HAVE_SYS_SYSCTL_H */
213 perfstat_cpu_total_t cputotal;
216 status = perfstat_cpu_total(NULL, &cputotal,
217 sizeof(perfstat_cpu_total_t), 1);
221 ERROR ("uptime plugin: perfstat_cpu_total: %s",
222 sstrerror (errno, errbuf, sizeof (errbuf)));
226 hertz = sysconf(_SC_CLK_TCK);
230 boottime = time(NULL) - cputotal.lbolt / hertz;
231 #endif /* HAVE_PERFSTAT */
234 } /* }}} int uptime_init */
236 static int uptime_read (void)
241 /* calculate the amount of time elapsed since boot, AKA uptime */
242 elapsed = time (NULL) - boottime;
244 uptime = (gauge_t) elapsed;
246 uptime_submit (uptime);
251 void module_register (void)
253 plugin_register_init ("uptime", uptime_init);
254 plugin_register_read ("uptime", uptime_read);
255 } /* void module_register */