sm_io.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. int len;
  108. char *p;
  109. if ((fd = open(io_filename, O_RDONLY)) < 0) {
  110. warning("cannot access %.200s: %.200s", io_filename, strerror(errno));
  111. return;
  112. }
  113. bzero(io_buf, io_maxsize);
  114. len = 0;
  115. p = io_buf;
  116. io_size = 0;
  117. while ((len = read(fd, p, io_maxsize - io_size)) > 0) {
  118. p += len;
  119. io_size += len;
  120. }
  121. close(fd);
  122. if (io_size == io_maxsize) {
  123. /* buffer is too small to hold all interface data */
  124. io_maxsize += SYMON_MAX_OBJSIZE;
  125. if (io_maxsize > SYMON_MAX_OBJSIZE * SYMON_MAX_DOBJECTS) {
  126. fatal("%s:%d: dynamic object limit (%d) exceeded for io data",
  127. __FILE__, __LINE__, SYMON_MAX_OBJSIZE * SYMON_MAX_DOBJECTS);
  128. }
  129. io_buf = xrealloc(io_buf, io_maxsize);
  130. gets_io();
  131. return;
  132. }
  133. if (io_size == -1) {
  134. warning("could not read io statistics from %.200s: %.200s", io_filename, strerror(errno));
  135. }
  136. }
  137. int
  138. get_io(char *symon_buf, int maxlen, struct stream *st)
  139. {
  140. char *line;
  141. struct io_device_stats stats;
  142. if (io_size <= 0) {
  143. return 0;
  144. }
  145. if ((line = strstr(io_buf, st->parg.io)) == NULL) {
  146. warning("could not find disk %.200s = %.200s", st->arg, st->parg.io);
  147. return 0;
  148. }
  149. line += strlen(st->parg.io);
  150. bzero(&stats, sizeof(struct io_device_stats));
  151. if (11 > sscanf(line, " %" SCNu64 " %" SCNu64
  152. " %" SCNu64 " %" SCNu64
  153. " %" SCNu64 " %" SCNu64
  154. " %" SCNu64 " %" SCNu64
  155. " %" SCNu64 " %" SCNu64
  156. " %" SCNu64 "\n",
  157. &stats.read_issued, &stats.read_merged,
  158. &stats.read_sectors, &stats.read_milliseconds,
  159. &stats.write_issued, &stats.write_merged,
  160. &stats.write_sectors, &stats.write_milliseconds,
  161. &stats.progress_ios, &stats.progress_milliseconds,
  162. &stats.progress_weight)) {
  163. #ifdef HAS_PROC_DISKSTATS
  164. if (4 > sscanf(line, " %" SCNu64 " %" SCNu64
  165. " %" SCNu64 " %" SCNu64 "\n",
  166. &stats.read_issued, &stats.read_sectors,
  167. &stats.write_issued, &stats.write_sectors)) {
  168. warning("could not parse disk statistics for %.200s", st->arg);
  169. return 0;
  170. }
  171. }
  172. #else
  173. warning("could not parse disk statistics for %.200s", st->arg);
  174. return 0;
  175. }
  176. #endif
  177. return snpack(symon_buf, maxlen, st->arg, MT_IO2,
  178. stats.read_issued,
  179. stats.write_issued,
  180. (u_int64_t) 0,
  181. (u_int64_t)(stats.read_sectors * DEV_BSIZE),
  182. (u_int64_t)(stats.write_sectors * DEV_BSIZE));
  183. }
  184. #else
  185. void
  186. init_io(struct stream *st)
  187. {
  188. fatal("io module not available");
  189. }
  190. void
  191. gets_io()
  192. {
  193. fatal("io module not available");
  194. }
  195. int
  196. get_io(char *symon_buf, int maxlen, struct stream *st)
  197. {
  198. fatal("io module not available");
  199. /* NOT REACHED */
  200. return 0;
  201. }
  202. #endif