Initial version in Go (switching from PHP to Go)

main
Yasen Pramatarov 2024-09-02 21:43:49 +03:00
parent 27ec647ef5
commit 53e618a9ed
4 changed files with 106 additions and 1 deletions

21
CHANGELOG.md 100644
View File

@ -0,0 +1,21 @@
# Changelog
All notable changes to this project will be documented in this file.
---
## Unreleased
#### Links
- upstream: https://code.lindeas.com/lindeas/jilo-agent/releases/tag/v0.1
- codeberg: https://codeberg.org/lindeas/jilo-agent/releases/tag/v0.1
- github: https://github.com/lindeas/jilo-agent/releases/tag/v0.1
- gitlab: https://gitlab.com/lindeas/jilo-agent/-/releases/v0.1
### Added
- Initial version in PHP
- Added endpoint for /nginx, /prosody, /jicofo, /nginx
- Added a config file
- New version in folder "go", written in Go
- Added endpoint for /nginx
- Initial vesion of a build script

View File

@ -1,3 +1,7 @@
# jilo-agent
Jilo Agent - a remote agent for Jilo Web
Jilo Agent - a remote agent for Jilo Web
Initial version is in PHP.
The current version is in "go" folder and is in Go.

3
go/build.sh 100755
View File

@ -0,0 +1,3 @@
#!/usr/bin/env bash
go build -o jilo-agent main.go

77
go/main.go 100644
View File

@ -0,0 +1,77 @@
/*
Jilo Agent
Description: Remote agent for Jilo Web with http API
Author: Yasen Pramatarov
License: GPLv2
Project URL: https://lindeas.com/jilo
Year: 2024
Version: 0.1
*/
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os/exec"
"strconv"
"strings"
)
// NginxData holds the nginx data structure for the API response to /nginx
type NginxData struct {
NginxState string `json:"nginx_status"`
NginxConnections int `json:"nginx_connections"`
}
// getNginxState checks the status of the nginx service
func getNginxState() string {
output, err := exec.Command("systemctl", "is-active", "nginx").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"
}
// getNginxConnections gets the number of active connections to the specified web port
func getNginxConnections() int {
output, err := exec.Command("bash", "-c", "netstat -an | grep ':80' | wc -l").Output()
if err != nil {
log.Printf("Error counting the Nginx connections: %v", err)
return -1
}
connections := strings.TrimSpace(string(output))
connectionsInt, err := strconv.Atoi(connections)
if err != nil {
log.Printf("Error converting connections to integer number: %v", err)
return -1
}
return connectionsInt
}
// nginxHandler handles the /nginx endpoint
func nginxHandler(w http.ResponseWriter, r *http.Request) {
data := NginxData {
NginxState: getNginxState(),
NginxConnections: getNginxConnections(),
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(data)
}
// main sets up the http server and the routes
func main() {
http.HandleFunc("/nginx", nginxHandler)
fmt.Println("Starting agent server on port 8080.")
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatalf("Could not start the server: %v\n", err)
}
}