| 1 | package exporter |
| 2 | |
| 3 | import ( |
| 4 | "regexp" |
| 5 | "strconv" |
| 6 | "strings" |
| 7 | |
| 8 | "github.com/gomodule/redigo/redis" |
| 9 | "github.com/prometheus/client_golang/prometheus" |
| 10 | log "github.com/sirupsen/logrus" |
| 11 | ) |
| 12 | |
| 13 | var reSentinelMaster = regexp.MustCompile(`^master\d+`) |
| 14 | |
| 15 | func (e *Exporter) handleMetricsSentinel(ch chan<- prometheus.Metric, fieldKey string, fieldValue string) { |
| 16 | switch fieldKey { |
| 17 | case |
| 18 | "sentinel_masters", |
| 19 | "sentinel_tilt", |
| 20 | "sentinel_running_scripts", |
| 21 | "sentinel_scripts_queue_length", |
| 22 | "sentinel_simulate_failure_flags": |
| 23 | val, _ := strconv.Atoi(fieldValue) |
| 24 | e.registerConstMetricGauge(ch, fieldKey, float64(val)) |
| 25 | return |
| 26 | } |
| 27 | |
| 28 | if masterName, masterStatus, masterAddress, masterSlaves, masterSentinels, ok := parseSentinelMasterString(fieldKey, fieldValue); ok { |
| 29 | masterStatusNum := 0.0 |
| 30 | if masterStatus == "ok" { |
| 31 | masterStatusNum = 1 |
| 32 | } |
| 33 | e.registerConstMetricGauge(ch, "sentinel_master_status", masterStatusNum, masterName, masterAddress, masterStatus) |
| 34 | e.registerConstMetricGauge(ch, "sentinel_master_slaves", masterSlaves, masterName, masterAddress) |
| 35 | e.registerConstMetricGauge(ch, "sentinel_master_sentinels", masterSentinels, masterName, masterAddress) |
| 36 | return |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func (e *Exporter) extractSentinelMetrics(ch chan<- prometheus.Metric, c redis.Conn) { |
| 41 | masterDetails, err := redis.Values(doRedisCmd(c, "SENTINEL", "MASTERS")) |
| 42 | if err != nil { |
| 43 | log.Debugf("Error getting sentinel master details %s:", err) |
| 44 | return |
| 45 | } |
| 46 | |
| 47 | log.Debugf("Sentinel master details: %#v", masterDetails) |
| 48 | |
| 49 | for _, masterDetail := range masterDetails { |
| 50 | masterDetailMap, err := redis.StringMap(masterDetail, nil) |
| 51 | if err != nil { |
| 52 | log.Debugf("Error getting masterDetailmap from masterDetail: %s, err: %s", masterDetail, err) |
| 53 | continue |
| 54 | } |
| 55 | |
| 56 | masterName, ok := masterDetailMap["name"] |
| 57 | if !ok { |
| 58 | continue |
| 59 | } |
| 60 | |
| 61 | masterIp, ok := masterDetailMap["ip"] |
| 62 | if !ok { |
| 63 | continue |
| 64 | } |
| 65 | |
| 66 | masterPort, ok := masterDetailMap["port"] |
| 67 | if !ok { |
| 68 | continue |
| 69 | } |
| 70 | masterAddr := masterIp + ":" + masterPort |
| 71 | |
| 72 | masterCkquorumMsg, err := redis.String(doRedisCmd(c, "SENTINEL", "CKQUORUM", masterName)) |
| 73 | log.Debugf("Sentinel ckquorum status for master %s: %s %s", masterName, masterCkquorumMsg, err) |
| 74 | masterCkquorumStatus := 1 |
| 75 | if err != nil { |
| 76 | masterCkquorumStatus = 0 |
| 77 | masterCkquorumMsg = err.Error() |
| 78 | } |
| 79 | e.registerConstMetricGauge(ch, "sentinel_master_ckquorum_status", float64(masterCkquorumStatus), masterName, masterCkquorumMsg) |
| 80 | |
| 81 | masterCkquorum, _ := strconv.ParseFloat(masterDetailMap["quorum"], 64) |
| 82 | masterFailoverTimeout, _ := strconv.ParseFloat(masterDetailMap["failover-timeout"], 64) |
| 83 | masterParallelSyncs, _ := strconv.ParseFloat(masterDetailMap["parallel-syncs"], 64) |
| 84 | masterDownAfterMs, _ := strconv.ParseFloat(masterDetailMap["down-after-milliseconds"], 64) |
| 85 | masterConfigEpoch, _ := strconv.ParseFloat(masterDetailMap["config-epoch"], 64) |
| 86 | masterLastOkPingReplyMs, _ := strconv.ParseFloat(masterDetailMap["last-ok-ping-reply"], 64) |
| 87 | masterLastOkPingReplySeconds := masterLastOkPingReplyMs / 1000.0 |
| 88 | |
| 89 | e.registerConstMetricGauge(ch, "sentinel_master_setting_ckquorum", masterCkquorum, masterName, masterAddr) |
| 90 | e.registerConstMetricGauge(ch, "sentinel_master_setting_failover_timeout", masterFailoverTimeout, masterName, masterAddr) |
| 91 | e.registerConstMetricGauge(ch, "sentinel_master_setting_parallel_syncs", masterParallelSyncs, masterName, masterAddr) |
| 92 | e.registerConstMetricGauge(ch, "sentinel_master_setting_down_after_milliseconds", masterDownAfterMs, masterName, masterAddr) |
| 93 | e.registerConstMetricGauge(ch, "sentinel_master_config_epoch", masterConfigEpoch, masterName, masterAddr) |
| 94 | e.registerConstMetricGauge(ch, "sentinel_master_last_ok_ping_reply_seconds", masterLastOkPingReplySeconds, masterName, masterAddr) |
| 95 | |
| 96 | sentinelDetails, _ := redis.Values(doRedisCmd(c, "SENTINEL", "SENTINELS", masterName)) |
| 97 | log.Debugf("Sentinel details for master %s: %s", masterName, sentinelDetails) |
| 98 | e.processSentinelSentinels(ch, sentinelDetails, masterName, masterAddr) |
| 99 | |
| 100 | slaveDetails, _ := redis.Values(doRedisCmd(c, "SENTINEL", "SLAVES", masterName)) |
| 101 | log.Debugf("Slave details for master %s: %s", masterName, slaveDetails) |
| 102 | e.processSentinelSlaves(ch, slaveDetails, masterName, masterAddr) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | func (e *Exporter) extractSentinelConfig(ch chan<- prometheus.Metric, c redis.Conn) { |
| 107 | if !e.options.InclConfigMetrics { |
| 108 | return |
| 109 | } |
| 110 | sentinelConfig, err := redis.StringMap(doRedisCmd(c, "SENTINEL", "config", "get", "*")) |
| 111 | if err != nil { |
| 112 | log.Errorf("Error getting sentinel config: %s", err) |
| 113 | return |
| 114 | } |
| 115 | |
| 116 | log.Debugf("Sentinel config: %v", sentinelConfig) |
| 117 | |
| 118 | for strKey, strVal := range sentinelConfig { |
| 119 | e.registerConstMetricGauge(ch, "sentinel_config_key_value", 1.0, strKey, strVal) |
| 120 | if val, err := strconv.ParseFloat(strVal, 64); err == nil { |
| 121 | e.registerConstMetricGauge(ch, "sentinel_config_value", val, strKey) |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | func (e *Exporter) processSentinelSentinels(ch chan<- prometheus.Metric, sentinelDetails []any, labels ...string) { |
| 127 | |
| 128 | // If we are here then this master is in ok state |
| 129 | masterOkSentinels := 1 |
| 130 | hasMasterLabels := len(labels) >= 2 |
| 131 | masterName := "" |
| 132 | masterAddr := "" |
| 133 | if hasMasterLabels { |
| 134 | masterName = labels[0] |
| 135 | masterAddr = labels[1] |
| 136 | } |
| 137 | |
| 138 | for _, sentinelDetail := range sentinelDetails { |
| 139 | sentinelDetailMap, err := redis.StringMap(sentinelDetail, nil) |
| 140 | if err != nil { |
| 141 | log.Debugf("Error getting sentinelDetailMap from sentinelDetail: %s, err: %s", sentinelDetail, err) |
| 142 | continue |
| 143 | } |
| 144 | |
| 145 | name := "" |
| 146 | if v, ok := sentinelDetailMap["name"]; ok { |
| 147 | name = v |
| 148 | } |
| 149 | ip := "" |
| 150 | if v, ok := sentinelDetailMap["ip"]; ok { |
| 151 | ip = v |
| 152 | } |
| 153 | port := "" |
| 154 | if v, ok := sentinelDetailMap["port"]; ok { |
| 155 | port = v |
| 156 | } |
| 157 | runid := "" |
| 158 | if v, ok := sentinelDetailMap["runid"]; ok { |
| 159 | runid = v |
| 160 | } |
| 161 | flags := "" |
| 162 | flagsFound := false |
| 163 | if v, ok := sentinelDetailMap["flags"]; ok { |
| 164 | flags = v |
| 165 | flagsFound = true |
| 166 | } |
| 167 | if e.options.InclSentinelPeerInfo && hasMasterLabels { |
| 168 | e.registerConstMetricGauge(ch, "sentinel_peer_info", 1, masterName, masterAddr, name, ip, port, runid, flags) |
| 169 | } |
| 170 | |
| 171 | if !flagsFound { |
| 172 | continue |
| 173 | } |
| 174 | if strings.Contains(flags, "o_down") { |
| 175 | continue |
| 176 | } |
| 177 | if strings.Contains(flags, "s_down") { |
| 178 | continue |
| 179 | } |
| 180 | masterOkSentinels = masterOkSentinels + 1 |
| 181 | } |
| 182 | e.registerConstMetricGauge(ch, "sentinel_master_ok_sentinels", float64(masterOkSentinels), labels...) |
| 183 | } |
| 184 | |
| 185 | func (e *Exporter) processSentinelSlaves(ch chan<- prometheus.Metric, slaveDetails []any, labels ...string) { |
| 186 | masterOkSlaves := 0 |
| 187 | for _, slaveDetail := range slaveDetails { |
| 188 | slaveDetailMap, err := redis.StringMap(slaveDetail, nil) |
| 189 | if err != nil { |
| 190 | log.Debugf("Error getting slavedetailMap from slaveDetail: %s, err: %s", slaveDetail, err) |
| 191 | continue |
| 192 | } |
| 193 | |
| 194 | slaveFlags, ok := slaveDetailMap["flags"] |
| 195 | if !ok { |
| 196 | continue |
| 197 | } |
| 198 | if strings.Contains(slaveFlags, "o_down") { |
| 199 | continue |
| 200 | } |
| 201 | if strings.Contains(slaveFlags, "s_down") { |
| 202 | continue |
| 203 | } |
| 204 | masterOkSlaves = masterOkSlaves + 1 |
| 205 | } |
| 206 | if len(labels) >= 2 { |
| 207 | e.registerConstMetricGauge(ch, "sentinel_master_ok_slaves", float64(masterOkSlaves), labels...) |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | /* |
| 212 | valid examples: |
| 213 | |
| 214 | master0:name=user03,status=sdown,address=192.169.2.52:6381,slaves=1,sentinels=5 |
| 215 | master1:name=user02,status=ok,address=192.169.2.54:6380,slaves=1,sentinels=5 |
| 216 | */ |
| 217 | func parseSentinelMasterString(master string, masterInfo string) (masterName string, masterStatus string, masterAddr string, masterSlaves float64, masterSentinels float64, ok bool) { |
| 218 | ok = false |
| 219 | if !reSentinelMaster.MatchString(master) { |
| 220 | return |
| 221 | } |
| 222 | matchedMasterInfo := make(map[string]string) |
| 223 | for kvPart := range strings.SplitSeq(masterInfo, ",") { |
| 224 | x := strings.Split(kvPart, "=") |
| 225 | if len(x) != 2 { |
| 226 | log.Errorf("Invalid format for sentinel's master string, got: %s", kvPart) |
| 227 | continue |
| 228 | } |
| 229 | matchedMasterInfo[x[0]] = x[1] |
| 230 | } |
| 231 | |
| 232 | masterName = matchedMasterInfo["name"] |
| 233 | masterStatus = matchedMasterInfo["status"] |
| 234 | masterAddr = matchedMasterInfo["address"] |
| 235 | masterSlaves, err := strconv.ParseFloat(matchedMasterInfo["slaves"], 64) |
| 236 | if err != nil { |
| 237 | log.Debugf("parseSentinelMasterString(): couldn't parse slaves value, got: %s, err: %s", matchedMasterInfo["slaves"], err) |
| 238 | return |
| 239 | } |
| 240 | masterSentinels, err = strconv.ParseFloat(matchedMasterInfo["sentinels"], 64) |
| 241 | if err != nil { |
| 242 | log.Debugf("parseSentinelMasterString(): couldn't parse sentinels value, got: %s, err: %s", matchedMasterInfo["sentinels"], err) |
| 243 | return |
| 244 | } |
| 245 | ok = true |
| 246 | |
| 247 | return |
| 248 | } |