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
- 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
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"

61
main.go
View File

@ -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 {}
}

BIN
meet.db

Binary file not shown.