github.com/netdata/go.d.plugin@v0.58.1/modules/mysql/collect_global_status.go (about)

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package mysql
     4  
     5  import (
     6  	"strings"
     7  )
     8  
     9  const queryShowGlobalStatus = "SHOW GLOBAL STATUS;"
    10  
    11  func (m *MySQL) collectGlobalStatus(mx map[string]int64) error {
    12  	// MariaDB: https://mariadb.com/kb/en/server-status-variables/
    13  	// MySQL: https://dev.mysql.com/doc/refman/8.0/en/server-status-variable-reference.html
    14  	q := queryShowGlobalStatus
    15  	m.Debugf("executing query: '%s'", q)
    16  
    17  	var name string
    18  	_, err := m.collectQuery(q, func(column, value string, _ bool) {
    19  		switch column {
    20  		case "Variable_name":
    21  			name = value
    22  		case "Value":
    23  			if !globalStatusKeys[name] {
    24  				return
    25  			}
    26  			switch name {
    27  			case "wsrep_connected":
    28  				mx[name] = parseInt(convertWsrepConnected(value))
    29  			case "wsrep_ready":
    30  				mx[name] = parseInt(convertWsrepReady(value))
    31  			case "wsrep_local_state":
    32  				// https://mariadb.com/kb/en/galera-cluster-status-variables/#wsrep_local_state
    33  				// https://github.com/codership/wsrep-API/blob/eab2d5d5a31672c0b7d116ef1629ff18392fd7d0/wsrep_api.h#L256
    34  				mx[name+"_undefined"] = boolToInt(value == "0")
    35  				mx[name+"_joiner"] = boolToInt(value == "1")
    36  				mx[name+"_donor"] = boolToInt(value == "2")
    37  				mx[name+"_joined"] = boolToInt(value == "3")
    38  				mx[name+"_synced"] = boolToInt(value == "4")
    39  				mx[name+"_error"] = boolToInt(parseInt(value) >= 5)
    40  			case "wsrep_cluster_status":
    41  				// https://www.percona.com/doc/percona-xtradb-cluster/LATEST/wsrep-status-index.html#wsrep_cluster_status
    42  				// https://github.com/codership/wsrep-API/blob/eab2d5d5a31672c0b7d116ef1629ff18392fd7d0/wsrep_api.h
    43  				// https://github.com/codership/wsrep-API/blob/f71cd270414ee70dde839cfc59c1731eea4230ea/examples/node/wsrep.c#L80
    44  				value = strings.ToUpper(value)
    45  				mx[name+"_primary"] = boolToInt(value == "PRIMARY")
    46  				mx[name+"_non_primary"] = boolToInt(value == "NON-PRIMARY")
    47  				mx[name+"_disconnected"] = boolToInt(value == "DISCONNECTED")
    48  			default:
    49  				mx[strings.ToLower(name)] = parseInt(value)
    50  			}
    51  		}
    52  	})
    53  	return err
    54  }
    55  
    56  func convertWsrepConnected(val string) string {
    57  	// https://www.percona.com/doc/percona-xtradb-cluster/LATEST/wsrep-status-index.html#wsrep_connected
    58  	switch val {
    59  	case "OFF":
    60  		return "0"
    61  	case "ON":
    62  		return "1"
    63  	default:
    64  		return "-1"
    65  	}
    66  }
    67  
    68  func convertWsrepReady(val string) string {
    69  	// https://www.percona.com/doc/percona-xtradb-cluster/LATEST/wsrep-status-index.html#wsrep_ready
    70  	switch val {
    71  	case "OFF":
    72  		return "0"
    73  	case "ON":
    74  		return "1"
    75  	default:
    76  		return "-1"
    77  	}
    78  }
    79  
    80  func boolToInt(v bool) int64 {
    81  	if v {
    82  		return 1
    83  	}
    84  	return 0
    85  }
    86  
    87  var globalStatusKeys = map[string]bool{
    88  	"Bytes_received":                        true,
    89  	"Bytes_sent":                            true,
    90  	"Queries":                               true,
    91  	"Questions":                             true,
    92  	"Slow_queries":                          true,
    93  	"Handler_commit":                        true,
    94  	"Handler_delete":                        true,
    95  	"Handler_prepare":                       true,
    96  	"Handler_read_first":                    true,
    97  	"Handler_read_key":                      true,
    98  	"Handler_read_next":                     true,
    99  	"Handler_read_prev":                     true,
   100  	"Handler_read_rnd":                      true,
   101  	"Handler_read_rnd_next":                 true,
   102  	"Handler_rollback":                      true,
   103  	"Handler_savepoint":                     true,
   104  	"Handler_savepoint_rollback":            true,
   105  	"Handler_update":                        true,
   106  	"Handler_write":                         true,
   107  	"Table_locks_immediate":                 true,
   108  	"Table_locks_waited":                    true,
   109  	"Table_open_cache_overflows":            true,
   110  	"Select_full_join":                      true,
   111  	"Select_full_range_join":                true,
   112  	"Select_range":                          true,
   113  	"Select_range_check":                    true,
   114  	"Select_scan":                           true,
   115  	"Sort_merge_passes":                     true,
   116  	"Sort_range":                            true,
   117  	"Sort_scan":                             true,
   118  	"Created_tmp_disk_tables":               true,
   119  	"Created_tmp_files":                     true,
   120  	"Created_tmp_tables":                    true,
   121  	"Connections":                           true,
   122  	"Aborted_connects":                      true,
   123  	"Max_used_connections":                  true,
   124  	"Binlog_cache_disk_use":                 true,
   125  	"Binlog_cache_use":                      true,
   126  	"Threads_connected":                     true,
   127  	"Threads_created":                       true,
   128  	"Threads_cached":                        true,
   129  	"Threads_running":                       true,
   130  	"Thread_cache_misses":                   true,
   131  	"Innodb_data_read":                      true,
   132  	"Innodb_data_written":                   true,
   133  	"Innodb_data_reads":                     true,
   134  	"Innodb_data_writes":                    true,
   135  	"Innodb_data_fsyncs":                    true,
   136  	"Innodb_data_pending_reads":             true,
   137  	"Innodb_data_pending_writes":            true,
   138  	"Innodb_data_pending_fsyncs":            true,
   139  	"Innodb_log_waits":                      true,
   140  	"Innodb_log_write_requests":             true,
   141  	"Innodb_log_writes":                     true,
   142  	"Innodb_os_log_fsyncs":                  true,
   143  	"Innodb_os_log_pending_fsyncs":          true,
   144  	"Innodb_os_log_pending_writes":          true,
   145  	"Innodb_os_log_written":                 true,
   146  	"Innodb_row_lock_current_waits":         true,
   147  	"Innodb_rows_inserted":                  true,
   148  	"Innodb_rows_read":                      true,
   149  	"Innodb_rows_updated":                   true,
   150  	"Innodb_rows_deleted":                   true,
   151  	"Innodb_buffer_pool_pages_data":         true,
   152  	"Innodb_buffer_pool_pages_dirty":        true,
   153  	"Innodb_buffer_pool_pages_free":         true,
   154  	"Innodb_buffer_pool_pages_flushed":      true,
   155  	"Innodb_buffer_pool_pages_misc":         true,
   156  	"Innodb_buffer_pool_pages_total":        true,
   157  	"Innodb_buffer_pool_bytes_data":         true,
   158  	"Innodb_buffer_pool_bytes_dirty":        true,
   159  	"Innodb_buffer_pool_read_ahead":         true,
   160  	"Innodb_buffer_pool_read_ahead_evicted": true,
   161  	"Innodb_buffer_pool_read_ahead_rnd":     true,
   162  	"Innodb_buffer_pool_read_requests":      true,
   163  	"Innodb_buffer_pool_write_requests":     true,
   164  	"Innodb_buffer_pool_reads":              true,
   165  	"Innodb_buffer_pool_wait_free":          true,
   166  	"Innodb_deadlocks":                      true,
   167  	"Qcache_hits":                           true,
   168  	"Qcache_lowmem_prunes":                  true,
   169  	"Qcache_inserts":                        true,
   170  	"Qcache_not_cached":                     true,
   171  	"Qcache_queries_in_cache":               true,
   172  	"Qcache_free_memory":                    true,
   173  	"Qcache_free_blocks":                    true,
   174  	"Qcache_total_blocks":                   true,
   175  	"Key_blocks_unused":                     true,
   176  	"Key_blocks_used":                       true,
   177  	"Key_blocks_not_flushed":                true,
   178  	"Key_read_requests":                     true,
   179  	"Key_write_requests":                    true,
   180  	"Key_reads":                             true,
   181  	"Key_writes":                            true,
   182  	"Open_files":                            true,
   183  	"Opened_files":                          true,
   184  	"Binlog_stmt_cache_disk_use":            true,
   185  	"Binlog_stmt_cache_use":                 true,
   186  	"Connection_errors_accept":              true,
   187  	"Connection_errors_internal":            true,
   188  	"Connection_errors_max_connections":     true,
   189  	"Connection_errors_peer_address":        true,
   190  	"Connection_errors_select":              true,
   191  	"Connection_errors_tcpwrap":             true,
   192  	"Com_delete":                            true,
   193  	"Com_insert":                            true,
   194  	"Com_select":                            true,
   195  	"Com_update":                            true,
   196  	"Com_replace":                           true,
   197  	"Opened_tables":                         true,
   198  	"Open_tables":                           true,
   199  	"wsrep_local_recv_queue":                true,
   200  	"wsrep_local_send_queue":                true,
   201  	"wsrep_received":                        true,
   202  	"wsrep_replicated":                      true,
   203  	"wsrep_received_bytes":                  true,
   204  	"wsrep_replicated_bytes":                true,
   205  	"wsrep_local_bf_aborts":                 true,
   206  	"wsrep_local_cert_failures":             true,
   207  	"wsrep_flow_control_paused_ns":          true,
   208  	"wsrep_cluster_weight":                  true,
   209  	"wsrep_cluster_size":                    true,
   210  	"wsrep_local_state":                     true,
   211  	"wsrep_open_transactions":               true,
   212  	"wsrep_thread_count":                    true,
   213  	"wsrep_connected":                       true,
   214  	"wsrep_ready":                           true,
   215  	"wsrep_cluster_status":                  true,
   216  }