Upgrade to Pro — share decks privately, control downloads, hide ads and more …

hbstyle-2014-08-14

rrreeeyyy
August 14, 2014

 hbstyle-2014-08-14

# 入門 Go ʕ◔ϖ◔ʔ ~ 監視プラグイン製作で覚える Go ~
ハートビーツ 社内勉強会 2014/08/14

※内容は個人の見解であり、所属する組織の公式見解でも組織を代表するものでもありません※

rrreeeyyy

August 14, 2014
Tweet

More Decks by rrreeeyyy

Other Decks in Programming

Transcript

  1. · (͋Δఔ౓) ΏΔ͘ॻ͚Δ · (͋Μ·Γ) ྫ֎ͱ͔Ͱࢮͳͳ͍ · Cross Compile ʹΑΔ഑෍ͷखܰ͞

    · ॻ͍ͯ, ίϯύΠϧͯ͠, ഑ͬͯ, ಈ͘ · (ࣾ಺Ͱ) CentOS 4 32bit ͱ͔Ͱಈ͍ͨͱ͍͏ᷚ · ฒߦϓϩάϥϛϯά͕(ׂͱ)؆୯ · (※্ه͸શͯݸਓͷײ૝Ͱ͢)
  2. cli-init codegangsta/cli ༻ͷͻͳܗΛੜ੒ͯ͘͠ΕΔ (Go ͷ CLI tool Λ࡞ΔͷʹΑ͘࢖ΘΕ͍ͯΔ) $ go

    get -d github.com/tcnksm/cli-init $ cd $GOPATH/src/github.com/tcnksm/cli-init && make install $ cli-init -s subcmd,subcmd2,subcmd3 [app-name] ࠓճ͸ $ cli-init check_users
  3. package main import ( "github.com/codegangsta/cli" "os" ) func main() {

    app := cli.NewApp() app.Name = "check_users" app.Version = Version app.Usage = "" app.Author = "YOSHIKAWA Ryota" app.Email = "[email protected]" app.Action = doMain app.Run(os.Args) } func doMain(c *cli.Context) { }
  4. : app.Flags = []cli.Flag{ cli.IntFlag{ Name: "w, warning", Value: 5,

    Usage: "Set WARNING status if more than INTEGER users are logged in"}, cli.IntFlag{ Name: "c, critical", Value: 10, Usage: "Set CRITICAL status if more than INTEGER users are logged in"}, } app.Run(os.Args) } func doMain(c *cli.Context) { fmt.Println(c.Int("w")) }
  5. Compile $ go build Execute $ ./check_users 5 $ ./check_users

    -w 10 10 $ ./check_users --help : $ ./check_users -v 0.1.0
  6. : user_count := 0 : scanner := bufio.NewScanner(bytes.NewReader(out)) for scanner.Scan()

    { if err := scanner.Err(); err != nil { log.Fatal(err) } user_count += 1 } :
  7. : switch { case user_count > c.Int("c"): fmt.Printf("USERS CRITICAL -

    %v users currently logged in", user_count) os.Exit(2) case user_count <= c.Int("c") && user_count > c.Int("w"): fmt.Printf("USERS WARNING - %v users currently logged in", user_count) os.Exit(1) case user_count <= c.Int("w") && user_count >= 0: fmt.Printf("USERS OK - %v users currently logged in", user_count) os.Exit(0) } fmt.Printf("UNKNOWN State.") os.Exit(3) :
  8. ·ͱΊ · Go ͸ΠϯϑϥΤϯδχΞ޲͚ͷྑ͍ݴޠ · CLI πʔϧ࡞Δ࣌͸ cli-init ࢖͏ͱศར ·

    Go ͷจ๏ͱ͔৭ʑ঺հ · goroutine ͱ͔ struct ͱ͔ channel ͱ͔... · select ͱ͔... ͷ࿩͸·͍͔ͨͭɻ