main
Yasen Pramatarov 2024-09-02 23:01:15 +03:00
parent 22d287f5f3
commit c4c863b9ff
3 changed files with 22 additions and 33 deletions

View File

@ -1,4 +0,0 @@
{
"agent_port": 8080,
"nginx_port": 80
}

View File

@ -0,0 +1,5 @@
{
"agent_port": 8080,
"nginx_port": 80,
"jicofo_stats_url": "http://localhost:8888/stats"
}

View File

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