Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 1 Jun 2014 03:14:03 +0000 (UTC)
From:      Luiz Otavio O Souza <loos@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r266936 - in head: etc usr.sbin/bsnmpd/modules usr.sbin/bsnmpd/modules/snmp_lm75
Message-ID:  <201406010314.s513E3DB092086@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: loos
Date: Sun Jun  1 03:14:03 2014
New Revision: 266936
URL: http://svnweb.freebsd.org/changeset/base/266936

Log:
  Adds the bsnmp module to export the temperature data from lm75 sensors on
  the system.
  
  Together with lm75(4) this module allows easy temperature monitoring over
  SNMP, specially for embedded systems.
  
  Manual page reviewed by:        brueffer (D128)

Added:
  head/usr.sbin/bsnmpd/modules/snmp_lm75/
  head/usr.sbin/bsnmpd/modules/snmp_lm75/BEGEMOT-LM75-MIB.txt   (contents, props changed)
  head/usr.sbin/bsnmpd/modules/snmp_lm75/Makefile   (contents, props changed)
  head/usr.sbin/bsnmpd/modules/snmp_lm75/lm75_tree.def   (contents, props changed)
  head/usr.sbin/bsnmpd/modules/snmp_lm75/snmp_lm75.3   (contents, props changed)
  head/usr.sbin/bsnmpd/modules/snmp_lm75/snmp_lm75.c   (contents, props changed)
Modified:
  head/etc/snmpd.config
  head/usr.sbin/bsnmpd/modules/Makefile

Modified: head/etc/snmpd.config
==============================================================================
--- head/etc/snmpd.config	Sun Jun  1 02:47:09 2014	(r266935)
+++ head/etc/snmpd.config	Sun Jun  1 03:14:03 2014	(r266936)
@@ -279,6 +279,11 @@ begemotSnmpdModulePath."mibII"	= "/usr/l
 #begemotNgControlNodeName = "snmpd"
 
 #
+# LM75 Sensor module
+#
+#begemotSnmpdModulePath."lm75" = "/usr/lib/snmp_lm75.so"
+
+#
 # pf(4) module
 #
 #begemotSnmpdModulePath."pf"	= "/usr/lib/snmp_pf.so"

Modified: head/usr.sbin/bsnmpd/modules/Makefile
==============================================================================
--- head/usr.sbin/bsnmpd/modules/Makefile	Sun Jun  1 02:47:09 2014	(r266935)
+++ head/usr.sbin/bsnmpd/modules/Makefile	Sun Jun  1 03:14:03 2014	(r266936)
@@ -12,6 +12,7 @@ SUBDIR=	${_snmp_atm} \
 	snmp_bridge \
 	snmp_hast \
 	snmp_hostres \
+	snmp_lm75 \
 	snmp_mibII \
 	snmp_target \
 	snmp_usm \

