Created
May 29, 2016 02:01
-
-
Save secondarykey/7401f5b5199d0ac237e5f65c94629780 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"os/exec" | |
"regexp" | |
"strconv" | |
"strings" | |
"time" | |
) | |
func main() { | |
if len(os.Args) == 2 && os.Args[1] == "config" { | |
outputCfg() | |
} else { | |
err := outputUptime() | |
if err != nil { | |
fmt.Printf("uptime error: %v", err) | |
os.Exit(1) | |
} | |
} | |
} | |
func outputCfg() { | |
fmt.Print( | |
`graph_title Uptime | |
graph_args --base 1000 -l 0 | |
graph_scale no | |
graph_category system | |
graph_vlabel uptime in days | |
uptime.label uptime | |
uptime.draw AREA | |
`) | |
} | |
func outputUptime() error { | |
now := uint64(time.Now().Unix()) | |
out, err := exec.Command("kstat", "-p", "unix:0:system_misc:boot_time").Output() | |
if err != nil { | |
return fmt.Errorf("kstat error: %v", err) | |
} | |
line := strings.TrimRight(string(out), "\n") | |
reg := regexp.MustCompile(`^unix:0:system_misc:boot_time\s+(\d+)$`) | |
if str := reg.FindStringSubmatch(line); str != nil { | |
i, err := strconv.ParseUint(str[1], 10, 64) | |
if err != nil { | |
return fmt.Errorf("kstat error: %v", err) | |
} | |
fmt.Printf("uptime.value %d\n", (now-i)/(60*60*24)) | |
} else { | |
return fmt.Errorf("find error: %s", line) | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment