diff options
Diffstat (limited to 'usr.sbin/sensorsd/sensorsd.c')
| -rw-r--r-- | usr.sbin/sensorsd/sensorsd.c | 170 |
1 files changed, 124 insertions, 46 deletions
diff --git a/usr.sbin/sensorsd/sensorsd.c b/usr.sbin/sensorsd/sensorsd.c index 01e17b4e670..e579cdd24b3 100644 --- a/usr.sbin/sensorsd/sensorsd.c +++ b/usr.sbin/sensorsd/sensorsd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sensorsd.c,v 1.31 2007/05/29 20:30:40 cnst Exp $ */ +/* $OpenBSD: sensorsd.c,v 1.32 2007/05/30 07:49:37 cnst Exp $ */ /* * Copyright (c) 2003 Henning Brauer <henning@openbsd.org> @@ -46,6 +46,13 @@ void parse_config(char *); int64_t get_val(char *, int, enum sensor_type); void reparse_cfg(int); +enum sensorsd_s_status { + SENSORSD_S_UNSPEC, /* status is unspecified */ + SENSORSD_S_INVALID, /* status is invalid, per SENSOR_FINVALID */ + SENSORSD_S_WITHIN, /* status is within limits */ + SENSORSD_S_OUTSIDE /* status is outside limits */ +}; + struct limits_t { TAILQ_ENTRY(limits_t) entries; char dxname[16]; /* device unix name */ @@ -56,11 +63,17 @@ struct limits_t { int64_t lower; /* lower limit */ int64_t upper; /* upper limit */ char *command; /* failure command */ - time_t status_changed; - enum sensor_status status; /* last status */ - enum sensor_status status2; - int count; /* stat change counter */ - u_int8_t watch; + time_t astatus_changed; + time_t ustatus_changed; + enum sensor_status astatus; /* last automatic status */ + enum sensor_status astatus2; + enum sensorsd_s_status ustatus; /* last user-limit status */ + enum sensorsd_s_status ustatus2; + int acount; /* stat change counter */ + int ucount; /* stat change counter */ + u_int8_t flags; /* sensorsd limit flags */ +#define SENSORSD_L_USERLIMIT 0x0001 /* user specified limit */ +#define SENSORSD_L_ISTATUS 0x0002 /* ignore automatic status */ }; TAILQ_HEAD(limits, limits_t) limits = TAILQ_HEAD_INITIALIZER(limits); @@ -122,8 +135,6 @@ main(int argc, char *argv[]) warn("sysctl"); continue; } - if (sensor.flags & SENSOR_FINVALID) - continue; if ((limit = calloc(1, sizeof(struct limits_t))) == NULL) err(1, "calloc"); @@ -189,45 +200,62 @@ check_sensors(void) struct limits_t *limit; size_t len; int mib[5]; - enum sensor_status newstatus; mib[0] = CTL_HW; mib[1] = HW_SENSORS; len = sizeof(sensor); - TAILQ_FOREACH(limit, &limits, entries) - if (limit->watch) { - mib[2] = limit->dev; - mib[3] = limit->type; - mib[4] = limit->numt; - if (sysctl(mib, 5, &sensor, &len, NULL, 0) == -1) - err(1, "sysctl"); - - newstatus = sensor.status; - /* unknown may as well mean producing valid - * status had failed so warn about it */ - if (newstatus == SENSOR_S_UNKNOWN) - newstatus = SENSOR_S_WARN; - else if (newstatus == SENSOR_S_UNSPEC) { - if (sensor.value > limit->upper || - sensor.value < limit->lower) - newstatus = SENSOR_S_CRIT; - else - newstatus = SENSOR_S_OK; - } + TAILQ_FOREACH(limit, &limits, entries) { + if ((limit->flags & SENSORSD_L_ISTATUS) && + !(limit->flags & SENSORSD_L_USERLIMIT)) + continue; + + mib[2] = limit->dev; + mib[3] = limit->type; + mib[4] = limit->numt; + if (sysctl(mib, 5, &sensor, &len, NULL, 0) == -1) + err(1, "sysctl"); + + if (!(limit->flags & SENSORSD_L_ISTATUS)) { + enum sensor_status newastatus = sensor.status; - if (limit->status != newstatus) { - if (limit->status2 != newstatus) { - limit->status2 = newstatus; - limit->count = 0; - } else if (++limit->count >= 3) { + if (limit->astatus != newastatus) { + if (limit->astatus2 != newastatus) { + limit->astatus2 = newastatus; + limit->acount = 0; + } else if (++limit->acount >= 3) { + limit->last_val = sensor.value; + limit->astatus2 = + limit->astatus = newastatus; + limit->astatus_changed = time(NULL); + } + } + } + + if (limit->flags & SENSORSD_L_USERLIMIT) { + enum sensorsd_s_status newustatus; + + if (sensor.flags & SENSOR_FINVALID) + newustatus = SENSORSD_S_INVALID; + else if (sensor.value > limit->upper || + sensor.value < limit->lower) + newustatus = SENSORSD_S_OUTSIDE; + else + newustatus = SENSORSD_S_WITHIN; + + if (limit->ustatus != newustatus) { + if (limit->ustatus2 != newustatus) { + limit->ustatus2 = newustatus; + limit->ucount = 0; + } else if (++limit->ucount >= 3) { limit->last_val = sensor.value; - limit->status2 = - limit->status = newstatus; - limit->status_changed = time(NULL); + limit->ustatus2 = + limit->ustatus = newustatus; + limit->ustatus_changed = time(NULL); } } } + } } void @@ -254,13 +282,61 @@ report(time_t last_report) struct limits_t *limit = NULL; TAILQ_FOREACH(limit, &limits, entries) { - if (limit->status_changed <= last_report) + if ((limit->astatus_changed <= last_report) && + (limit->ustatus_changed <= last_report)) continue; - syslog(LOG_ALERT, "hw.sensors.%s.%s%d: %s limits, value: %s", - limit->dxname, sensor_type_s[limit->type], limit->numt, - (limit->status != SENSOR_S_OK) ? "exceed" : "within", - print_sensor(limit->type, limit->last_val)); + if (limit->astatus_changed > last_report) { + const char *as = NULL; + + switch (limit->astatus) { + case SENSOR_S_UNSPEC: + as = ""; + break; + case SENSOR_S_OK: + as = ", OK"; + break; + case SENSOR_S_WARN: + as = ", WARN"; + break; + case SENSOR_S_CRIT: + as = ", CRITICAL"; + break; + case SENSOR_S_UNKNOWN: + as = ", UNKNOWN"; + break; + } + syslog(LOG_ALERT, "%s.%s%d: %s%s", + limit->dxname, sensor_type_s[limit->type], + limit->numt, + print_sensor(limit->type, limit->last_val), as); + } + + if (limit->ustatus_changed > last_report) { + char us[BUFSIZ]; + + switch (limit->ustatus) { + case SENSORSD_S_UNSPEC: + snprintf(us, sizeof(us), + "ustatus uninitialised"); + break; + case SENSORSD_S_INVALID: + snprintf(us, sizeof(us), "marked invalid"); + break; + case SENSORSD_S_WITHIN: + snprintf(us, sizeof(us), "within limits: %s", + print_sensor(limit->type, limit->last_val)); + break; + case SENSORSD_S_OUTSIDE: + snprintf(us, sizeof(us), "exceeds limits: %s", + print_sensor(limit->type, limit->last_val)); + break; + } + syslog(LOG_ALERT, "%s.%s%d: %s", + limit->dxname, sensor_type_s[limit->type], + limit->numt, us); + } + if (limit->command) { int i = 0, n = 0, r; char *cmd = limit->command; @@ -402,12 +478,12 @@ parse_config(char *cf) next = TAILQ_NEXT(p, entries); snprintf(node, sizeof(node), "hw.sensors.%s.%s%d", p->dxname, sensor_type_s[p->type], p->numt); + p->flags = 0; if (cgetent(&buf, cfa, node) != 0) - if (cgetent(&buf, cfa, sensor_type_s[p->type]) != 0) { - p->watch = 0; + if (cgetent(&buf, cfa, sensor_type_s[p->type]) != 0) continue; - } - p->watch = 1; + if (cgetcap(buf, "istatus", ':')) + p->flags |= SENSORSD_L_ISTATUS; if (cgetstr(buf, "low", &ebuf) < 0) ebuf = NULL; p->lower = get_val(ebuf, 0, p->type); @@ -420,6 +496,8 @@ parse_config(char *cf) asprintf(&(p->command), "%s", ebuf); free(buf); buf = NULL; + if (p->lower != LLONG_MIN || p->upper != LLONG_MAX) + p->flags |= SENSORSD_L_USERLIMIT; } free(cfa); } |
