github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/providers/data/golang/choriadata/choria.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 choriadata
     6  
     7  import (
     8  	"context"
     9  	"os"
    10  	"runtime"
    11  
    12  	"github.com/choria-io/go-choria/build"
    13  	"github.com/choria-io/go-choria/providers/agent/mcorpc/ddl/common"
    14  	"github.com/choria-io/go-choria/providers/data"
    15  	"github.com/choria-io/go-choria/providers/data/ddl"
    16  	"github.com/choria-io/go-choria/providers/data/plugin"
    17  	"github.com/choria-io/go-choria/server/agents"
    18  )
    19  
    20  type ChoriaData struct{}
    21  
    22  func ChoriaPlugin() *plugin.DataPlugin {
    23  	return plugin.NewDataPlugin("choria", New)
    24  }
    25  
    26  func New(_ data.Framework) (data.Plugin, error) {
    27  	return &ChoriaData{}, nil
    28  }
    29  
    30  func (s *ChoriaData) Run(_ context.Context, q data.Query, si agents.ServerInfoSource) (map[string]data.OutputItem, error) {
    31  	machines, _ := si.MachinesStatus()
    32  	stats := si.Stats()
    33  	classes := si.Classes()
    34  	agents := si.KnownAgents()
    35  	machineNames := []string{}
    36  
    37  	for _, m := range machines {
    38  		machineNames = append(machineNames, m.Name)
    39  	}
    40  
    41  	result := map[string]data.OutputItem{
    42  		"agents":           agents,
    43  		"agents_count":     len(agents),
    44  		"built":            si.BuildInfo().BuildDate(),
    45  		"classes":          classes,
    46  		"classes_count":    len(classes),
    47  		"commit":           si.BuildInfo().SHA(),
    48  		"configfile":       si.ConfigFile(),
    49  		"connected_broker": si.ConnectedServer(),
    50  		"cpus":             runtime.NumCPU(),
    51  		"filtered":         stats.Filtered,
    52  		"go_version":       runtime.Version(),
    53  		"goroutines":       runtime.NumGoroutine(),
    54  		"license":          si.BuildInfo().License(),
    55  		"machines_count":   len(machines),
    56  		"machines":         machineNames,
    57  		"passed":           stats.Passed,
    58  		"pid":              os.Getpid(),
    59  		"provisioning":     si.Provisioning(),
    60  		"replies":          stats.Replies,
    61  		"total":            stats.Total,
    62  		"ttlexpired":       stats.TTLExpired,
    63  		"events":           stats.Events,
    64  		"unvalidated":      stats.Invalid,
    65  		"uptime":           si.UpTime(),
    66  		"validated":        stats.Valid,
    67  		"version":          si.BuildInfo().Version(),
    68  	}
    69  
    70  	return result, nil
    71  }
    72  
    73  func (s *ChoriaData) DLL() (*ddl.DDL, error) {
    74  	sddl := &ddl.DDL{
    75  		Metadata: ddl.Metadata{
    76  			License:     "Apache-2.0",
    77  			Author:      "R.I.Pienaar <rip@devco.net>",
    78  			Timeout:     1,
    79  			Name:        "choria",
    80  			Version:     build.Version,
    81  			URL:         "https://choria.io",
    82  			Description: "Data about a the running Choria instance",
    83  			Provider:    "golang",
    84  		},
    85  		Output: map[string]*common.OutputItem{
    86  			"agents": {
    87  				Description: "Known agents hosted by this server",
    88  				DisplayAs:   "Agents",
    89  				Type:        common.OutputTypeArray,
    90  			},
    91  			"pid": {
    92  				Description: "The process ID of the running process",
    93  				DisplayAs:   "PID",
    94  				Type:        common.OutputTypeInteger,
    95  			},
    96  			"agents_count": {
    97  				Description: "Number of active agents on the node",
    98  				DisplayAs:   "Agents",
    99  				Type:        common.OutputTypeInteger,
   100  			},
   101  			"classes": {
   102  				Description: "List of classes this machine is tagged with",
   103  				DisplayAs:   "Class Names",
   104  				Type:        common.OutputTypeArray,
   105  			},
   106  			"classes_count": {
   107  				Description: "Number of classes this node is tagged with",
   108  				DisplayAs:   "Classes",
   109  				Type:        common.OutputTypeInteger,
   110  			},
   111  			"machines": {
   112  				Description: "The names of running machines",
   113  				DisplayAs:   "Machines Names",
   114  				Type:        common.OutputTypeArray,
   115  			},
   116  			"machines_count": {
   117  				Description: "The number of running Autonomous Agents",
   118  				DisplayAs:   "Machines",
   119  				Type:        common.OutputTypeInteger,
   120  			},
   121  			"configfile": {
   122  				Description: "The path to the running configuration",
   123  				DisplayAs:   "Configuration File",
   124  				Type:        common.OutputTypeString,
   125  			},
   126  			"connected_broker": {
   127  				Description: "The Choria Broker this server is connected to",
   128  				DisplayAs:   "Connected Broker",
   129  				Type:        common.OutputTypeString,
   130  			},
   131  			"provisioning": {
   132  				Description: "If the node is currently in Provisioning mode",
   133  				DisplayAs:   "Provisioning",
   134  				Type:        common.OutputTypeBoolean,
   135  			},
   136  			"uptime": {
   137  				Description: "The time, in seconds, that the server has been up",
   138  				DisplayAs:   "Uptime",
   139  				Type:        common.OutputTypeInteger,
   140  			},
   141  			"total": {
   142  				Description: "The number of messages this server processed",
   143  				DisplayAs:   "Total Messages",
   144  				Type:        common.OutputTypeInteger,
   145  			},
   146  			"validated": {
   147  				Description: "The number of messages this server processed that passed validation",
   148  				DisplayAs:   "Valid Messages",
   149  				Type:        common.OutputTypeInteger,
   150  			},
   151  			"unvalidated": {
   152  				Description: "The number of messages this server processed that did not pass validation",
   153  				DisplayAs:   "Invalid Messages",
   154  				Type:        common.OutputTypeInteger,
   155  			},
   156  			"passed": {
   157  				Description: "The number of messages this server processed that matched filters",
   158  				DisplayAs:   "Passed Messages",
   159  				Type:        common.OutputTypeInteger,
   160  			},
   161  			"filtered": {
   162  				Description: "The number of messages this server processed that did not match filters",
   163  				DisplayAs:   "Filtered Messages",
   164  				Type:        common.OutputTypeInteger,
   165  			},
   166  			"replies": {
   167  				Description: "The number of reply messages this server sent",
   168  				DisplayAs:   "Reply Messages",
   169  				Type:        common.OutputTypeInteger,
   170  			},
   171  			"ttlexpired": {
   172  				Description: "The number of messages this server rejected due to TTL expiration",
   173  				DisplayAs:   "Expired Messages",
   174  				Type:        common.OutputTypeInteger,
   175  			},
   176  			"version": {
   177  				Description: "The running version of the server",
   178  				DisplayAs:   "Version",
   179  				Type:        common.OutputTypeString,
   180  			},
   181  			"go_version": {
   182  				Description: "Version of Go used to build the server",
   183  				DisplayAs:   "Golang",
   184  				Type:        common.OutputTypeString,
   185  			},
   186  			"goroutines": {
   187  				Description: "The number of active Go Routines in the server process",
   188  				DisplayAs:   "Go Routines",
   189  				Type:        common.OutputTypeInteger,
   190  			},
   191  			"cpus": {
   192  				Description: "The number of logical CPUs available to the Go runtime",
   193  				DisplayAs:   "CPUs",
   194  				Type:        common.OutputTypeInteger,
   195  			},
   196  			"license": {
   197  				Description: "The license this binary is released under",
   198  				DisplayAs:   "License",
   199  				Type:        common.OutputTypeString,
   200  			},
   201  			"built": {
   202  				Description: "The time when the build was performed",
   203  				DisplayAs:   "Built",
   204  				Type:        common.OutputTypeString,
   205  			},
   206  			"commit": {
   207  				Description: "The source commit used to build this instance",
   208  				DisplayAs:   "Commit",
   209  				Type:        common.OutputTypeString,
   210  			},
   211  		},
   212  	}
   213  
   214  	return sddl, nil
   215  }