sm_if.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* $Id: sm_if.c,v 1.15 2005/10/18 19:58:11 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/ioctl.h>
  39. #include <sys/socket.h>
  40. #include <sys/sockio.h>
  41. #include <net/if.h>
  42. #include <net/if_dl.h>
  43. #include <net/if_types.h>
  44. #include <netinet/in.h>
  45. #include <netinet/in_var.h>
  46. #include <netipx/ipx.h>
  47. #include <netipx/ipx_if.h>
  48. #include <errno.h>
  49. #include <limits.h>
  50. #include <string.h>
  51. #include "error.h"
  52. #include "symon.h"
  53. /* Globals for this module start with if_ */
  54. static int if_s = -1;
  55. void
  56. init_if(struct stream *st)
  57. {
  58. if (if_s == -1) {
  59. if ((if_s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  60. fatal("%s:%d: socket failed, %.200",
  61. __FILE__, __LINE__, strerror(errno));
  62. }
  63. }
  64. strncpy(st->parg.ifr.ifr_name, st->arg, IFNAMSIZ - 1);
  65. st->parg.ifr.ifr_name[IFNAMSIZ - 1] = '\0';
  66. info("started module if(%.200s)", st->arg);
  67. }
  68. void
  69. gets_if()
  70. {
  71. }
  72. int
  73. get_if(char *symon_buf, int maxlen, struct stream *st)
  74. {
  75. struct if_data ifdata;
  76. st->parg.ifr.ifr_data = (caddr_t) &ifdata;
  77. if (ioctl(if_s, SIOCGIFDATA, &st->parg.ifr)) {
  78. warning("if(%.200s) failed (ioctl error)", st->arg);
  79. return 0;
  80. }
  81. return snpack(symon_buf, maxlen, st->arg, MT_IF,
  82. ifdata.ifi_ipackets,
  83. ifdata.ifi_opackets,
  84. ifdata.ifi_ibytes,
  85. ifdata.ifi_obytes,
  86. ifdata.ifi_imcasts,
  87. ifdata.ifi_omcasts,
  88. ifdata.ifi_ierrors,
  89. ifdata.ifi_oerrors,
  90. ifdata.ifi_collisions,
  91. ifdata.ifi_iqdrops);
  92. }