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"
23 #include "common.h"
24 #include "plugin.h"
26 #if KERNEL_LINUX
27 # define STAT_FILE "/proc/stat"
28 /* Using /proc filesystem to retrieve the boot time, Linux only. */
29 /* #endif KERNEL_LINUX */
31 #elif HAVE_LIBKSTAT
32 /* Using kstats chain to retrieve the boot time on Solaris / OpenSolaris systems */
33 /* #endif HAVE_LIBKSTAT */
35 #elif HAVE_SYS_SYSCTL_H
36 # include <sys/sysctl.h>
37 /* Using sysctl interface to retrieve the boot time on *BSD / Darwin / OS X systems */
38 /* #endif HAVE_SYS_SYSCTL_H */
40 #else
41 # error "No applicable input method."
42 #endif
44 /*
45 * Global variables
46 */
47 /* boottime always used, no OS distinction */
48 static time_t boottime;
50 #if HAVE_LIBKSTAT
51 extern kstat_ctl_t *kc;
52 #endif /* #endif HAVE_LIBKSTAT */
54 static void uptime_submit (gauge_t uptime)
55 {
56 value_t values[1];
57 value_list_t vl = VALUE_LIST_INIT;
59 values[0].gauge = uptime;
61 vl.values = values;
62 vl.values_len = 1;
64 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
65 sstrncpy (vl.plugin, "uptime", sizeof (vl.plugin));
66 sstrncpy (vl.type, "uptime", sizeof (vl.type));
68 plugin_dispatch_values (&vl);
69 }
71 static int uptime_init (void) /* {{{ */
72 {
73 /*
74 * On most unix systems the uptime is calculated by looking at the boot
75 * time (stored in unix time, since epoch) and the current one. We are
76 * going to do the same, reading the boot time value while executing
77 * the uptime_init function (there is no need to read, every time the
78 * plugin_read is called, a value that won't change). However, since
79 * uptime_init is run only once, if the function fails in retrieving
80 * the boot time, the plugin is unregistered and there is no chance to
81 * try again later. Nevertheless, this is very unlikely to happen.
82 */
84 #if KERNEL_LINUX
85 unsigned long starttime;
86 char buffer[1024];
87 int ret;
88 FILE *fh;
90 ret = 0;
92 fh = fopen (STAT_FILE, "r");
94 if (fh == NULL)
95 {
96 char errbuf[1024];
97 ERROR ("uptime plugin: Cannot open "STAT_FILE": %s",
98 sstrerror (errno, errbuf, sizeof (errbuf)));
99 return (-1);
100 }
102 while (fgets (buffer, 1024, fh) != NULL)
103 {
104 /* look for the btime string and read the value */
105 ret = sscanf (buffer, "btime %lu", &starttime);
106 /* avoid further loops if btime has been found and read
107 * correctly (hopefully) */
108 if (ret == 1)
109 break;
110 }
112 fclose (fh);
114 /* loop done, check if no value has been found/read */
115 if (ret != 1)
116 {
117 ERROR ("uptime plugin: No value read from "STAT_FILE"");
118 return (-1);
119 }
121 boottime = (time_t) starttime;
123 if (boottime == 0)
124 {
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 went fine. */
139 if (kc == NULL)
140 {
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 {
148 ERROR ("uptime plugin: Cannot find unix:0:system_misc kstat.");
149 return (-1);
150 }
152 if (kstat_read (kc, ksp, NULL) < 0)
153 {
154 ERROR ("uptime plugin: kstat_read failed.");
155 return (-1);
156 }
158 knp = (kstat_named_t *) kstat_data_lookup (ksp, "boot_time");
159 if (knp == NULL)
160 {
161 ERROR ("uptime plugin: kstat_data_lookup (boot_time) failed.");
162 return (-1);
163 }
165 boottime = (time_t) knp->value.ui32;
167 if (boottime == 0)
168 {
169 ERROR ("uptime plugin: kstat_data_lookup returned success, "
170 "but `boottime' is zero!");
171 return (-1);
172 }
173 /* #endif HAVE_LIBKSTAT */
175 # elif HAVE_SYS_SYSCTL_H
176 struct timeval boottv;
177 size_t boottv_len;
178 int status;
180 int mib[2];
182 mib[0] = CTL_KERN;
183 mib[1] = KERN_BOOTTIME;
185 boottv_len = sizeof (boottv);
186 memset (&boottv, 0, boottv_len);
188 status = sysctl (mib, STATIC_ARRAY_SIZE (mib), &boottv, &boottv_len,
189 /* new_value = */ NULL, /* new_length = */ 0);
190 if (status != 0)
191 {
192 char errbuf[1024];
193 ERROR ("uptime plugin: No value read from sysctl interface: %s",
194 sstrerror (errno, errbuf, sizeof (errbuf)));
195 return (-1);
196 }
198 boottime = boottv.tv_sec;
200 if (boottime == 0)
201 {
202 ERROR ("uptime plugin: sysctl(3) returned success, "
203 "but `boottime' is zero!");
204 return (-1);
205 }
206 #endif /* HAVE_SYS_SYSCTL_H */
208 return (0);
209 } /* }}} int uptime_init */
211 static int uptime_read (void)
212 {
213 gauge_t uptime;
214 time_t elapsed;
216 /* calculate the ammount of time elapsed since boot, AKA uptime */
217 elapsed = time (NULL) - boottime;
219 uptime = (gauge_t) elapsed;
221 uptime_submit (uptime);
223 return (0);
224 }
226 void module_register (void)
227 {
228 plugin_register_init ("uptime", uptime_init);
229 plugin_register_read ("uptime", uptime_read);
230 } /* void module_register */