sm_smart.c 5.2 KB

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