sm_if.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* $Id: sm_if.c,v 1.4 2005/10/18 19:58:09 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2004 Matthew Gream
  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 <errno.h>
  47. #include <limits.h>
  48. #include <string.h>
  49. #include "error.h"
  50. #include "symon.h"
  51. /* Globals for this module start with if_ */
  52. static int if_s = -1;
  53. void
  54. init_if(struct stream *st)
  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. }
  61. }
  62. strncpy(st->parg.ifr.ifdr_name, st->arg, sizeof(st->parg.ifr.ifdr_name));
  63. info("started module if(%.200s)", st->arg);
  64. }
  65. void
  66. gets_if()
  67. {
  68. /* EMPTY */
  69. }
  70. int
  71. get_if(char *symon_buf, int maxlen, struct stream *st)
  72. {
  73. const struct if_data* ifi;
  74. if (ioctl(if_s, SIOCGIFDATA, (caddr_t)&st->parg.ifr)) {
  75. warning("if(%.200s) failed (ioctl error)", st->arg);
  76. return 0;
  77. }
  78. ifi = &st->parg.ifr.ifdr_data;
  79. return snpack(symon_buf, maxlen, st->arg, MT_IF,
  80. (u_int32_t) ifi->ifi_ipackets,
  81. (u_int32_t) ifi->ifi_opackets,
  82. (u_int32_t) ifi->ifi_ibytes,
  83. (u_int32_t) ifi->ifi_obytes,
  84. (u_int32_t) ifi->ifi_imcasts,
  85. (u_int32_t) ifi->ifi_omcasts,
  86. (u_int32_t) ifi->ifi_ierrors,
  87. (u_int32_t) ifi->ifi_oerrors,
  88. (u_int32_t) ifi->ifi_collisions,
  89. (u_int32_t) ifi->ifi_iqdrops);
  90. }