Adds support for simultaneous multiple agents checks

main
Yasen Pramatarov 2024-10-13 23:16:16 +03:00
parent e26bb3cc0f
commit 7665b074d7
4 changed files with 63 additions and 24 deletions

View File

@ -14,3 +14,5 @@ All notable changes to this project will be documented in this file.
### Added ### Added
- Initial version - Initial version
- Adds support for loading or creating the SQL table
- Adds simultaneous checks for multiple servers and agents

View File

@ -1,9 +1,21 @@
# endpoint item servers:
item: "https://meet.lindeas.com/jvb" meet.lindeas.com:
agents:
# check interval in minutes agent1:
check: 5 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 to store in
database: "./meet.db" database_path: "./meet.db"

59
main.go
View File

@ -11,20 +11,22 @@ import (
"gopkg.in/yaml.v2" "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 { type Config struct {
RemoteEndpoint string `yaml:"item"` Servers map[string]Server `yaml:"servers"`
CheckPeriod int `yaml:"check"` DatabasePath string `yaml:"database_path"`
DatabasePath string `yaml:"database"`
}
var defaultConfig = Config {
RemoteEndpoint: "https://meet.example.com/jvb",
CheckPeriod: 5,
DatabasePath: "./meet.example.com.db",
} }
// Loading the config file
func readConfig(filePath string) Config { func readConfig(filePath string) Config {
config := defaultConfig var config Config
file, err := ioutil.ReadFile(filePath) file, err := ioutil.ReadFile(filePath)
if err != nil { if err != nil {
@ -66,6 +68,7 @@ func setupDatabase(dbPath string) (*sql.DB, error) {
} }
func checkEndpoint(endpoint string) (int, int64) { func checkEndpoint(endpoint string) (int, int64) {
log.Println("Sending HTTP get request to Jilo agent:", endpoint)
start := time.Now() start := time.Now()
resp, err := http.Get(endpoint) resp, err := http.Get(endpoint)
if err != nil { if err != nil {
@ -75,6 +78,7 @@ func checkEndpoint(endpoint string) (int, int64) {
defer resp.Body.Close() defer resp.Body.Close()
elapsed := time.Since(start).Milliseconds() elapsed := time.Since(start).Milliseconds()
log.Printf("Received response: %d, Time taken: %d ms", resp.StatusCode, elapsed)
return resp.StatusCode, elapsed return resp.StatusCode, elapsed
} }
@ -85,26 +89,47 @@ func saveData(db *sql.DB, statusCode int, responseTime int64) {
} }
} }
// Main routine
func main() { 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") config := readConfig("jilo-server.conf")
// Connect to or setup the database // Connect to or setup the database
log.Println("Initializing the database...")
db, err := setupDatabase(config.DatabasePath) db, err := setupDatabase(config.DatabasePath)
if err != nil { if err != nil {
log.Fatal("Failed to initialize the database:", err) log.Fatal("Failed to initialize the database:", err)
} }
defer db.Close() defer db.Close()
ticker := time.NewTicker(time.Duration(config.CheckPeriod) * time.Minute)
defer ticker.Stop()
log.Println("Starting endpoint checker...") log.Println("Starting endpoint checker...")
for { // Iterate over the servers and agents
statusCode, responseTime := checkEndpoint(config.RemoteEndpoint) for serverName, server := range config.Servers {
log.Printf("Endpoint check: Status code: %d, Response time: %d ms", statusCode, responseTime) 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()
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 <-ticker.C
} }
}(serverName, agentName, agent)
}
}
// Prevent the main from exiting
select {}
} }

BIN
meet.db

Binary file not shown.