diff --git a/agent/agent.go b/agent/agent.go index b32cfcb..a657496 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -14,6 +14,7 @@ import ( _ "flashcat.cloud/categraf/inputs/docker" _ "flashcat.cloud/categraf/inputs/elasticsearch" _ "flashcat.cloud/categraf/inputs/exec" + _ "flashcat.cloud/categraf/inputs/greenplum" _ "flashcat.cloud/categraf/inputs/http_response" _ "flashcat.cloud/categraf/inputs/ipvs" _ "flashcat.cloud/categraf/inputs/jenkins" diff --git a/conf/input.greenplum/greenplum.toml b/conf/input.greenplum/greenplum.toml new file mode 100644 index 0000000..d078a9c --- /dev/null +++ b/conf/input.greenplum/greenplum.toml @@ -0,0 +1,3 @@ +# # collect interval +# interval = 15 + diff --git a/inputs/greenplum/greenplum.go b/inputs/greenplum/greenplum.go new file mode 100644 index 0000000..b66254a --- /dev/null +++ b/inputs/greenplum/greenplum.go @@ -0,0 +1,67 @@ +package greenplum + +import ( + "log" + "strings" + "os/exec" + + "flashcat.cloud/categraf/config" + "flashcat.cloud/categraf/inputs" + "flashcat.cloud/categraf/types" +) + +const inputName = "greenplum" + +type Greenplum struct { + config.PluginConfig +} + +func init() { + inputs.Add(inputName, func() inputs.Input { + return &Greenplum{} + }) +} + +func (ins *Greenplum) Gather(slist *types.SampleList) { + var tags = map[string]string{} + bin, err := exec.LookPath("gpstate") + if err != nil { + return + } + + out, err := exec.Command(bin, "-m").Output() + if err != nil { + return + } + stringOut := string(out) + const splitHeader string = "Mirror Datadir Port Status Data Status" + const lastHeader string = "gpadmin-[INFO]:---" + stateValue := stringOut[(strings.Index(stringOut, splitHeader) + len(splitHeader)) : strings.LastIndex(stringOut, lastHeader)-47] + stateValue = strings.TrimSpace(stateValue) + gpstate := strings.Fields(stateValue) + if len(gpstate)%7 != 0 { + log.Println("E! failed to parse gpstate -m output: %v", gpstate) + return + } + line := len(gpstate) / 7 + for i := 0; i < line; i++ { + gptags := map[string]string{ + "Mirror": gpstate[i*7+2], + "Datadir": gpstate[i*7+3], + "Port": gpstate[i*7+4], + } + for tag, value := range tags { + gptags[tag] = value + } + var status int32 = 0 + var dataStatus int32 = 0 + if gpstate[i*7+5] == "Passive" { + status = 1 + } + if gpstate[i*7+6] == "Synchronized" { + dataStatus = 1 + } + slist.PushSample(inputName, "Status", status, gptags) + slist.PushSample(inputName, "Data_Status", dataStatus, gptags) + } +} \ No newline at end of file