sm_smart.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (c) 2009-2012 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. #include "diskname.h"
  45. #ifdef HAS_IOCATAREQUEST
  46. #ifndef HAS_ATA_SMART_CMD
  47. #define ATA_SMART_CMD 0xb0
  48. #endif
  49. /* per drive storage structure */
  50. struct smart_device {
  51. char name[MAX_PATH_LEN];
  52. int fd;
  53. int type;
  54. int failed;
  55. struct ata_ioc_request cmd;
  56. struct smart_values data;
  57. };
  58. static struct smart_device *smart_devs = NULL;
  59. static int smart_cur = 0;
  60. void
  61. init_smart(struct stream *st)
  62. {
  63. struct disknamectx c;
  64. int fd;
  65. int i;
  66. char drivename[MAX_PATH_LEN];
  67. struct ata_ioc_request *p;
  68. if (sizeof(struct smart_values) != DISK_BLOCK_LEN) {
  69. fatal("smart: internal error: smart values structure is broken");
  70. }
  71. if (st->arg == NULL) {
  72. fatal("smart: need a <disk device|name> argument");
  73. }
  74. initdisknamectx(&c, st->arg, drivename, sizeof(drivename));
  75. fd = -1;
  76. while (nextdiskname(&c))
  77. if ((fd = open(drivename, O_RDONLY, O_NONBLOCK)) != -1)
  78. break;
  79. if (fd < 0)
  80. fatal("smart: cannot open '%.200s'", st->arg);
  81. /* look for drive in our global table */
  82. for (i = 0; i < smart_cur; i++) {
  83. if (strncmp(smart_devs[i].name, drivename, MAX_PATH_LEN) == 0) {
  84. st->parg.smart = i;
  85. return;
  86. }
  87. }
  88. /* this is a new drive; allocate the command and data block */
  89. if (smart_cur > SYMON_MAX_DOBJECTS) {
  90. fatal("%s:%d: dynamic object limit (%d) exceeded for smart data",
  91. __FILE__, __LINE__, SYMON_MAX_DOBJECTS);
  92. }
  93. smart_devs = xrealloc(smart_devs, (smart_cur + 1) * sizeof(struct smart_device));
  94. bzero(&smart_devs[smart_cur], sizeof(struct smart_device));
  95. /* rewire all bufferlocations, as our addresses may have changed */
  96. for (i = 0; i <= smart_cur; i++) {
  97. smart_devs[i].cmd.data = (caddr_t)&smart_devs[i].data;
  98. }
  99. /* store drivename in new block */
  100. snprintf(smart_devs[smart_cur].name, MAX_PATH_LEN, "%s", drivename);
  101. /* populate ata command header */
  102. p = &smart_devs[smart_cur].cmd;
  103. p->u.ata.command = ATA_SMART_CMD;
  104. p->timeout = SMART_TIMEOUT;
  105. p->u.ata.feature = ATA_SMART_READ_VALUES;
  106. p->u.ata.lba = SMART_CYLINDER << 8;
  107. p->flags = ATA_CMD_READ;
  108. p->count = DISK_BLOCK_LEN;
  109. /* store filedescriptor to device */
  110. smart_devs[smart_cur].fd = fd;
  111. /* store smart dev entry in stream to facilitate quick get */
  112. st->parg.smart = smart_cur;
  113. smart_cur++;
  114. info("started module smart(%.200s)", st->arg);
  115. }
  116. void
  117. gets_smart()
  118. {
  119. int i;
  120. for (i = 0; i < smart_cur; i++) {
  121. if (ioctl(smart_devs[i].fd, IOCATAREQUEST, &smart_devs[i].cmd) || smart_devs[i].cmd.error) {
  122. warning("smart: ioctl for drive '%s' failed: %d",
  123. &smart_devs[i].name, errno);
  124. smart_devs[i].failed = 1;
  125. }
  126. /* Some drives do not calculate the smart checksum correctly;
  127. * additional code that identifies these drives would increase our
  128. * footprint and the amount of datajuggling we need to do; we would
  129. * rather ignore the checksums.
  130. */
  131. smart_devs[i].failed = 0;
  132. }
  133. return;
  134. }
  135. int
  136. get_smart(char *symon_buf, int maxlen, struct stream *st)
  137. {
  138. struct smart_report sr;
  139. if ((st->parg.smart < smart_cur) &&
  140. (!smart_devs[st->parg.smart].failed))
  141. {
  142. smart_parse(&smart_devs[st->parg.smart].data, &sr);
  143. return snpack(symon_buf, maxlen, st->arg, MT_SMART,
  144. sr.read_error_rate,
  145. sr.reallocated_sectors,
  146. sr.spin_retries,
  147. sr.air_flow_temp,
  148. sr.temperature,
  149. sr.reallocations,
  150. sr.current_pending,
  151. sr.uncorrectables,
  152. sr.soft_read_error_rate,
  153. sr.g_sense_error_rate,
  154. sr.temperature2,
  155. sr.free_fall_protection);
  156. }
  157. return 0;
  158. }
  159. #else
  160. void
  161. init_smart(struct stream *st)
  162. {
  163. fatal("smart module not available");
  164. }
  165. void
  166. gets_smart()
  167. {
  168. fatal("smart module not available");
  169. }
  170. int
  171. get_smart(char *symon_buf, int maxlen, struct stream *st)
  172. {
  173. fatal("io module not available");
  174. /* NOT REACHED */
  175. return 0;
  176. }
  177. #endif