sm_if.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* $Id: sm_if.c,v 1.9 2003/12/20 16:30:44 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2001-2003 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. */
  38. #include <sys/types.h>
  39. #include <sys/ioctl.h>
  40. #include <sys/socket.h>
  41. #include <sys/sockio.h>
  42. #include <net/if.h>
  43. #include <net/if_dl.h>
  44. #include <net/if_types.h>
  45. #include <netinet/in.h>
  46. #include <netinet/in_var.h>
  47. #include <netns/ns.h>
  48. #include <netns/ns_if.h>
  49. #include <netipx/ipx.h>
  50. #include <netipx/ipx_if.h>
  51. #include <netiso/iso.h>
  52. #include <netiso/iso_var.h>
  53. #include <errno.h>
  54. #include <limits.h>
  55. #include <string.h>
  56. #include "error.h"
  57. #include "symon.h"
  58. /* Globals for this module start with if_ */
  59. static int if_s = -1;
  60. /* Prepare if module for first use */
  61. void
  62. init_if(char *s)
  63. {
  64. if (if_s == -1)
  65. if ((if_s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
  66. fatal("%s:%d: socket failed, %.200",
  67. __FILE__, __LINE__, strerror(errno));
  68. info("started module if(%.200s)", s);
  69. }
  70. /* Get interface statistics */
  71. int
  72. get_if(char *symon_buf, int maxlen, char *interface)
  73. {
  74. struct ifreq ifr;
  75. struct if_data ifdata;
  76. strncpy(ifr.ifr_name, interface, IFNAMSIZ - 1);
  77. ifr.ifr_name[IFNAMSIZ - 1] = '\0';
  78. ifr.ifr_data = (caddr_t) & ifdata;
  79. if (ioctl(if_s, SIOCGIFDATA, &ifr)) {
  80. warning("if(%.200s) failed (ioctl error)", interface);
  81. return 0;
  82. }
  83. return snpack(symon_buf, maxlen, interface, MT_IF,
  84. ifdata.ifi_ipackets,
  85. ifdata.ifi_opackets,
  86. ifdata.ifi_ibytes,
  87. ifdata.ifi_obytes,
  88. ifdata.ifi_imcasts,
  89. ifdata.ifi_omcasts,
  90. ifdata.ifi_ierrors,
  91. ifdata.ifi_oerrors,
  92. ifdata.ifi_collisions,
  93. ifdata.ifi_iqdrops);
  94. }