rrd based system stats
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

177 lines
4.5 KiB

  1. /***************************************************************************
  2. * Copyright (C) 04/2007 by Olaf Rempel *
  3. * razzor@kopf-tisch.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <libnfnetlink/libnfnetlink.h>
  24. #include <libnetfilter_conntrack/libnetfilter_conntrack.h>
  25. #include "logging.h"
  26. #include "plugins.h"
  27. #include "probe.h"
  28. #define DS_L3PROTO 1
  29. #define DS_L4PROTO 2
  30. #define DS_TCP 3
  31. struct sammler_plugin plugin;
  32. static struct nfct_handle *cth;
  33. struct ct_values {
  34. int total;
  35. int local;
  36. int nated;
  37. int unreplied;
  38. int proto_tcp;
  39. int proto_udp;
  40. int proto_unknown;
  41. int tcp_state[10];
  42. };
  43. static const char *l3proto_ds_def = {
  44. "DS:total:GAUGE:15:0:U "
  45. "DS:local:GAUGE:15:0:U "
  46. "DS:nated:GAUGE:15:0:U "
  47. "DS:unreplied:GAUGE:15:0:U "
  48. };
  49. static const char *l4proto_ds_def = {
  50. "DS:tcp:GAUGE:15:0:U "
  51. "DS:udp:GAUGE:15:0:U "
  52. "DS:unknown:GAUGE:15:0:U "
  53. };
  54. static const char *tcp_ds_def = {
  55. "DS:none:GAUGE:15:0:U "
  56. "DS:syn_sent:GAUGE:15:0:U "
  57. "DS:syn_recv:GAUGE:15:0:U "
  58. "DS:established:GAUGE:15:0:U "
  59. "DS:fin_wait:GAUGE:15:0:U "
  60. "DS:close_wait:GAUGE:15:0:U "
  61. "DS:last_ack:GAUGE:15:0:U "
  62. "DS:time_wait:GAUGE:15:0:U "
  63. "DS:close:GAUGE:15:0:U "
  64. "DS:listen:GAUGE:15:0:U "
  65. };
  66. static const char * get_ds(int ds_id)
  67. {
  68. switch (ds_id) {
  69. case DS_L3PROTO:
  70. return l3proto_ds_def;
  71. case DS_L4PROTO:
  72. return l4proto_ds_def;
  73. case DS_TCP:
  74. return tcp_ds_def;
  75. default:
  76. return NULL;
  77. }
  78. }
  79. static int probe_cb(void *arg, unsigned int flags, int type, void *privdata)
  80. {
  81. struct nfct_conntrack *ct = (struct nfct_conntrack *)arg;
  82. struct ct_values *data = (struct ct_values *)privdata;
  83. data->total++;
  84. if (ct->status & IPS_NAT_MASK)
  85. data->nated++;
  86. else
  87. data->local++;
  88. if (!(ct->status & IPS_SEEN_REPLY))
  89. data->unreplied++;
  90. switch (ct->tuple[NFCT_DIR_ORIGINAL].protonum) {
  91. case IPPROTO_TCP:
  92. data->proto_tcp++;
  93. data->tcp_state[ct->protoinfo.tcp.state]++;
  94. break;
  95. case IPPROTO_UDP:
  96. data->proto_udp++;
  97. break;
  98. default:
  99. data->proto_unknown++;
  100. break;
  101. }
  102. return 0;
  103. }
  104. static int probe(void)
  105. {
  106. struct ct_values data;
  107. memset(&data, 0, sizeof(data));
  108. nfct_register_callback(cth, probe_cb, &data);
  109. if (nfct_dump_conntrack_table(cth, AF_INET) < 0) {
  110. log_print(LOG_WARN, "plugin conntrack: nfct_dump_conntrack_table()");
  111. return -1;
  112. }
  113. probe_submit(&plugin, "conntrack.rrd", DS_L3PROTO, "%d:%d:%d:%d",
  114. data.total, data.local, data.nated, data.unreplied);
  115. probe_submit(&plugin, "conntrack-proto.rrd", DS_L4PROTO, "%d:%d:%d",
  116. data.proto_tcp, data.proto_udp, data.proto_unknown);
  117. probe_submit(&plugin, "conntrack-tcp.rrd", DS_TCP, "%d:%d:%d:%d:%d:%d:%d:%d:%d:%d",
  118. data.tcp_state[0], data.tcp_state[1], data.tcp_state[2], data.tcp_state[3],
  119. data.tcp_state[4], data.tcp_state[5], data.tcp_state[6], data.tcp_state[7],
  120. data.tcp_state[8], data.tcp_state[9]);
  121. return 0;
  122. }
  123. static int init(void)
  124. {
  125. cth = nfct_open(CONNTRACK, 0);
  126. if (cth == NULL) {
  127. log_print(LOG_WARN, "plugin conntrack: nfct_open()");
  128. return -1;
  129. }
  130. return 0;
  131. }
  132. static int fini(void)
  133. {
  134. nfct_close(cth);
  135. return 0;
  136. }
  137. struct sammler_plugin plugin = {
  138. .name = "conntrack",
  139. .interval = 10,
  140. .init = &init,
  141. .fini = &fini,
  142. .probe = &probe,
  143. .get_ds = &get_ds,
  144. };