sm_io.c 6.2 KB

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