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 UPTIME_FILE "/proc/uptime"
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
35 /* #endif HAVE_LIBKSTAT */
37 #elif HAVE_SYS_SYSCTL_H
38 #include <sys/sysctl.h>
39 /* Using sysctl interface to retrieve the boot time on *BSD / Darwin / OS X
41 /* #endif HAVE_SYS_SYSCTL_H */
44 #include <libperfstat.h>
45 #include <sys/protosw.h>
46 /* Using perfstat_cpu_total to retrive the boot time in AIX */
47 /* #endif HAVE_PERFSTAT */
50 #error "No applicable input method."
56 /* boottime always used, no OS distinction */
57 static time_t boottime;
60 extern kstat_ctl_t *kc;
61 #endif /* #endif HAVE_LIBKSTAT */
63 static void uptime_submit(gauge_t value) {
64 value_list_t vl = VALUE_LIST_INIT;
66 vl.values = &(value_t){.gauge = value};
69 sstrncpy(vl.plugin, "uptime", sizeof(vl.plugin));
70 sstrncpy(vl.type, "uptime", sizeof(vl.type));
72 plugin_dispatch_values(&vl);
75 static int uptime_init(void) /* {{{ */
78 * On most unix systems the uptime is calculated by looking at the boot
79 * time (stored in unix time, since epoch) and the current one. We are
80 * going to do the same, reading the boot time value while executing
81 * the uptime_init function (there is no need to read, every time the
82 * plugin_read is called, a value that won't change). However, since
83 * uptime_init is run only once, if the function fails in retrieving
84 * the boot time, the plugin is unregistered and there is no chance to
85 * try again later. Nevertheless, this is very unlikely to happen.
89 FILE *fh = fopen(UPTIME_FILE, "r");
92 ERROR("uptime plugin: Cannot open " UPTIME_FILE ": %s",
93 sstrerror(errno, errbuf, sizeof(errbuf)));
97 double uptime_seconds = 0.0;
98 if (fscanf(fh, "%lf", &uptime_seconds) != 1) {
99 ERROR("uptime plugin: No value read from " UPTIME_FILE "");
106 if (uptime_seconds == 0.0) {
107 ERROR("uptime plugin: uptime read from " UPTIME_FILE ", "
112 boottime = time(NULL) - (long)uptime_seconds;
113 /* #endif KERNEL_LINUX */
122 /* kstats chain already opened by update_kstat (using *kc), verify everything
125 ERROR("uptime plugin: kstat chain control structure not available.");
129 ksp = kstat_lookup(kc, "unix", 0, "system_misc");
131 ERROR("uptime plugin: Cannot find unix:0:system_misc kstat.");
135 if (kstat_read(kc, ksp, NULL) < 0) {
136 ERROR("uptime plugin: kstat_read failed.");
140 knp = (kstat_named_t *)kstat_data_lookup(ksp, "boot_time");
142 ERROR("uptime plugin: kstat_data_lookup (boot_time) failed.");
146 boottime = (time_t)knp->value.ui32;
149 ERROR("uptime plugin: kstat_data_lookup returned success, "
150 "but `boottime' is zero!");
153 /* #endif HAVE_LIBKSTAT */
155 #elif HAVE_SYS_SYSCTL_H
156 struct timeval boottv = {0};
160 int mib[] = {CTL_KERN, KERN_BOOTTIME};
162 boottv_len = sizeof(boottv);
164 status = sysctl(mib, STATIC_ARRAY_SIZE(mib), &boottv, &boottv_len,
165 /* new_value = */ NULL, /* new_length = */ 0);
168 ERROR("uptime plugin: No value read from sysctl interface: %s",
169 sstrerror(errno, errbuf, sizeof(errbuf)));
173 boottime = boottv.tv_sec;
176 ERROR("uptime plugin: sysctl(3) returned success, "
177 "but `boottime' is zero!");
180 /* #endif HAVE_SYS_SYSCTL_H */
184 perfstat_cpu_total_t cputotal;
187 status = perfstat_cpu_total(NULL, &cputotal, sizeof(perfstat_cpu_total_t), 1);
190 ERROR("uptime plugin: perfstat_cpu_total: %s",
191 sstrerror(errno, errbuf, sizeof(errbuf)));
195 hertz = sysconf(_SC_CLK_TCK);
199 boottime = time(NULL) - cputotal.lbolt / hertz;
200 #endif /* HAVE_PERFSTAT */
203 } /* }}} int uptime_init */
205 static int uptime_read(void) {
209 /* calculate the amount of time elapsed since boot, AKA uptime */
210 elapsed = time(NULL) - boottime;
212 uptime = (gauge_t)elapsed;
214 uptime_submit(uptime);
219 void module_register(void) {
220 plugin_register_init("uptime", uptime_init);
221 plugin_register_read("uptime", uptime_read);
222 } /* void module_register */