sm_smart.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (c) 2009 Willem Dijkstra
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * - Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following
  13. * disclaimer in the documentation and/or other materials provided
  14. * with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  19. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  20. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  22. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  26. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. */
  30. #include <sys/param.h>
  31. #include <sys/stat.h>
  32. #include <sys/types.h>
  33. #include <sys/ata.h>
  34. #include <errno.h>
  35. #include <fcntl.h>
  36. #include <strings.h>
  37. #include <string.h>
  38. #include <stdlib.h>
  39. #include "conf.h"
  40. #include "data.h"
  41. #include "error.h"
  42. #include "xmalloc.h"
  43. #include "smart.h"
  44. #ifdef HAS_IOCATAREQUEST
  45. #ifndef HAS_ATA_SMART_CMD
  46. #define ATA_SMART_CMD 0xb0
  47. #endif
  48. /* per drive storage structure */
  49. struct smart_device {
  50. char name[MAXPATHLEN];
  51. int fd;
  52. int type;
  53. int failed;
  54. struct ata_ioc_request cmd;
  55. struct smart_values data;
  56. };
  57. static struct smart_device *smart_devs = NULL;
  58. static int smart_cur = 0;
  59. void
  60. init_smart(struct stream *st)
  61. {
  62. int i;
  63. char drivename[MAXPATHLEN];
  64. struct ata_ioc_request *p;
  65. if (sizeof(struct smart_values) != DISK_BLOCK_LEN) {
  66. fatal("smart: internal error: smart values structure is broken");
  67. }
  68. if (st->arg == NULL) {
  69. fatal("smart: need a <device> argument");
  70. }
  71. bzero(drivename, MAXPATHLEN);
  72. snprintf(drivename, MAXPATHLEN, "/dev/%s", st->arg);
  73. /* look for drive in our global table */
  74. for (i = 0; i < smart_cur; i++) {
  75. if (strncmp(smart_devs[i].name, drivename, MAXPATHLEN) == 0) {
  76. st->parg.smart = i;
  77. return;
  78. }
  79. }
  80. /* this is a new drive; allocate the command and data block */
  81. if (smart_cur > SYMON_MAX_DOBJECTS) {
  82. fatal("%s:%d: dynamic object limit (%d) exceeded for smart data",
  83. __FILE__, __LINE__, SYMON_MAX_DOBJECTS);
  84. }
  85. smart_devs = xrealloc(smart_devs, (smart_cur + 1) * sizeof(struct smart_device));
  86. bzero(&smart_devs[smart_cur], sizeof(struct smart_device));
  87. /* rewire all bufferlocations, as our addresses may have changed */
  88. for (i = 0; i <= smart_cur; i++) {
  89. smart_devs[i].cmd.data = (caddr_t)&smart_devs[i].data;
  90. }
  91. /* store drivename in new block */
  92. snprintf(smart_devs[smart_cur].name, MAXPATHLEN, "%s", drivename);
  93. /* populate ata command header */
  94. p = &smart_devs[smart_cur].cmd;
  95. p->u.ata.command = ATA_SMART_CMD;
  96. p->timeout = SMART_TIMEOUT;
  97. p->u.ata.feature = ATA_SMART_READ_VALUES;
  98. p->u.ata.lba = SMART_CYLINDER << 8;
  99. p->flags = ATA_CMD_READ;
  100. p->count = DISK_BLOCK_LEN;
  101. /* store filedescriptor to device */
  102. smart_devs[smart_cur].fd = open(drivename, O_RDONLY | O_NONBLOCK);
  103. if (errno) {
  104. fatal("smart: could not open '%s' for read; %.200s", drivename, strerror(errno));
  105. }
  106. /* store smart dev entry in stream to facilitate quick get */
  107. st->parg.smart = smart_cur;
  108. smart_cur++;
  109. info("started module smart(%.200s)", st->arg);
  110. }
  111. void
  112. gets_smart()
  113. {
  114. int i;
  115. for (i = 0; i < smart_cur; i++) {
  116. if (ioctl(smart_devs[i].fd, IOCATAREQUEST, &smart_devs[i].cmd) || smart_devs[i].cmd.error) {
  117. warning("smart: ioctl for drive '%s' failed: %d",
  118. &smart_devs[i].name, errno);
  119. smart_devs[i].failed = 1;
  120. }
  121. /* Some drives do not calculate the smart checksum correctly;
  122. * additional code that identifies these drives would increase our
  123. * footprint and the amount of datajuggling we need to do; we would
  124. * rather ignore the checksums.
  125. */
  126. smart_devs[i].failed = 0;
  127. }
  128. return;
  129. }
  130. int
  131. get_smart(char *symon_buf, int maxlen, struct stream *st)
  132. {
  133. struct smart_report sr;
  134. if ((st->parg.smart < smart_cur) &&
  135. (!smart_devs[st->parg.smart].failed))
  136. {
  137. smart_parse(&smart_devs[st->parg.smart].data, &sr);
  138. return snpack(symon_buf, maxlen, st->arg, MT_SMART,
  139. sr.read_error_rate,
  140. sr.reallocated_sectors,
  141. sr.spin_retries,
  142. sr.air_flow_temp,
  143. sr.temperature,
  144. sr.reallocations,
  145. sr.current_pending,
  146. sr.uncorrectables,
  147. sr.soft_read_error_rate,
  148. sr.g_sense_error_rate,
  149. sr.temperature2,
  150. sr.free_fall_protection);
  151. }
  152. return 0;
  153. }
  154. #else
  155. void
  156. init_smart(struct stream *st)
  157. {
  158. fatal("smart module not available");
  159. }
  160. void
  161. gets_smart()
  162. {
  163. fatal("smart module not available");
  164. }
  165. int
  166. get_smart(char *symon_buf, int maxlen, struct stream *st)
  167. {
  168. fatal("io module not available");
  169. /* NOT REACHED */
  170. return 0;
  171. }
  172. #endif