sm_io.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (c) 2001-2012 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 transfer statistics from kernel and return them in
  32. * symon_buf as
  33. *
  34. * total nr of transfers : total seeks : total bytes transferred
  35. */
  36. #include <sys/param.h>
  37. #include <sys/sysctl.h>
  38. #include <sys/disk.h>
  39. #include <limits.h>
  40. #include <string.h>
  41. #include "conf.h"
  42. #include "error.h"
  43. #include "symon.h"
  44. #include "xmalloc.h"
  45. /* Globals for this module start with io_ */
  46. static char *io_dkstr = NULL;
  47. static struct diskstats *io_dkstats = NULL;
  48. static char **io_dknames = NULL;
  49. static char **io_dkuids = NULL;
  50. static int io_dks = 0;
  51. static int io_maxdks = 0;
  52. static size_t io_maxstr = 0;
  53. void
  54. gets_io()
  55. {
  56. int mib[3];
  57. char *p;
  58. int dks;
  59. size_t size;
  60. size_t strsize;
  61. /* how much memory is needed */
  62. mib[0] = CTL_HW;
  63. mib[1] = HW_DISKCOUNT;
  64. size = sizeof(dks);
  65. if (sysctl(mib, 2, &dks, &size, NULL, 0) < 0) {
  66. fatal("%s:%d: sysctl failed: can't get hw.diskcount",
  67. __FILE__, __LINE__);
  68. }
  69. mib[0] = CTL_HW;
  70. mib[1] = HW_DISKNAMES;
  71. strsize = 0;
  72. if (sysctl(mib, 2, NULL, &strsize, NULL, 0) < 0) {
  73. fatal("%s:%d: sysctl failed: can't get hw.disknames",
  74. __FILE__, __LINE__);
  75. }
  76. /* increase buffers if necessary */
  77. if (dks > io_maxdks || strsize > io_maxstr) {
  78. io_maxdks = dks;
  79. io_maxstr = strsize;
  80. if (io_maxdks > SYMON_MAX_DOBJECTS) {
  81. fatal("%s:%d: dynamic object limit (%d) exceeded for diskstat structures",
  82. __FILE__, __LINE__, SYMON_MAX_DOBJECTS);
  83. }
  84. if (io_maxstr > SYMON_MAX_OBJSIZE) {
  85. fatal("%s:%d: string size exceeded (%d)",
  86. __FILE__, __LINE__, SYMON_MAX_OBJSIZE);
  87. }
  88. io_dkstats = xrealloc(io_dkstats, io_maxdks * sizeof(struct diskstats));
  89. io_dknames = xrealloc(io_dknames, io_maxdks * sizeof(char *));
  90. io_dkuids = xrealloc(io_dkuids, io_maxdks * sizeof(char *));
  91. io_dkstr = xrealloc(io_dkstr, io_maxstr + 1);
  92. }
  93. /* read data in anger */
  94. mib[0] = CTL_HW;
  95. mib[1] = HW_DISKNAMES;
  96. if (sysctl(mib, 2, io_dkstr, &io_maxstr, NULL, 0) < 0) {
  97. fatal("%s:%d: io can't get hw.disknames"
  98. __FILE__, __LINE__);
  99. }
  100. io_dkstr[io_maxstr] = '\0';
  101. mib[0] = CTL_HW;
  102. mib[1] = HW_DISKSTATS;
  103. size = io_maxdks * sizeof(struct diskstats);
  104. if (sysctl(mib, 2, io_dkstats, &size, NULL, 0) < 0) {
  105. fatal("%s:%d: io can't get hw.diskstats"
  106. __FILE__, __LINE__);
  107. }
  108. p = io_dkstr;
  109. io_dks = 0;
  110. io_dknames[io_dks] = p;
  111. while ((*p != '\0') && ((p - io_dkstr) < io_maxstr)) {
  112. if ((*p == ',') && (*p+1 != '\0')) {
  113. *p = '\0';
  114. io_dks++; p++;
  115. io_dknames[io_dks] = p;
  116. io_dkuids[io_dks] = NULL;
  117. }
  118. if ((*p == ':') && (*p+1 != '\0')) {
  119. *p = '\0';
  120. io_dkuids[io_dks] = p+1;
  121. }
  122. p++;
  123. }
  124. }
  125. void
  126. init_io(struct stream *st)
  127. {
  128. info("started module io(%.200s)", st->arg);
  129. }
  130. int
  131. get_io(char *symon_buf, int maxlen, struct stream *st)
  132. {
  133. int i;
  134. /* look for disk */
  135. for (i = 0; i <= io_dks; i++) {
  136. if ((strncmp(io_dknames[i], st->arg,
  137. (io_dkstr + io_maxstr - io_dknames[i])) == 0)
  138. || (io_dkuids[i] && (strncmp(io_dkuids[i], st->arg,
  139. (io_dkstr + io_maxstr - io_dkuids[i])) == 0)))
  140. #ifdef HAS_IO2
  141. return snpack(symon_buf, maxlen, st->arg, MT_IO2,
  142. io_dkstats[i].ds_rxfer,
  143. io_dkstats[i].ds_wxfer,
  144. io_dkstats[i].ds_seek,
  145. io_dkstats[i].ds_rbytes,
  146. io_dkstats[i].ds_wbytes);
  147. #else
  148. return snpack(symon_buf, maxlen, st->arg, MT_IO1,
  149. io_dkstats[i].ds_xfer,
  150. io_dkstats[i].ds_seek,
  151. io_dkstats[i].ds_bytes);
  152. #endif
  153. }
  154. return 0;
  155. }