You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-stats-server/main.go

204 lines
4.3 KiB
Go

10 months ago
package main
import (
10 months ago
"bufio"
10 months ago
"fmt"
10 months ago
"log"
10 months ago
"net"
"os"
"os/exec"
"strings"
10 months ago
)
var systemdServiceUnit = strings.TrimSpace(`
[Unit]
Description=Stats Server
After=network.target
[Service]
Type=simple
Restart=always
RestartSec=15
ExecStart=/usr/local/bin/stats-server serve
[Install]
WantedBy=default.target
`)
// commands is a map of commands that can be run on the server, originally made by https://github.com/bachoseven
var commands = map[string]string{
"cpu": `top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | sed "s/^/100 - /" | bc`,
"memory": `free -m | awk '/Mem/{print $3 " " $2}'`,
"network": `cat /sys/class/net/[e]*/statistics/{r,t}x_bytes`,
"storage": `df -Ph | grep mmcblk0p5 | awk '{print $2 " " $3}' | sed 's/G//g'`,
"uptime": `cut -f1 -d. /proc/uptime`,
}
func init() {
log.SetFlags(0)
}
// runShellCommand runs a system command and returns its output
func runShellCommand(command string) (string, error) {
10 months ago
cmd := exec.Command("bash", "-c", command)
output, err := cmd.CombinedOutput()
if err != nil {
return string(output), err
10 months ago
}
10 months ago
return string(output), nil
10 months ago
}
// handleConnection handles one command per connection
10 months ago
func handleConnection(conn net.Conn) {
defer conn.Close()
10 months ago
scanner := bufio.NewScanner(conn)
if !scanner.Scan() {
log.Printf("error reading from %s, %s", conn.RemoteAddr(), scanner.Err())
return
}
10 months ago
10 months ago
command := scanner.Text()
log.Printf("received command %s from %s", command, conn.RemoteAddr())
10 months ago
shellCmd, valid := commands[strings.TrimSpace(string(command))]
if !valid {
10 months ago
fmt.Fprintln(conn, "invalid command")
return
10 months ago
}
output, err := runShellCommand(shellCmd)
if err != nil {
log.Fatal(err)
return
}
10 months ago
fmt.Fprintln(conn, output)
10 months ago
}
func runCommand(args ...string) error {
cmd := exec.Command(args[0], args[1:]...)
if output, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("%w: %s", err, output)
}
return nil
}
func setupSystemdService() error {
if _, err := os.Stat("/usr/local/bin/stats-server"); err == nil {
log.Println("binary already exists in /usr/local/bin, updating...")
}
log.Println("copying itself to /usr/local/bin/stats-server")
if err := runCommand("cp", "-f", os.Args[0], "/usr/local/bin/stats-server"); err != nil {
return err
}
log.Println("writing unit to /etc/systemd/system/stats-server.service")
if err := os.WriteFile(
"/etc/systemd/system/stats-server.service",
[]byte(systemdServiceUnit),
os.ModePerm,
); err != nil {
return err
}
log.Println("running systemctl daemon-reload")
if err := runCommand("systemctl", "daemon-reload"); err != nil {
return err
}
log.Println("running systemctl enable stats-server.service")
if err := runCommand("systemctl", "enable", "stats-server.service"); err != nil {
return err
}
log.Println("running systemctl (re)start stats-server.service")
if err := runCommand("systemctl", "restart", "stats-server.service"); err != nil {
return err
}
return nil
}
func startTCPServer() error {
host, ok := os.LookupEnv("HOST")
if !ok {
host = ":12345"
}
ln, err := net.Listen("tcp", host)
10 months ago
if err != nil {
return err
10 months ago
}
defer ln.Close()
log.Printf("listening on %s...", host)
10 months ago
for {
conn, err := ln.Accept()
if err != nil {
log.Printf("error accepting connection: %s", err)
10 months ago
continue
}
log.Printf("connection from %s", conn.RemoteAddr())
10 months ago
go handleConnection(conn)
}
}
10 months ago
var helpText = strings.TrimSpace(`
usage: stats-server [setup|serve]
This is a simple tcp server that returns system stats, it can be run as a
systemd service or as a standalone server.
protocol commands:
cpu returns cpu usage
memory returns memory usage
network returns network usage
storage returns storage usage
uptime returns uptime
10 months ago
subcommands:
setup auto-install and setup systemd service
serve start tcp server
config, environment variables:
HOST tcp host to bind to (default: :12345)
`)
func showHelp() {
fmt.Println(helpText)
os.Exit(1)
}
func main() {
if len(os.Args) != 2 {
showHelp()
}
switch os.Args[1] {
case "setup":
10 months ago
log.Println("starting setup")
if err := setupSystemdService(); err != nil {
log.Fatal(err)
}
10 months ago
log.Println("setup completed")
case "serve":
10 months ago
log.Println("starting server")
if err := startTCPServer(); err != nil {
log.Fatal(err)
}
log.Println("server exited")
10 months ago
default:
showHelp()
}
}