diff --git a/plugins/uptimerobot/uptimerobot.go b/plugins/uptimerobot/uptimerobot.go index d53524d..4805bac 100644 --- a/plugins/uptimerobot/uptimerobot.go +++ b/plugins/uptimerobot/uptimerobot.go @@ -3,9 +3,12 @@ package uptimerobot import ( "fmt" "time" + "strings" ur "git.uwaterloo.ca/csc/cscsysbot/utils/uptimerobot" "git.uwaterloo.ca/csc/cscsysbot/plugins/background" + + "github.com/go-chat-bot/bot" ) func uptimeRobot() { @@ -28,7 +31,7 @@ func uptimeRobot() { if lastStatus != mon.Status { if (mon.Type == ur.MonitorTypePort) { - background.Messages <- fmt.Sprintf("Uptime Robot: %s -> %s, %s:%d (%s)\n", lastStatus, mon.Status, mon.URL, mon.Type, int(mon.Port.(float64))) + background.Messages <- fmt.Sprintf("Uptime Robot: %s -> %s, %s:%d (%s)\n", lastStatus, mon.Status, mon.URL, int(mon.Port.(float64)), mon.Type) } else { background.Messages <- fmt.Sprintf("Uptime Robot: %s -> %s, %s (%s)\n", lastStatus, mon.Status, mon.URL, mon.Type) } @@ -40,6 +43,81 @@ func uptimeRobot() { } } +func inList(l []ur.MonitorStatus, s ur.MonitorStatus) bool { + for _, i := range l { + if i == s { + return true + } + } + + return false +} + +func monitorQuery(command *bot.Cmd) (string, error) { + var lines []string + var statuses []ur.MonitorStatus + + for _, arg := range command.Args { + switch strings.ToLower(arg) { + case "up": + statuses = append(statuses, ur.MonitorStatusUp) + case "down": + statuses = append(statuses, ur.MonitorStatusDown) + case "paused": + statuses = append(statuses, ur.MonitorStatusPaused) + case "seemsdown": + fallthrough + case "seems_down": + statuses = append(statuses, ur.MonitorStatusSeemsDown) + case "notchecked": + fallthrough + case "not_checked": + statuses = append(statuses, ur.MonitorStatusNotCheckedYet) + default: + lines = append(lines, fmt.Sprintf("Unknown monitor status: %q", strings.ToLower(arg))) + } + } + + if len(statuses) == 0 { + lines = append(lines, fmt.Sprintf("Please provided at least on status to filter with.")) + return strings.Join(lines, "\n"), nil + } + + monitors, err := ur.GetMonitors() + if err != nil { + return "", nil + } + + match := 0 + for _, mon := range monitors.Monitors { + if !inList(statuses, mon.Status) { + continue; + } + + match++ + var status string + + if (mon.Type == ur.MonitorTypePort) { + status = fmt.Sprintf("Uptime Robot: %s, %s:%d (%s)\n", mon.Status, mon.URL, int(mon.Port.(float64)), mon.Type) + } else { + status = fmt.Sprintf("Uptime Robot: %s, %s (%s)\n", mon.Status, mon.URL, mon.Type) + } + lines = append(lines, status) + } + + if match == 0 { + lines = append(lines, "No monitors matched the requested statuses") + } + + return strings.Join(lines, "\n"), nil +} + func init() { go uptimeRobot() + + bot.RegisterCommand( + "monitors", + "Query Uptime Robot Monitors status", + "Retricts monitors to those matching the list of provided statuses (ex: !monitors paused down to show up and down monitors)", + monitorQuery) }