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 **/
22 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
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 */
32 #elif HAVE_LIBKSTAT
33 /* Using kstats chain to retrieve the boot time on Solaris / OpenSolaris systems
34 */
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
40 * systems */
41 /* #endif HAVE_SYS_SYSCTL_H */
43 #elif HAVE_PERFSTAT
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 */
49 #else
50 #error "No applicable input method."
51 #endif
53 /*
54 * Global variables
55 */
56 /* boottime always used, no OS distinction */
57 static time_t boottime;
59 #if HAVE_LIBKSTAT
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};
67 vl.values_len = 1;
69 sstrncpy(vl.plugin, "uptime", sizeof(vl.plugin));
70 sstrncpy(vl.type, "uptime", sizeof(vl.type));
72 plugin_dispatch_values(&vl);
73 }
75 static int uptime_init(void) /* {{{ */
76 {
77 /*
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.
86 */
88 #if KERNEL_LINUX
89 unsigned long starttime;
90 char buffer[1024];
91 int ret;
92 FILE *fh;
94 ret = 0;
96 fh = fopen(STAT_FILE, "r");
98 if (fh == NULL) {
99 char errbuf[1024];
100 ERROR("uptime plugin: Cannot open " STAT_FILE ": %s",
101 sstrerror(errno, errbuf, sizeof(errbuf)));
102 return (-1);
103 }
105 while (fgets(buffer, 1024, fh) != NULL) {
106 /* look for the btime string and read the value */
107 ret = sscanf(buffer, "btime %lu", &starttime);
108 /* avoid further loops if btime has been found and read
109 * correctly (hopefully) */
110 if (ret == 1)
111 break;
112 }
114 fclose(fh);
116 /* loop done, check if no value has been found/read */
117 if (ret != 1) {
118 ERROR("uptime plugin: No value read from " STAT_FILE "");
119 return (-1);
120 }
122 boottime = (time_t)starttime;
124 if (boottime == 0) {
125 ERROR("uptime plugin: btime read from " STAT_FILE ", "
126 "but `boottime' is zero!");
127 return (-1);
128 }
129 /* #endif KERNEL_LINUX */
131 #elif HAVE_LIBKSTAT
132 kstat_t *ksp;
133 kstat_named_t *knp;
135 ksp = NULL;
136 knp = NULL;
138 /* kstats chain already opened by update_kstat (using *kc), verify everything
139 * went fine. */
140 if (kc == NULL) {
141 ERROR("uptime plugin: kstat chain control structure not available.");
142 return (-1);
143 }
145 ksp = kstat_lookup(kc, "unix", 0, "system_misc");
146 if (ksp == NULL) {
147 ERROR("uptime plugin: Cannot find unix:0:system_misc kstat.");
148 return (-1);
149 }
151 if (kstat_read(kc, ksp, NULL) < 0) {
152 ERROR("uptime plugin: kstat_read failed.");
153 return (-1);
154 }
156 knp = (kstat_named_t *)kstat_data_lookup(ksp, "boot_time");
157 if (knp == NULL) {
158 ERROR("uptime plugin: kstat_data_lookup (boot_time) failed.");
159 return (-1);
160 }
162 boottime = (time_t)knp->value.ui32;
164 if (boottime == 0) {
165 ERROR("uptime plugin: kstat_data_lookup returned success, "
166 "but `boottime' is zero!");
167 return (-1);
168 }
169 /* #endif HAVE_LIBKSTAT */
171 #elif HAVE_SYS_SYSCTL_H
172 struct timeval boottv = {0};
173 size_t boottv_len;
174 int status;
176 int mib[] = {CTL_KERN, KERN_BOOTTIME};
178 boottv_len = sizeof(boottv);
180 status = sysctl(mib, STATIC_ARRAY_SIZE(mib), &boottv, &boottv_len,
181 /* new_value = */ NULL, /* new_length = */ 0);
182 if (status != 0) {
183 char errbuf[1024];
184 ERROR("uptime plugin: No value read from sysctl interface: %s",
185 sstrerror(errno, errbuf, sizeof(errbuf)));
186 return (-1);
187 }
189 boottime = boottv.tv_sec;
191 if (boottime == 0) {
192 ERROR("uptime plugin: sysctl(3) returned success, "
193 "but `boottime' is zero!");
194 return (-1);
195 }
196 /* #endif HAVE_SYS_SYSCTL_H */
198 #elif HAVE_PERFSTAT
199 int status;
200 perfstat_cpu_total_t cputotal;
201 int hertz;
203 status = perfstat_cpu_total(NULL, &cputotal, sizeof(perfstat_cpu_total_t), 1);
204 if (status < 0) {
205 char errbuf[1024];
206 ERROR("uptime plugin: perfstat_cpu_total: %s",
207 sstrerror(errno, errbuf, sizeof(errbuf)));
208 return (-1);
209 }
211 hertz = sysconf(_SC_CLK_TCK);
212 if (hertz <= 0)
213 hertz = HZ;
215 boottime = time(NULL) - cputotal.lbolt / hertz;
216 #endif /* HAVE_PERFSTAT */
218 return (0);
219 } /* }}} int uptime_init */
221 static int uptime_read(void) {
222 gauge_t uptime;
223 time_t elapsed;
225 /* calculate the amount of time elapsed since boot, AKA uptime */
226 elapsed = time(NULL) - boottime;
228 uptime = (gauge_t)elapsed;
230 uptime_submit(uptime);
232 return (0);
233 }
235 void module_register(void) {
236 plugin_register_init("uptime", uptime_init);
237 plugin_register_read("uptime", uptime_read);
238 } /* void module_register */