Added: head/usr.sbin/bsnmpd/modules/snmp_lm75/BEGEMOT-LM75-MIB.txt
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/usr.sbin/bsnmpd/modules/snmp_lm75/BEGEMOT-LM75-MIB.txt	Sun Jun  1 03:14:03 2014	(r266936)
@@ -0,0 +1,160 @@
+--
+-- Copyright (c) 2014 Luiz Otavio O Souza <loos@FreeBSD.org>
+-- All rights reserved.
+--
+-- Redistribution and use in source and binary forms, with or without
+-- modification, are permitted provided that the following conditions
+-- are met:
+-- 1. Redistributions of source code must retain the above copyright
+--    notice, this list of conditions and the following disclaimer.
+-- 2. Redistributions in binary form must reproduce the above copyright
+--    notice, this list of conditions and the following disclaimer in the
+--    documentation and/or other materials provided with the distribution.
+--
+-- THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+-- ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+-- SUCH DAMAGE.
+--
+-- $FreeBSD$
+--
+
+BEGEMOT-LM75-MIB DEFINITIONS ::= BEGIN
+
+IMPORTS
+    MODULE-IDENTITY, OBJECT-TYPE, NOTIFICATION-TYPE,
+    Counter64, Integer32
+	FROM SNMPv2-SMI
+    TEXTUAL-CONVENTION, RowStatus
+	FROM SNMPv2-TC
+    begemot
+	FROM BEGEMOT-MIB;
+
+begemotLoos MODULE-IDENTITY
+    LAST-UPDATED "201402240000Z"
+    ORGANIZATION "FreeBSD"
+    CONTACT-INFO
+	    "		Luiz Otavio O Souza
+
+	     Postal:	N/A
+
+	     Fax:	N/A
+
+	     E-Mail:	loos@FreeBSD.org"
+    DESCRIPTION
+	    "The Begemot MIB for reading lm75 sensors data."
+    REVISION     "201402240000Z"
+    DESCRIPTION
+	    "Initial revision."
+    ::= { begemot 400 }
+
+begemotLm75Objects	OBJECT IDENTIFIER ::= { begemotLm75 1 }
+
+-- ---------------------------------------------------------- --
+-- Configuration parameters
+-- ---------------------------------------------------------- --
+
+lm75Sensor	OBJECT IDENTIFIER ::= { begemotlm75Objects 1 }
+
+lm75Sensors	OBJECT-TYPE
+    SYNTAX	Integer32
+    MAX-ACCESS	read-only
+    STATUS	current
+    DESCRIPTION
+	"Number of LM75 sensors in the system."
+    ::= { lm75Sensors 1 }
+
+-- ---------------------------------------------------------- --
+-- TempSensor Table
+-- ---------------------------------------------------------- --
+lm75SensorTable OBJECT-TYPE
+    SYNTAX	SEQUENCE OF Lm75SensorEntry
+    MAX-ACCESS	not-accessible
+    STATUS	current
+    DESCRIPTION
+	"A table containing information about all temperature sensors."
+    ::= { begemotLm75Objects 2 }
+
+loosTempSensorEntry OBJECT-TYPE
+    SYNTAX	Lm75SensorEntry
+    MAX-ACCESS	not-accessible
+    STATUS	current
+    DESCRIPTION
+	"Table entry that describes one temperature sensor."
+    INDEX	{ lm75SensorIndex }
+    ::= { lm75SensorTable 1 }
+
+Lm75SensorEntry ::= SEQUENCE {
+    lm75SensorIndex			Integer32,
+    lm75SensorSysctlIndex		Integer32,
+    lm75SensorDesc			OCTET STRING,
+    lm75SensorLocation			OCTET STRING,
+    lm75SensorPnpInfo			OCTET STRING,
+    lm75SensorParent			OCTET STRING,
+    lm75SensorTemperature		Integer32
+}
+
+lm75SensorIndex OBJECT-TYPE
+    SYNTAX	Integer32
+    MAX-ACCESS	read-only
+    STATUS	current
+    DESCRIPTION
+	"LM75 Sensor index."
+    ::= { lm75SensorEntry 1 }
+
+lm75SensorSysctlIndex OBJECT-TYPE
+    SYNTAX	Integer32
+    MAX-ACCESS	read-only
+    STATUS	current
+    DESCRIPTION
+	"LM75 Sensor sysctl index."
+    ::= { lm75SensorEntry 2 }
+
+lm75SensorDesc OBJECT-TYPE
+    SYNTAX	OCTET STRING
+    MAX-ACCESS	read-only
+    STATUS	current
+    DESCRIPTION
+	"LM75 Sensor description."
+    ::= { lm75SensorEntry 3 }
+
+lm75SensorLocation OBJECT-TYPE
+    SYNTAX	OCTET STRING
+    MAX-ACCESS	read-only
+    STATUS	current
+    DESCRIPTION
+	"LM75 Sensor location."
+    ::= { lm75SensorEntry 4 }
+
+lm75SensorPnpInfo OBJECT-TYPE
+    SYNTAX	OCTET STRING
+    MAX-ACCESS	read-only
+    STATUS	current
+    DESCRIPTION
+	"LM75 Sensor pnp information."
+    ::= { lm75SensorEntry 5 }
+
+lm75SensorParent OBJECT-TYPE
+    SYNTAX	OCTET STRING
+    MAX-ACCESS	read-only
+    STATUS	current
+    DESCRIPTION
+	"LM75 Sensor parent bus."
+    ::= { lm75SensorEntry 6 }
+
+lm75SensorTemperature OBJECT-TYPE
+    SYNTAX	Integer32
+    MAX-ACCESS	read-only
+    STATUS	current
+    DESCRIPTION
+	"LM75 Sensor temperature."
+    ::= { lm75SensorEntry 7 }
+
+END

