sm_if.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (c) 2001-2007 Willem Dijkstra
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * - Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following
  13. * disclaimer in the documentation and/or other materials provided
  14. * with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  19. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  20. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  22. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  26. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. */
  30. /*
  31. * Get current interface statistics from kernel and return them in symon_buf as
  32. *
  33. * ipackets : opackets : ibytes : obytes : imcasts : omcasts : ierrors :
  34. * oerrors : colls : drops
  35. */
  36. #include <sys/types.h>
  37. #include <sys/ioctl.h>
  38. #include <sys/socket.h>
  39. #include <sys/sockio.h>
  40. #include <net/if.h>
  41. #include <net/if_dl.h>
  42. #include <net/if_types.h>
  43. #include <netinet/in.h>
  44. #include <netinet/in_var.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. void
  53. init_if(struct stream *st)
  54. {
  55. if (if_s == -1) {
  56. if ((if_s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  57. fatal("%s:%d: socket failed, %.200",
  58. __FILE__, __LINE__, strerror(errno));
  59. }
  60. }
  61. strncpy(st->parg.ifr.ifr_name, st->arg, IFNAMSIZ - 1);
  62. st->parg.ifr.ifr_name[IFNAMSIZ - 1] = '\0';
  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. struct if_data ifdata;
  74. st->parg.ifr.ifr_data = (caddr_t) &ifdata;
  75. if (ioctl(if_s, SIOCGIFDATA, &st->parg.ifr)) {
  76. warning("if(%.200s) failed (ioctl error)", st->arg);
  77. return 0;
  78. }
  79. return snpack(symon_buf, maxlen, st->arg, MT_IF2,
  80. (u_int64_t) ifdata.ifi_ipackets,
  81. (u_int64_t) ifdata.ifi_opackets,
  82. (u_int64_t) ifdata.ifi_ibytes,
  83. (u_int64_t) ifdata.ifi_obytes,
  84. (u_int64_t) ifdata.ifi_imcasts,
  85. (u_int64_t) ifdata.ifi_omcasts,
  86. (u_int64_t) ifdata.ifi_ierrors,
  87. (u_int64_t) ifdata.ifi_oerrors,
  88. (u_int64_t) ifdata.ifi_collisions,
  89. (u_int64_t) ifdata.ifi_iqdrops);
  90. }