github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/scout/data/scoutdata/scout.go (about) 1 // Copyright (c) 2021, R.I. Pienaar and the Choria Project contributors 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 5 package scoutdata 6 7 import ( 8 "context" 9 "fmt" 10 "time" 11 12 "github.com/choria-io/go-choria/aagent/watchers/nagioswatcher" 13 "github.com/choria-io/go-choria/build" 14 "github.com/choria-io/go-choria/providers/agent/mcorpc/ddl/common" 15 "github.com/choria-io/go-choria/providers/data" 16 "github.com/choria-io/go-choria/providers/data/ddl" 17 "github.com/choria-io/go-choria/providers/data/plugin" 18 "github.com/choria-io/go-choria/server/agents" 19 ) 20 21 type ScoutData struct{} 22 23 func ChoriaPlugin() *plugin.DataPlugin { 24 return plugin.NewDataPlugin("scout", New) 25 } 26 27 func New(_ data.Framework) (data.Plugin, error) { 28 return &ScoutData{}, nil 29 } 30 31 func (s *ScoutData) Run(_ context.Context, q data.Query, si agents.ServerInfoSource) (map[string]data.OutputItem, error) { 32 query, ok := q.(string) 33 if !ok { 34 return nil, fmt.Errorf("could not parse query as a string") 35 } 36 37 machines, err := si.MachinesStatus() 38 if err != nil { 39 return nil, fmt.Errorf("could not retrieve machine status: %s", err) 40 } 41 42 result := make(map[string]data.OutputItem) 43 for _, m := range machines { 44 if m.Scout && m.Name == query { 45 result["name"] = m.Name 46 result["version"] = m.Version 47 result["state"] = m.State 48 result["path"] = m.Path 49 result["id"] = m.ID 50 result["start_time"] = m.StartTimeUTC 51 result["uptime"] = time.Now().Unix() - m.StartTimeUTC 52 result["history"] = []string{} 53 hist := []string{} 54 n, ok := m.ScoutState.(*nagioswatcher.StateNotification) 55 if ok { 56 for _, h := range n.History { 57 hist = append(hist, nagioswatcher.StateName(h.Status)) 58 } 59 } 60 result["history"] = hist 61 62 break 63 } 64 } 65 66 return result, nil 67 } 68 69 func (s *ScoutData) DLL() (*ddl.DDL, error) { 70 sddl := &ddl.DDL{ 71 Metadata: ddl.Metadata{ 72 License: "Apache-2.0", 73 Author: "R.I.Pienaar <rip@devco.net>", 74 Timeout: 1, 75 Name: "scout", 76 Version: build.Version, 77 URL: "https://choria.io", 78 Description: "Data about a specific Scout check", 79 Provider: "golang", 80 }, 81 Query: &common.InputItem{ 82 Prompt: "Check", 83 Description: "The Scout check to retrieve data for", 84 Type: common.InputTypeString, 85 Optional: false, 86 Validation: "^[a-zA-Z][a-zA-Z0-9_-]+$", 87 MaxLength: 50, 88 }, 89 Output: map[string]*common.OutputItem{ 90 "name": { 91 Description: "The name of the Scout check", 92 DisplayAs: "Name", 93 Type: common.OutputTypeString, 94 }, 95 "version": { 96 Description: "The version of the Scout check state machine", 97 DisplayAs: "Version", 98 Type: common.OutputTypeString, 99 }, 100 "state": { 101 Description: "The state the Scout check is in", 102 DisplayAs: "State", 103 Type: common.OutputTypeString, 104 }, 105 "path": { 106 Description: "The path on disk where the Scout check is stored", 107 DisplayAs: "Path", 108 Type: common.OutputTypeString, 109 }, 110 "id": { 111 Description: "The unique ID of the running state machine", 112 DisplayAs: "ID", 113 Type: common.OutputTypeString, 114 }, 115 "start_time": { 116 Description: "The time the check started in UTC", 117 DisplayAs: "Start Time", 118 Type: common.OutputTypeInteger, 119 }, 120 "uptime": { 121 Description: "The time the check has been running in seconds", 122 DisplayAs: "Uptime", 123 Type: common.OutputTypeInteger, 124 }, 125 "history": { 126 Description: "Recent past states of the check", 127 DisplayAs: "History", 128 Type: common.OutputTypeHash, 129 }, 130 }, 131 } 132 133 return sddl, nil 134 }