Skip to content

Instantly share code, notes, and snippets.

@rrreeeyyy
Last active August 29, 2015 14:05
Show Gist options
  • Save rrreeeyyy/50574284bc43a4e5fe97 to your computer and use it in GitHub Desktop.
Save rrreeeyyy/50574284bc43a4e5fe97 to your computer and use it in GitHub Desktop.
すごいてきとう
package main
import (
"bufio"
"bytes"
"fmt"
"log"
"os"
"os/exec"
"github.com/codegangsta/cli"
)
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.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) {
user_count := 0
command_line := "/usr/bin/who"
out, err := exec.Command("/bin/sh", "-c", command_line).Output()
if err != nil {
log.Fatal(err)
}
scanner := bufio.NewScanner(bytes.NewReader(out))
for scanner.Scan() {
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
user_count += 1
}
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)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment