Created
May 28, 2016 06:40
-
-
Save ftnk/6b7f3216eb58f06ed2616e1bceb62c15 to your computer and use it in GitHub Desktop.
Munin plugin
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" | |
) | |
type Uptime struct { | |
uptime uint64 | |
} | |
func main() { | |
var uptime *Uptime = new(Uptime) | |
if len(os.Args) == 2 && os.Args[1] == "config" { | |
outputCfg() | |
} else { | |
getData(uptime) | |
printValues(uptime) | |
} | |
} | |
func printValues(uptime *Uptime) { | |
fmt.Printf("uptime.value %d\n", (*uptime).uptime) | |
} | |
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 getData(uptime *Uptime) { | |
now := uint64(time.Now().Unix()) | |
out, err := exec.Command("kstat", "-p", | |
"unix:0:system_misc:boot_time").Output() | |
if err != nil { | |
fmt.Println("error: ", err) | |
os.Exit(1) | |
} | |
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 { | |
panic(fmt.Sprintf("Failed to parse %s", str[1])) | |
} | |
uptime.uptime = (now - i) / (60 * 60 * 24) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
自分が書くならこんな感じですかね?
https://gist.github.com/secondarykey/7401f5b5199d0ac237e5f65c94629780
とにかくerrorの伝達をした方が良いと思います。
関数内でpanic()やos.Exit()を使用するのは避けた方がよいかと。
getData()とprintValue()の分割をやめたのは特に意味はないです。
※というより構造はオリジナルの方が良いかと
return でuint64も返して、printしてたらごちゃごちゃしたのでまとめただけです。