sm_io.c 5.5 KB

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