sm_smart.c 5.7 KB

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