Added: head/usr.sbin/bsnmpd/modules/snmp_lm75/Makefile
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/usr.sbin/bsnmpd/modules/snmp_lm75/Makefile	Sun Jun  1 03:14:03 2014	(r266936)
@@ -0,0 +1,13 @@
+# $FreeBSD$
+
+.include <bsd.own.mk>
+
+MOD=    lm75
+SRCS=   snmp_lm75.c
+XSYM=   begemotLm75
+MAN=    snmp_lm75.3
+
+BMIBS=  BEGEMOT-LM75-MIB.txt
+DEFS=   ${MOD}_tree.def
+
+.include <bsd.snmpmod.mk>

Added: head/usr.sbin/bsnmpd/modules/snmp_lm75/lm75_tree.def
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/usr.sbin/bsnmpd/modules/snmp_lm75/lm75_tree.def	Sun Jun  1 03:14:03 2014	(r266936)
@@ -0,0 +1,56 @@
+#-
+# Copyright (c) 2014 Luiz Otavio O Souza <loos@FreeBSD.org>
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+# SUCH DAMAGE.
+#
+# $FreeBSD$
+#
+
+(1 internet
+  (4 private
+    (1 enterprises
+      (12325 fokus
+        (1 begemot
+          (400 begemotLm75
+            (1 begemotLm75Objects
+              (1 lm75Sensors
+                (1 lm75Sensors INTEGER32 op_lm75Sensors GET)
+              )
+              (2 lm75SensorTable
+                (1 lm75SensorEntry : OCTETSTRING op_lm75SensorTable
+                  (1 lm75SensorIndex INTEGER32 GET)
+                  (2 lm75SensorSysctlIndex INTEGER32 GET)
+                  (3 lm75SensorDesc OCTETSTRING GET)
+                  (4 lm75SensorLocation OCTETSTRING GET)
+                  (5 lm75SensorPnpInfo OCTETSTRING GET)
+                  (6 lm75SensorParent OCTETSTRING GET)
+                  (7 lm75SensorTemperature INTEGER32 GET)
+                )
+              )
+            )
+          )
+        )
+      )
+    )
+  )
+)

Added: head/usr.sbin/bsnmpd/modules/snmp_lm75/snmp_lm75.3
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/usr.sbin/bsnmpd/modules/snmp_lm75/snmp_lm75.3	Sun Jun  1 03:14:03 2014	(r266936)
@@ -0,0 +1,60 @@
+.\"-
+.\" Copyright (c) 2014 Luiz Otavio O Souza <loos@FreeBSD.org>
+.\" All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\" $FreeBSD$
+.\"
+.Dd Jun 1, 2014
+.Dt SNMP_LM75 3
+.Os
+.Sh NAME
+.Nm snmp_lm75
+.Nd "LM75 Sensor module for"
+.Xr bsnmpd 1
+.Sh LIBRARY
+.Pq begemotSnmpdModulePath."lm75" = "/usr/lib/snmp_lm75.so"
+.Sh DESCRIPTION
+The
+.Nm snmp_lm75
+module implements a private BEGEMOT-LM75-MIB, which allows
+reading the temperature of the LM75 sensors on the system.
+.Pp
+The module reads the sensor(s) temperature using the
+.Xr sysctl 8
+API.
+.Sh FILES
+.Bl -tag -width "XXXXXXXXX"
+.It Pa /usr/share/snmp/defs/lm75_tree.def
+The description of the MIB tree implemented by
+.Nm .
+.It Pa /usr/share/snmp/mibs/BEGEMOT-LM75-MIB.txt
+The private BEGEMOT-LM75-MIB that is implemented by this module.
+.El
+.Sh SEE ALSO
+.Xr bsnmpd 1 ,
+.Xr gensnmptree 1 ,
+.Xr snmpmod 3 ,
+.Xr lm75 4
+.Sh AUTHORS
+.An Luiz Otavio O Souza Aq loos@FreeBSD.org .

