diff --git a/go/config.json b/go/config.json deleted file mode 100644 index 079931a..0000000 --- a/go/config.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "agent_port": 8080, - "nginx_port": 80 -} diff --git a/go/jilo-agent.json b/go/jilo-agent.json new file mode 100644 index 0000000..58695b4 --- /dev/null +++ b/go/jilo-agent.json @@ -0,0 +1,5 @@ +{ + "agent_port": 8080, + "nginx_port": 80, + "jicofo_stats_url": "http://localhost:8888/stats" +} diff --git a/go/main.go b/go/main.go index 089910c..86210ac 100644 --- a/go/main.go +++ b/go/main.go @@ -27,6 +27,7 @@ import ( type Config struct { AgentPort int `json:"agent_port"` NginxPort int `json:"nginx_port"` + JicofoStatsURL string `json:"jicofo_stats_url"` } // NginxData holds the nginx data structure for the API response to /nginx @@ -41,25 +42,11 @@ type JicofoData struct { JicofoAPIData map[string]interface{} `json:"jicofo_api_data"` } -// getNginxState checks the status of the nginx service -func getNginxState() string { - output, err := exec.Command("systemctl", "is-active", "nginx").Output() +// getServiceState checks the status of the speciied service +func getServiceState(service string) string { + output, err := exec.Command("systemctl", "is-active", service).Output() if err != nil { - log.Printf("Error checking the nginx state: %v", err) - return "error" - } - state := strings.TrimSpace(string(output)) - if state == "active" { - return "running" - } - return "not running" -} - -// getJicofoState checks the status of the Jicofo service -func getJicofoState() string { - output, err := exec.Command("systemctl", "is-active", "jicofo").Output() - if err != nil { - log.Printf("Error checking the jicofo state: %v", err) + log.Printf("Error checking the service \"%v\" state: %v", service, err) return "error" } state := strings.TrimSpace(string(output)) @@ -70,8 +57,8 @@ func getJicofoState() string { } // getNginxConnections gets the number of active connections to the specified web port -func getNginxConnections(nginxPort int) int { - cmd := fmt.Sprintf("netstat -an | grep ':%d' | wc -l", nginxPort) +func getNginxConnections(port int) int { + cmd := fmt.Sprintf("netstat -an | grep ':%d' | wc -l", port) output, err := exec.Command("bash", "-c", cmd).Output() if err != nil { log.Printf("Error counting the Nginx connections: %v", err) @@ -86,12 +73,13 @@ func getNginxConnections(nginxPort int) int { return connectionsInt } -// getJicofoAPIData gets the response from Jicofo stats API -func getJicofoAPIData() map[string]interface{} { - output, err := exec.Command("bash", "-c", "curl -s http://localhost:8888/stats").Output() +// getJitsiAPIData gets the response from the specified Jitsi stats API +func getJitsiAPIData(service string, url string) map[string]interface{} { + cmd := fmt.Sprintf("curl -s %v", url) + output, err := exec.Command("bash", "-c", cmd).Output() if err != nil { - log.Printf("Error getting the Jicofo API stats: %v", err) - return map[string]interface{}{"error": "failed to get the Jicofo API stats"} + log.Printf("Error getting the \"%v\" API stats: %v", service, err) + return map[string]interface{}{"error": "failed to get the Jitsi API stats"} } var result map[string]interface{} if err := json.Unmarshal(output, &result); err != nil { @@ -126,7 +114,7 @@ func loadConfig(filename string) (Config, error) { // nginxHandler handles the /nginx endpoint func nginxHandler(config Config, w http.ResponseWriter, r *http.Request) { data := NginxData { - NginxState: getNginxState(), + NginxState: getServiceState("nginx"), NginxConnections: getNginxConnections(config.NginxPort), } w.Header().Set("Content-Type", "application/json") @@ -136,8 +124,8 @@ func nginxHandler(config Config, w http.ResponseWriter, r *http.Request) { // jicofoHandler handles the /jicofo endpoint func jicofoHandler(config Config, w http.ResponseWriter, r *http.Request) { data := JicofoData { - JicofoState: getJicofoState(), - JicofoAPIData: getJicofoAPIData(), + JicofoState: getServiceState("jicofo"), + JicofoAPIData: getJitsiAPIData("jicofo", config.JicofoStatsURL), } w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(data) @@ -148,7 +136,7 @@ func jicofoHandler(config Config, w http.ResponseWriter, r *http.Request) { func main() { // load the configuration - config, err := loadConfig("config.json") + config, err := loadConfig("jilo-agent.json") if err != nil { log.Fatalf("Error loading the config file: %v\n", err) }