linux 2.6 conntrack 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.

140 lines
3.6KB

  1. /***************************************************************************
  2. * Copyright (C) 07/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; version 2 of the License *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program; if not, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************/
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <string.h>
  23. #include <getopt.h>
  24. #include <signal.h>
  25. #include <errno.h>
  26. #include "configfile.h"
  27. #include "conntrack.h"
  28. #include "database.h"
  29. #include "logging.h"
  30. #define DEFAULT_CONFIG "ctstats.conf"
  31. #define DEFAULT_LOGFILE "ctstats.log"
  32. #define DEFAULT_INTERVALL 300
  33. static struct option opts[] = {
  34. {"config", 1, 0, 'c'},
  35. {"debug", 0, 0, 'd'},
  36. {"help", 0, 0, 'h'},
  37. {0, 0, 0, 0}
  38. };
  39. static int sig_received = 0;
  40. static void my_sighandler(int s)
  41. {
  42. sig_received = 1;
  43. }
  44. int main(int argc, char *argv[])
  45. {
  46. char *config = DEFAULT_CONFIG;
  47. int code, arg = 0, debug = 0;
  48. do {
  49. code = getopt_long(argc, argv, "c:dh", opts, &arg);
  50. switch (code) {
  51. case 'c': /* config */
  52. config = optarg;
  53. break;
  54. case 'd': /* debug */
  55. debug = 1;
  56. break;
  57. case 'h': /* help */
  58. printf("Usage: ctstat [options]\n"
  59. "Options: \n"
  60. " --config -c configfile use this configfile\n"
  61. " --debug -d do not fork and log to stderr\n"
  62. " --help -h this help\n"
  63. "\n");
  64. exit(0);
  65. break;
  66. case '?': /* error */
  67. exit(-1);
  68. break;
  69. default: /* unknown / all options parsed */
  70. break;
  71. }
  72. } while (code != -1);
  73. /* parse config file */
  74. if (config_parse(config))
  75. exit(1);
  76. /* init netlink & hashtables */
  77. if (conntrack_init())
  78. exit(1);
  79. /* init database connection */
  80. if (database_init()) {
  81. conntrack_close();
  82. exit(1);
  83. }
  84. /* check logfile */
  85. const char *logfile = config_get_string("global", "logfile", DEFAULT_LOGFILE);
  86. if (logfile != NULL && debug == 0) {
  87. /* start logging */
  88. if (log_init(logfile))
  89. exit(1);
  90. /* zum daemon mutieren */
  91. daemon(-1, 0);
  92. }
  93. log_print(LOG_EVERYTIME, "ctstats started (pid: %d)", getpid());
  94. signal(SIGINT, my_sighandler);
  95. signal(SIGTERM, my_sighandler);
  96. /* start event listener */
  97. conntrack_start_event_thread();
  98. int intervall = config_get_int("global", "intervall", DEFAULT_INTERVALL);
  99. struct timeval tv;
  100. tv.tv_sec = intervall;
  101. tv.tv_usec = 0;
  102. while (sig_received == 0) {
  103. select(0, NULL, NULL, NULL, &tv);
  104. errno = 0;
  105. tv.tv_sec = intervall;
  106. tv.tv_usec = 0;
  107. database_analyse();
  108. }
  109. database_close();
  110. conntrack_close();
  111. log_close();
  112. return 0;
  113. }