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.
 
 
 

95 lines
3.4 KiB

  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 <sys/socket.h>
  21. #include <netinet/in.h>
  22. #include <arpa/inet.h>
  23. #include <mysql/mysql.h>
  24. #include "configfile.h"
  25. #include "conntrack.h"
  26. #include "database.h"
  27. #include "hashtable.h"
  28. #include "logging.h"
  29. static MYSQL *dbh;
  30. static void purge_hash_cb(const struct hash_entry *entry, void *privdata)
  31. {
  32. int long *now = (long *)privdata;
  33. char query[256];
  34. int len = snprintf(query, sizeof(query),
  35. "INSERT INTO stats SET timestamp='%lu', srcip='%u', proto='%u', dport='%u', flags='%u', srcbytes='%llu', dstbytes='%llu', count='%u'",
  36. *now, ntohl(entry->src_ip), entry->protonum, ntohs(entry->dst_port),
  37. entry->flags, entry->src_bytes, entry->dst_bytes, entry->count);
  38. if (mysql_real_query(dbh, query, len +1) != 0)
  39. log_print(LOG_WARN, "purge_hash_cb: mysql_real_query(): %s", mysql_error(dbh));
  40. }
  41. int database_analyse(void)
  42. {
  43. struct hash_table *hash = conntrack_get_hash();
  44. if (mysql_ping(dbh) != 0) {
  45. log_print(LOG_WARN, "database_analyse: mysql_ping(): %s", mysql_error(dbh));
  46. purge_hash(hash, NULL, NULL);
  47. return 0;
  48. }
  49. long now = time(NULL);
  50. purge_hash(hash, purge_hash_cb, (void *)&now);
  51. return 0;
  52. }
  53. int database_init(void)
  54. {
  55. dbh = mysql_init(NULL);
  56. if (dbh == NULL) {
  57. log_print(LOG_ERROR, "database_init: mysql_init(): %s", mysql_error(dbh));
  58. return -1;
  59. }
  60. const char *hostname = config_get_string("mysql", "hostname", NULL);
  61. const char *username = config_get_string("mysql", "username", NULL);
  62. const char *password = config_get_string("mysql", "password", NULL);
  63. MYSQL *ret = mysql_real_connect(dbh, hostname, username, password, NULL, 0, NULL, 0);
  64. if (ret != dbh) {
  65. log_print(LOG_ERROR, "database_init: mysql_real_connect(): %s", mysql_error(dbh));
  66. mysql_close(dbh);
  67. return -1;
  68. }
  69. const char *database = config_get_string("mysql", "database", NULL);
  70. if (mysql_select_db(dbh, database) != 0) {
  71. log_print(LOG_ERROR, "database_init: mysql_select_db(): %s", mysql_error(dbh));
  72. mysql_close(dbh);
  73. return -1;
  74. }
  75. return 0;
  76. }
  77. int database_close(void)
  78. {
  79. mysql_close(dbh);
  80. return 0;
  81. }