sm_if.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* $Id: sm_if.c,v 1.9 2007/12/11 18:31:37 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2001-2007 Willem Dijkstra
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * - Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following
  14. * disclaimer in the documentation and/or other materials provided
  15. * with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  27. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. /*
  32. * Get current interface statistics from kernel and return them in symon_buf as
  33. *
  34. * ipackets : opackets : ibytes : obytes : imcasts : omcasts : ierrors :
  35. * oerrors : colls : drops
  36. */
  37. #include <sys/types.h>
  38. #include <sys/stat.h>
  39. #include <errno.h>
  40. #include <fcntl.h>
  41. #include <string.h>
  42. #include <unistd.h>
  43. #include "conf.h"
  44. #include "xmalloc.h"
  45. #include "error.h"
  46. #include "symon.h"
  47. /* Globals for this module start with if_ */
  48. static void *if_buf = NULL;
  49. static int if_size = 0;
  50. static int if_maxsize = 0;
  51. struct if_device_stats
  52. {
  53. u_int64_t rx_packets; /* total packets received */
  54. u_int64_t tx_packets; /* total packets transmitted */
  55. u_int64_t rx_bytes; /* total bytes received */
  56. u_int64_t tx_bytes; /* total bytes transmitted */
  57. u_int64_t rx_errors; /* bad packets received */
  58. u_int64_t tx_errors; /* packet transmit problems */
  59. u_int64_t rx_dropped; /* no space in linux buffers */
  60. u_int64_t tx_dropped; /* no space available in linux */
  61. u_int64_t multicast; /* multicast packets received */
  62. u_int64_t collisions;
  63. u_int64_t rx_frame_errors; /* recv'd frame alignment error */
  64. u_int64_t rx_fifo_errors; /* recv'r fifo overrun */
  65. u_int64_t tx_carrier_errors;
  66. u_int64_t tx_fifo_errors;
  67. u_int64_t rx_compressed;
  68. u_int64_t tx_compressed;
  69. /* aggregates */
  70. u_int64_t errors_in;
  71. u_int64_t errors_out;
  72. u_int64_t drops;
  73. };
  74. void
  75. init_if(struct stream *st)
  76. {
  77. if (if_buf == NULL) {
  78. if_maxsize = SYMON_MAX_OBJSIZE;
  79. if_buf = xmalloc(if_maxsize);
  80. }
  81. info("started module if(%.200s)", st->arg);
  82. }
  83. void
  84. gets_if()
  85. {
  86. int fd;
  87. if ((fd = open("/proc/net/dev", O_RDONLY)) < 0) {
  88. warning("cannot access /proc/net/dev: %.200s", strerror(errno));
  89. return;
  90. }
  91. bzero(if_buf, if_maxsize);
  92. if_size = read(fd, if_buf, if_maxsize);
  93. close(fd);
  94. if (if_size == if_maxsize) {
  95. /* buffer is too small to hold all interface data */
  96. if_maxsize += SYMON_MAX_OBJSIZE;
  97. if (if_maxsize > SYMON_MAX_OBJSIZE * SYMON_MAX_DOBJECTS) {
  98. fatal("%s:%d: dynamic object limit (%d) exceeded for if data",
  99. __FILE__, __LINE__, SYMON_MAX_OBJSIZE * SYMON_MAX_DOBJECTS);
  100. }
  101. if_buf = xrealloc(if_buf, if_maxsize);
  102. gets_if();
  103. return;
  104. }
  105. if (if_size == -1) {
  106. warning("could not read if statistics from /proc/net/dev: %.200s", strerror(errno));
  107. }
  108. }
  109. int
  110. get_if(char *symon_buf, int maxlen, struct stream *st)
  111. {
  112. char *line;
  113. struct if_device_stats stats;
  114. if (if_size <= 0) {
  115. return 0;
  116. }
  117. if ((line = strstr(if_buf, st->arg)) == NULL) {
  118. warning("could not find interface %s", st->arg);
  119. return 0;
  120. }
  121. line += strlen(st->arg);
  122. bzero(&stats, sizeof(struct if_device_stats));
  123. /* Inter-| Receive | Transmit
  124. * face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
  125. */
  126. if (16 > sscanf(line, ":%Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu\n",
  127. &stats.rx_bytes, &stats.rx_packets, &stats.rx_errors, &stats.rx_dropped, &stats.rx_fifo_errors,
  128. &stats.rx_frame_errors, &stats.rx_compressed, &stats.multicast,
  129. &stats.tx_bytes, &stats.tx_packets, &stats.tx_errors, &stats.tx_dropped, &stats.tx_fifo_errors,
  130. &stats.collisions, &stats.tx_carrier_errors, &stats.tx_compressed)) {
  131. warning("could not parse interface statistics for %.200s", st->arg);
  132. return 0;
  133. }
  134. stats.errors_in = (stats.rx_errors + stats.rx_fifo_errors + stats.rx_frame_errors);
  135. stats.errors_out = (stats.tx_errors + stats.tx_fifo_errors + stats.tx_carrier_errors);
  136. stats.drops = (stats.rx_dropped + stats.tx_dropped);
  137. return snpack(symon_buf, maxlen, st->arg, MT_IF2,
  138. (u_int64_t) stats.rx_packets,
  139. (u_int64_t) stats.tx_packets,
  140. (u_int64_t) stats.rx_bytes,
  141. (u_int64_t) stats.tx_bytes,
  142. (u_int64_t) stats.multicast,
  143. (u_int64_t) 0,
  144. (u_int64_t) stats.errors_in,
  145. (u_int64_t) stats.errors_out,
  146. (u_int64_t) stats.collisions,
  147. (u_int64_t) stats.drops);
  148. }