Пример #1
0
//Command Standard command parser
func (x WinRM) Command(message models.Message, publishMsgs chan<- models.Message, connector models.Connector) {
	for _, command := range connector.Commands {
		if match, tokens := parse.Match(command.Match, message.In.Text); match {
			args := parse.Substitute(command.Args, tokens)

			tokens["STDOUT"] = sendCommand(command.Cmd, args, connector)

			var color = "NONE"
			var match = false
			if match, _ = parse.Match(command.Green, tokens["STDOUT"]); match {
				color = "SUCCESS"
			}
			if match, _ = parse.Match(command.Yellow, tokens["STDOUT"]); match {
				color = "WARN"
			}
			if match, _ = parse.Match(command.Red, tokens["STDOUT"]); match {
				color = "FAIL"
			}
			message.In.Tags = parse.TagAppend(message.In.Tags, connector.Tags+","+command.Tags)
			message.Out.Text = connector.ID + " " + command.Name
			message.Out.Detail = parse.Substitute(command.Output, tokens)
			message.Out.Status = color
			publishMsgs <- message
		}
	}
}
Пример #2
0
func aliasCommands(message *models.Message, config *models.Config) {
	for _, alias := range config.Aliases {
		if match, tokens := parse.Match(alias.Match, message.In.Text); match {
			message.In.Text = parse.Substitute(alias.Output, tokens)
		}
	}
}
Пример #3
0
func (x Response) Command(message models.Message, publishMsgs chan<- models.Message, connector models.Connector) {
	if connector.Debug {
		log.Print("Incoming command message for " + connector.ID + " (" + connector.Type + ")")
		log.Printf("%+v", message)
	}
	for _, c := range connector.Commands {
		if match, tokens := parse.Match(c.Match, message.In.Text); match {
			if len(c.Outputs) == 0 {
				message.Out.Text = parse.Substitute(c.Output, tokens)
			} else {
				i := rand.Intn(len(c.Outputs))
				message.Out.Text = parse.Substitute(c.Outputs[i], tokens)
			}
			message.In.Tags = parse.TagAppend(message.In.Tags, connector.Tags+","+c.Tags)
			publishMsgs <- message
		}
	}
}
Пример #4
0
func webhookHandler(w http.ResponseWriter, r *http.Request) {
	reqUrl := r.URL.Path
	reqQs, err := url.QueryUnescape(r.URL.RawQuery)
	if err != nil {
		log.Print(err)
	}
	if webhook.Connector.Debug {
		log.Print("Webhook Incoming URL: " + reqUrl)
	}
	rawbody, err := ioutil.ReadAll(r.Body)
	body := string(rawbody)
	if err != nil {
		log.Print(err)
	}
	defer r.Body.Close()
	if webhook.Connector.Debug {
		log.Print("Webhook Incoming Body: " + body)
	}

	// for new relic (lame)
	if strings.HasPrefix(body, "deployment=%7B") {
		body, err = url.QueryUnescape(strings.Replace(body, "deployment=", "", 1))
		if err != nil {
			log.Print(err)
		}
	}
	if strings.HasPrefix(body, "alert=%7B") {
		body, err = url.QueryUnescape(strings.Replace(body, "alert=", "", 1))
		if err != nil {
			log.Print(err)
		}
	}

	bodyParsed, err := gabs.ParseJSON([]byte(body))
	isJson := true
	if err != nil {
		log.Print(err)
		isJson = false
	}
	for _, c := range webhook.Connector.Commands {
		if match, _ := parse.Match(c.Match, reqUrl); match {
			if webhook.Connector.Debug {
				log.Print("Webhook Match: " + c.Match)
			}
			tokens := make(map[string]string)
			tokens["?"] = reqQs
			tokens["*"] = body
			if isJson {
				if match, subs := parse.SubstitutionVars(c.Output); match {
					for _, sub := range subs {
						if webhook.Connector.Debug {
							log.Print("Webhook Sub: " + sub)
						}
						value, ok := bodyParsed.Path(parse.Strip(sub)).Data().(string)
						if ok {
							if webhook.Connector.Debug {
								log.Print("Webhook Val: " + value)
							}
							tokens[parse.Strip(sub)] = value
						}
					}

				}
			}
			out := parse.Substitute(c.Output, tokens)
			if c.Process {
				var m models.Message
				m.In.ConnectorType = webhook.Connector.Type
				m.In.ConnectorID = webhook.Connector.ID
				m.In.Tags = parse.TagAppend(webhook.Connector.Tags, c.Tags)
				m.In.Text = out
				m.In.Process = true
				webhook.CommandMsgs <- m
			} else {
				var color = "NONE"
				var match = false
				if match, _ = parse.Match(c.Green, out); match {
					color = "SUCCESS"
				}
				if match, _ = parse.Match(c.Yellow, out); match {
					color = "WARN"
				}
				if match, _ = parse.Match(c.Red, out); match {
					color = "FAIL"
				}
				var m models.Message
				m.In.ConnectorType = webhook.Connector.Type
				m.In.ConnectorID = webhook.Connector.ID
				m.In.Tags = parse.TagAppend(webhook.Connector.Tags, c.Tags)
				m.In.Text = reqUrl
				m.In.Process = false
				if c.Name != "" {
					m.Out.Text = c.Name
					m.Out.Detail = out
				} else {
					m.Out.Text = out
				}
				m.Out.Status = color
				webhook.CommandMsgs <- m
			}
		}
	}
	w.WriteHeader(http.StatusOK)
	w.Write([]byte("JaneBot"))
}
Пример #5
0
func checkRM(commandMsgs chan<- models.Message, command models.Command, connector models.Connector) {

	// command vars
	var state = command.Green
	var stateReset = true
	var counter = 1
	var interval = 1
	var sampling = 1
	var remind = 0
	if command.Interval > 0 {
		interval = command.Interval
	}
	if command.Sampling > 0 {
		sampling = command.Sampling
	}
	if command.Remind > 0 {
		remind = command.Remind
	}

	// loop commands
	for {

		// reset vars
		var color = "NONE"
		var match = false
		var newstate = ""
		var sendAlert = false

		// make the call
		out := sendCommand(command.Cmd, command.Args, connector)

		// interpret results
		if match, _ = parse.Match(command.Green, out); match {
			newstate = command.Green
			color = "SUCCESS"
		}
		if match, _ = parse.Match(command.Yellow, out); match {
			newstate = command.Yellow
			color = "WARN"
		}
		if match, _ = parse.Match(command.Red, out); match {
			newstate = command.Red
			color = "FAIL"
		}

		// handle state change
		if newstate != state {

			if stateReset {
				counter = 1
				stateReset = false
			}

			// sampling
			if counter == sampling {
				sendAlert = true
			}

			// change to green
			if newstate == command.Green {
				sendAlert = true
			}

		}

		// handle non-green state
		if newstate != command.Green && counter == remind && remind > 1 {
			sendAlert = true

		}

		// send message
		if sendAlert {
			var tokens = parse.Tokens()
			tokens["STDOUT"] = out
			var message models.Message
			message.In.ConnectorType = connector.Type
			message.In.ConnectorID = connector.ID
			message.In.Tags = parse.TagAppend(connector.Tags, command.Tags)
			message.In.Process = false
			message.Out.Text = connector.ID + " " + command.Name
			message.Out.Detail = parse.Substitute(command.Output, tokens)
			message.Out.Status = color
			commandMsgs <- message
			state = newstate
			counter = 0
			stateReset = true
		}

		// wait
		counter += 1
		time.Sleep(time.Duration(interval) * time.Minute)

	}

}