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.
 
 
 

142 lines
2.9 KiB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <mysql/mysql.h>
  5. #include "logging.h"
  6. #include "mysql_helper.h"
  7. void * init_connection(const char *host, const char *user, const char *pass)
  8. {
  9. MYSQL *con = NULL;
  10. con = mysql_init(con);
  11. if (con == NULL)
  12. return NULL;
  13. con = mysql_real_connect(con, host, user, pass, NULL, 0, NULL, 0);
  14. if (con == NULL) {
  15. log_print(LOG_ERROR, "p_mysql: %s", mysql_error(con));
  16. return NULL;
  17. }
  18. return con;
  19. }
  20. int ping_connection(void *mysql)
  21. {
  22. MYSQL *con = mysql;
  23. return mysql_ping(con);
  24. }
  25. int close_connection(void *mysql)
  26. {
  27. MYSQL *con = mysql;
  28. mysql_close(con);
  29. return 0;
  30. }
  31. int get_stats(void *mysql, struct mysql_stats *stats)
  32. {
  33. MYSQL *con = mysql;
  34. char *query;
  35. if (mysql_get_server_version(con) >= 50002)
  36. query = "SHOW GLOBAL STATUS";
  37. else
  38. query = "SHOW STATUS";
  39. if (mysql_real_query(con, query, strlen(query))) {
  40. log_print(LOG_ERROR, "p_mysql: %s", mysql_error(con));
  41. return -1;
  42. }
  43. MYSQL_RES *res;
  44. res = mysql_store_result(con);
  45. if (res == NULL) {
  46. log_print(LOG_ERROR, "p_mysql: %s", mysql_error(con));
  47. return -1;
  48. }
  49. MYSQL_ROW row;
  50. while ((row = mysql_fetch_row(res))) {
  51. char *key;
  52. unsigned long long val;
  53. key = row[0];
  54. val = atoll(row[1]);
  55. if (!strncmp(key, "Bytes_", 6)) {
  56. if (!strcmp(key +6, "received"))
  57. stats->bytes_received = val;
  58. else if (!strcmp(key +6, "sent"))
  59. stats->bytes_sent = val;
  60. } else if (!strncmp(key, "Com_", 4)) {
  61. if (!strcmp(key +4, "delete"))
  62. stats->com_delete = val;
  63. else if (!strcmp(key +4, "insert"))
  64. stats->com_insert = val;
  65. else if (!strcmp(key +4, "select"))
  66. stats->com_select = val;
  67. else if (!strcmp(key +4, "update"))
  68. stats->com_update = val;
  69. } else if (!strcmp(key, "Connections")) {
  70. stats->connections = val;
  71. } else if (!strncmp(key, "Qcache_", 7)) {
  72. if (!strcmp(key +7, "free_blocks"))
  73. stats->qc_free_blocks = val;
  74. else if (!strcmp(key +7, "free_memory"))
  75. stats->qc_free_memory = val;
  76. else if (!strcmp(key +7, "hits"))
  77. stats->qc_hits = val;
  78. else if (!strcmp(key +7, "inserts"))
  79. stats->qc_inserts = val;
  80. else if (!strcmp(key +7, "lowmem_prunes"))
  81. stats->qc_lowmem_prunes = val;
  82. else if (!strcmp(key +7, "not_cached"))
  83. stats->qc_not_cached = val;
  84. else if (!strcmp(key +7, "queries_in_cache"))
  85. stats->qc_queries_in_cache = val;
  86. else if (!strcmp(key +7, "total_blocks"))
  87. stats->qc_total_blocks = val;
  88. } else if (!strcmp(key, "Questions")) {
  89. stats->questions = val;
  90. } else if (!strncmp(key, "Threads_", 8)) {
  91. if (!strcmp(key, "Threads_cached"))
  92. stats->threads_cached = val;
  93. else if (!strcmp(key, "Threads_connected"))
  94. stats->threads_connected = val;
  95. else if (!strcmp(key, "Threads_created"))
  96. stats->threads_created = val;
  97. else if (!strcmp(key, "Threads_running"))
  98. stats->threads_running = val;
  99. }
  100. }
  101. mysql_free_result(res);
  102. return 0;
  103. }