sm_sensor.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. /* Globals for this module start with sn_ */
  51. static struct sensor sn_sensor;
  52. void
  53. privinit_sensor()
  54. {
  55. /* EMPTY */
  56. }
  57. void
  58. init_sensor(struct stream *st)
  59. {
  60. #ifndef HAS_SENSORDEV
  61. long l = strtol(st->arg, NULL, 10);
  62. st->parg.sn.mib[0] = CTL_HW;
  63. st->parg.sn.mib[1] = HW_SENSORS;
  64. st->parg.sn.mib[2] = (int) (l & SYMON_SENSORMASK);
  65. #else /* HAS_SENSORDEV */
  66. char *devname, *typename, *bufp, *bufpo;
  67. int dev, numt, i;
  68. enum sensor_type type;
  69. struct sensordev sensordev;
  70. size_t sdlen = sizeof(sensordev);
  71. st->parg.sn.mib[0] = CTL_HW;
  72. st->parg.sn.mib[1] = HW_SENSORS;
  73. bufpo = xstrdup(st->arg);
  74. bufp = bufpo;
  75. if ((devname = strsep(&bufp, ".")) == NULL)
  76. fatal("%s:%d: sensor(%.200s): incomplete specification",
  77. __FILE__, __LINE__, st->arg);
  78. /* convert sensor device string to an integer */
  79. for (dev = 0; dev < MAXSENSORDEVICES; dev++) {
  80. st->parg.sn.mib[2] = dev;
  81. if (sysctl(st->parg.sn.mib, 3, &sensordev, &sdlen, NULL, 0) == -1) {
  82. if (errno == ENOENT)
  83. break;
  84. if (errno == ENXIO)
  85. continue;
  86. fatal("%s:%d: sensor(%.200s): sensor %d unknown errno %d",
  87. __FILE__, __LINE__, st->arg, dev, errno);
  88. }
  89. if (strcmp(devname, sensordev.xname) == 0)
  90. break;
  91. }
  92. if (strcmp(devname, sensordev.xname) != 0)
  93. fatal("sensor(%.200s): device not found: %.200s",
  94. st->arg, devname);
  95. /* convert sensor_type string to an integer */
  96. if ((typename = strsep(&bufp, ".")) == NULL)
  97. fatal("%s:%d: sensor(%.200s): incomplete specification",
  98. __FILE__, __LINE__, st->arg);
  99. numt = -1;
  100. for (i = 0; typename[i] != '\0'; i++)
  101. if (isdigit(typename[i])) {
  102. numt = atoi(&typename[i]);
  103. typename[i] = '\0';
  104. break;
  105. }
  106. for (type = 0; type < SENSOR_MAX_TYPES; type++)
  107. if (strcmp(typename, sensor_type_s[type]) == 0)
  108. break;
  109. if (type == SENSOR_MAX_TYPES)
  110. fatal("sensor(%.200s): sensor type not recognised: %.200s",
  111. st->arg, typename);
  112. if (sensordev.maxnumt[type] == 0)
  113. fatal("sensor(%.200s): no sensors of such type on this device: %.200s",
  114. st->arg, typename);
  115. st->parg.sn.mib[3] = type;
  116. if (numt == -1) {
  117. warning("sensor(%.200s): sensor number not specified, using 0",
  118. st->arg);
  119. numt = 0;
  120. }
  121. if (!(numt < sensordev.maxnumt[type]))
  122. fatal("sensor(%.200s): no such sensor attached to this device: %.200s%i",
  123. st->arg, typename, numt);
  124. st->parg.sn.mib[4] = numt;
  125. xfree(bufpo);
  126. #endif /* !HAS_SENSORDEV */
  127. info("started module sensor(%.200s)", st->arg);
  128. }
  129. int
  130. get_sensor(char *symon_buf, int maxlen, struct stream *st)
  131. {
  132. size_t len = sizeof(sn_sensor);
  133. double t;
  134. if (sysctl(st->parg.sn.mib,
  135. sizeof(st->parg.sn.mib)/sizeof(st->parg.sn.mib[0]),
  136. &sn_sensor, &len, NULL, 0) == -1) {
  137. if (errno != ENOENT)
  138. warning("%s:%d: sensor(%.200s): sysctl error: %.200s",
  139. __FILE__, __LINE__, st->arg, strerror(errno));
  140. else
  141. warning("sensor(%.200s): sensor not found",
  142. st->arg);
  143. return 0;
  144. } else {
  145. switch (sn_sensor.type) {
  146. case SENSOR_TEMP:
  147. t = (double) (sn_sensor.value / 1000.0 / 1000.0) - 273.15;
  148. break;
  149. case SENSOR_FANRPM:
  150. t = (double) sn_sensor.value;
  151. break;
  152. case SENSOR_VOLTS_DC:
  153. t = (double) (sn_sensor.value / 1000.0 / 1000.0);
  154. break;
  155. default:
  156. t = (double) sn_sensor.value;
  157. }
  158. return snpack(symon_buf, maxlen, st->arg, MT_SENSOR, t);
  159. }
  160. }