sm_smart.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #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 <disk device|name> argument");
  86. if ((diskbyname(st->arg, drivename, sizeof(drivename)) == 0))
  87. fatal("smart: '%.200s' is not a disk device", st->arg);
  88. /* look for drive in our global table */
  89. for (i = 0; i < smart_cur; i++) {
  90. if (strncmp(smart_devs[i].name, drivename, sizeof(drivename)) == 0) {
  91. st->parg.smart = i;
  92. return;
  93. }
  94. }
  95. /* this is a new drive; allocate the command and data block */
  96. if (smart_cur > SYMON_MAX_DOBJECTS) {
  97. fatal("%s:%d: dynamic object limit (%d) exceeded for smart data",
  98. __FILE__, __LINE__, SYMON_MAX_DOBJECTS);
  99. }
  100. smart_devs = xrealloc(smart_devs, (smart_cur + 1) * sizeof(struct smart_device));
  101. bzero(&smart_devs[smart_cur], sizeof(struct smart_device));
  102. /* store drivename in new block */
  103. strlcpy(smart_devs[smart_cur].name, drivename, sizeof(smart_devs[0].name));
  104. /* store filedescriptor to device */
  105. if ((smart_devs[smart_cur].fd = open(drivename, O_RDONLY | O_NONBLOCK)) < 0)
  106. fatal("smart: could not open '%.200s' for read: %.200s", drivename, strerror(errno));
  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 = %.200s)", st->arg, smart_devs[st->parg.smart].name);
  111. }
  112. void
  113. gets_smart()
  114. {
  115. int i;
  116. for (i = 0; i < smart_cur; i++) {
  117. /* populate ata command header */
  118. memcpy(&smart_devs[i].cmd, (void *) &smart_cmd, sizeof(struct hd_drive_cmd_hdr));
  119. if (ioctl(smart_devs[i].fd, HDIO_DRIVE_CMD, &smart_devs[i].cmd)) {
  120. warning("smart: ioctl for drive '%s' failed: %d",
  121. &smart_devs[i].name, errno);
  122. smart_devs[i].failed = 1;
  123. }
  124. /* Linux does not allow checking the smart return code using the
  125. * HDIO_DRIVE_CMD */
  126. /* Some drives do not calculate the smart checksum correctly;
  127. * additional code that identifies these drives would increase our
  128. * footprint and the amount of datajuggling we need to do; we would
  129. * rather ignore the checksums.
  130. */
  131. smart_devs[i].failed = 0;
  132. }
  133. return;
  134. }
  135. int
  136. get_smart(char *symon_buf, int maxlen, struct stream *st)
  137. {
  138. struct smart_report sr;
  139. if ((st->parg.smart < smart_cur) &&
  140. (!smart_devs[st->parg.smart].failed))
  141. {
  142. smart_parse(&smart_devs[st->parg.smart].data, &sr);
  143. return snpack(symon_buf, maxlen, st->arg, MT_SMART,
  144. sr.read_error_rate,
  145. sr.reallocated_sectors,
  146. sr.spin_retries,
  147. sr.air_flow_temp,
  148. sr.temperature,
  149. sr.reallocations,
  150. sr.current_pending,
  151. sr.uncorrectables,
  152. sr.soft_read_error_rate,
  153. sr.g_sense_error_rate,
  154. sr.temperature2,
  155. sr.free_fall_protection);
  156. }
  157. return 0;
  158. }