forked from sysown/proxysql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProxySQL_Admin.cpp
More file actions
8718 lines (8121 loc) · 342 KB
/
ProxySQL_Admin.cpp
File metadata and controls
8718 lines (8121 loc) · 342 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "../deps/json/json.hpp"
using json = nlohmann::json;
#define PROXYJSON
#include <iostream> // std::cout
#include <sstream> // std::stringstream
#include <fstream>
#include <algorithm> // std::sort
#include <memory>
#include <vector> // std::vector
#include <unordered_set>
#include "prometheus/exposer.h"
#include "prometheus/counter.h"
#include "openssl/ssl.h"
#include "openssl/err.h"
#include "Base_Thread.h"
#include "MySQL_HostGroups_Manager.h"
#include "PgSQL_HostGroups_Manager.h"
#include "mysql.h"
#include "proxysql_admin.h"
#include "Discovery_Schema.h"
#include "Query_Tool_Handler.h"
#include "re2/re2.h"
#include "re2/regexp.h"
#include "proxysql.h"
#include "proxysql_config.h"
#include "proxysql_restapi.h"
#include "proxysql_utils.h"
#include "prometheus_helpers.h"
#include "cpp.h"
#include "MySQL_Data_Stream.h"
#include "PgSQL_Data_Stream.h"
#include "MySQL_Query_Processor.h"
#include "PgSQL_Query_Processor.h"
#include "ProxySQL_HTTP_Server.hpp" // HTTP server
#include "MySQL_Authentication.hpp"
#include "PgSQL_Authentication.h"
#include "MySQL_LDAP_Authentication.hpp"
#include "MySQL_PreparedStatement.h"
#include "ProxySQL_Cluster.hpp"
#include "ProxySQL_Statistics.hpp"
#include "MySQL_Logger.hpp"
#include "PgSQL_Logger.hpp"
#include "MCP_Thread.h"
#include "SQLite3_Server.h"
#include "Web_Interface.hpp"
#include <dirent.h>
#include <search.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <sys/socket.h>
#include <resolv.h>
#include <arpa/inet.h>
#include <pthread.h>
#ifndef SPOOKYV2
#include "SpookyV2.h"
#define SPOOKYV2
#endif
#include <fcntl.h>
#include <sys/utsname.h>
#include "platform.h"
#include "microhttpd.h"
#if (defined(__i386__) || defined(__x86_64__) || defined(__ARM_ARCH_3__) || defined(__mips__)) && defined(__linux)
// currently only support x86-32, x86-64, ARM, and MIPS on Linux
#include "coredumper/coredumper.h"
#endif
#include <uuid/uuid.h>
#include "PgSQL_Protocol.h"
#include "MySQL_Query_Cache.h"
#include "PgSQL_Query_Cache.h"
//#include "usual/time.h"
using std::string;
using std::unique_ptr;
#ifdef WITHGCOV
extern "C" void __gcov_dump();
extern "C" void __gcov_reset();
#endif
#ifdef DEBUG
//#define BENCHMARK_FASTROUTING_LOAD
#endif // DEBUG
//#define MYSQL_THREAD_IMPLEMENTATION
#define SELECT_VERSION_COMMENT "select @@version_comment limit 1"
#define SELECT_VERSION_COMMENT_LEN 32
#define SELECT_DB_USER "select DATABASE(), USER() limit 1"
#define SELECT_DB_USER_LEN 33
#define SELECT_CHARSET_VARIOUS "select @@character_set_client, @@character_set_connection, @@character_set_server, @@character_set_database limit 1"
#define SELECT_CHARSET_VARIOUS_LEN 115
#define READ_ONLY_OFF "\x01\x00\x00\x01\x02\x23\x00\x00\x02\x03\x64\x65\x66\x00\x00\x00\x0d\x56\x61\x72\x69\x61\x62\x6c\x65\x5f\x6e\x61\x6d\x65\x00\x0c\x21\x00\x0f\x00\x00\x00\xfd\x01\x00\x1f\x00\x00\x1b\x00\x00\x03\x03\x64\x65\x66\x00\x00\x00\x05\x56\x61\x6c\x75\x65\x00\x0c\x21\x00\x0f\x00\x00\x00\xfd\x01\x00\x1f\x00\x00\x05\x00\x00\x04\xfe\x00\x00\x02\x00\x0e\x00\x00\x05\x09\x72\x65\x61\x64\x5f\x6f\x6e\x6c\x79\x03\x4f\x46\x46\x05\x00\x00\x06\xfe\x00\x00\x02\x00"
#define READ_ONLY_ON "\x01\x00\x00\x01\x02\x23\x00\x00\x02\x03\x64\x65\x66\x00\x00\x00\x0d\x56\x61\x72\x69\x61\x62\x6c\x65\x5f\x6e\x61\x6d\x65\x00\x0c\x21\x00\x0f\x00\x00\x00\xfd\x01\x00\x1f\x00\x00\x1b\x00\x00\x03\x03\x64\x65\x66\x00\x00\x00\x05\x56\x61\x6c\x75\x65\x00\x0c\x21\x00\x0f\x00\x00\x00\xfd\x01\x00\x1f\x00\x00\x05\x00\x00\x04\xfe\x00\x00\x02\x00\x0d\x00\x00\x05\x09\x72\x65\x61\x64\x5f\x6f\x6e\x6c\x79\x02\x4f\x4e\x05\x00\x00\x06\xfe\x00\x00\x02\x00"
#define READ_ONLY_0 "\x01\x00\x00\x01\x01\x28\x00\x00\x02\x03\x64\x65\x66\x00\x00\x00\x12\x40\x40\x67\x6c\x6f\x62\x61\x6c\x2e\x72\x65\x61\x64\x5f\x6f\x6e\x6c\x79\x00\x0c\x3f\x00\x01\x00\x00\x00\x08\x80\x00\x00\x00\x00\x05\x00\x00\x03\xfe\x00\x00\x02\x00\x02\x00\x00\x04\x01\x30\x05\x00\x00\x05\xfe\x00\x00\x02\x00"
#define READ_ONLY_1 "\x01\x00\x00\x01\x01\x28\x00\x00\x02\x03\x64\x65\x66\x00\x00\x00\x12\x40\x40\x67\x6c\x6f\x62\x61\x6c\x2e\x72\x65\x61\x64\x5f\x6f\x6e\x6c\x79\x00\x0c\x3f\x00\x01\x00\x00\x00\x08\x80\x00\x00\x00\x00\x05\x00\x00\x03\xfe\x00\x00\x02\x00\x02\x00\x00\x04\x01\x31\x05\x00\x00\x05\xfe\x00\x00\x02\x00"
struct MHD_Daemon *Admin_HTTP_Server;
extern ProxySQL_Statistics *GloProxyStats;
template<enum SERVER_TYPE>
int ProxySQL_Test___PurgeDigestTable(bool async_purge, bool parallel, char **msg);
extern char *ssl_key_fp;
extern char *ssl_cert_fp;
extern char *ssl_ca_fp;
// ProxySQL_Admin shared variables
int admin___web_verbosity = 0;
char * proxysql_version = NULL;
#include "proxysql_find_charset.h"
template <typename T, typename std::enable_if<std::is_integral<T>::value, bool>::type = true>
T j_get_srv_default_int_val(
const json& j, uint32_t hid, const string& key, const function<bool(T)>& val_check);
static const vector<string> mysql_servers_tablenames = {
"mysql_servers",
"mysql_replication_hostgroups",
"mysql_group_replication_hostgroups",
"mysql_galera_hostgroups",
"mysql_aws_aurora_hostgroups",
"mysql_hostgroup_attributes",
"mysql_servers_ssl_params",
};
static const vector<string> pgsql_servers_tablenames = {
"pgsql_servers",
"pgsql_replication_hostgroups",
// "pgsql_group_replication_hostgroups",
// "pgsql_galera_hostgroups",
// "pgsql_aws_aurora_hostgroups",
"pgsql_hostgroup_attributes",
};
static const vector<string> mysql_firewall_tablenames = {
"mysql_firewall_whitelist_users",
"mysql_firewall_whitelist_rules",
"mysql_firewall_whitelist_sqli_fingerprints",
};
static const vector<string> pgsql_firewall_tablenames = {
"pgsql_firewall_whitelist_users",
"pgsql_firewall_whitelist_rules",
"pgsql_firewall_whitelist_sqli_fingerprints",
};
static const vector<string> mysql_query_rules_tablenames = { "mysql_query_rules", "mysql_query_rules_fast_routing" };
static const vector<string> pgsql_query_rules_tablenames = { "pgsql_query_rules", "pgsql_query_rules_fast_routing" };
static const vector<string> scheduler_tablenames = { "scheduler" };
static const vector<string> proxysql_servers_tablenames = { "proxysql_servers" };
static const vector<string> restapi_tablenames = { "restapi_routes" };
static unordered_map<string, const vector<string>&> module_tablenames = {
{ "mysql_servers", mysql_servers_tablenames },
{ "mysql_firewall", mysql_firewall_tablenames },
{ "mysql_query_rules", mysql_query_rules_tablenames },
{ "scheduler", scheduler_tablenames },
{ "proxysql_servers", proxysql_servers_tablenames },
{ "restapi", restapi_tablenames },
{ "pgsql_servers", pgsql_servers_tablenames },
{ "pgsql_firewall", pgsql_firewall_tablenames },
{ "pgsql_query_rules", pgsql_query_rules_tablenames },
};
static void BQE1(SQLite3DB *db, const vector<string>& tbs, const string& p1, const string& p2, const string& p3) {
string query;
for (auto it = tbs.begin(); it != tbs.end(); it++) {
if (p1 != "") {
query = p1 + *it;
db->execute(query.c_str());
}
if (p2 != "" && p3 != "") {
query = p2 + *it + p3 + *it;
db->execute(query.c_str());
}
}
}
static int round_intv_to_time_interval(const char* name, int _intv) {
int intv = _intv;
if (intv > 300) {
intv = 600;
} else {
if (intv > 120) {
intv = 300;
} else {
if (intv > 60) {
intv = 120;
} else {
if (intv > 30) {
intv = 60;
} else {
if (intv > 10) {
intv = 30;
} else {
if (intv > 5) {
intv = 10;
} else {
if (intv > 1) {
intv = 5;
}
}
}
}
}
}
}
if (intv != _intv) {
proxy_warning("Variable '%s' rounded to interval '%d'\n", name, intv);
}
return intv;
}
#ifdef __APPLE__
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif // MSG_NOSIGNAL
#endif // __APPLE__
#define SAFE_SQLITE3_STEP(_stmt) do {\
do {\
rc=(*proxy_sqlite3_step)(_stmt);\
if (rc!=SQLITE_DONE) {\
assert(rc==SQLITE_LOCKED);\
usleep(100);\
}\
} while (rc!=SQLITE_DONE);\
} while (0)
typedef struct _arg_proxysql_adm_t {
struct sockaddr * addr;
socklen_t addr_size;
int client_t;
} arg_proxysql_adm;
void StringToHex(unsigned char *string, unsigned char *hexstring, size_t l) {
unsigned char ch;
size_t i, j;
for (i=0, j=0; i<l; i++, j+=2) {
ch=string[i];
ch = ch >> 4;
if (ch <= 9) {
hexstring[j]= '0' + ch;
} else {
hexstring[j]= 'A' + ch - 10;
}
ch = string[i];
ch = ch & 0x0F;
if (ch <= 9) {
hexstring[j+1]= '0' + ch;
} else {
hexstring[j+1]= 'A' + ch - 10;
}
}
}
struct cpu_timer
{
cpu_timer() {
begin = monotonic_time();
}
~cpu_timer()
{
unsigned long long end = monotonic_time();
#ifdef DEBUG
std::cerr << double( end - begin ) / 1000000 << " secs.\n" ;
#endif
begin=end-begin; // make the compiler happy
};
unsigned long long begin;
};
char *s_strdup(char *s) {
char *ret=NULL;
if (s) {
ret=strdup(s);
}
return ret;
}
int admin_load_main_=0;
bool admin_nostart_=false;
int __admin_refresh_interval=0;
bool admin_proxysql_mysql_paused = false;
bool admin_proxysql_pgsql_paused = false;
int admin_old_wait_timeout;
extern MySQL_Query_Cache *GloMyQC;
extern PgSQL_Query_Cache* GloPgQC;
extern MySQL_Authentication *GloMyAuth;
extern PgSQL_Authentication *GloPgAuth;
extern MySQL_LDAP_Authentication *GloMyLdapAuth;
extern ProxySQL_Admin *GloAdmin;
extern MySQL_Query_Processor* GloMyQPro;
extern PgSQL_Query_Processor* GloPgQPro;
extern MySQL_Threads_Handler *GloMTH;
extern MySQL_Logger *GloMyLogger;
extern PgSQL_Logger* GloPgSQL_Logger;
extern MySQL_STMT_Manager_v14 *GloMyStmt;
extern MySQL_Monitor *GloMyMon;
extern PgSQL_Threads_Handler* GloPTH;
extern MCP_Threads_Handler* GloMCPH;
extern void (*flush_logs_function)();
extern Web_Interface *GloWebInterface;
extern ProxySQL_Cluster *GloProxyCluster;
#ifdef PROXYSQLCLICKHOUSE
extern ClickHouse_Authentication *GloClickHouseAuth;
extern ClickHouse_Server *GloClickHouseServer;
#endif /* PROXYSQLCLICKHOUSE */
extern SQLite3_Server *GloSQLite3Server;
extern char * binary_sha1;
extern int ProxySQL_create_or_load_TLS(bool bootstrap, std::string& msg);
#define PANIC(msg) { perror(msg); exit(EXIT_FAILURE); }
pthread_mutex_t sock_mutex = PTHREAD_MUTEX_INITIALIZER;
//pthread_mutex_t admin_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t users_mutex = PTHREAD_MUTEX_INITIALIZER;
//pthread_mutex_t test_mysql_firewall_whitelist_mutex = PTHREAD_MUTEX_INITIALIZER;
//std::unordered_map<std::string, void *> map_test_mysql_firewall_whitelist_rules;
//char rand_del[6];
//static int http_handler(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **ptr) {
MHD_Result http_handler(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, long unsigned int *upload_data_size, void **ptr) {
return (MHD_Result) GloAdmin->AdminHTTPServer->handler(cls, connection, url, method, version, upload_data, upload_data_size, ptr);
}
#define LINESIZE 2048
#include "ProxySQL_Admin_Tables_Definitions.h"
static char * admin_variables_names[]= {
(char *)"admin_credentials",
(char *)"stats_credentials",
(char *)"stats_mysql_connections",
(char *)"stats_mysql_connection_pool",
(char *)"stats_mysql_query_cache",
(char *)"stats_mysql_query_digest_to_disk",
(char *)"stats_system_cpu",
(char *)"stats_system_memory",
(char *)"mysql_ifaces",
(char *)"pgsql_ifaces",
(char *)"telnet_admin_ifaces",
(char *)"telnet_stats_ifaces",
(char *)"refresh_interval",
(char *)"read_only",
// (char *)"hash_passwords",
(char *)"vacuum_stats",
(char *)"version",
(char *)"cluster_username",
(char *)"cluster_password",
(char *)"cluster_check_interval_ms",
(char *)"cluster_check_status_frequency",
(char *)"cluster_mysql_query_rules_diffs_before_sync",
(char *)"cluster_mysql_servers_diffs_before_sync",
(char *)"cluster_mysql_users_diffs_before_sync",
(char *)"cluster_proxysql_servers_diffs_before_sync",
(char *)"cluster_mysql_variables_diffs_before_sync",
(char *)"cluster_admin_variables_diffs_before_sync",
(char *)"cluster_ldap_variables_diffs_before_sync",
(char *)"cluster_mysql_query_rules_save_to_disk",
(char *)"cluster_mysql_servers_save_to_disk",
(char *)"cluster_mysql_users_save_to_disk",
(char *)"cluster_proxysql_servers_save_to_disk",
(char *)"cluster_mysql_variables_save_to_disk",
(char *)"cluster_admin_variables_save_to_disk",
(char *)"cluster_ldap_variables_save_to_disk",
(char *)"cluster_mysql_servers_sync_algorithm",
(char *)"checksum_mysql_query_rules",
(char *)"checksum_mysql_servers",
(char *)"checksum_mysql_users",
(char *)"checksum_mysql_variables",
(char *)"checksum_admin_variables",
(char *)"checksum_ldap_variables",
(char *)"restapi_enabled",
(char *)"restapi_port",
(char *)"web_enabled",
(char *)"web_port",
(char *)"web_verbosity",
(char *)"prometheus_memory_metrics_interval",
#ifdef DEBUG
(char *)"debug",
(char *)"debug_output",
#endif /* DEBUG */
(char *)"coredump_generation_interval_ms",
(char *)"coredump_generation_threshold",
(char *)"ssl_keylog_file",
NULL
};
using metric_name = std::string;
using metric_help = std::string;
using metric_tags = std::map<std::string, std::string>;
using admin_counter_tuple =
std::tuple<
p_admin_counter::metric,
metric_name,
metric_help,
metric_tags
>;
using admin_gauge_tuple =
std::tuple<
p_admin_gauge::metric,
metric_name,
metric_help,
metric_tags
>;
using admin_dyn_counter_tuple =
std::tuple<
p_admin_dyn_counter::metric,
metric_name,
metric_help,
metric_tags
>;
using admin_dyn_gauge_tuple =
std::tuple<
p_admin_dyn_gauge::metric,
metric_name,
metric_help,
metric_tags
>;
using admin_counter_vector = std::vector<admin_counter_tuple>;
using admin_gauge_vector = std::vector<admin_gauge_tuple>;
using admin_dyn_counter_vector = std::vector<admin_dyn_counter_tuple>;
using admin_dyn_gauge_vector = std::vector<admin_dyn_gauge_tuple>;
/**
* @brief Metrics map holding the metrics for the 'ProxySQL_Admin' module.
*
* @note Some metrics in this map, share a common "id name", because
* they differ only by label, because of this, HELP is shared between
* them. For better visual identification of this groups they are
* sepparated using a line separator comment.
*/
const std::tuple<admin_counter_vector, admin_gauge_vector, admin_dyn_counter_vector, admin_dyn_gauge_vector>
admin_metrics_map = std::make_tuple(
admin_counter_vector {
std::make_tuple (
p_admin_counter::uptime,
"proxysql_uptime_seconds_total",
"The total uptime of ProxySQL.",
metric_tags {}
),
std::make_tuple (
p_admin_counter::jemalloc_allocated,
"proxysql_jemalloc_allocated_bytes_total",
"Bytes allocated by the application.",
metric_tags {}
)
},
admin_gauge_vector {
std::make_tuple (
p_admin_gauge::mysql_listener_paused,
"proxysql_mysql_listener_paused",
"MySQL listener paused because of PROXYSQL PAUSE.",
metric_tags {}
),
std::make_tuple(
p_admin_gauge::pgsql_listener_paused,
"proxysql_pgsql_listener_paused",
"PgSQL listener paused because of PROXYSQL PAUSE.",
metric_tags {}
),
// memory metrics
std::make_tuple (
p_admin_gauge::connpool_memory_bytes,
"proxysql_connpool_memory_bytes",
"Memory used by the connection pool to store connections metadata.",
metric_tags {}
),
std::make_tuple (
p_admin_gauge::sqlite3_memory_bytes,
"proxysql_sqlite3_memory_bytes",
"Memory used by SQLite.",
metric_tags {}
),
// ====================================================================
std::make_tuple (
p_admin_gauge::jemalloc_resident,
"proxysql_jemalloc_bytes",
"Jemalloc memory usage stadistics (resident|active|mapped|metadata).",
metric_tags {
{ "type", "resident" }
}
),
std::make_tuple (
p_admin_gauge::jemalloc_active,
"proxysql_jemalloc_bytes",
"Jemalloc memory usage stadistics (resident|active|mapped|metadata).",
metric_tags {
{ "type", "active" }
}
),
std::make_tuple (
p_admin_gauge::jemalloc_mapped,
"proxysql_jemalloc_bytes",
"Jemalloc memory usage stadistics (resident|active|mapped|metadata).",
metric_tags {
{ "type", "mapped" }
}
),
std::make_tuple (
p_admin_gauge::jemalloc_metadata,
"proxysql_jemalloc_bytes",
"Jemalloc memory usage stadistics (resident|active|mapped|metadata).",
metric_tags {
{ "type", "metadata" }
}
),
std::make_tuple (
p_admin_gauge::jemalloc_retained,
"proxysql_jemalloc_bytes",
"Jemalloc memory usage stadistics (resident|active|mapped|metadata).",
metric_tags {
{ "type", "retained" }
}
),
// ====================================================================
std::make_tuple (
p_admin_gauge::query_digest_memory_bytes,
"proxysql_query_digest_memory_bytes",
"Memory used to store data related to stats_mysql_query_digest.",
metric_tags {}
),
std::make_tuple (
p_admin_gauge::auth_memory_bytes,
"proxysql_auth_memory_bytes",
"Memory used by the authentication module to store user credentials and attributes.",
metric_tags {}
),
std::make_tuple (
p_admin_gauge::mysql_query_rules_memory_bytes,
"proxysql_mysql_query_rules_memory_bytes",
"Number of bytes used by 'mysql_query_rules' rules.",
metric_tags {}
),
std::make_tuple (
p_admin_gauge::mysql_firewall_users_table,
"proxysql_mysql_firewall_users_table_bytes",
"Number of bytes used by 'mysql_firewall_users' entries.",
metric_tags {}
),
std::make_tuple (
// TODO: Check why 'global_firewall_whitelist_users_result___size' never updated
p_admin_gauge::mysql_firewall_users_config,
"proxysql_mysql_firewall_users_config_bytes",
"Full 'mysql_firewall_users' config 'resultset' size.",
metric_tags {}
),
std::make_tuple (
p_admin_gauge::mysql_firewall_rules_table,
"proxysql_mysql_firewall_rules_table_bytes",
"Number of bytes used by 'mysql_firewall_rules' entries.",
metric_tags {}
),
std::make_tuple (
p_admin_gauge::mysql_firewall_rules_config,
"proxysql_mysql_firewall_rules_config_bytes",
"Full 'mysql_firewall_users' config 'resultset' size.",
metric_tags {}
),
std::make_tuple (
p_admin_gauge::stack_memory_mysql_threads,
"proxysql_stack_memory_mysql_threads_bytes",
"Stack size used by 'mysql_threads'.",
metric_tags {}
),
std::make_tuple (
p_admin_gauge::stack_memory_admin_threads,
"proxysql_stack_memory_admin_threads_bytes",
"Stack size used by 'admin_threads'.",
metric_tags {}
),
std::make_tuple (
p_admin_gauge::stack_memory_cluster_threads,
"proxysql_stack_memory_cluster_threads",
"Stack size used by 'cluster_threads'.",
metric_tags {}
),
// stmt metrics
std::make_tuple (
p_admin_gauge::stmt_client_active_total,
"proxysql_stmt_client_active",
"The total number of prepared statements that are in use by clients.",
metric_tags {}
),
std::make_tuple (
p_admin_gauge::stmt_client_active_unique,
"proxysql_stmt_client_active_unique",
"This variable tracks the number of unique prepared statements currently in use by clients.",
metric_tags {}
),
std::make_tuple (
p_admin_gauge::stmt_server_active_total,
"proxysql_stmt_server_active",
"The total number of prepared statements currently available across all backend connections.",
metric_tags {}
),
std::make_tuple (
p_admin_gauge::stmt_server_active_unique,
"proxysql_stmt_server_active_unique",
"The number of unique prepared statements currently available across all backend connections.",
metric_tags {}
),
std::make_tuple (
p_admin_gauge::stmt_max_stmt_id,
"proxysql_stmt_max_stmt_id",
"When a new global prepared statement is created, a new \"stmt_id\" is used. Stmt_Max_Stmt_id represents the maximum \"stmt_id\" ever used.",
metric_tags {}
),
std::make_tuple (
p_admin_gauge::stmt_cached,
"proxysql_stmt_cached",
"This is the number of global prepared statements for which proxysql has metadata.",
metric_tags {}
),
std::make_tuple(
p_admin_gauge::prepare_stmt_metadata_memory_bytes,
"prepare_stmt_metadata_memory_bytes",
"Memory used to store meta data related to prepare statements.",
metric_tags{}
),
std::make_tuple(
p_admin_gauge::prepare_stmt_backend_memory_bytes,
"prepare_stmt_backend_memory_bytes",
"Memory used by backend server related to prepare statements.",
metric_tags{}
),
std::make_tuple (
p_admin_gauge::fds_in_use,
"proxysql_fds_in_use",
"The number of file descriptors currently in use by ProxySQL.",
metric_tags {}
),
std::make_tuple (
p_admin_gauge::version_info,
"proxysql_version_info",
"ProxySQL version.",
metric_tags {
{ "version", PROXYSQL_VERSION },
{ "version_comment", std::string { "ProxySQL version " } + PROXYSQL_VERSION + ", codename " + PROXYSQL_CODENAME }
}
)
},
admin_dyn_counter_vector {},
admin_dyn_gauge_vector {
std::make_tuple (
p_admin_dyn_gauge::proxysql_servers_clients_status_last_seen_at,
"proxysql_servers_clients_status_last_seen_at",
"Last time a query was executed in the local Admin interface by the remote ProxySQL instance.",
metric_tags {}
)
}
);
ProxySQL_Admin *SPA=NULL;
void * (*child_func[3]) (void *arg);
const std::vector<std::string> LOAD_ADMIN_VARIABLES_TO_MEMORY = {
"LOAD ADMIN VARIABLES TO MEMORY" ,
"LOAD ADMIN VARIABLES TO MEM" ,
"LOAD ADMIN VARIABLES FROM DISK" };
const std::vector<std::string> SAVE_ADMIN_VARIABLES_FROM_MEMORY = {
"SAVE ADMIN VARIABLES FROM MEMORY" ,
"SAVE ADMIN VARIABLES FROM MEM" ,
"SAVE ADMIN VARIABLES TO DISK" };
const std::vector<std::string> LOAD_ADMIN_VARIABLES_FROM_MEMORY = {
"LOAD ADMIN VARIABLES FROM MEMORY" ,
"LOAD ADMIN VARIABLES FROM MEM" ,
"LOAD ADMIN VARIABLES TO RUNTIME" ,
"LOAD ADMIN VARIABLES TO RUN" };
const std::vector<std::string> SAVE_ADMIN_VARIABLES_TO_MEMORY = {
"SAVE ADMIN VARIABLES TO MEMORY" ,
"SAVE ADMIN VARIABLES TO MEM" ,
"SAVE ADMIN VARIABLES FROM RUNTIME" ,
"SAVE ADMIN VARIABLES FROM RUN" };
const std::vector<std::string> LOAD_MYSQL_SERVERS_FROM_MEMORY = {
"LOAD MYSQL SERVERS FROM MEMORY" ,
"LOAD MYSQL SERVERS FROM MEM" ,
"LOAD MYSQL SERVERS TO RUNTIME" ,
"LOAD MYSQL SERVERS TO RUN" };
const std::vector<std::string> SAVE_MYSQL_SERVERS_TO_MEMORY = {
"SAVE MYSQL SERVERS TO MEMORY" ,
"SAVE MYSQL SERVERS TO MEM" ,
"SAVE MYSQL SERVERS FROM RUNTIME" ,
"SAVE MYSQL SERVERS FROM RUN" };
const std::vector<std::string> LOAD_MYSQL_USERS_FROM_MEMORY = {
"LOAD MYSQL USERS FROM MEMORY" ,
"LOAD MYSQL USERS FROM MEM" ,
"LOAD MYSQL USERS TO RUNTIME" ,
"LOAD MYSQL USERS TO RUN" };
const std::vector<std::string> SAVE_MYSQL_USERS_TO_MEMORY = {
"SAVE MYSQL USERS TO MEMORY" ,
"SAVE MYSQL USERS TO MEM" ,
"SAVE MYSQL USERS FROM RUNTIME" ,
"SAVE MYSQL USERS FROM RUN" };
const std::vector<std::string> LOAD_MYSQL_VARIABLES_FROM_MEMORY = {
"LOAD MYSQL VARIABLES FROM MEMORY" ,
"LOAD MYSQL VARIABLES FROM MEM" ,
"LOAD MYSQL VARIABLES TO RUNTIME" ,
"LOAD MYSQL VARIABLES TO RUN" };
const std::vector<std::string> SAVE_MYSQL_VARIABLES_TO_MEMORY = {
"SAVE MYSQL VARIABLES TO MEMORY" ,
"SAVE MYSQL VARIABLES TO MEM" ,
"SAVE MYSQL VARIABLES FROM RUNTIME" ,
"SAVE MYSQL VARIABLES FROM RUN" };
// PgSQL
const std::vector<std::string> LOAD_PGSQL_SERVERS_FROM_MEMORY = {
"LOAD PGSQL SERVERS FROM MEMORY" ,
"LOAD PGSQL SERVERS FROM MEM" ,
"LOAD PGSQL SERVERS TO RUNTIME" ,
"LOAD PGSQL SERVERS TO RUN" };
const std::vector<std::string> SAVE_PGSQL_SERVERS_TO_MEMORY = {
"SAVE PGSQL SERVERS TO MEMORY" ,
"SAVE PGSQL SERVERS TO MEM" ,
"SAVE PGSQL SERVERS FROM RUNTIME" ,
"SAVE PGSQL SERVERS FROM RUN" };
const std::vector<std::string> LOAD_PGSQL_USERS_FROM_MEMORY = {
"LOAD PGSQL USERS FROM MEMORY" ,
"LOAD PGSQL USERS FROM MEM" ,
"LOAD PGSQL USERS TO RUNTIME" ,
"LOAD PGSQL USERS TO RUN" };
const std::vector<std::string> SAVE_PGSQL_USERS_TO_MEMORY = {
"SAVE PGSQL USERS TO MEMORY" ,
"SAVE PGSQL USERS TO MEM" ,
"SAVE PGSQL USERS FROM RUNTIME" ,
"SAVE PGSQL USERS FROM RUN" };
const std::vector<std::string> LOAD_PGSQL_VARIABLES_FROM_MEMORY = {
"LOAD PGSQL VARIABLES FROM MEMORY" ,
"LOAD PGSQL VARIABLES FROM MEM" ,
"LOAD PGSQL VARIABLES TO RUNTIME" ,
"LOAD PGSQL VARIABLES TO RUN" };
const std::vector<std::string> SAVE_PGSQL_VARIABLES_TO_MEMORY = {
"SAVE PGSQL VARIABLES TO MEMORY" ,
"SAVE PGSQL VARIABLES TO MEM" ,
"SAVE PGSQL VARIABLES FROM RUNTIME" ,
"SAVE PGSQL VARIABLES FROM RUN" };
//
const std::vector<std::string> LOAD_COREDUMP_FROM_MEMORY = {
"LOAD COREDUMP FROM MEMORY" ,
"LOAD COREDUMP FROM MEM" ,
"LOAD COREDUMP TO RUNTIME" ,
"LOAD COREDUMP TO RUN" };
unordered_map<string,std::tuple<string, vector<string>, vector<string>>> load_save_disk_commands;
static void generate_load_save_disk_commands(std::vector<std::string>& vec1, std::vector<std::string>& vec2, const string& name) {
string s;
if (vec1.size() == 0) {
s = "LOAD " + name + " TO MEMORY"; vec1.push_back(s);
s = "LOAD " + name + " TO MEM"; vec1.push_back(s);
s = "LOAD " + name + " FROM DISK"; vec1.push_back(s);
}
if (vec2.size() == 0) {
s = "SAVE " + name + " FROM MEMORY"; vec2.push_back(s);
s = "SAVE " + name + " FROM MEM"; vec2.push_back(s);
s = "SAVE " + name + " TO DISK"; vec2.push_back(s);
}
}
static void generate_load_save_disk_commands(const string& name, const string& command) {
std::vector<std::string> vec1;
std::vector<std::string> vec2;
generate_load_save_disk_commands(vec1, vec2, command);
std::tuple<string, vector<string>, vector<string>> a = tuple<string, vector<string>, vector<string>>{command, vec1, vec2};
load_save_disk_commands[name] = a;
}
bool is_admin_command_or_alias(const std::vector<std::string>& cmds, char *query_no_space, int query_no_space_length);
incoming_servers_t::incoming_servers_t() {}
incoming_servers_t::incoming_servers_t(
SQLite3_result* incoming_mysql_servers_v2,
SQLite3_result* incoming_replication_hostgroups,
SQLite3_result* incoming_group_replication_hostgroups,
SQLite3_result* incoming_galera_hostgroups,
SQLite3_result* incoming_aurora_hostgroups,
SQLite3_result* incoming_hostgroup_attributes,
SQLite3_result* incoming_mysql_servers_ssl_params,
SQLite3_result* runtime_mysql_servers
) :
incoming_mysql_servers_v2(incoming_mysql_servers_v2),
incoming_replication_hostgroups(incoming_replication_hostgroups),
incoming_group_replication_hostgroups(incoming_group_replication_hostgroups),
incoming_galera_hostgroups(incoming_galera_hostgroups),
incoming_aurora_hostgroups(incoming_aurora_hostgroups),
incoming_hostgroup_attributes(incoming_hostgroup_attributes),
incoming_mysql_servers_ssl_params(incoming_mysql_servers_ssl_params),
runtime_mysql_servers(runtime_mysql_servers)
{}
runtime_mysql_servers_checksum_t::runtime_mysql_servers_checksum_t() : epoch(0) {}
runtime_mysql_servers_checksum_t::runtime_mysql_servers_checksum_t(const std::string& checksum, time_t epoch) :
value(checksum), epoch(epoch) {}
mysql_servers_v2_checksum_t::mysql_servers_v2_checksum_t() : epoch(0) {}
mysql_servers_v2_checksum_t::mysql_servers_v2_checksum_t(const std::string& checksum, time_t epoch) :
value(checksum), epoch(epoch) {}
runtime_pgsql_servers_checksum_t::runtime_pgsql_servers_checksum_t() : epoch(0) {}
runtime_pgsql_servers_checksum_t::runtime_pgsql_servers_checksum_t(const std::string& checksum, time_t epoch) :
value(checksum), epoch(epoch) {}
pgsql_servers_v2_checksum_t::pgsql_servers_v2_checksum_t() : epoch(0) {}
pgsql_servers_v2_checksum_t::pgsql_servers_v2_checksum_t(const std::string& checksum, time_t epoch) :
value(checksum), epoch(epoch) {}
incoming_pgsql_servers_t::incoming_pgsql_servers_t() {}
incoming_pgsql_servers_t::incoming_pgsql_servers_t(
SQLite3_result* incoming_pgsql_servers_v2,
SQLite3_result* incoming_replication_hostgroups,
SQLite3_result* incoming_hostgroup_attributes,
SQLite3_result* runtime_pgsql_servers
) :
incoming_pgsql_servers_v2(incoming_pgsql_servers_v2),
incoming_replication_hostgroups(incoming_replication_hostgroups),
incoming_hostgroup_attributes(incoming_hostgroup_attributes),
runtime_pgsql_servers(runtime_pgsql_servers)
{}
peer_runtime_mysql_servers_t::peer_runtime_mysql_servers_t() : resultset(nullptr), checksum() {}
peer_runtime_mysql_servers_t::peer_runtime_mysql_servers_t(
SQLite3_result* resultset, const runtime_mysql_servers_checksum_t& checksum
) : resultset(resultset), checksum(checksum)
{}
peer_mysql_servers_v2_t::peer_mysql_servers_v2_t() : resultset(nullptr), checksum() {}
peer_mysql_servers_v2_t::peer_mysql_servers_v2_t(
SQLite3_result* resultset, const mysql_servers_v2_checksum_t& checksum
) : resultset(resultset), checksum(checksum)
{}
peer_runtime_pgsql_servers_t::peer_runtime_pgsql_servers_t() : resultset(nullptr), checksum() {}
peer_runtime_pgsql_servers_t::peer_runtime_pgsql_servers_t(
SQLite3_result* resultset, const runtime_pgsql_servers_checksum_t& checksum
) : resultset(resultset), checksum(checksum)
{}
peer_pgsql_servers_v2_t::peer_pgsql_servers_v2_t() : resultset(nullptr), checksum() {}
peer_pgsql_servers_v2_t::peer_pgsql_servers_v2_t(
SQLite3_result* resultset, const pgsql_servers_v2_checksum_t& checksum
) : resultset(resultset), checksum(checksum)
{}
ProxySQL_Config& ProxySQL_Admin::proxysql_config() {
static ProxySQL_Config instance = ProxySQL_Config(admindb);
if (instance.admindb != admindb) {
instance.admindb = admindb;
}
return instance;
}
ProxySQL_Restapi& ProxySQL_Admin::proxysql_restapi() {
static ProxySQL_Restapi instance = ProxySQL_Restapi(admindb);
if (instance.admindb != admindb) {
instance.admindb = admindb;
}
return instance;
}
template <enum SERVER_TYPE pt>
int ProxySQL_Admin::FlushDigestTableToDisk(SQLite3DB *_db) {
umap_query_digest uqd;
umap_query_digest_text uqdt;
if constexpr (pt == SERVER_TYPE_MYSQL) {
if (!GloMyQPro) return 0;
GloMyQPro->get_query_digests_reset(&uqd, &uqdt);
} else if constexpr (pt == SERVER_TYPE_PGSQL) {
if (!GloPgQPro) return 0;
GloPgQPro->get_query_digests_reset(&uqd, &uqdt);
}
int r = uqd.size();
SQLite3DB * sdb = _db;
sdb->execute("BEGIN");
int rc;
sqlite3_stmt *statement1=NULL;
sqlite3_stmt *statement32=NULL;
char *query1=NULL;
char *query32=NULL;
std::string query32s = "";
if constexpr (pt == SERVER_TYPE_MYSQL) {
query1 = (char*)"INSERT INTO history_mysql_query_digest VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15)";
query32s = "INSERT INTO history_mysql_query_digest VALUES " + generate_multi_rows_query(32, 15);
} else if constexpr (pt == SERVER_TYPE_PGSQL) {
query1 = (char*)"INSERT INTO history_pgsql_query_digest VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15)";
query32s = "INSERT INTO history_pgsql_query_digest VALUES " + generate_multi_rows_query(32, 15);
}
query32 = (char *)query32s.c_str();
rc = sdb->prepare_v2(query1, &statement1);
ASSERT_SQLITE_OK(rc, sdb);
rc = sdb->prepare_v2(query32, &statement32);
ASSERT_SQLITE_OK(rc, sdb);
int row_idx=0;
int max_bulk_row_idx=r/32;
max_bulk_row_idx=max_bulk_row_idx*32;
query_digest_stats_pointers_t qdsp;
time_t __now;
time(&__now);
unsigned long long curtime=monotonic_time();
time_t seen_time;
for (std::unordered_map<uint64_t, void *>::iterator it=uqd.begin(); it!=uqd.end(); ++it) {
QP_query_digest_stats * qds = (QP_query_digest_stats *)it->second;
int idx=row_idx%32;
if (row_idx<max_bulk_row_idx) { // bulk
rc=(*proxy_sqlite3_bind_int64)(statement32, (idx*15)+1, __now); ASSERT_SQLITE_OK(rc, sdb);
rc=(*proxy_sqlite3_bind_int64)(statement32, (idx*15)+2, qds->hid); ASSERT_SQLITE_OK(rc, sdb);
rc=(*proxy_sqlite3_bind_text)(statement32, (idx*15)+3, qds->schemaname, -1, SQLITE_TRANSIENT); ASSERT_SQLITE_OK(rc, sdb);
rc=(*proxy_sqlite3_bind_text)(statement32, (idx*15)+4, qds->username, -1, SQLITE_TRANSIENT); ASSERT_SQLITE_OK(rc, sdb);
rc=(*proxy_sqlite3_bind_text)(statement32, (idx*15)+5, qds->client_address, -1, SQLITE_TRANSIENT); ASSERT_SQLITE_OK(rc, sdb);
sprintf(qdsp.digest,"0x%016llX", (long long unsigned int)qds->digest);
rc=(*proxy_sqlite3_bind_text)(statement32, (idx*15)+6, qdsp.digest, -1, SQLITE_TRANSIENT); ASSERT_SQLITE_OK(rc, sdb);
if (qds->digest_text) {
rc=(*proxy_sqlite3_bind_text)(statement32, (idx*15)+7, qds->digest_text, -1, SQLITE_TRANSIENT); ASSERT_SQLITE_OK(rc, sdb);
} else {
std::unordered_map<uint64_t, char *>::iterator it2;
it2=uqdt.find(qds->digest);
if (it2 != uqdt.end()) {
rc=(*proxy_sqlite3_bind_text)(statement32, (idx*15)+7, it2->second, -1, SQLITE_TRANSIENT); ASSERT_SQLITE_OK(rc, sdb);
} else {
// LCOV_EXCL_START
assert(0);
// LCOV_EXCL_STOP
}
}