sm_if.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* $Id: sm_if.c,v 1.3 2005/10/18 19:58:06 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2005 Fredrik Soderblom
  4. * Copyright (c) 2005 Willem Dijkstra
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * - Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * - Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following
  15. * disclaimer in the documentation and/or other materials provided
  16. * with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  21. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  22. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  23. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  24. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  26. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  28. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. */
  32. /*
  33. * Get current interface statistics from kernel and return them in symon_buf as
  34. *
  35. * ipackets : opackets : ibytes : obytes : imcasts : omcasts : ierrors :
  36. * oerrors : colls : drops
  37. */
  38. #include <sys/types.h>
  39. #include <sys/ioctl.h>
  40. #include <sys/socket.h>
  41. #include <sys/sockio.h>
  42. #include <sys/sysctl.h>
  43. #include <net/if.h>
  44. #include <net/if_mib.h>
  45. #include <errno.h>
  46. #include <limits.h>
  47. #include <string.h>
  48. #include "error.h"
  49. #include "symon.h"
  50. #include "xmalloc.h"
  51. /* Globals for this module start with if_ */
  52. static int if_cur = 0;
  53. static int if_max = 0;
  54. struct ifmibdata *if_md = NULL;
  55. void
  56. init_if(struct stream *st)
  57. {
  58. info("started module if(%.200s)", st->arg);
  59. }
  60. int
  61. get_ifcount(void)
  62. {
  63. int name[5] = {CTL_NET, PF_LINK, NETLINK_GENERIC, IFMIB_SYSTEM, IFMIB_IFCOUNT};
  64. int count;
  65. size_t len;
  66. len = sizeof(int);
  67. if (sysctl(name, 5, &count, &len, NULL, 0) != -1) {
  68. return count;
  69. } else {
  70. return -1;
  71. }
  72. }
  73. int
  74. get_ifmib_general(int row, struct ifmibdata *ifmd)
  75. {
  76. int name[6] = {CTL_NET, PF_LINK, NETLINK_GENERIC, IFMIB_IFDATA, row, IFDATA_GENERAL};
  77. size_t len;
  78. len = sizeof(*ifmd);
  79. return sysctl(name, 6, ifmd, &len, (void *)0, 0);
  80. }
  81. void
  82. gets_if()
  83. {
  84. int i;
  85. /* how much memory is needed */
  86. if_cur = get_ifcount();
  87. /* increase buffers if necessary */
  88. if (if_cur > if_max) {
  89. if_max = if_cur;
  90. if (if_max > SYMON_MAX_DOBJECTS) {
  91. fatal("%s:%d: dynamic object limit (%d) exceeded for ifmibdata structures",
  92. __FILE__, __LINE__, SYMON_MAX_DOBJECTS);
  93. }
  94. if_md = xrealloc(if_md, if_max * sizeof(struct ifmibdata));
  95. }
  96. /* read data */
  97. for (i = 1; i <= if_cur; i++) {
  98. get_ifmib_general(i, &if_md[i - 1]);
  99. }
  100. }
  101. int
  102. get_if(char *symon_buf, int maxlen, struct stream *st)
  103. {
  104. int i;
  105. struct if_data ifdata;
  106. for (i = 1; i <= if_cur; i++) {
  107. if (!strcmp(if_md[i - 1].ifmd_name, st->arg)) {
  108. ifdata = if_md[i - 1].ifmd_data;
  109. return snpack(symon_buf, maxlen, st->arg, MT_IF,
  110. ifdata.ifi_ipackets,
  111. ifdata.ifi_opackets,
  112. ifdata.ifi_ibytes,
  113. ifdata.ifi_obytes,
  114. ifdata.ifi_imcasts,
  115. ifdata.ifi_omcasts,
  116. ifdata.ifi_ierrors,
  117. ifdata.ifi_oerrors,
  118. ifdata.ifi_collisions,
  119. ifdata.ifi_iqdrops);
  120. }
  121. }
  122. return 0;
  123. }