Path: blob/main/usr.sbin/bsnmpd/modules/snmp_lm75/snmp_lm75.c
109180 views
/*-1* Copyright (c) 2014 Luiz Otavio O Souza <[email protected]>2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526#include <sys/param.h>27#include <sys/queue.h>28#include <sys/sysctl.h>2930#include <bsnmp/snmpmod.h>3132#include <stdio.h>33#include <stdlib.h>34#include <string.h>35#include <syslog.h>3637#include "lm75_oid.h"38#include "lm75_tree.h"3940#ifndef LM75BUF41#define LM75BUF 6442#endif43#define TZ_ZEROC 273244#define UPDATE_INTERVAL 500 /* update interval in ticks */4546static struct lmodule *module;4748static const struct asn_oid oid_lm75 = OIDX_begemotLm75;4950/* the Object Resource registration index */51static u_int lm75_index = 0;5253/* Number of available sensors in the system. */54static int lm75_sensors;5556/*57* Structure that describes single sensor.58*/59struct lm75_snmp_sensor {60TAILQ_ENTRY(lm75_snmp_sensor) link;61int32_t index;62int32_t sysctlidx;63int32_t temp;64char desc[LM75BUF];65char location[LM75BUF];66char parent[LM75BUF];67char pnpinfo[LM75BUF];68};6970static TAILQ_HEAD(, lm75_snmp_sensor) sensors =71TAILQ_HEAD_INITIALIZER(sensors);7273/* Ticks of the last sensors reading. */74static uint64_t last_sensors_update;7576static void free_sensors(void);77static int lm75_fini(void);78static int lm75_init(struct lmodule *mod, int argc, char *argv[]);79static void lm75_start(void);80static int update_sensors(void);8182const struct snmp_module config = {83.comment =84"This module implements the BEGEMOT MIB for reading LM75 sensors data.",85.init = lm75_init,86.start = lm75_start,87.fini = lm75_fini,88.tree = lm75_ctree,89.tree_size = lm75_CTREE_SIZE,90};9192static int93lm75_init(struct lmodule *mod, int argc __unused, char *argv[] __unused)94{9596module = mod;9798lm75_sensors = 0;99100return(0);101}102103static void104lm75_start(void)105{106107lm75_index = or_register(&oid_lm75,108"The MIB module for reading lm75 sensors data.", module);109}110111static int112lm75_fini(void)113{114115or_unregister(lm75_index);116free_sensors();117closelog();118119return (0);120}121122static void123free_sensors(void)124{125struct lm75_snmp_sensor *sensor;126127while ((sensor = TAILQ_FIRST(&sensors)) != NULL) {128TAILQ_REMOVE(&sensors, sensor, link);129free(sensor);130}131}132133static int134sysctlname(int *oid, int nlen, char *name, size_t len)135{136int mib[12];137138if (nlen > (int)(sizeof(mib) / sizeof(int) - 2))139return (-1);140141mib[0] = 0;142mib[1] = 1;143memcpy(mib + 2, oid, nlen * sizeof(int));144145if (sysctl(mib, nlen + 2, name, &len, 0, 0) == -1)146return (-1);147148return (0);149}150151static int152sysctlgetnext(int *oid, int nlen, int *next, size_t *nextlen)153{154int mib[12];155156if (nlen > (int)(sizeof(mib) / sizeof(int) - 2))157return (-1);158159mib[0] = 0;160mib[1] = 2;161memcpy(mib + 2, oid, nlen * sizeof(int));162163if (sysctl(mib, nlen + 2, next, nextlen, 0, 0) == -1)164return (-1);165166return (0);167}168169static int170update_sensor_sysctl(void *obuf, size_t *obuflen, int idx, const char *name)171{172char buf[LM75BUF];173int mib[5];174size_t len;175176/* Fill out the mib information. */177snprintf(buf, sizeof(buf) - 1, "dev.lm75.%d.%s", idx, name);178len = sizeof(mib) / sizeof(int);179if (sysctlnametomib(buf, mib, &len) == -1)180return (-1);181182if (len != 4)183return (-1);184185/* Read the sysctl data. */186if (sysctl(mib, len, obuf, obuflen, NULL, 0) == -1)187return (-1);188189return (0);190}191192static void193update_sensor(struct lm75_snmp_sensor *sensor, int idx)194{195size_t len;196197len = sizeof(sensor->desc);198update_sensor_sysctl(sensor->desc, &len, idx, "%desc");199200len = sizeof(sensor->location);201update_sensor_sysctl(sensor->location, &len, idx, "%location");202203len = sizeof(sensor->pnpinfo);204update_sensor_sysctl(sensor->pnpinfo, &len, idx, "%pnpinfo");205206len = sizeof(sensor->parent);207update_sensor_sysctl(sensor->parent, &len, idx, "%parent");208}209210static int211add_sensor(char *buf)212{213int idx, temp;214size_t len;215struct lm75_snmp_sensor *sensor;216217if (sscanf(buf, "dev.lm75.%d.temperature", &idx) != 1)218return (-1);219220/* Read the sensor temperature. */221len = sizeof(temp);222if (update_sensor_sysctl(&temp, &len, idx, "temperature") != 0)223return (-1);224225/* Add the sensor data to the table. */226sensor = calloc(1, sizeof(*sensor));227if (sensor == NULL) {228syslog(LOG_ERR, "Unable to allocate %zu bytes for resource",229sizeof(*sensor));230return (-1);231}232sensor->index = ++lm75_sensors;233sensor->sysctlidx = idx;234sensor->temp = (temp - TZ_ZEROC) / 10;235TAILQ_INSERT_TAIL(&sensors, sensor, link);236237update_sensor(sensor, idx);238239return (0);240}241242static int243update_sensors(void)244{245char buf[LM75BUF];246int i, root[5], *next, *oid;247size_t len, nextlen, rootlen;248static uint64_t now;249250now = get_ticks();251if (now - last_sensors_update < UPDATE_INTERVAL)252return (0);253254last_sensors_update = now;255256/* Reset the sensor data. */257free_sensors();258lm75_sensors = 0;259260/* Start from the lm75 default root node. */261rootlen = 2;262if (sysctlnametomib("dev.lm75", root, &rootlen) == -1)263return (0);264265oid = (int *)malloc(sizeof(int) * rootlen);266if (oid == NULL) {267perror("malloc");268return (-1);269}270memcpy(oid, root, rootlen * sizeof(int));271len = rootlen;272273/* Traverse the sysctl(3) interface and find the active sensors. */274for (;;) {275276/* Find the size of the next mib. */277nextlen = 0;278if (sysctlgetnext(oid, len, NULL, &nextlen) == -1) {279free(oid);280return (0);281}282/* Allocate and read the next mib. */283next = (int *)malloc(nextlen);284if (next == NULL) {285syslog(LOG_ERR,286"Unable to allocate %zu bytes for resource",287nextlen);288free(oid);289return (-1);290}291if (sysctlgetnext(oid, len, next, &nextlen) == -1) {292free(oid);293free(next);294return (0);295}296free(oid);297/* Check if we care about the next mib. */298for (i = 0; i < (int)rootlen; i++)299if (next[i] != root[i]) {300free(next);301return (0);302}303oid = (int *)malloc(nextlen);304if (oid == NULL) {305syslog(LOG_ERR,306"Unable to allocate %zu bytes for resource",307nextlen);308free(next);309return (-1);310}311memcpy(oid, next, nextlen);312free(next);313len = nextlen / sizeof(int);314315/* Find the mib name. */316if (sysctlname(oid, len, buf, sizeof(buf)) != 0)317continue;318319if (strstr(buf, "temperature"))320if (add_sensor(buf) != 0) {321free(oid);322return (-1);323}324}325326return (0);327}328329int330op_lm75Sensors(struct snmp_context *context __unused, struct snmp_value *value,331u_int sub, u_int iidx __unused, enum snmp_op op)332{333asn_subid_t which;334335if (update_sensors() == -1)336return (SNMP_ERR_RES_UNAVAIL);337338which = value->var.subs[sub - 1];339340switch (op) {341case SNMP_OP_GET:342switch (which) {343case LEAF_lm75Sensors:344value->v.integer = lm75_sensors;345break;346default:347return (SNMP_ERR_RES_UNAVAIL);348}349break;350case SNMP_OP_SET:351return (SNMP_ERR_NOT_WRITEABLE);352case SNMP_OP_GETNEXT:353case SNMP_OP_ROLLBACK:354case SNMP_OP_COMMIT:355return (SNMP_ERR_NOERROR);356default:357return (SNMP_ERR_RES_UNAVAIL);358}359360return (SNMP_ERR_NOERROR);361}362363int364op_lm75SensorTable(struct snmp_context *context __unused,365struct snmp_value *value, u_int sub, u_int iidx __unused, enum snmp_op op)366{367struct lm75_snmp_sensor *sensor;368asn_subid_t which;369int ret;370371if (update_sensors() == -1)372return (SNMP_ERR_RES_UNAVAIL);373374which = value->var.subs[sub - 1];375376switch (op) {377case SNMP_OP_GETNEXT:378sensor = NEXT_OBJECT_INT(&sensors, &value->var, sub);379if (sensor == NULL)380return (SNMP_ERR_NOSUCHNAME);381value->var.len = sub + 1;382value->var.subs[sub] = sensor->index;383break;384case SNMP_OP_GET:385if (value->var.len - sub != 1)386return (SNMP_ERR_NOSUCHNAME);387sensor = FIND_OBJECT_INT(&sensors, &value->var, sub);388if (sensor == NULL)389return (SNMP_ERR_NOSUCHNAME);390break;391case SNMP_OP_SET:392return (SNMP_ERR_NOT_WRITEABLE);393case SNMP_OP_ROLLBACK:394case SNMP_OP_COMMIT:395return (SNMP_ERR_NOERROR);396default:397return (SNMP_ERR_RES_UNAVAIL);398}399400ret = SNMP_ERR_NOERROR;401402switch (which) {403case LEAF_lm75SensorIndex:404value->v.integer = sensor->index;405break;406case LEAF_lm75SensorSysctlIndex:407value->v.integer = sensor->sysctlidx;408break;409case LEAF_lm75SensorDesc:410ret = string_get(value, sensor->desc, -1);411break;412case LEAF_lm75SensorLocation:413ret = string_get(value, sensor->location, -1);414break;415case LEAF_lm75SensorPnpInfo:416ret = string_get(value, sensor->pnpinfo, -1);417break;418case LEAF_lm75SensorParent:419ret = string_get(value, sensor->parent, -1);420break;421case LEAF_lm75SensorTemperature:422value->v.integer = sensor->temp;423break;424default:425ret = SNMP_ERR_RES_UNAVAIL;426break;427}428429return (ret);430}431432433