sm_sensor.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* $Id: sm_sensor.c,v 1.1 2003/10/12 17:32:22 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2001-2003 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. * This code is not re-entrant. It uses sysctl and can be run as any user.
  37. */
  38. #include <conf.h>
  39. #include <sys/param.h>
  40. #include <sys/sysctl.h>
  41. #ifdef HAS_SENSORS_H
  42. #include <sys/sensors.h>
  43. #endif HAS_SENSORS_H
  44. #include <errno.h>
  45. #include <limits.h>
  46. #include <stdlib.h>
  47. #include <string.h>
  48. #include "error.h"
  49. #include "symon.h"
  50. #ifndef HAS_SENSORS_H
  51. void
  52. init_sensor(char *s)
  53. {
  54. fatal("sensor module requires system upgrade to OpenBSD 3.4");
  55. }
  56. int
  57. get_sensor(char *symon_buf, int maxlen, char *s)
  58. {
  59. fatal("sensor module requires system upgrade to OpenBSD 3.4");
  60. }
  61. #else
  62. /* Globals for this module start with sn_ */
  63. static int sn_mib[] = {CTL_HW, HW_SENSORS, 0};
  64. static struct sensor sn_sensor;
  65. /* Prepare if module for first use */
  66. void
  67. init_sensor(char *s)
  68. {
  69. info("started module sensors(%s)", s);
  70. }
  71. /* Get sensor statistics */
  72. int
  73. get_sensor(char *symon_buf, int maxlen, char *s)
  74. {
  75. size_t len;
  76. long l;
  77. int i;
  78. double t;
  79. bzero((void *) &sn_sensor, sizeof(sn_sensor));
  80. l = strtol(s, NULL, 10);
  81. i = (int) (l & SYMON_SENSORMASK);
  82. sn_mib[2] = i;
  83. len = sizeof(sn_sensor);
  84. if (sysctl(sn_mib, 3, &sn_sensor, &len, NULL, 0) == -1) {
  85. warning("%s:%d: sensor can't get sensor %.200s -- %.200s",
  86. __FILE__, __LINE__, s, strerror(errno));
  87. return 0;
  88. } else {
  89. switch (sn_sensor.type) {
  90. case SENSOR_TEMP:
  91. t = (double) (sn_sensor.value / 1000.0 / 1000.0) - 273.16;
  92. break;
  93. case SENSOR_FANRPM:
  94. t = (double) sn_sensor.value;
  95. break;
  96. case SENSOR_VOLTS_DC:
  97. t = (double) (sn_sensor.value / 1000.0 / 1000.0);
  98. break;
  99. default:
  100. t = (double) sn_sensor.value;
  101. }
  102. return snpack(symon_buf, maxlen, s, MT_SENSOR, t);
  103. }
  104. }
  105. #endif /* HAS_SENSORS_H */