sm_if.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* $Id: sm_if.c,v 1.6 2005/10/19 20:06:05 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2001-2005 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. unsigned long rx_packets; /* total packets received */
  54. unsigned long tx_packets; /* total packets transmitted */
  55. unsigned long rx_bytes; /* total bytes received */
  56. unsigned long tx_bytes; /* total bytes transmitted */
  57. unsigned long rx_errors; /* bad packets received */
  58. unsigned long tx_errors; /* packet transmit problems */
  59. unsigned long rx_dropped; /* no space in linux buffers */
  60. unsigned long tx_dropped; /* no space available in linux */
  61. unsigned long multicast; /* multicast packets received */
  62. unsigned long collisions;
  63. unsigned long rx_frame_errors; /* recv'd frame alignment error */
  64. unsigned long rx_fifo_errors; /* recv'r fifo overrun */
  65. unsigned long tx_carrier_errors;
  66. unsigned long tx_fifo_errors;
  67. unsigned long rx_compressed;
  68. unsigned long tx_compressed;
  69. };
  70. void
  71. init_if(struct stream *st)
  72. {
  73. if (if_buf == NULL) {
  74. if_maxsize = SYMON_MAX_OBJSIZE;
  75. if_buf = xmalloc(if_maxsize);
  76. }
  77. info("started module if(%.200s)", st->arg);
  78. }
  79. void
  80. gets_if()
  81. {
  82. int fd;
  83. if ((fd = open("/proc/net/dev", O_RDONLY)) < 0) {
  84. warning("cannot access /proc/net/dev: %.200s", strerror(errno));
  85. return;
  86. }
  87. bzero(if_buf, if_maxsize);
  88. if_size = read(fd, if_buf, if_maxsize);
  89. close(fd);
  90. if (if_size == if_maxsize) {
  91. /* buffer is too small to hold all interface data */
  92. if_maxsize += SYMON_MAX_OBJSIZE;
  93. if (if_maxsize > SYMON_MAX_OBJSIZE * SYMON_MAX_DOBJECTS) {
  94. fatal("%s:%d: dynamic object limit (%d) exceeded for if data",
  95. __FILE__, __LINE__, SYMON_MAX_OBJSIZE * SYMON_MAX_DOBJECTS);
  96. }
  97. if_buf = xrealloc(if_buf, if_maxsize);
  98. gets_if();
  99. return;
  100. }
  101. if (if_size == -1) {
  102. warning("could not read if statistics from /proc/net/dev: %.200s", strerror(errno));
  103. }
  104. }
  105. int
  106. get_if(char *symon_buf, int maxlen, struct stream *st)
  107. {
  108. char *line;
  109. struct if_device_stats stats;
  110. if (if_size <= 0) {
  111. return 0;
  112. }
  113. if ((line = strstr(if_buf, st->arg)) == NULL) {
  114. warning("could not find interface %s", st->arg);
  115. return 0;
  116. }
  117. line += strlen(st->arg);
  118. bzero(&stats, sizeof(struct if_device_stats));
  119. /* Inter-| Receive | Transmit
  120. * face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
  121. */
  122. if (16 > sscanf(line, ":%lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n",
  123. &stats.rx_bytes, &stats.rx_packets, &stats.rx_errors, &stats.rx_dropped, &stats.rx_fifo_errors,
  124. &stats.rx_frame_errors, &stats.rx_compressed, &stats.multicast,
  125. &stats.tx_bytes, &stats.tx_packets, &stats.tx_errors, &stats.tx_dropped, &stats.tx_fifo_errors,
  126. &stats.collisions, &stats.tx_carrier_errors, &stats.tx_compressed)) {
  127. warning("could not parse interface statistics for %.200s", st->arg);
  128. return 0;
  129. }
  130. return snpack(symon_buf, maxlen, st->arg, MT_IF,
  131. stats.rx_packets,
  132. stats.tx_packets,
  133. stats.rx_bytes,
  134. stats.tx_bytes,
  135. stats.multicast,
  136. 0,
  137. (stats.rx_errors + stats.rx_fifo_errors + stats.rx_frame_errors),
  138. (stats.tx_errors + stats.tx_fifo_errors + stats.tx_carrier_errors),
  139. stats.collisions,
  140. (stats.rx_dropped + stats.tx_dropped));
  141. }