sm_smart.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 "diskbyname.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. int i;
  81. char drivename[MAX_PATH_LEN];
  82. if (sizeof(struct smart_values) != DISK_BLOCK_LEN) {
  83. fatal("smart: internal error: smart values structure is broken");
  84. }
  85. if (st->arg == NULL)
  86. fatal("smart: need a <disk device|name> argument");
  87. if (diskbyname(st->arg, drivename, sizeof(drivename)) == 0)
  88. fatal("smart: '%.200s' is not a disk device", 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, sizeof(drivename)) == 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. strlcpy(smart_devs[smart_cur].name, drivename, sizeof(smart_devs[0].name));
  105. /* store filedescriptor to device */
  106. if ((smart_devs[smart_cur].fd = open(drivename, O_RDONLY | O_NONBLOCK)) < 0)
  107. fatal("smart: could not open '%.200s' for read: %.200s", drivename, strerror(errno));
  108. /* store smart dev entry in stream to facilitate quick get */
  109. st->parg.smart = smart_cur;
  110. smart_cur++;
  111. info("started module smart(%.200s = %.200s)", st->arg, smart_devs[st->parg.smart].name);
  112. }
  113. void
  114. gets_smart()
  115. {
  116. int i;
  117. for (i = 0; i < smart_cur; i++) {
  118. /* populate ata command header */
  119. memcpy(&smart_devs[i].cmd, (void *) &smart_cmd, sizeof(struct hd_drive_cmd_hdr));
  120. if (ioctl(smart_devs[i].fd, HDIO_DRIVE_CMD, &smart_devs[i].cmd)) {
  121. warning("smart: ioctl for drive '%s' failed: %d",
  122. &smart_devs[i].name, errno);
  123. smart_devs[i].failed = 1;
  124. }
  125. /* Linux does not allow checking the smart return code using the
  126. * HDIO_DRIVE_CMD */
  127. /* Some drives do not calculate the smart checksum correctly;
  128. * additional code that identifies these drives would increase our
  129. * footprint and the amount of datajuggling we need to do; we would
  130. * rather ignore the checksums.
  131. */
  132. smart_devs[i].failed = 0;
  133. }
  134. return;
  135. }
  136. int
  137. get_smart(char *symon_buf, int maxlen, struct stream *st)
  138. {
  139. struct smart_report sr;
  140. if ((st->parg.smart < smart_cur) &&
  141. (!smart_devs[st->parg.smart].failed))
  142. {
  143. smart_parse(&smart_devs[st->parg.smart].data, &sr);
  144. return snpack(symon_buf, maxlen, st->arg, MT_SMART,
  145. sr.read_error_rate,
  146. sr.reallocated_sectors,
  147. sr.spin_retries,
  148. sr.air_flow_temp,
  149. sr.temperature,
  150. sr.reallocations,
  151. sr.current_pending,
  152. sr.uncorrectables,
  153. sr.soft_read_error_rate,
  154. sr.g_sense_error_rate,
  155. sr.temperature2,
  156. sr.free_fall_protection);
  157. }
  158. return 0;
  159. }