sm_if.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) 2005 Fredrik Soderblom
  3. * Copyright (c) 2005-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/ioctl.h>
  39. #include <sys/socket.h>
  40. #include <sys/sockio.h>
  41. #include <sys/sysctl.h>
  42. #include <net/if.h>
  43. #include <net/if_mib.h>
  44. #include <errno.h>
  45. #include <limits.h>
  46. #include <string.h>
  47. #include "error.h"
  48. #include "symon.h"
  49. #include "xmalloc.h"
  50. /* Globals for this module start with if_ */
  51. static int if_cur = 0;
  52. static int if_max = 0;
  53. struct ifmibdata *if_md = NULL;
  54. void
  55. init_if(struct stream *st)
  56. {
  57. info("started module if(%.200s)", st->arg);
  58. }
  59. int
  60. get_ifcount(void)
  61. {
  62. int name[5] = {CTL_NET, PF_LINK, NETLINK_GENERIC, IFMIB_SYSTEM, IFMIB_IFCOUNT};
  63. int count;
  64. size_t len;
  65. len = sizeof(int);
  66. if (sysctl(name, 5, &count, &len, NULL, 0) != -1) {
  67. return count;
  68. } else {
  69. return -1;
  70. }
  71. }
  72. int
  73. get_ifmib_general(int row, struct ifmibdata *ifmd)
  74. {
  75. int name[6] = {CTL_NET, PF_LINK, NETLINK_GENERIC, IFMIB_IFDATA, row, IFDATA_GENERAL};
  76. size_t len;
  77. len = sizeof(*ifmd);
  78. return sysctl(name, 6, ifmd, &len, (void *)0, 0);
  79. }
  80. void
  81. gets_if()
  82. {
  83. int i;
  84. /* how much memory is needed */
  85. if_cur = get_ifcount();
  86. /* increase buffers if necessary */
  87. if (if_cur > if_max) {
  88. if_max = if_cur;
  89. if (if_max > SYMON_MAX_DOBJECTS) {
  90. fatal("%s:%d: dynamic object limit (%d) exceeded for ifmibdata structures",
  91. __FILE__, __LINE__, SYMON_MAX_DOBJECTS);
  92. }
  93. if_md = xrealloc(if_md, if_max * sizeof(struct ifmibdata));
  94. }
  95. /* read data */
  96. for (i = 1; i <= if_cur; i++) {
  97. get_ifmib_general(i, &if_md[i - 1]);
  98. }
  99. }
  100. int
  101. get_if(char *symon_buf, int maxlen, struct stream *st)
  102. {
  103. int i;
  104. struct if_data ifdata;
  105. for (i = 1; i <= if_cur; i++) {
  106. if (!strcmp(if_md[i - 1].ifmd_name, st->arg)) {
  107. ifdata = if_md[i - 1].ifmd_data;
  108. return snpack(symon_buf, maxlen, st->arg, MT_IF2,
  109. (u_int64_t) ifdata.ifi_ipackets,
  110. (u_int64_t) ifdata.ifi_opackets,
  111. (u_int64_t) ifdata.ifi_ibytes,
  112. (u_int64_t) ifdata.ifi_obytes,
  113. (u_int64_t) ifdata.ifi_imcasts,
  114. (u_int64_t) ifdata.ifi_omcasts,
  115. (u_int64_t) ifdata.ifi_ierrors,
  116. (u_int64_t) ifdata.ifi_oerrors,
  117. (u_int64_t) ifdata.ifi_collisions,
  118. (u_int64_t) ifdata.ifi_iqdrops);
  119. }
  120. }
  121. return 0;
  122. }