+func getFieldValueStr(line, substr string) string {
+ if idx := strings.Index(line, substr); idx != -1 {
+ return line[idx+len(substr):]
+ }
+ return ""
+}
+
+func getUint32FromString(s string) uint32 {
+ if val, err := strconv.ParseUint(s, 10, 32); err == nil {
+ return uint32(val)
+ }
+ return 0
+}
+
+func getUint64FromString(s string) uint64 {
+ if val, err := strconv.ParseUint(s, 10, 64); err == nil {
+ return uint64(val)
+ }
+ return 0
+}
+
+func getFloatFromString(s string, bitSize int) float64 {
+ if val, err := strconv.ParseFloat(s, bitSize); err == nil {
+ return val
+ }
+ return 0
+}
+
+func getFloat64FromString(s string) float64 {
+ return getFloatFromString(s, 64)
+}
+
+func getFloat32FromString(s string) float32 {
+ return float32(getFloatFromString(s, 32))
+}
+
+func getValueString(values string, key string) string {
+ slice := strings.Split(values, ",")
+ for _, s := range slice {
+ if lineContains(s, key) {
+ return getFieldValueStr(s, key)
+ }
+ }
+ return ""
+}
+
+func getCommandstatsValues(values string) (string, string, string) {
+ calls := getValueString(values, "calls=")
+ usec := getValueString(values, "usec=")
+ usecPerCall := getValueString(values, "usec_per_call=")
+ return calls, usec, usecPerCall
+}
+
+func updateCommandstatsValues(i interface{}, values string) {
+ stype := reflect.ValueOf(i).Elem()
+ callsStr, usecStr, usecPerCallStr := getCommandstatsValues(values)
+
+ callsField := stype.FieldByName("Calls")
+ callsField.Set(reflect.ValueOf(getUint32FromString(callsStr)))
+
+ usecField := stype.FieldByName("Usec")
+ usecField.Set(reflect.ValueOf(getUint32FromString(usecStr)))
+
+ usecPerCallField := stype.FieldByName("UsecPerCall")
+ usecPerCallField.Set(reflect.ValueOf(getFloat32FromString(usecPerCallStr)))
+}
+
+func getKeyspaceValues(values string) (string, string, string) {
+ keys := getValueString(values, "keys=")
+ expires := getValueString(values, "expires=")
+ avgttl := getValueString(values, "avg_ttl=")
+ return keys, expires, avgttl
+}
+
+func updateKeyspaceValues(i interface{}, values string) {
+ stype := reflect.ValueOf(i).Elem()
+ keysStr, expiresStr, avgttlStr := getKeyspaceValues(values)
+
+ keysField := stype.FieldByName("Keys")
+ keysField.Set(reflect.ValueOf(getUint32FromString(keysStr)))
+
+ expiresField := stype.FieldByName("Expires")
+ expiresField.Set(reflect.ValueOf(getUint32FromString(expiresStr)))
+
+ avgttlField := stype.FieldByName("AvgTtl")
+ avgttlField.Set(reflect.ValueOf(getUint32FromString(avgttlStr)))
+}
+
+func updateServerInfoFields(config ConfigInfo, info *DbInfo) {
+ if value, ok := config["uptime_in_days"]; ok {
+ info.Fields.Server.UptimeInDays = getUint32FromString(value)
+ }
+}
+
+func updateClientInfoFields(config ConfigInfo, info *DbInfo) {
+ if value, ok := config["connected_clients"]; ok {
+ info.Fields.Clients.ConnectedClients = getUint32FromString(value)
+ }
+ if value, ok := config["client_recent_max_input_buffer"]; ok {
+ info.Fields.Clients.ClientRecentMaxInputBuffer = getUint32FromString(value)
+ }
+ if value, ok := config["client_recent_max_output_buffer"]; ok {
+ info.Fields.Clients.ClientRecentMaxOutputBuffer = getUint32FromString(value)
+ }
+}
+
+func updateMemoryInfoFields(config ConfigInfo, info *DbInfo) {
+ if value, ok := config["used_memory"]; ok {
+ info.Fields.Memory.UsedMemory = getUint64FromString(value)
+ }
+ if value, ok := config["used_memory_human"]; ok {
+ info.Fields.Memory.UsedMemoryHuman = value
+ }
+ if value, ok := config["used_memory_rss"]; ok {
+ info.Fields.Memory.UsedMemoryRss = getUint64FromString(value)
+ }
+ if value, ok := config["used_memory_rss_human"]; ok {
+ info.Fields.Memory.UsedMemoryRssHuman = value
+ }
+ if value, ok := config["used_memory_peak"]; ok {
+ info.Fields.Memory.UsedMemoryPeak = getUint64FromString(value)
+ }
+ if value, ok := config["used_memory_peak_human"]; ok {
+ info.Fields.Memory.UsedMemoryPeakHuman = value
+ }
+ if value, ok := config["used_memory_peak_perc"]; ok {
+ info.Fields.Memory.UsedMemoryPeakPerc = value
+ }
+ if value, ok := config["mem_fragmentation_ratio"]; ok {
+ info.Fields.Memory.MemFragmentationRatio = getFloat32FromString(value)
+ }
+ if value, ok := config["mem_fragmentation_bytes"]; ok {
+ info.Fields.Memory.MemFragmentationBytes = getUint32FromString(value)
+ }
+}
+
+func updateStatsInfoFields(config ConfigInfo, info *DbInfo) {
+ if value, ok := config["total_connections_received"]; ok {
+ info.Fields.Stats.TotalConnectionsReceived = getUint32FromString(value)
+ }
+ if value, ok := config["total_commands_processed"]; ok {
+ info.Fields.Stats.TotalCommandsProcessed = getUint32FromString(value)
+ }
+ if value, ok := config["sync_full"]; ok {
+ info.Fields.Stats.SyncFull = getUint32FromString(value)
+ }
+ if value, ok := config["sync_partial_ok"]; ok {
+ info.Fields.Stats.SyncPartialOk = getUint32FromString(value)
+ }
+ if value, ok := config["sync_partial_err"]; ok {
+ info.Fields.Stats.SyncPartialErr = getUint32FromString(value)
+ }
+ if value, ok := config["pubsub_channels"]; ok {
+ info.Fields.Stats.PubsubChannels = getUint32FromString(value)
+ }
+}
+
+func updateCpuInfoFields(config ConfigInfo, info *DbInfo) {
+ if value, ok := config["used_cpu_sys"]; ok {
+ info.Fields.Cpu.UsedCpuSys = getFloat64FromString(value)
+ }
+ if value, ok := config["used_cpu_user"]; ok {
+ info.Fields.Cpu.UsedCpuUser = getFloat64FromString(value)
+ }
+}
+
+func updateCommandstatsInfoFields(config ConfigInfo, info *DbInfo) {
+ if values, ok := config["cmdstat_replconf"]; ok {
+ updateCommandstatsValues(&info.Fields.Commandstats.CmdstatReplconf, values)
+ }
+ if values, ok := config["cmdstat_keys"]; ok {
+ updateCommandstatsValues(&info.Fields.Commandstats.CmdstatKeys, values)
+ }
+ if values, ok := config["cmdstat_role"]; ok {
+ updateCommandstatsValues(&info.Fields.Commandstats.CmdstatRole, values)
+ }
+ if values, ok := config["cmdstat_psync"]; ok {
+ updateCommandstatsValues(&info.Fields.Commandstats.CmdstatPsync, values)
+ }
+ if values, ok := config["cmdstat_mset"]; ok {
+ updateCommandstatsValues(&info.Fields.Commandstats.CmdstatMset, values)
+ }
+ if values, ok := config["cmdstat_publish"]; ok {
+ updateCommandstatsValues(&info.Fields.Commandstats.CmdstatPublish, values)
+ }
+ if values, ok := config["cmdstat_info"]; ok {
+ updateCommandstatsValues(&info.Fields.Commandstats.CmdstatInfo, values)
+ }
+ if values, ok := config["cmdstat_ping"]; ok {
+ updateCommandstatsValues(&info.Fields.Commandstats.CmdstatPing, values)
+ }
+ if values, ok := config["cmdstat_client"]; ok {
+ updateCommandstatsValues(&info.Fields.Commandstats.CmdstatClient, values)
+ }
+ if values, ok := config["cmdstat_command"]; ok {
+ updateCommandstatsValues(&info.Fields.Commandstats.CmdstatCommand, values)
+ }
+ if values, ok := config["cmdstat_subscribe"]; ok {
+ updateCommandstatsValues(&info.Fields.Commandstats.CmdstatSubscribe, values)
+ }
+ if values, ok := config["cmdstat_monitor"]; ok {
+ updateCommandstatsValues(&info.Fields.Commandstats.CmdstatMonitor, values)
+ }
+ if values, ok := config["cmdstat_config"]; ok {
+ updateCommandstatsValues(&info.Fields.Commandstats.CmdstatConfig, values)
+ }
+ if values, ok := config["cmdstat_slaveof"]; ok {
+ updateCommandstatsValues(&info.Fields.Commandstats.CmdstatSlaveof, values)
+ }
+}
+
+func updateKeyspaceInfoFields(config ConfigInfo, info *DbInfo) {
+ if values, ok := config["db0"]; ok {
+ updateKeyspaceValues(&info.Fields.Keyspace.Db, values)
+ }
+}
+
+func getConfigInfo(input []string) ConfigInfo {
+ config := ConfigInfo{}