コード例 #1
0
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)
}
コード例 #2
0
func main() {

	// struct to quantify states
	type volume_state_count struct {
		count      int
		percentage float64
	}

	// map of volume states
	var volume_states = make(map[string]volume_state_count)

	var (
		Volume_Status string
		Total         int
		Percentage    float64
	)

	// Initialize the check - this will return an UNKNOWN result
	// until more results are added.
	check := nagiosplugin.NewCheck()
	// If we exit early or panic() we'll still output a result.
	defer check.Finish()

	// obtain data here
	//connString := fmt.Sprint(user, ":", password, "@tcp(", hostname, ":", port, ")/cinder")
	connString := fmt.Sprintf("%s:%s@tcp(%s:%d)/cinder", user, password, hostname, port)
	db, err := sql.Open("mysql", connString)

	if err != nil {
		check.Exitf(nagiosplugin.UNKNOWN, fmt.Sprintf("Could not create database connection: %s", err.Error()))
	}
	defer db.Close()

	// Open doesn't open a connection. Validate DSN data:
	err = db.Ping()
	if err != nil {
		check.Exitf(nagiosplugin.UNKNOWN, fmt.Sprintf("Could not open database connection: %s", err.Error()))
	}

	// Prepare statement for reading data
	stmt, err := db.Prepare("SELECT status AS `Volume_Status`, COUNT(1) AS `Total` ,COUNT(1) / t.cnt * 100 AS `Percentage` FROM volumes v CROSS JOIN (SELECT COUNT(1) AS cnt FROM volumes WHERE created_at > DATE_SUB(NOW(), INTERVAL ? HOUR)) t WHERE v.created_at > DATE_SUB(NOW(), INTERVAL ? HOUR) GROUP BY v.status;")
	if err != nil {
		check.Exitf(nagiosplugin.UNKNOWN, fmt.Sprintf("Could not prepare statement: %s", err.Error()))
	}
	defer stmt.Close()

	// Query the results for the last n hours
	rows, err := stmt.Query(hours, hours)
	if err != nil {
		check.Exitf(nagiosplugin.UNKNOWN, fmt.Sprintf("Could not execute query: %s", err.Error()))
	}
	defer rows.Close()

	for rows.Next() {
		err = rows.Scan(&Volume_Status, &Total, &Percentage)
		if err != nil {
			check.Exitf(nagiosplugin.UNKNOWN, fmt.Sprintf("Invalid result set: %s", err.Error()))
		}
		volume_states[Volume_Status] = volume_state_count{count: Total, percentage: Percentage}
	}

	/*fmt.Println("Database Results:")
	  for key, value := range volume_states {
	      fmt.Println("Key:", key, "Value:", value)
	  }*/

	//check for state(s)
	states := strings.Split(stateslist, ",")
	for index, state := range states {

		if state_count, ok := volume_states[state]; ok {
			if state_count.percentage < float64(warning) {
				check.AddResult(nagiosplugin.OK, "Cinder Volume OK")
				check.AddPerfDatum(fmt.Sprintf("Volumes in state '%s'", state), "%", state_count.percentage, float64(warning), float64(critical), 0.0, 100.0)
				check.AddPerfDatum("Count", "", float64(state_count.count), 0.0, 0.0, 0.0, 0.0)
				//check.Finish()
			} else if state_count.percentage >= float64(critical) {
				check.AddResult(nagiosplugin.CRITICAL, "Cinder Volume CRITICAL")
				check.AddPerfDatum(fmt.Sprintf("Volumes in state '%s'", state), "%", state_count.percentage, float64(warning), float64(critical), 0.0, 100.0)
				check.AddPerfDatum("Count", "", float64(state_count.count), 0.0, 0.0, 0.0, 0.0)
				//check.Finish()
			} else {
				check.AddResult(nagiosplugin.WARNING, "Cinder Volume WARNING")
				check.AddPerfDatum(fmt.Sprintf("Volumes in state '%s'", state), "%", state_count.percentage, float64(warning), float64(critical), 0.0, 100.0)
				check.AddPerfDatum("Count", "", float64(state_count.count), 0.0, 0.0, 0.0, 0.0)
				//check.Finish()
			}

		} else { // if the map doesn't contain the state key then no volumes are in that state and therefore non exceed the threshold
			check.AddResult(nagiosplugin.OK, "Cinder Volume OK")
			check.AddPerfDatum(fmt.Sprintf("Volumes in state '%s'", state), "%", state_count.percentage, float64(warning), float64(critical), 0.0, 100.0)
			check.AddPerfDatum("Count", "", float64(state_count.count), 0.0, 0.0, 0.0, 0.0)
			//check.Finish()
		}
		fmt.Println(index)
	}
	check.Finish()

}