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

hbstyle-2014-0814

 hbstyle-2014-0814

rrreeeyyy

August 14, 2014
Tweet

More Decks by rrreeeyyy

Other Decks in Programming

Transcript

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

    · ॻ͍ͯ, ίϯύΠϧͯ͠, ഑ͬͯ, ಈ͘ · (ࣾ಺Ͱ) CentOS 4 32bit ͱ͔Ͱಈ͍ͨͱ͍͏ᷚ
  2. 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) { }
  3. : 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")) }
  4. Compile $ go build Execute $ ./check_users 5 $ ./check_users

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

    { if err := scanner.Err(); err != nil { log.Fatal(err) } user_count += 1 } :
  6. : 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) :
  7. ·ͱΊ · Go ͸ΠϯϑϥΤϯδχΞ޲͚ͷྑ͍ݴޠ · CLI πʔϧ࡞Δ࣌͸ cli-init ࢖͏ͱศར ·

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