Adds configuration file config.json
parent
813c0565f7
commit
1a8ef2ec3a
|
@ -0,0 +1,4 @@
|
||||||
|
{
|
||||||
|
"agent_port": 8080,
|
||||||
|
"nginx_port": 80
|
||||||
|
}
|
81
go/main.go
81
go/main.go
|
@ -14,19 +14,33 @@ package main
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Config holds the structure of the configuration file
|
||||||
|
type Config struct {
|
||||||
|
AgentPort int `json:"agent_port"`
|
||||||
|
NginxPort int `json:"nginx_port"`
|
||||||
|
}
|
||||||
|
|
||||||
// NginxData holds the nginx data structure for the API response to /nginx
|
// NginxData holds the nginx data structure for the API response to /nginx
|
||||||
type NginxData struct {
|
type NginxData struct {
|
||||||
NginxState string `json:"nginx_status"`
|
NginxState string `json:"nginx_state"`
|
||||||
NginxConnections int `json:"nginx_connections"`
|
NginxConnections int `json:"nginx_connections"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// JicofoData holds the Jicofo data structure for the API response to /jicofo
|
||||||
|
type JicofoData struct {
|
||||||
|
JicofoState string `json:"jicofo_state"`
|
||||||
|
JicofoAPIData map[string]interface{} `json:"jicofo_api_data"`
|
||||||
|
}
|
||||||
|
|
||||||
// getNginxState checks the status of the nginx service
|
// getNginxState checks the status of the nginx service
|
||||||
func getNginxState() string {
|
func getNginxState() string {
|
||||||
output, err := exec.Command("systemctl", "is-active", "nginx").Output()
|
output, err := exec.Command("systemctl", "is-active", "nginx").Output()
|
||||||
|
@ -42,8 +56,9 @@ func getNginxState() 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() int {
|
func getNginxConnections(nginxPort int) int {
|
||||||
output, err := exec.Command("bash", "-c", "netstat -an | grep ':80' | wc -l").Output()
|
cmd := fmt.Sprintf("netstat -an | grep ':%d' | wc -l", nginxPort)
|
||||||
|
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)
|
||||||
return -1
|
return -1
|
||||||
|
@ -57,11 +72,48 @@ func getNginxConnections() int {
|
||||||
return connectionsInt
|
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()
|
||||||
|
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"}
|
||||||
|
}
|
||||||
|
var result map[string]interface{}
|
||||||
|
if err := json.Unmarshal(output, &result); err != nil {
|
||||||
|
log.Printf("Error in parsing the JSON: %v", err)
|
||||||
|
return map[string]interface{}{"error": "invalid JSON format"}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// loadConfig loads the configuration from a JSON config file
|
||||||
|
func loadConfig(filename string) (Config, error) {
|
||||||
|
var config Config
|
||||||
|
|
||||||
|
file, err := os.Open(filename)
|
||||||
|
if err != nil {
|
||||||
|
return config, err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
bytes, err := ioutil.ReadAll(file)
|
||||||
|
if err != nil {
|
||||||
|
return config, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.Unmarshal(bytes, &config); err != nil {
|
||||||
|
return config, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return config, nil
|
||||||
|
}
|
||||||
|
|
||||||
// nginxHandler handles the /nginx endpoint
|
// nginxHandler handles the /nginx endpoint
|
||||||
func nginxHandler(w http.ResponseWriter, r *http.Request) {
|
func nginxHandler(config Config, w http.ResponseWriter, r *http.Request) {
|
||||||
data := NginxData {
|
data := NginxData {
|
||||||
NginxState: getNginxState(),
|
NginxState: getNginxState(),
|
||||||
NginxConnections: getNginxConnections(),
|
NginxConnections: getNginxConnections(config.NginxPort),
|
||||||
}
|
}
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
json.NewEncoder(w).Encode(data)
|
json.NewEncoder(w).Encode(data)
|
||||||
|
@ -69,9 +121,22 @@ func nginxHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
// main sets up the http server and the routes
|
// main sets up the http server and the routes
|
||||||
func main() {
|
func main() {
|
||||||
http.HandleFunc("/nginx", nginxHandler)
|
|
||||||
fmt.Println("Starting agent server on port 8080.")
|
// load the configuration
|
||||||
if err := http.ListenAndServe(":8080", nil); err != nil {
|
config, err := loadConfig("config.json")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error loading the config file: %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// endpoints
|
||||||
|
http.HandleFunc("/nginx", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
nginxHandler(config, w, r)
|
||||||
|
})
|
||||||
|
|
||||||
|
// start the http server
|
||||||
|
agentPortStr := fmt.Sprintf(":%d", config.AgentPort)
|
||||||
|
fmt.Printf("Starting agent server on port %d.\n", config.AgentPort)
|
||||||
|
if err := http.ListenAndServe(agentPortStr, nil); err != nil {
|
||||||
log.Fatalf("Could not start the server: %v\n", err)
|
log.Fatalf("Could not start the server: %v\n", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue