sm_sensor.c 5.4 KB

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