sm_if.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* $Id: sm_if.c,v 1.1 2005/01/14 16:12:55 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2005 Fredrik Soderblom
  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. */
  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. /* Globals for this module start with if_ */
  51. static int if_s = -1;
  52. /* Prepare if module for first use */
  53. void
  54. init_if(char *s)
  55. {
  56. if (if_s == -1)
  57. if ((if_s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
  58. fatal("%s:%d: socket failed, %.200",
  59. __FILE__, __LINE__, strerror(errno));
  60. info("started module if(%.200s)", s);
  61. }
  62. int
  63. get_ifcount(void)
  64. {
  65. int name[5], count;
  66. size_t len;
  67. name[0] = CTL_NET;
  68. name[1] = PF_LINK;
  69. name[2] = NETLINK_GENERIC;
  70. name[3] = IFMIB_SYSTEM;
  71. name[4] = IFMIB_IFCOUNT;
  72. len = sizeof(int);
  73. if (sysctl(name, 5, &count, &len, NULL, 0) != -1)
  74. return(count);
  75. else
  76. return(-1);
  77. }
  78. int
  79. get_ifmib_general(int row, struct ifmibdata *ifmd)
  80. {
  81. int name[6];
  82. size_t len;
  83. name[0] = CTL_NET;
  84. name[1] = PF_LINK;
  85. name[2] = NETLINK_GENERIC;
  86. name[3] = IFMIB_IFDATA;
  87. name[4] = row;
  88. name[5] = IFDATA_GENERAL;
  89. len = sizeof(*ifmd);
  90. return sysctl(name, 6, ifmd, &len, (void *)0, 0);
  91. }
  92. /* Get interface statistics */
  93. void
  94. gets_if()
  95. {
  96. }
  97. int
  98. get_if(char *symon_buf, int maxlen, char *interface)
  99. {
  100. int i;
  101. struct ifmibdata ifmd;
  102. struct if_data ifdata;
  103. int ifcount = get_ifcount();
  104. for (i = 1; i <= ifcount; i++) {
  105. get_ifmib_general(i, &ifmd);
  106. if (!strcmp(ifmd.ifmd_name, interface))
  107. break;
  108. }
  109. ifdata = ifmd.ifmd_data;
  110. return snpack(symon_buf, maxlen, interface, MT_IF,
  111. ifdata.ifi_ipackets,
  112. ifdata.ifi_opackets,
  113. ifdata.ifi_ibytes,
  114. ifdata.ifi_obytes,
  115. ifdata.ifi_imcasts,
  116. ifdata.ifi_omcasts,
  117. ifdata.ifi_ierrors,
  118. ifdata.ifi_oerrors,
  119. ifdata.ifi_collisions,
  120. ifdata.ifi_iqdrops);
  121. }