sm_smart.c 5.8 KB

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