sm_sensor.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. continue;
  83. if (strcmp(devname, sensordev.xname) == 0)
  84. break;
  85. }
  86. if (strcmp(devname, sensordev.xname) != 0)
  87. fatal("sensor(%.200s): device not found: %.200s",
  88. st->arg, devname);
  89. /* convert sensor_type string to an integer */
  90. if ((typename = strsep(&bufp, ".")) == NULL)
  91. fatal("%s:%d: sensor(%.200s): incomplete specification",
  92. __FILE__, __LINE__, st->arg);
  93. numt = -1;
  94. for (i = 0; typename[i] != '\0'; i++)
  95. if (isdigit(typename[i])) {
  96. numt = atoi(&typename[i]);
  97. typename[i] = '\0';
  98. break;
  99. }
  100. for (type = 0; type < SENSOR_MAX_TYPES; type++)
  101. if (strcmp(typename, sensor_type_s[type]) == 0)
  102. break;
  103. if (type == SENSOR_MAX_TYPES)
  104. fatal("sensor(%.200s): sensor type not recognised: %.200s",
  105. st->arg, typename);
  106. if (sensordev.maxnumt[type] == 0)
  107. fatal("sensor(%.200s): no sensors of such type on this device: %.200s",
  108. st->arg, typename);
  109. st->parg.sn.mib[3] = type;
  110. if (numt == -1) {
  111. warning("sensor(%.200s): sensor number not specified, using 0",
  112. st->arg);
  113. numt = 0;
  114. }
  115. if (!(numt < sensordev.maxnumt[type]))
  116. fatal("sensor(%.200s): no such sensor attached to this device: %.200s%i",
  117. st->arg, typename, numt);
  118. st->parg.sn.mib[4] = numt;
  119. xfree(bufpo);
  120. #endif /* !HAS_SENSORDEV */
  121. info("started module sensor(%.200s)", st->arg);
  122. }
  123. int
  124. get_sensor(char *symon_buf, int maxlen, struct stream *st)
  125. {
  126. size_t len = sizeof(sn_sensor);
  127. double t;
  128. if (sysctl(st->parg.sn.mib,
  129. sizeof(st->parg.sn.mib)/sizeof(st->parg.sn.mib[0]),
  130. &sn_sensor, &len, NULL, 0) == -1) {
  131. if (errno != ENOENT)
  132. warning("%s:%d: sensor(%.200s): sysctl error: %.200s",
  133. __FILE__, __LINE__, st->arg, strerror(errno));
  134. else
  135. warning("sensor(%.200s): sensor not found",
  136. st->arg);
  137. return 0;
  138. } else {
  139. switch (sn_sensor.type) {
  140. case SENSOR_TEMP:
  141. t = (double) (sn_sensor.value / 1000.0 / 1000.0) - 273.15;
  142. break;
  143. case SENSOR_FANRPM:
  144. t = (double) sn_sensor.value;
  145. break;
  146. case SENSOR_VOLTS_DC:
  147. t = (double) (sn_sensor.value / 1000.0 / 1000.0);
  148. break;
  149. default:
  150. t = (double) sn_sensor.value;
  151. }
  152. return snpack(symon_buf, maxlen, st->arg, MT_SENSOR, t);
  153. }
  154. }