sm_smart.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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/ataio.h>
  34. #include <dev/ata/atavar.h>
  35. #include <dev/ic/wdcreg.h>
  36. #include <errno.h>
  37. #include <fcntl.h>
  38. #include <strings.h>
  39. #include <string.h>
  40. #include <stdlib.h>
  41. #include "conf.h"
  42. #include "data.h"
  43. #include "error.h"
  44. #include "xmalloc.h"
  45. #include "smart.h"
  46. /* per drive storage structure */
  47. struct smart_device {
  48. char name[MAXPATHLEN];
  49. int fd;
  50. int type;
  51. int failed;
  52. struct atareq cmd;
  53. struct smart_values data;
  54. };
  55. static struct smart_device *smart_devs = NULL;
  56. static int smart_cur = 0;
  57. void
  58. init_smart(struct stream *st)
  59. {
  60. int i;
  61. char drivename[MAXPATHLEN];
  62. struct atareq *p;
  63. if (sizeof(struct smart_values) != DISK_BLOCK_LEN) {
  64. fatal("smart: internal error: smart values structure is broken");
  65. }
  66. if (st->arg == NULL) {
  67. fatal("smart: need a <device> argument");
  68. }
  69. bzero(drivename, MAXPATHLEN);
  70. snprintf(drivename, MAXPATHLEN, "/dev/%s", st->arg);
  71. /* look for drive in our global table */
  72. for (i = 0; i < smart_cur; i++) {
  73. if (strncmp(smart_devs[i].name, drivename, MAXPATHLEN) == 0) {
  74. st->parg.smart = i;
  75. return;
  76. }
  77. }
  78. /* this is a new drive; allocate the command and data block */
  79. if (smart_cur > SYMON_MAX_DOBJECTS) {
  80. fatal("%s:%d: dynamic object limit (%d) exceeded for smart data",
  81. __FILE__, __LINE__, SYMON_MAX_DOBJECTS);
  82. }
  83. smart_devs = xrealloc(smart_devs, (smart_cur + 1) * sizeof(struct smart_device));
  84. bzero(&smart_devs[smart_cur], sizeof(struct smart_device));
  85. /* rewire all bufferlocations, as our addresses may have changed */
  86. for (i = 0; i <= smart_cur; i++) {
  87. smart_devs[i].cmd.databuf = (caddr_t)&smart_devs[i].data;
  88. }
  89. /* store drivename in new block */
  90. snprintf(smart_devs[smart_cur].name, MAXPATHLEN, "%s", drivename);
  91. /* populate ata command header */
  92. p = &smart_devs[smart_cur].cmd;
  93. p->command = ATAPI_SMART;
  94. p->timeout = SMART_TIMEOUT;
  95. p->features = ATA_SMART_READ_VALUES;
  96. p->cylinder = htole16(SMART_CYLINDER);
  97. p->flags = ATACMD_READ;
  98. p->databuf = (caddr_t) &smart_devs[smart_cur].data;
  99. p->datalen = DISK_BLOCK_LEN;
  100. /* store filedescriptor to device */
  101. smart_devs[smart_cur].fd = open(drivename, O_RDONLY | O_NONBLOCK);
  102. if (errno) {
  103. fatal("smart: could not open '%s' for read", drivename);
  104. }
  105. /* store smart dev entry in stream to facilitate quick get */
  106. st->parg.smart = smart_cur;
  107. smart_cur++;
  108. info("started module smart(%.200s)", st->arg);
  109. }
  110. void
  111. gets_smart()
  112. {
  113. int i;
  114. for (i = 0; i < smart_cur; i++) {
  115. if (ioctl(smart_devs[i].fd, ATAIOCCOMMAND, &smart_devs[i].cmd)) {
  116. warning("smart: ioctl for drive '%s' failed: %d",
  117. &smart_devs[i].name, errno);
  118. smart_devs[i].failed = 1;
  119. }
  120. /* Some drives do not calculate the smart checksum correctly;
  121. * additional code that identifies these drives would increase our
  122. * footprint and the amount of datajuggling we need to do; we would
  123. * rather ignore the checksums.
  124. */
  125. smart_devs[i].failed = 0;
  126. }
  127. return;
  128. }
  129. int
  130. get_smart(char *symon_buf, int maxlen, struct stream *st)
  131. {
  132. struct smart_report sr;
  133. if ((st->parg.smart < smart_cur) &&
  134. (!smart_devs[st->parg.smart].failed))
  135. {
  136. smart_parse(&smart_devs[st->parg.smart].data, &sr);
  137. return snpack(symon_buf, maxlen, st->arg, MT_SMART,
  138. sr.read_error_rate,
  139. sr.reallocated_sectors,
  140. sr.spin_retries,
  141. sr.air_flow_temp,
  142. sr.temperature,
  143. sr.reallocations,
  144. sr.current_pending,
  145. sr.uncorrectables,
  146. sr.soft_read_error_rate,
  147. sr.g_sense_error_rate,
  148. sr.temperature2,
  149. sr.free_fall_protection);
  150. }
  151. return 0;
  152. }