Added: head/usr.sbin/bsnmpd/modules/snmp_lm75/snmp_lm75.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/usr.sbin/bsnmpd/modules/snmp_lm75/snmp_lm75.c	Sun Jun  1 03:14:03 2014	(r266936)
@@ -0,0 +1,436 @@
+/*-
+ * Copyright (c) 2014 Luiz Otavio O Souza <loos@FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+#include <sys/queue.h>
+#include <sys/sysctl.h>
+
+#include <bsnmp/snmpmod.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <syslog.h>
+
+#include "lm75_oid.h"
+#include "lm75_tree.h"
+
+#ifndef	LM75BUF
+#define	LM75BUF		64
+#endif
+#define	TZ_ZEROC	2732
+#define	UPDATE_INTERVAL	500	/* update interval in ticks */
+
+static struct lmodule *module;
+
+static const struct asn_oid oid_lm75 = OIDX_begemotLm75;
+
+/* the Object Resource registration index */
+static u_int lm75_index = 0;
+
+/* Number of available sensors in the system. */
+static int lm75_sensors;
+
+/*
+ * Structure that describes single sensor.
+ */
+struct lm75_snmp_sensor {
+	TAILQ_ENTRY(lm75_snmp_sensor) link;
+	int32_t		index;
+	int32_t		sysctlidx;
+	int32_t		temp;
+	char		desc[LM75BUF];
+	char		location[LM75BUF];
+	char		parent[LM75BUF];
+	char		pnpinfo[LM75BUF];
+};
+
+static TAILQ_HEAD(, lm75_snmp_sensor) sensors =
+    TAILQ_HEAD_INITIALIZER(sensors);
+
+/* Ticks of the last sensors reading. */
+static uint64_t last_sensors_update;
+
+static void free_sensors(void);
+static int lm75_fini(void);
+static int lm75_init(struct lmodule *mod, int argc, char *argv[]);
+static void lm75_start(void);
+static int update_sensors(void);
+
+const struct snmp_module config = {
+    .comment   =
+	"This module implements the BEGEMOT MIB for reading LM75 sensors data.",
+    .init      = lm75_init,
+    .start     = lm75_start,
+    .fini      = lm75_fini,
+    .tree      = lm75_ctree,
+    .tree_size = lm75_CTREE_SIZE,
+};
+
+static int
+lm75_init(struct lmodule *mod, int argc __unused, char *argv[] __unused)
+{
+
+	module = mod;
+
+	lm75_sensors = 0;
+	openlog("snmp_lm75", LOG_NDELAY | LOG_PID, LOG_DAEMON);
+
+	return(0);
+}
+
+static void
+lm75_start(void)
+{
+
+	lm75_index = or_register(&oid_lm75,
+	    "The MIB module for reading lm75 sensors data.", module);
+}
+
+static int
+lm75_fini(void)
+{
+
+	or_unregister(lm75_index);
+	free_sensors();
+	closelog();
+
+	return (0);
+}
+
+static void
+free_sensors(void)
+{
+	struct lm75_snmp_sensor *sensor;
+
+	while ((sensor = TAILQ_FIRST(&sensors)) != NULL) {
+		TAILQ_REMOVE(&sensors, sensor, link);
+		free(sensor);
+	}
+}
+
+static int
+sysctlname(int *oid, int nlen, char *name, size_t len)
+{
+	int mib[12];
+
+	if (nlen > (int)sizeof(mib) + 2)
+		return (-1);
+
+	mib[0] = 0;
+	mib[1] = 1;
+	memcpy(mib + 2, oid, nlen * sizeof(int));
+
+	if (sysctl(mib, nlen + 2, name, &len, 0, 0) == -1)
+		return (-1);
+
+	return (0);
+}
+
+static int
+sysctlgetnext(int *oid, int nlen, int *next, size_t *nextlen)
+{
+	int mib[12];
+
+	if (nlen  > (int)sizeof(mib) + 2)
+		return (-1);
+
+	mib[0] = 0;
+	mib[1] = 2;
+	memcpy(mib + 2, oid, nlen * sizeof(int));
+
+	if (sysctl(mib, nlen + 2, next, nextlen, 0, 0) == -1)
+		return (-1);
+
+	return (0);
+}
+
+static int
+update_sensor_sysctl(char *obuf, size_t *obuflen, int idx, const char *name)
+{
+	char buf[LM75BUF];
+	int mib[5];
+	size_t len;
+
+	/* Fill out the mib information. */
+	snprintf(buf, sizeof(buf) - 1, "dev.lm75.%d.%s", idx, name);
+	len = 4;
+	if (sysctlnametomib(buf, mib, &len) == -1)
+		return (-1);
+
+	/* Read the sysctl data. */
+	if (sysctl(mib, len, obuf, obuflen, NULL, 0) == -1)
+		return (-1);
+
+	return (0);
+}
+
+static void
+update_sensor(struct lm75_snmp_sensor *sensor, int idx)
+{
+	size_t len;
+
+	len = sizeof(sensor->desc);
+	update_sensor_sysctl(sensor->desc, &len, idx, "%desc");
+
+	len = sizeof(sensor->location);
+	update_sensor_sysctl(sensor->location, &len, idx, "%location");
+
+	len = sizeof(sensor->pnpinfo);
+	update_sensor_sysctl(sensor->pnpinfo, &len, idx, "%pnpinfo");
+
+	len = sizeof(sensor->parent);
+	update_sensor_sysctl(sensor->parent, &len, idx, "%parent");
+}
+
+static int
+add_sensor(char *buf, size_t nlen)
+{
+	int idx, mib[5], temp;
+	size_t len;
+	struct lm75_snmp_sensor *sensor;
+
+	if (sscanf(buf, "dev.lm75.%d.temperature", &idx) != 1)
+		return (-1);
+
+	/* Fill out the mib information. */
+	if (sysctlnametomib(buf, mib, &nlen) == -1)
+		return (-1);
+
+	/* Read the sensor temperature. */
+	len = sizeof(temp);
+	if (sysctl(mib, nlen, &temp, &len, NULL, 0) == -1)
+		return (-1);
+
+	/* Add the sensor data to the table. */
+	sensor = calloc(1, sizeof(*sensor));
+	if (sensor == NULL) {
+		syslog(LOG_ERR, "Unable to allocate %zu bytes for resource",
+		    sizeof(*sensor));
+		return (-1);
+	}
+	sensor->index = ++lm75_sensors;
+	sensor->sysctlidx = idx;
+	sensor->temp = (temp - TZ_ZEROC) / 10;
+	TAILQ_INSERT_TAIL(&sensors, sensor, link);
+
+	update_sensor(sensor, idx);
+
+	return (0);
+}
+
+static int
+update_sensors(void)
+{
+	char buf[LM75BUF];
+	int i, root[5], *next, *oid;
+	size_t len, nextlen, rootlen;
+	static uint64_t now;
+
+	now = get_ticks();
+	if (now - last_sensors_update < UPDATE_INTERVAL)
+		return (0);
+
+	last_sensors_update = now;
+
+	/* Reset the sensor data. */
+	free_sensors();
+	lm75_sensors = 0;
+
+	/* Start from the lm75 default root node. */
+	rootlen = 2;
+	if (sysctlnametomib("dev.lm75", root, &rootlen) == -1)
+		return (0);
+
+	oid = (int *)malloc(sizeof(int) * rootlen);
+	if (oid == NULL) {
+		perror("malloc");
+		return (-1);
+	}
+	memcpy(oid, root, rootlen * sizeof(int));
+	len = rootlen;
+
+	/* Traverse the sysctl(3) interface and find the active sensors. */
+	for (;;) {
+
+		/* Find the size of the next mib. */
+		nextlen = 0;
+		if (sysctlgetnext(oid, len, NULL, &nextlen) == -1) {
+			free(oid);
+			return (0);
+		}
+		/* Alocate and read the next mib. */
+		next = (int *)malloc(nextlen);
+		if (next == NULL) {
+			syslog(LOG_ERR,
+			    "Unable to allocate %zu bytes for resource",
+			    nextlen);
+			free(oid);
+			return (-1);
+		}
+		if (sysctlgetnext(oid, len, next, &nextlen) == -1) {
+			free(oid);
+			free(next);
+			return (0);
+		}
+		free(oid);
+		/* Check if we care about the next mib. */
+		for (i = 0; i < (int)rootlen; i++)
+			if (next[i] != root[i]) {
+				free(next);
+				return (0);
+			}
+		oid = (int *)malloc(nextlen);
+		if (oid == NULL) {
+			syslog(LOG_ERR,
+			    "Unable to allocate %zu bytes for resource",
+			    nextlen);
+			free(next);
+			return (-1);
+		}
+		memcpy(oid, next, nextlen);
+		free(next);
+		len = nextlen / sizeof(int);
+
+		/* Find the mib name. */
+		if (sysctlname(oid, len, buf, sizeof(buf)) != 0)
+			continue;
+
+		if (strstr(buf, "temperature"))
+			if (add_sensor(buf, len) != 0) {
+				free(oid);
+				return (-1);
+			}
+	}
+
+	return (0);
+}
+
+int
+op_lm75Sensors(struct snmp_context *context __unused, struct snmp_value *value,
+    u_int sub, u_int iidx __unused, enum snmp_op op)
+{
+	asn_subid_t which;
+
+	if (update_sensors() == -1)
+		return (SNMP_ERR_RES_UNAVAIL);
+
+	which = value->var.subs[sub - 1];
+
+	switch (op) {
+	case SNMP_OP_GET:
+		switch (which) {
+		case LEAF_lm75Sensors:
+			value->v.integer = lm75_sensors;
+			break;
+		default:
+			return (SNMP_ERR_RES_UNAVAIL);
+		}
+		break;
+	case SNMP_OP_SET:
+		return (SNMP_ERR_NOT_WRITEABLE);
+	case SNMP_OP_GETNEXT:
+	case SNMP_OP_ROLLBACK:
+	case SNMP_OP_COMMIT:
+		return (SNMP_ERR_NOERROR);
+	default:
+		return (SNMP_ERR_RES_UNAVAIL);
+	}
+
+	return (SNMP_ERR_NOERROR);
+}
+
+int
+op_lm75SensorTable(struct snmp_context *context __unused,
+    struct snmp_value *value, u_int sub, u_int iidx __unused, enum snmp_op op)
+{
+	struct lm75_snmp_sensor *sensor;
+	asn_subid_t which;
+	int ret;
+
+	if (update_sensors() == -1)
+		return (SNMP_ERR_RES_UNAVAIL);
+
+	which = value->var.subs[sub - 1];
+
+	switch (op) {
+	case SNMP_OP_GETNEXT:
+		sensor = NEXT_OBJECT_INT(&sensors, &value->var, sub);
+		if (sensor == NULL)
+			return (SNMP_ERR_NOSUCHNAME);
+		value->var.len = sub + 1;
+		value->var.subs[sub] = sensor->index;
+		break;
+	case SNMP_OP_GET:
+		if (value->var.len - sub != 1)
+			return (SNMP_ERR_NOSUCHNAME);
+		sensor = FIND_OBJECT_INT(&sensors, &value->var, sub);
+		if (sensor == NULL)
+			return (SNMP_ERR_NOSUCHNAME);
+		break;
+	case SNMP_OP_SET:
+		return (SNMP_ERR_NOT_WRITEABLE);
+	case SNMP_OP_ROLLBACK:
+	case SNMP_OP_COMMIT:
+		return (SNMP_ERR_NOERROR);
+	default:
+		return (SNMP_ERR_RES_UNAVAIL);
+	}
+
+	ret = SNMP_ERR_NOERROR;
+
+	switch (which) {
+	case LEAF_lm75SensorIndex:
+		value->v.integer = sensor->index;
+		break;
+	case LEAF_lm75SensorSysctlIndex:
+		value->v.integer = sensor->sysctlidx;
+		break;
+	case LEAF_lm75SensorDesc:
+		ret = string_get(value, sensor->desc, -1);
+		break;
+	case LEAF_lm75SensorLocation:
+		ret = string_get(value, sensor->location, -1);
+		break;
+	case LEAF_lm75SensorPnpInfo:
+		ret = string_get(value, sensor->pnpinfo, -1);
+		break;
+	case LEAF_lm75SensorParent:
+		ret = string_get(value, sensor->parent, -1);
+		break;
+	case LEAF_lm75SensorTemperature:
+		value->v.integer = sensor->temp;
+		break;
+	default:
+		ret = SNMP_ERR_RES_UNAVAIL;
+		break;
+	}
+
+	return (ret);
+}



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201406010314.s513E3DB092086>