sm_sensor.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* $Id: sm_sensor.c,v 1.7 2005/10/18 19:58:11 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2001-2005 Willem Dijkstra
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * - Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following
  14. * disclaimer in the documentation and/or other materials provided
  15. * with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  27. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. /*
  32. * Get sensor data from the kernel and return in symon_buf as
  33. *
  34. * num : value
  35. *
  36. */
  37. #include "conf.h"
  38. #include <sys/param.h>
  39. #include <sys/sysctl.h>
  40. #include <errno.h>
  41. #include <limits.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include "error.h"
  45. #include "symon.h"
  46. #ifndef HAS_SENSORS_H
  47. void
  48. privinit_sensor()
  49. {
  50. fatal("sensor support not available");
  51. }
  52. void
  53. init_sensor(struct stream *st)
  54. {
  55. fatal("sensor support not available");
  56. }
  57. int
  58. get_sensor(char *symon_buf, int maxlen, struct stream *st)
  59. {
  60. fatal("sensor support not available");
  61. return 0;
  62. }
  63. #else
  64. #include <sys/sensors.h>
  65. /* Globals for this module start with sn_ */
  66. static int sn_mib[] = {CTL_HW, HW_SENSORS, 0};
  67. static struct sensor sn_sensor;
  68. void
  69. privinit_sensor()
  70. {
  71. /* EMPTY */
  72. }
  73. void
  74. init_sensor(struct stream *st)
  75. {
  76. long l = strtol(st->arg, NULL, 10);
  77. st->parg.sn = (int) (l & SYMON_SENSORMASK);
  78. info("started module sensors(%.200s)", st->arg);
  79. }
  80. int
  81. get_sensor(char *symon_buf, int maxlen, struct stream *st)
  82. {
  83. size_t len;
  84. double t;
  85. bzero((void *) &sn_sensor, sizeof(sn_sensor));
  86. sn_mib[2] = st->parg.sn;
  87. len = sizeof(sn_sensor);
  88. if (sysctl(sn_mib, 3, &sn_sensor, &len, NULL, 0) == -1) {
  89. warning("%s:%d: sensor can't get sensor %.200s -- %.200s",
  90. __FILE__, __LINE__, st->arg, strerror(errno));
  91. return 0;
  92. } else {
  93. switch (sn_sensor.type) {
  94. case SENSOR_TEMP:
  95. t = (double) (sn_sensor.value / 1000.0 / 1000.0) - 273.16;
  96. break;
  97. case SENSOR_FANRPM:
  98. t = (double) sn_sensor.value;
  99. break;
  100. case SENSOR_VOLTS_DC:
  101. t = (double) (sn_sensor.value / 1000.0 / 1000.0);
  102. break;
  103. default:
  104. t = (double) sn_sensor.value;
  105. }
  106. return snpack(symon_buf, maxlen, st->arg, MT_SENSOR, t);
  107. }
  108. }
  109. #endif /* HAS_SENSORS_H */