From 7665b074d74b4ae7ec8346eec3e8a40728cdce13 Mon Sep 17 00:00:00 2001 From: Yasen Pramatarov Date: Sun, 13 Oct 2024 23:16:16 +0300 Subject: [PATCH] Adds support for simultaneous multiple agents checks --- CHANGELOG.md | 2 ++ jilo-server.conf | 24 ++++++++++++++----- main.go | 61 +++++++++++++++++++++++++++++++++-------------- meet.db | Bin 12288 -> 12288 bytes 4 files changed, 63 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 75ccd5d..e207f1c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,3 +14,5 @@ All notable changes to this project will be documented in this file. ### Added - Initial version +- Adds support for loading or creating the SQL table +- Adds simultaneous checks for multiple servers and agents diff --git a/jilo-server.conf b/jilo-server.conf index 48c5766..b28a3f3 100644 --- a/jilo-server.conf +++ b/jilo-server.conf @@ -1,9 +1,21 @@ -# endpoint item -item: "https://meet.lindeas.com/jvb" - -# check interval in minutes -check: 5 +servers: + meet.lindeas.com: + agents: + agent1: + endpoint: "https://meet.lindeas.com:8081/jvb" + check_period: 5 + agent2: + endpoint: "https://meet.lindeas.com:8081/jicofo" + check_period: 5 + meet.example.com: + agents: + agent1: + endpoint: "https://meet.example.com:8081/jvb" + check_period: 10 + agent2: + endpoint: "https://meet.example.com:8081/jicofo" + check_period: 10 # database to store in -database: "./meet.db" +database_path: "./meet.db" diff --git a/main.go b/main.go index 540a87c..04668ce 100644 --- a/main.go +++ b/main.go @@ -11,20 +11,22 @@ import ( "gopkg.in/yaml.v2" ) +// Structures +type Agent struct { + Endpoint string `yaml:"endpoint"` + CheckPeriod int `yaml:"check_period"` +} +type Server struct { + Agents map[string]Agent `yaml:"agents"` +} type Config struct { - RemoteEndpoint string `yaml:"item"` - CheckPeriod int `yaml:"check"` - DatabasePath string `yaml:"database"` -} - -var defaultConfig = Config { - RemoteEndpoint: "https://meet.example.com/jvb", - CheckPeriod: 5, - DatabasePath: "./meet.example.com.db", + Servers map[string]Server `yaml:"servers"` + DatabasePath string `yaml:"database_path"` } +// Loading the config file func readConfig(filePath string) Config { - config := defaultConfig + var config Config file, err := ioutil.ReadFile(filePath) if err != nil { @@ -66,6 +68,7 @@ func setupDatabase(dbPath string) (*sql.DB, error) { } func checkEndpoint(endpoint string) (int, int64) { + log.Println("Sending HTTP get request to Jilo agent:", endpoint) start := time.Now() resp, err := http.Get(endpoint) if err != nil { @@ -75,6 +78,7 @@ func checkEndpoint(endpoint string) (int, int64) { defer resp.Body.Close() elapsed := time.Since(start).Milliseconds() + log.Printf("Received response: %d, Time taken: %d ms", resp.StatusCode, elapsed) return resp.StatusCode, elapsed } @@ -85,26 +89,47 @@ func saveData(db *sql.DB, statusCode int, responseTime int64) { } } +// Main routine func main() { - // config file + // First flush all the logs + log.SetFlags(log.LstdFlags | log.Lshortfile) + + // Config file + log.Println("Reading the config file...") config := readConfig("jilo-server.conf") // Connect to or setup the database + log.Println("Initializing the database...") db, err := setupDatabase(config.DatabasePath) if err != nil { log.Fatal("Failed to initialize the database:", err) } defer db.Close() - ticker := time.NewTicker(time.Duration(config.CheckPeriod) * time.Minute) - defer ticker.Stop() - log.Println("Starting endpoint checker...") - for { - statusCode, responseTime := checkEndpoint(config.RemoteEndpoint) - log.Printf("Endpoint check: Status code: %d, Response time: %d ms", statusCode, responseTime) + // Iterate over the servers and agents + for serverName, server := range config.Servers { + for agentName, agent := range server.Agents { + go func(serverName, agentName string, agent Agent) { + // Ticker for the periodic checks + ticker := time.NewTicker(time.Duration(agent.CheckPeriod) * time.Minute) + defer ticker.Stop() - <-ticker.C + for { + log.Printf("Checking agent [%s - %s]: %s", serverName, agentName, agent.Endpoint) + statusCode, responseTime := checkEndpoint(agent.Endpoint) + log.Printf("Agent [%s - %s]: Status code: %d, Response time: %d ms", serverName, agentName, statusCode, responseTime) + + saveData(db, statusCode, responseTime) + + // Sleep until the next tick + <-ticker.C + } + }(serverName, agentName, agent) + } } + + // Prevent the main from exiting + select {} } diff --git a/meet.db b/meet.db index a6613233247b038b9dfa557e2d18b1257e8290a5..110dd8aad978e703c063164d774d4aa03904e150 100644 GIT binary patch delta 192 zcmZojXh@hK&B!`Y#+i|IW5N=C9v1!%2L4C