sm_io.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (c) 2001-2006 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. /*
  31. * Get current disk statistics from kernel and return them in symon_buf as
  32. *
  33. * total_rxfer, total_wxfer, total_seeks, total_rbytes, total_wbytes
  34. *
  35. */
  36. #include <sys/param.h>
  37. #include <sys/types.h>
  38. #include <sys/stat.h>
  39. #include <errno.h>
  40. #include <fcntl.h>
  41. #include <string.h>
  42. #include <unistd.h>
  43. #include "conf.h"
  44. #include "xmalloc.h"
  45. #include "error.h"
  46. #include "symon.h"
  47. /* Globals for this module start with io_ */
  48. static void *io_buf = NULL;
  49. static int io_size = 0;
  50. static int io_maxsize = 0;
  51. struct io_device_stats
  52. {
  53. u_int64_t read_issued;
  54. u_int64_t read_merged;
  55. u_int64_t read_sectors;
  56. u_int64_t read_milliseconds;
  57. u_int64_t write_issued;
  58. u_int64_t write_merged;
  59. u_int64_t write_sectors;
  60. u_int64_t write_milliseconds;
  61. u_int64_t progress_ios;
  62. u_int64_t progress_milliseconds;
  63. u_int64_t progress_weight;
  64. };
  65. #ifdef HAS_PROC_DISKSTATS
  66. char *io_filename = "/proc/diskstats";
  67. #else
  68. #ifdef HAS_PROC_PARTITIONS
  69. char *io_filename = "/proc/partitions";
  70. #endif
  71. #endif
  72. #if defined(HAS_PROC_DISKSTATS) || defined(HAS_PROC_PARTITIONS)
  73. void
  74. init_io(struct stream *st)
  75. {
  76. if (io_buf == NULL) {
  77. io_maxsize = SYMON_MAX_OBJSIZE;
  78. io_buf = xmalloc(io_maxsize);
  79. }
  80. info("started module io(%.200s)", st->arg);
  81. }
  82. void
  83. gets_io()
  84. {
  85. int fd;
  86. if ((fd = open(io_filename, O_RDONLY)) < 0) {
  87. warning("cannot access %.200s: %.200s", io_filename, strerror(errno));
  88. return;
  89. }
  90. bzero(io_buf, io_maxsize);
  91. io_size = read(fd, io_buf, io_maxsize);
  92. close(fd);
  93. if (io_size == io_maxsize) {
  94. /* buffer is too small to hold all interface data */
  95. io_maxsize += SYMON_MAX_OBJSIZE;
  96. if (io_maxsize > SYMON_MAX_OBJSIZE * SYMON_MAX_DOBJECTS) {
  97. fatal("%s:%d: dynamic object limit (%d) exceeded for io data",
  98. __FILE__, __LINE__, SYMON_MAX_OBJSIZE * SYMON_MAX_DOBJECTS);
  99. }
  100. io_buf = xrealloc(io_buf, io_maxsize);
  101. gets_io();
  102. return;
  103. }
  104. if (io_size == -1) {
  105. warning("could not read io statistics from %.200s: %.200s", io_filename, strerror(errno));
  106. }
  107. }
  108. int
  109. get_io(char *symon_buf, int maxlen, struct stream *st)
  110. {
  111. char *line;
  112. struct io_device_stats stats;
  113. if (io_size <= 0) {
  114. return 0;
  115. }
  116. if ((line = strstr(io_buf, st->arg)) == NULL) {
  117. warning("could not find disk %s", st->arg);
  118. return 0;
  119. }
  120. line += strlen(st->arg);
  121. bzero(&stats, sizeof(struct io_device_stats));
  122. if (11 > sscanf(line, " %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
  123. &stats.read_issued, &stats.read_merged,
  124. &stats.read_sectors, &stats.read_milliseconds,
  125. &stats.write_issued, &stats.write_merged,
  126. &stats.write_sectors, &stats.write_milliseconds,
  127. &stats.progress_ios, &stats.progress_milliseconds,
  128. &stats.progress_weight)) {
  129. #ifdef HAS_PROC_DISKSTATS
  130. if (4 > sscanf(line, " %llu %llu %llu %llu\n",
  131. &stats.read_issued, &stats.read_sectors,
  132. &stats.write_issued, &stats.write_sectors)) {
  133. warning("could not parse disk statistics for %.200s", st->arg);
  134. return 0;
  135. }
  136. }
  137. #else
  138. warning("could not parse disk statistics for %.200s", st->arg);
  139. return 0;
  140. }
  141. #endif
  142. return snpack(symon_buf, maxlen, st->arg, MT_IO2,
  143. stats.read_issued,
  144. stats.write_issued,
  145. (u_int64_t) 0,
  146. (u_int64_t)(stats.read_sectors * DEV_BSIZE),
  147. (u_int64_t)(stats.write_sectors * DEV_BSIZE));
  148. }
  149. #else
  150. void
  151. init_io(struct stream *st)
  152. {
  153. fatal("io module not available");
  154. }
  155. void
  156. gets_io()
  157. {
  158. fatal("io module not available");
  159. }
  160. int
  161. get_io(char *symon_buf, int maxlen, struct stream *st)
  162. {
  163. fatal("io module not available");
  164. /* NOT REACHED */
  165. return 0;
  166. }
  167. #endif