sm_io.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 "diskname.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. struct disknamectx c;
  79. size_t lead = sizeof("/dev/") - 1;
  80. if (io_buf == NULL) {
  81. io_maxsize = SYMON_MAX_OBJSIZE;
  82. io_buf = xmalloc(io_maxsize);
  83. }
  84. if (st->arg == NULL)
  85. fatal("io: need a <device>|<devicename> argument");
  86. /* Retrieve io stats to search for devicename */
  87. gets_io();
  88. initdisknamectx(&c, st->arg, st->parg.io, sizeof(st->parg.io));
  89. while (nextdiskname(&c) != NULL) {
  90. /* devices are named sdX, not /dev/sdX */
  91. if (strncmp(st->parg.io, "/dev/", lead) == 0)
  92. memmove(&st->parg.io[0], &st->parg.io[0] + lead, sizeof(st->parg.io) - lead);
  93. if (strstr(io_buf, st->parg.io)) {
  94. if (strcmp(st->arg, st->parg.io) == 0)
  95. info("started module io(%.200s)", st->parg.io);
  96. else
  97. info("started module io(%.200s = %.200s)", st->arg, st->parg.io);
  98. return;
  99. }
  100. }
  101. warning("io(%.200s): not mounted", st->arg);
  102. }
  103. void
  104. gets_io()
  105. {
  106. int fd;
  107. if ((fd = open(io_filename, O_RDONLY)) < 0) {
  108. warning("cannot access %.200s: %.200s", io_filename, strerror(errno));
  109. return;
  110. }
  111. bzero(io_buf, io_maxsize);
  112. io_size = read(fd, io_buf, io_maxsize);
  113. close(fd);
  114. if (io_size == io_maxsize) {
  115. /* buffer is too small to hold all interface data */
  116. io_maxsize += SYMON_MAX_OBJSIZE;
  117. if (io_maxsize > SYMON_MAX_OBJSIZE * SYMON_MAX_DOBJECTS) {
  118. fatal("%s:%d: dynamic object limit (%d) exceeded for io data",
  119. __FILE__, __LINE__, SYMON_MAX_OBJSIZE * SYMON_MAX_DOBJECTS);
  120. }
  121. io_buf = xrealloc(io_buf, io_maxsize);
  122. gets_io();
  123. return;
  124. }
  125. if (io_size == -1) {
  126. warning("could not read io statistics from %.200s: %.200s", io_filename, strerror(errno));
  127. }
  128. }
  129. int
  130. get_io(char *symon_buf, int maxlen, struct stream *st)
  131. {
  132. char *line;
  133. struct io_device_stats stats;
  134. if (io_size <= 0) {
  135. return 0;
  136. }
  137. if ((line = strstr(io_buf, st->parg.io)) == NULL) {
  138. warning("could not find disk %.200s = %.200s", st->arg, st->parg.io);
  139. return 0;
  140. }
  141. line += strlen(st->parg.io);
  142. bzero(&stats, sizeof(struct io_device_stats));
  143. if (11 > sscanf(line, " %" SCNu64 " %" SCNu64
  144. " %" SCNu64 " %" SCNu64
  145. " %" SCNu64 " %" SCNu64
  146. " %" SCNu64 " %" SCNu64
  147. " %" SCNu64 " %" SCNu64
  148. " %" SCNu64 "\n",
  149. &stats.read_issued, &stats.read_merged,
  150. &stats.read_sectors, &stats.read_milliseconds,
  151. &stats.write_issued, &stats.write_merged,
  152. &stats.write_sectors, &stats.write_milliseconds,
  153. &stats.progress_ios, &stats.progress_milliseconds,
  154. &stats.progress_weight)) {
  155. #ifdef HAS_PROC_DISKSTATS
  156. if (4 > sscanf(line, " %" SCNu64 " %" SCNu64
  157. " %" SCNu64 " %" SCNu64 "\n",
  158. &stats.read_issued, &stats.read_sectors,
  159. &stats.write_issued, &stats.write_sectors)) {
  160. warning("could not parse disk statistics for %.200s", st->arg);
  161. return 0;
  162. }
  163. }
  164. #else
  165. warning("could not parse disk statistics for %.200s", st->arg);
  166. return 0;
  167. }
  168. #endif
  169. return snpack(symon_buf, maxlen, st->arg, MT_IO2,
  170. stats.read_issued,
  171. stats.write_issued,
  172. (u_int64_t) 0,
  173. (u_int64_t)(stats.read_sectors * DEV_BSIZE),
  174. (u_int64_t)(stats.write_sectors * DEV_BSIZE));
  175. }
  176. #else
  177. void
  178. init_io(struct stream *st)
  179. {
  180. fatal("io module not available");
  181. }
  182. void
  183. gets_io()
  184. {
  185. fatal("io module not available");
  186. }
  187. int
  188. get_io(char *symon_buf, int maxlen, struct stream *st)
  189. {
  190. fatal("io module not available");
  191. /* NOT REACHED */
  192. return 0;
  193. }
  194. #endif