sm_sensor.c 5.6 KB

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