func queryApi(api_url string, user string, pass string, data *map[string]interface{}) { tr := &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, } client := &http.Client{Transport: tr} req, err := http.NewRequest("GET", api_url, nil) req.SetBasicAuth(user, pass) res, err := client.Do(req) if err != nil { nagiosplugin.Exit(nagiosplugin.UNKNOWN, "Can not connect to Graylog2 API") } defer res.Body.Close() body, err := ioutil.ReadAll(res.Body) if err != nil { nagiosplugin.Exit(nagiosplugin.UNKNOWN, "Got no response from Graylog2 API") } err = json.Unmarshal(body, data) if err != nil { nagiosplugin.Exit(nagiosplugin.UNKNOWN, "Can not parse JSON from Graylog2 API") } if res.StatusCode != 200 { nagiosplugin.Exit(nagiosplugin.UNKNOWN, "Got wrong return code from Graylog2 API, please check all command line parameters") } }
func main() { flag.Parse() checkArguments() check := nagiosplugin.NewCheck() defer check.Finish() var data map[string]interface{} queryApi(parseUrl(*api_url)+"/streams/"+url.QueryEscape(*stream)+"/alerts/check", url.QueryEscape(*user), url.QueryEscape(*pass), &data) total := data["total_triggered"].(float64) results := data["results"].([]interface{}) var stream_title string queryApi(parseUrl(*api_url)+"/streams/"+url.QueryEscape(*stream), url.QueryEscape(*user), url.QueryEscape(*pass), &data) stream_title = data["title"].(string) for i, result := range results { i = i mappedResult := result.(map[string]interface{}) mappedCondition := mappedResult["condition"].(map[string]interface{}) if *condition != "<ID>" && *condition == mappedCondition["id"] && mappedResult["triggered"] == true { nagiosplugin.Exit(nagiosplugin.CRITICAL, "Alert triggered for stream '"+stream_title+"' condition: "+*condition) } } if total > 0 && *condition == "<ID>" { nagiosplugin.Exit(nagiosplugin.CRITICAL, fmt.Sprintf("%g", total)+" alert/s triggered for stream "+stream_title) } check.AddResult(nagiosplugin.OK, "No stream alerts triggered for stream: "+stream_title) }
func parseUrl(unparsed_url string) string { parsed_url, err := url.Parse(unparsed_url) if err != nil { nagiosplugin.Exit(nagiosplugin.UNKNOWN, "Can not parse given URL") } if !strings.Contains(parsed_url.Host, ":") { nagiosplugin.Exit(nagiosplugin.UNKNOWN, "Please give the API port number in the form http://hostname:port") } connection_string := parsed_url.Scheme + "://" + parsed_url.Host return connection_string }