sm_smart.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (c) 2008-2011 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/ioctl.h>
  31. #include <sys/stat.h>
  32. #include <sys/types.h>
  33. #include <errno.h>
  34. #include <fcntl.h>
  35. #include <strings.h>
  36. #include <string.h>
  37. #include <stdlib.h>
  38. #include <linux/hdreg.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. #ifndef HAS_HDDRIVECMDHDR
  46. typedef unsigned char task_ioreg_t;
  47. struct hd_drive_cmd_hdr {
  48. task_ioreg_t command;
  49. task_ioreg_t sector_number;
  50. task_ioreg_t feature;
  51. task_ioreg_t sector_count;
  52. };
  53. #endif
  54. /* Ata command register set for requesting smart values */
  55. static struct hd_drive_cmd_hdr smart_cmd = {
  56. WIN_SMART, /* command code */
  57. 0, /* sector number */
  58. SMART_READ_VALUES, /* feature */
  59. 1 /* sector count */
  60. };
  61. /* Per drive storage structure; the ata cmd is followed by the data buffer that
  62. * is filled by the ioctl. There can be no room between the two; hence the
  63. * pragma for byte alignment.
  64. */
  65. #pragma pack(1)
  66. struct smart_device {
  67. struct hd_drive_cmd_hdr cmd;
  68. struct smart_values data;
  69. char name[MAX_PATH_LEN];
  70. int fd;
  71. int type;
  72. int failed;
  73. };
  74. #pragma pack()
  75. static struct smart_device *smart_devs = NULL;
  76. static int smart_cur = 0;
  77. void
  78. init_smart(struct stream *st)
  79. {
  80. struct disknamectx c;
  81. int fd;
  82. int i;
  83. char drivename[MAX_PATH_LEN];
  84. if (sizeof(struct smart_values) != DISK_BLOCK_LEN) {
  85. fatal("smart: internal error: smart values structure is broken");
  86. }
  87. if (st->arg == NULL)
  88. fatal("smart: need a <disk device|name> argument");
  89. initdisknamectx(&c, st->arg, drivename, sizeof(drivename));
  90. fd = -1;
  91. while (nextdiskname(&c))
  92. if ((fd = open(drivename, O_RDONLY | O_NONBLOCK)) != -1)
  93. break;
  94. if (fd < 0)
  95. fatal("smart: cannot open '%.200s'", st->arg);
  96. /* look for drive in our global table */
  97. for (i = 0; i < smart_cur; i++) {
  98. if (strncmp(smart_devs[i].name, drivename, sizeof(drivename)) == 0) {
  99. st->parg.smart = i;
  100. return;
  101. }
  102. }
  103. /* this is a new drive; allocate the command and data block */
  104. if (smart_cur > SYMON_MAX_DOBJECTS) {
  105. fatal("%s:%d: dynamic object limit (%d) exceeded for smart data",
  106. __FILE__, __LINE__, SYMON_MAX_DOBJECTS);
  107. }
  108. smart_devs = xrealloc(smart_devs, (smart_cur + 1) * sizeof(struct smart_device));
  109. bzero(&smart_devs[smart_cur], sizeof(struct smart_device));
  110. /* store drivename in new block */
  111. strlcpy(smart_devs[smart_cur].name, drivename, sizeof(smart_devs[0].name));
  112. /* store filedescriptor to device */
  113. smart_devs[smart_cur].fd = fd;
  114. /* store smart dev entry in stream to facilitate quick get */
  115. st->parg.smart = smart_cur;
  116. smart_cur++;
  117. info("started module smart(%.200s = %.200s)", st->arg, smart_devs[st->parg.smart].name);
  118. }
  119. void
  120. gets_smart()
  121. {
  122. int i;
  123. for (i = 0; i < smart_cur; i++) {
  124. /* populate ata command header */
  125. memcpy(&smart_devs[i].cmd, (void *) &smart_cmd, sizeof(struct hd_drive_cmd_hdr));
  126. if (ioctl(smart_devs[i].fd, HDIO_DRIVE_CMD, &smart_devs[i].cmd)) {
  127. warning("smart: ioctl for drive '%.200s' failed: %.200s",
  128. &smart_devs[i].name, strerror(errno));
  129. smart_devs[i].failed = 1;
  130. }
  131. /* Linux does not allow checking the smart return code using the
  132. * HDIO_DRIVE_CMD */
  133. /* Some drives do not calculate the smart checksum correctly;
  134. * additional code that identifies these drives would increase our
  135. * footprint and the amount of datajuggling we need to do; we would
  136. * rather ignore the checksums.
  137. */
  138. smart_devs[i].failed = 0;
  139. }
  140. return;
  141. }
  142. int
  143. get_smart(char *symon_buf, int maxlen, struct stream *st)
  144. {
  145. struct smart_report sr;
  146. if ((st->parg.smart < smart_cur) &&
  147. (!smart_devs[st->parg.smart].failed))
  148. {
  149. smart_parse(&smart_devs[st->parg.smart].data, &sr);
  150. return snpack(symon_buf, maxlen, st->arg, MT_SMART,
  151. sr.read_error_rate,
  152. sr.reallocated_sectors,
  153. sr.spin_retries,
  154. sr.air_flow_temp,
  155. sr.temperature,
  156. sr.reallocations,
  157. sr.current_pending,
  158. sr.uncorrectables,
  159. sr.soft_read_error_rate,
  160. sr.g_sense_error_rate,
  161. sr.temperature2,
  162. sr.free_fall_protection);
  163. }
  164. return 0;
  165. }