sm_if.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (c) 2001-2016 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 <inttypes.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. snprintf(st->parg.ifname, sizeof(st->parg.ifname), "%s:", st->arg);
  82. info("started module if(%.200s)", st->arg);
  83. }
  84. void
  85. gets_if()
  86. {
  87. int fd;
  88. if ((fd = open("/proc/net/dev", O_RDONLY)) < 0) {
  89. warning("cannot access /proc/net/dev: %.200s", strerror(errno));
  90. return;
  91. }
  92. bzero(if_buf, if_maxsize);
  93. if_size = read(fd, if_buf, if_maxsize);
  94. close(fd);
  95. if (if_size == if_maxsize) {
  96. /* buffer is too small to hold all interface data */
  97. if_maxsize += SYMON_MAX_OBJSIZE;
  98. if (if_maxsize > SYMON_MAX_OBJSIZE * SYMON_MAX_DOBJECTS) {
  99. fatal("%s:%d: dynamic object limit (%d) exceeded for if data",
  100. __FILE__, __LINE__, SYMON_MAX_OBJSIZE * SYMON_MAX_DOBJECTS);
  101. }
  102. if_buf = xrealloc(if_buf, if_maxsize);
  103. gets_if();
  104. return;
  105. }
  106. if (if_size == -1) {
  107. warning("could not read if statistics from /proc/net/dev: %.200s", strerror(errno));
  108. }
  109. }
  110. int
  111. get_if(char *symon_buf, int maxlen, struct stream *st)
  112. {
  113. char *line;
  114. struct if_device_stats stats;
  115. if (if_size <= 0) {
  116. return 0;
  117. }
  118. if ((line = strstr(if_buf, st->parg.ifname)) == NULL) {
  119. warning("could not find interface %s", st->arg);
  120. return 0;
  121. }
  122. line += strlen(st->parg.ifname);
  123. bzero(&stats, sizeof(struct if_device_stats));
  124. /* Inter-| Receive | Transmit
  125. * face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
  126. */
  127. if (16 > sscanf(line, "%" SCNu64 " %" SCNu64 " %" SCNu64 " %" SCNu64 " %" SCNu64 " %"
  128. SCNu64 " %" SCNu64 " %" SCNu64 " %"
  129. SCNu64 " %" SCNu64 " %" SCNu64 " %" SCNu64 " %" SCNu64 " %"
  130. SCNu64 " %" SCNu64 " %" SCNu64 "\n",
  131. &stats.rx_bytes, &stats.rx_packets, &stats.rx_errors, &stats.rx_dropped, &stats.rx_fifo_errors,
  132. &stats.rx_frame_errors, &stats.rx_compressed, &stats.multicast,
  133. &stats.tx_bytes, &stats.tx_packets, &stats.tx_errors, &stats.tx_dropped, &stats.tx_fifo_errors,
  134. &stats.collisions, &stats.tx_carrier_errors, &stats.tx_compressed)) {
  135. warning("could not parse interface statistics for %.200s", st->arg);
  136. return 0;
  137. }
  138. stats.errors_in = (stats.rx_errors + stats.rx_fifo_errors + stats.rx_frame_errors);
  139. stats.errors_out = (stats.tx_errors + stats.tx_fifo_errors + stats.tx_carrier_errors);
  140. stats.drops = (stats.rx_dropped + stats.tx_dropped);
  141. return snpack(symon_buf, maxlen, st->arg, MT_IF2,
  142. (u_int64_t) stats.rx_packets,
  143. (u_int64_t) stats.tx_packets,
  144. (u_int64_t) stats.rx_bytes,
  145. (u_int64_t) stats.tx_bytes,
  146. (u_int64_t) stats.multicast,
  147. (u_int64_t) 0,
  148. (u_int64_t) stats.errors_in,
  149. (u_int64_t) stats.errors_out,
  150. (u_int64_t) stats.collisions,
  151. (u_int64_t) stats.drops);
  152. }