diff --git a/node/monitoring/monitoring.go b/node/monitoring/monitoring.go index 8ed7c4a..38c155d 100644 --- a/node/monitoring/monitoring.go +++ b/node/monitoring/monitoring.go @@ -117,6 +117,6 @@ func WritePromtailConfig(filePath string, lokiIP string, lokiPort string, host s } // GetGrafanaUrl returns the URL of the Grafana dashboard. -func GetGrafanaUrl(monitoringHostIP string) string { +func GetGrafanaURL(monitoringHostIP string) string { return fmt.Sprintf("http://%s:%d/dashboards", monitoringHostIP, constants.AvalanchegoGrafanaPort) } diff --git a/node/ssh.go b/node/ssh.go index b6352b2..14c2399 100644 --- a/node/ssh.go +++ b/node/ssh.go @@ -319,6 +319,21 @@ func (h *Node) RegisterWithMonitoring(targets []Node, chainID string) error { wg.Wait() if wgResults.HasErrors() { return wgResults.Error() + } + // provide dashboards for targets + tmpdir, err := os.MkdirTemp("", constants.ServiceGrafana) + if err != nil { + return err + } + defer os.RemoveAll(tmpdir) + if err := monitoring.Setup(tmpdir); err != nil { + return err + } + if err := h.RunSSHSetupMonitoringFolders(); err != nil { + return err + } + if err := h.RunSSHCopyMonitoringDashboards(tmpdir); err != nil { + } avalancheGoPorts, machinePorts, ltPorts := getPrometheusTargets(targets) h.Logger.Infof("avalancheGoPorts: %v, machinePorts: %v, ltPorts: %v", avalancheGoPorts, machinePorts, ltPorts) @@ -338,3 +353,31 @@ func (h *Node) RegisterWithMonitoring(targets []Node, chainID string) error { return nil } + +func (h *Node) RunSSHCopyMonitoringDashboards(monitoringDashboardPath string) error { + // TODO: download dashboards from github instead + remoteDashboardsPath := utils.GetRemoteComposeServicePath("grafana", "dashboards") + if !utils.DirectoryExists(monitoringDashboardPath) { + return fmt.Errorf("%s does not exist", monitoringDashboardPath) + } + if err := h.MkdirAll(remoteDashboardsPath, constants.SSHFileOpsTimeout); err != nil { + return err + } + dashboards, err := os.ReadDir(monitoringDashboardPath) + if err != nil { + return err + } + for _, dashboard := range dashboards { + if err := h.Upload( + filepath.Join(monitoringDashboardPath, dashboard.Name()), + filepath.Join(remoteDashboardsPath, dashboard.Name()), + constants.SSHFileOpsTimeout, + ); err != nil { + return err + } + } + if composeFileExists, err := h.FileExists(utils.GetRemoteComposeFile()); err == nil && composeFileExists { + return h.RestartDockerComposeService(utils.GetRemoteComposeFile(), constants.ServiceGrafana, constants.SSHScriptTimeout) + } + return nil +} diff --git a/utils/file.go b/utils/file.go index 1b67cbe..4dec430 100644 --- a/utils/file.go +++ b/utils/file.go @@ -18,6 +18,18 @@ func FileExists(filename string) bool { return !info.IsDir() } +// DirectoryExists checks if a directory exists. +// +// dirName: the name of the directory to check. +// bool: returns true if the directory exists, false otherwise. +func DirectoryExists(dirName string) bool { + info, err := os.Stat(dirName) + if os.IsNotExist(err) { + return false + } + return info.IsDir() +} + // ExpandHome expands ~ symbol to home directory func ExpandHome(path string) string { if path == "" {