diff options
-rw-r--r-- | cache.go | 40 | ||||
-rw-r--r-- | osversion.go | 27 | ||||
-rw-r--r-- | uptime.go | 38 |
3 files changed, 62 insertions, 43 deletions
diff --git a/cache.go b/cache.go index c7a68b7..d7839b8 100644 --- a/cache.go +++ b/cache.go @@ -59,19 +59,38 @@ func (cache *cacheWrapper) bap(requestPath string) { bapIndex() return } + split := strings.Split(requestPath[1:], "/") - switch split[1] { + format := split[0] + query := split[1] + + unNullPage(requestPath) + + if cache.isFresh(requestPath) { + return + } + + var bytes []byte + var err error + + switch query { case "osversion": - bapOSVersion(split[0]) - case "pkgs": - bapPkgs(split[0]) + bytes, err = osVersionQuery(format) case "uptime": - bapUptime(split[0]) - case "usercount": - bapUserCount(split[0]) - case "users": - bapUsers(split[0]) - default: + bytes, err = uptimeQuery(format) + } + + if err != nil { + log.Printf("Could not query %s: %s", requestPath, err.Error()) + bytes = []byte("Internal Error") + } + + cache.Lock() + defer cache.Unlock() + + cache.pages[requestPath] = &page{ + raw: bytes, + expires: time.Now().Add(cacheDuration), } } @@ -118,6 +137,5 @@ func bapIndex() { func bapPkgs(format string) {} func bapQuery(format string) {} -func bapUptime(format string) {} func bapUserCount(format string) {} func bapUsers(format string) {} diff --git a/osversion.go b/osversion.go index eadc960..3ad501a 100644 --- a/osversion.go +++ b/osversion.go @@ -3,37 +3,10 @@ package main import ( "errors" "fmt" - "log" "os/exec" "strings" - "time" ) -// Checks the cache for the freshness of the osversion query. -// If stale, store the query again. -func bapOSVersion(format string) { - path := fmt.Sprintf("/%s/osversion", format) - unNullPage(path) - - if cache.isFresh(path) { - return - } - - bytes, err := osVersionQuery(format) - if err != nil { - log.Printf("Could not query OS version: %s", err.Error()) - bytes = []byte("Internal Error") - } - - cache.Lock() - defer cache.Unlock() - - cache.pages[path] = &page{ - raw: bytes, - expires: time.Now().Add(cacheDuration), - } -} - // executes uname and responds with "$OS $VERSION" in []byte{} func osVersionQuery(format string) ([]byte, error) { out, err := exec.Command("/usr/bin/uname", "-a").Output() 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 } |