sm_sensor.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* $Id: sm_sensor.c,v 1.10 2007/02/11 20:26:56 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2001-2005 Willem Dijkstra
  4. * Copyright (c) 2006-2007 Constantine A. Murenin
  5. * <cnst+symon@bugmail.mojo.ru>
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * - Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * - Redistributions in binary form must reproduce the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer in the documentation and/or other materials provided
  17. * with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  29. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. /*
  34. * Get sensor data from the kernel and return in symon_buf as
  35. *
  36. * num : value
  37. *
  38. */
  39. #include "conf.h"
  40. #include <sys/param.h>
  41. #include <sys/sysctl.h>
  42. #include <errno.h>
  43. #include <limits.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include <ctype.h>
  47. #include "error.h"
  48. #include "symon.h"
  49. /* Globals for this module start with sn_ */
  50. static struct sensor sn_sensor;
  51. #ifndef HAS_SENSORS_H
  52. void
  53. privinit_sensor()
  54. {
  55. fatal("sensor support not available");
  56. }
  57. void
  58. init_sensor(struct stream *st)
  59. {
  60. fatal("sensor support not available");
  61. }
  62. int
  63. get_sensor(char *symon_buf, int maxlen, struct stream *st)
  64. {
  65. fatal("sensor support not available");
  66. return 0;
  67. }
  68. #else
  69. #include <sys/sensors.h>
  70. void
  71. privinit_sensor()
  72. {
  73. /* EMPTY */
  74. }
  75. void
  76. init_sensor(struct stream *st)
  77. {
  78. #ifndef HAS_SENSORDEV
  79. long l = strtol(st->arg, NULL, 10);
  80. st->parg.sn.mib[0] = CTL_HW;
  81. st->parg.sn.mib[1] = HW_SENSORS;
  82. st->parg.sn.mib[2] = (int) (l & SYMON_SENSORMASK);
  83. #else /* HAS_SENSORDEV */
  84. char *devname, *typename, *bufp, *bufpo;
  85. int dev, numt, i;
  86. enum sensor_type type;
  87. struct sensordev sensordev;
  88. size_t sdlen = sizeof(sensordev);
  89. st->parg.sn.mib[0] = CTL_HW;
  90. st->parg.sn.mib[1] = HW_SENSORS;
  91. bufpo = xstrdup(st->arg);
  92. bufp = bufpo;
  93. if ((devname = strsep(&bufp, ".")) == NULL)
  94. fatal("sensor(%.200s): incomplete specification", st->arg);
  95. /* convert sensor device string to an integer */
  96. for (dev = 0; dev < MAXSENSORDEVICES; dev++) {
  97. st->parg.sn.mib[2] = dev;
  98. if (sysctl(st->parg.sn.mib, 3, &sensordev, &sdlen, NULL, 0) == -1)
  99. continue;
  100. if (strcmp(devname, sensordev.xname) == 0)
  101. break;
  102. }
  103. if (strcmp(devname, sensordev.xname) != 0)
  104. fatal("sensor(%.200s): device not found: %.200s",
  105. st->arg, devname);
  106. /* convert sensor_type string to an integer */
  107. if ((typename = strsep(&bufp, ".")) == NULL)
  108. fatal("sensor(%.200s): incomplete specification", st->arg);
  109. numt = -1;
  110. for (i = 0; typename[i] != '\0'; i++)
  111. if (isdigit(typename[i])) {
  112. numt = atoi(&typename[i]);
  113. typename[i] = '\0';
  114. break;
  115. }
  116. for (type = 0; type < SENSOR_MAX_TYPES; type++)
  117. if (strcmp(typename, sensor_type_s[type]) == 0)
  118. break;
  119. if (type == SENSOR_MAX_TYPES)
  120. fatal("sensor(%.200s): sensor type not recognised: %.200s",
  121. st->arg, typename);
  122. if (sensordev.maxnumt[type] == 0)
  123. fatal("sensor(%.200s): no sensors of such type on this device: %.200s",
  124. st->arg, typename);
  125. st->parg.sn.mib[3] = type;
  126. if (numt == -1) {
  127. warning("sensor(%.200s): sensor number not specified, using 0",
  128. st->arg);
  129. numt = 0;
  130. }
  131. if (!(numt < sensordev.maxnumt[type]))
  132. fatal("sensor(%.200s): no such sensor attached to this device: %.200s%i",
  133. st->arg, typename, numt);
  134. st->parg.sn.mib[4] = numt;
  135. xfree(bufpo);
  136. #endif /* !HAS_SENSORDEV */
  137. info("started module sensor(%.200s)", st->arg);
  138. }
  139. int
  140. get_sensor(char *symon_buf, int maxlen, struct stream *st)
  141. {
  142. size_t len = sizeof(sn_sensor);
  143. double t;
  144. if (sysctl(st->parg.sn.mib,
  145. sizeof(st->parg.sn.mib)/sizeof(st->parg.sn.mib[0]),
  146. &sn_sensor, &len, NULL, 0) == -1) {
  147. if (errno != ENOENT)
  148. warning("%s:%d: sensor(%.200s): sysctl error: %.200s",
  149. __FILE__, __LINE__, st->arg, strerror(errno));
  150. else
  151. warning("sensor(%.200s): sensor not found",
  152. st->arg);
  153. return 0;
  154. } else {
  155. switch (sn_sensor.type) {
  156. case SENSOR_TEMP:
  157. t = (double) (sn_sensor.value / 1000.0 / 1000.0) - 273.15;
  158. break;
  159. case SENSOR_FANRPM:
  160. t = (double) sn_sensor.value;
  161. break;
  162. case SENSOR_VOLTS_DC:
  163. t = (double) (sn_sensor.value / 1000.0 / 1000.0);
  164. break;
  165. default:
  166. t = (double) sn_sensor.value;
  167. }
  168. return snpack(symon_buf, maxlen, st->arg, MT_SENSOR, t);
  169. }
  170. }
  171. #endif /* HAS_SENSORS_H */