sm_if.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (c) 2001-2007 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 interface statistics from kernel and return them in symon_buf as
  32. *
  33. * ipackets : opackets : ibytes : obytes : imcasts : omcasts : ierrors :
  34. * oerrors : colls : drops
  35. */
  36. #include <sys/types.h>
  37. #include <sys/stat.h>
  38. #include <errno.h>
  39. #include <fcntl.h>
  40. #include <string.h>
  41. #include <unistd.h>
  42. #include "conf.h"
  43. #include "xmalloc.h"
  44. #include "error.h"
  45. #include "symon.h"
  46. /* Globals for this module start with if_ */
  47. static void *if_buf = NULL;
  48. static int if_size = 0;
  49. static int if_maxsize = 0;
  50. struct if_device_stats
  51. {
  52. u_int64_t rx_packets; /* total packets received */
  53. u_int64_t tx_packets; /* total packets transmitted */
  54. u_int64_t rx_bytes; /* total bytes received */
  55. u_int64_t tx_bytes; /* total bytes transmitted */
  56. u_int64_t rx_errors; /* bad packets received */
  57. u_int64_t tx_errors; /* packet transmit problems */
  58. u_int64_t rx_dropped; /* no space in linux buffers */
  59. u_int64_t tx_dropped; /* no space available in linux */
  60. u_int64_t multicast; /* multicast packets received */
  61. u_int64_t collisions;
  62. u_int64_t rx_frame_errors; /* recv'd frame alignment error */
  63. u_int64_t rx_fifo_errors; /* recv'r fifo overrun */
  64. u_int64_t tx_carrier_errors;
  65. u_int64_t tx_fifo_errors;
  66. u_int64_t rx_compressed;
  67. u_int64_t tx_compressed;
  68. /* aggregates */
  69. u_int64_t errors_in;
  70. u_int64_t errors_out;
  71. u_int64_t drops;
  72. };
  73. void
  74. init_if(struct stream *st)
  75. {
  76. if (if_buf == NULL) {
  77. if_maxsize = SYMON_MAX_OBJSIZE;
  78. if_buf = xmalloc(if_maxsize);
  79. }
  80. info("started module if(%.200s)", st->arg);
  81. }
  82. void
  83. gets_if()
  84. {
  85. int fd;
  86. if ((fd = open("/proc/net/dev", O_RDONLY)) < 0) {
  87. warning("cannot access /proc/net/dev: %.200s", strerror(errno));
  88. return;
  89. }
  90. bzero(if_buf, if_maxsize);
  91. if_size = read(fd, if_buf, if_maxsize);
  92. close(fd);
  93. if (if_size == if_maxsize) {
  94. /* buffer is too small to hold all interface data */
  95. if_maxsize += SYMON_MAX_OBJSIZE;
  96. if (if_maxsize > SYMON_MAX_OBJSIZE * SYMON_MAX_DOBJECTS) {
  97. fatal("%s:%d: dynamic object limit (%d) exceeded for if data",
  98. __FILE__, __LINE__, SYMON_MAX_OBJSIZE * SYMON_MAX_DOBJECTS);
  99. }
  100. if_buf = xrealloc(if_buf, if_maxsize);
  101. gets_if();
  102. return;
  103. }
  104. if (if_size == -1) {
  105. warning("could not read if statistics from /proc/net/dev: %.200s", strerror(errno));
  106. }
  107. }
  108. int
  109. get_if(char *symon_buf, int maxlen, struct stream *st)
  110. {
  111. char *line;
  112. struct if_device_stats stats;
  113. if (if_size <= 0) {
  114. return 0;
  115. }
  116. if ((line = strstr(if_buf, st->arg)) == NULL) {
  117. warning("could not find interface %s", st->arg);
  118. return 0;
  119. }
  120. line += strlen(st->arg);
  121. bzero(&stats, sizeof(struct if_device_stats));
  122. /* Inter-| Receive | Transmit
  123. * face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
  124. */
  125. if (16 > sscanf(line, ":%Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu\n",
  126. &stats.rx_bytes, &stats.rx_packets, &stats.rx_errors, &stats.rx_dropped, &stats.rx_fifo_errors,
  127. &stats.rx_frame_errors, &stats.rx_compressed, &stats.multicast,
  128. &stats.tx_bytes, &stats.tx_packets, &stats.tx_errors, &stats.tx_dropped, &stats.tx_fifo_errors,
  129. &stats.collisions, &stats.tx_carrier_errors, &stats.tx_compressed)) {
  130. warning("could not parse interface statistics for %.200s", st->arg);
  131. return 0;
  132. }
  133. stats.errors_in = (stats.rx_errors + stats.rx_fifo_errors + stats.rx_frame_errors);
  134. stats.errors_out = (stats.tx_errors + stats.tx_fifo_errors + stats.tx_carrier_errors);
  135. stats.drops = (stats.rx_dropped + stats.tx_dropped);
  136. return snpack(symon_buf, maxlen, st->arg, MT_IF2,
  137. (u_int64_t) stats.rx_packets,
  138. (u_int64_t) stats.tx_packets,
  139. (u_int64_t) stats.rx_bytes,
  140. (u_int64_t) stats.tx_bytes,
  141. (u_int64_t) stats.multicast,
  142. (u_int64_t) 0,
  143. (u_int64_t) stats.errors_in,
  144. (u_int64_t) stats.errors_out,
  145. (u_int64_t) stats.collisions,
  146. (u_int64_t) stats.drops);
  147. }