Created
August 15, 2015 10:01
-
-
Save falzm/bd356e3128ebfcc7b419 to your computer and use it in GitHub Desktop.
RIQ - Riemann Index Query
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 ( | |
"flag" | |
"fmt" | |
"os" | |
"time" | |
"github.com/amir/raidman" | |
) | |
const ( | |
defaultRiemannServer = "localhost:5555" | |
defaultQuery = "true" | |
) | |
var ( | |
riemannServer = flag.String("server", defaultRiemannServer, "Riemann server (host:port)") | |
) | |
func init() { | |
flag.Usage = func() { | |
fmt.Fprintf(os.Stderr, `riq - Query Riemann index | |
Usage: | |
riq [options] [<query (default: "true")>] | |
Options: | |
`) | |
flag.PrintDefaults() | |
fmt.Println(`Riemann query syntax reference: | |
https://github.com/aphyr/riemann/blob/master/test/riemann/query_test.clj`) | |
} | |
flag.Parse() | |
} | |
func main() { | |
var query string | |
c, err := raidman.Dial("tcp", *riemannServer) | |
if err != nil { | |
die("unable to connect to Riemann server\n") | |
} | |
if len(flag.Args()) == 1 { | |
query = flag.Arg(0) | |
} else { | |
query = "true" | |
} | |
events, err := c.Query(query) | |
if err != nil { | |
die("unable to query Riemann server: %s", err) | |
} | |
hosts := make(map[string]map[string]raidman.Event) | |
for _, event := range events { | |
if _, present := hosts[event.Host]; !present { | |
hosts[event.Host] = make(map[string]raidman.Event) | |
} | |
hosts[event.Host][event.Service] = event | |
} | |
for host, services := range hosts { | |
for service, event := range services { | |
fmt.Printf("%s: %s: %s (%s)\n", host, service, event.State, time.Unix(event.Time, 0)) | |
} | |
} | |
} | |
func die(format string, a ...interface{}) { | |
fmt.Printf(format, a) | |
os.Exit(1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment