about summary refs log tree commit diff stats
path: root/uptime.go
diff options
context:
space:
mode:
Diffstat (limited to 'uptime.go')
-rw-r--r--uptime.go38
1 files changed, 33 insertions, 5 deletions
diff --git a/uptime.go b/uptime.go
index 10581f0..3c874ea 100644
--- a/uptime.go
+++ b/uptime.go
@@ -1,10 +1,38 @@
 package main
 
-import "net/http"
+import (
+	"fmt"
+	"os/exec"
+	"strings"
+)
 
-// Uptime handles the /<format>/uptime endpoint.
-// Sends uptime and load
-func Uptime(w http.ResponseWriter, r *http.Request, format string) error {
+// executes uptime and responds with a []byte{}
+func uptimeQuery(format string) ([]byte, error) {
+	out, err := exec.Command("/usr/bin/uptime").Output()
+	if err != nil {
+		return nil, fmt.Errorf("Could not execute /usr/bin/uptime: %w", err)
+	}
 
-	return nil
+	split := strings.Split(string(out), ",")
+
+	if format == "plain" {
+		cut := fmt.Sprintf("%s,%s,%s,%s", split[0], split[1], split[3], split[4])
+		return []byte(cut), nil
+	}
+
+	timeup := strings.Split(string(split[0]), " ")
+	time := timeup[0]
+	up := fmt.Sprintf("%s,%s", timeup[1:], split[1])
+
+	loadcomb := strings.Split(string(split[3]), " ")
+	loads := fmt.Sprintf("%s,%s,%s", loadcomb[1], split[3], split[4])
+
+	json := fmt.Sprintf(`
+{
+	"time": "%s",
+	"up": "%s",
+	"load": "%s"
+}`, time, up, loads)
+
+	return []byte(json), nil
 }