1 /* $OpenBSD: sensors.c,v 1.11 2007/03/23 14:48:22 ckuethe Exp $ */
4 * Copyright (c) 2007 Deanna Phillips <deanna@openbsd.org>
5 * Copyright (c) 2003 Henning Brauer <henning@openbsd.org>
6 * Copyright (c) 2006 Constantine A. Murenin <cnst+openbsd@bugmail.mojo.ru>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/param.h>
22 #include <sys/sysctl.h>
23 #include <sys/sensors.h>
34 struct sensordev sensordev;
37 static char * fmttime(double);
42 return (subwin(stdscr, LINES-5-1, 0, 5, 0));
46 closesensors(WINDOW *w)
60 mvwaddstr(wnd, 0, 0, "Sensor");
61 mvwaddstr(wnd, 0, 34, "Value");
62 mvwaddstr(wnd, 0, 45, "Status");
63 mvwaddstr(wnd, 0, 58, "Description");
69 enum sensor_type type;
70 size_t slen, sdlen, idmax_len;
71 int mib[5], dev, numt, idmax;
74 maxsensordevices = MAXSENSORDEVICES;
75 idmax_len = sizeof(idmax);
76 if (sysctlbyname("hw.sensors.dev_idmax", &idmax, &idmax_len,
78 maxsensordevices = idmax;
82 slen = sizeof(struct sensor);
83 sdlen = sizeof(struct sensordev);
91 for (dev = 0; dev < maxsensordevices; dev++) {
93 if (sysctl(mib, 3, &sensordev, &sdlen, NULL, 0) == -1) {
98 for (type = 0; type < SENSOR_MAX_TYPES; type++) {
100 for (numt = 0; numt < sensordev.maxnumt[type]; numt++) {
102 if (sysctl(mib, 5, &sensor, &slen, NULL, 0)
108 if (sensor.flags & SENSOR_FINVALID)
117 const char *drvstat[] = {
119 "empty", "ready", "powerup", "online", "idle", "active",
120 "rebuild", "powerdown", "fail", "pfail"
127 mvwaddstr(wnd, row, 0, "No sensors found.");
139 mvwprintw(wnd, row, 0, "%s.%s%d", sensordev.xname,
140 sensor_type_s[sensor.type], sensor.numt);
141 switch (sensor.type) {
143 mvwprintw(wnd, row, 24, "%10.2f degC",
144 (sensor.value - 273150000) / 1000000.0);
147 mvwprintw(wnd, row, 24, "%11lld RPM", sensor.value);
149 case SENSOR_VOLTS_DC:
150 mvwprintw(wnd, row, 24, "%10.2f V DC",
151 sensor.value / 1000000.0);
154 mvwprintw(wnd, row, 24, "%10.2f A", sensor.value / 1000000.0);
156 case SENSOR_INDICATOR:
157 mvwprintw(wnd, row, 24, "%15s", sensor.value? "On" : "Off");
160 mvwprintw(wnd, row, 24, "%11lld raw", sensor.value);
163 mvwprintw(wnd, row, 24, "%14.2f%%", sensor.value / 1000.0);
166 mvwprintw(wnd, row, 24, "%15.2f lx", sensor.value / 1000000.0);
169 if (0 < sensor.value &&
170 (size_t)sensor.value < sizeof(drvstat)/sizeof(drvstat[0])) {
171 mvwprintw(wnd, row, 24, "%15s", drvstat[sensor.value]);
175 case SENSOR_TIMEDELTA:
176 mvwprintw(wnd, row, 24, "%15s", fmttime(sensor.value / 1000000000.0));
178 case SENSOR_WATTHOUR:
179 mvwprintw(wnd, row, 24, "%12.2f Wh", sensor.value / 1000000.0);
182 mvwprintw(wnd, row, 24, "%10.2f Ah", sensor.value / 1000000.0);
185 mvwprintw(wnd, row, 24, "%10lld", sensor.value);
188 if (sensor.desc[0] != '\0')
189 mvwprintw(wnd, row, 58, "(%s)", sensor.desc);
191 switch (sensor.status) {
192 case SENSOR_S_UNSPEC:
194 case SENSOR_S_UNKNOWN:
195 mvwaddstr(wnd, row, 45, "unknown");
198 mvwaddstr(wnd, row, 45, "WARNING");
201 mvwaddstr(wnd, row, 45, "CRITICAL");
204 mvwaddstr(wnd, row, 45, "OK");
210 #define SECS_PER_DAY 86400
211 #define SECS_PER_HOUR 3600
212 #define SECS_PER_MIN 60
221 static char outbuf[LEN];
228 if (in >= SECS_PER_DAY ){
231 } else if (in >= SECS_PER_HOUR ){
234 } else if (in >= SECS_PER_MIN ){
237 } else if (in >= 1 ){
239 /* in *= 1; */ /* no op */
240 } else if (in == 0 ){ /* direct comparisons to floats are scary */
242 } else if (in >= 1e-3 ){
245 } else if (in >= 1e-6 ){
248 } else if (in >= 1e-9 ){
258 snprintf(outbuf, LEN,
259 tiny ? "%s%lf %s" : "%s%.3lf %s",
260 signbit == -1 ? "-" : "", in, unit);