Beispiel #1
0
func ivyHandler(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error {
	m.Parse()
	out, err := ivy.Eval(strings.Join(m.Args(), " ") + "\n")
	if err != nil {
		return err
	}

	fmt.Fprint(w, out)
	return nil
}
Beispiel #2
0
func (p *promH) silencesCmd(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error {
	if err := m.Parse(); err != nil {
		return err
	}
	ss, err := am.NewSilenceAPI(p.amclient).List(ctx)
	if err != nil {
		return err
	}

	if len(ss) == 0 {
		fmt.Fprint(w, "There are no active silences")
		return nil
	}

	for _, s := range ss {
		fmt.Fprintf(w, "%#v", s)
	}
	return nil
}
Beispiel #3
0
func (p *promH) alertsCmd(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error {
	if err := m.Parse(); err != nil {
		return err
	}
	as, err := am.NewAlertAPI(p.amclient).List(ctx)
	if err != nil {
		return err
	}

	if len(as) == 0 {
		fmt.Fprint(w, "There are no outstanding alerts")
		return nil
	}

	for _, a := range as {
		fmt.Fprintf(w, "%s: Started at %s, #v", a.Labels["alertname"], a.StartsAt, a.Labels)
	}
	return nil
}
Beispiel #4
0
func brainFuckHandler(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error {
	m.Parse()

	var program = strings.Join(m.Args(), " ") + "\n"
	fmt.Println(program)

	// https://github.com/gfranxman/GFY

	// prepare our vm
	data := make([]uint8, MAX_PROG_LEN)
	data_ptr := 0
	loop_depth := 0
	instruction_ptr := 0

	// execution loop
	for instruction_ptr = 0; instruction_ptr < len(program); instruction_ptr++ {
		switch program[instruction_ptr] {
		case '>':
			// increment the data pointer (to point to the next cell to the right).
			data_ptr++

		case '<':
			// decrement the data pointer (to point to the next cell to the left).
			data_ptr--

		case '+':
			// increment (increase by one) the byte at the data pointer.
			data[data_ptr]++

		case '-':
			// decrement (decrease by one) the byte at the data pointer.
			data[data_ptr]--

		case '.':
			// output the value of the byte at the data pointer.
			print(string(uint8(data[data_ptr])))

		case ',':
			// accept one byte of input, storing its value in the byte at the data pointer.
			var b = make([]uint8, 1)
			_, _ = os.Stdin.Read(b)
			data[data_ptr] = b[0]

		case '[':
			// if the byte at the data pointer is zero, then
			//    instead of moving the instruction pointer forward to the next command,
			//    jump it forward to the command after the matching ] command.
			// * interesting note -- the spec does not mention that the jump instructions should be nestable,
			//    but without this feature my test suite fails.
			if data[data_ptr] == 0 {
				instruction_ptr++
				// allow nested [ ] pairs by looping until we hit the end-jump for our loop depth
				for loop_depth > 0 || program[instruction_ptr] != ']' {
					if program[instruction_ptr] == '[' {
						loop_depth++
					} else if program[instruction_ptr] == ']' {
						loop_depth--
					}
					instruction_ptr++
				}
			}

		case ']':
			// if the byte at the data pointer is nonzero,
			//    then instead of moving the instruction pointer forward to the next command,
			//    jump it back to the command after the matching [ command.
			// * interesting note -- the spec calls for a check that the datapointer is pointing to a non-zero value,
			//    but doing so causes my test suite of bf programs to fail.
			instruction_ptr--
			// allow nested [ ] pairs by looping until we hit the end-jump for our loop depth
			for loop_depth > 0 || program[instruction_ptr] != '[' {
				if program[instruction_ptr] == ']' {
					loop_depth++
				} else if program[instruction_ptr] == '[' {
					loop_depth--
				}
				instruction_ptr--
			}
			instruction_ptr--
		} // end-switch
	}

	fmt.Fprint(w, program)
	return nil
}
Beispiel #5
0
func (p *promH) graphCmd(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error {
	defText, defGraph := false, false
	if !hugot.IsTextOnly(w) {
		defText = true
		defGraph = false
	} else {
		defText = false
		defGraph = true
	}

	_ = m.Bool("t", defText, "Render the graphs as text sparkline.")
	webGraph := m.Bool("g", defGraph, "Render the graphs as a png.")
	dur := m.Duration("d", 15*time.Minute, "how far back to render")

	if err := m.Parse(); err != nil {
		return err
	}

	if len(m.Args()) == 0 {
		return fmt.Errorf("you need to give a query")
	}
	q := strings.Join(m.Args(), " ")
	s := time.Now().Add(-1 * *dur)
	e := time.Now()

	if *webGraph {
		nu := *p.URL()
		nu.Path = nu.Path + "graph/thing.png"
		qs := nu.Query()
		qs.Set("q", q)
		qs.Set("s", fmt.Sprintf("%d", s.UnixNano()))
		qs.Set("e", fmt.Sprintf("%d", e.UnixNano()))
		nu.RawQuery = qs.Encode()

		m := hugot.Message{
			Channel: m.Channel,
			Attachments: []hugot.Attachment{
				{
					Fallback: "fallback",
					Pretext:  "image",
					ImageURL: nu.String(),
				},
			},
		}
		w.Send(ctx, &m)
		return nil
	}

	qapi := prom.NewQueryAPI(*p.client)
	d, err := qapi.QueryRange(ctx, q, prom.Range{s, e, 15 * time.Second})
	if err != nil {
		return err
	}

	switch d.Type() {
	case model.ValScalar:
		m := d.(*model.Scalar)
		glog.Infof("scalar %v", m)
	case model.ValVector:
		m := d.(model.Vector)
		glog.Infof("vector %v", m)
	case model.ValMatrix:
		mx := d.(model.Matrix)
		sort.Sort(mx)
		for _, ss := range mx {
			l := line(ss.Values)
			fmt.Fprintf(w, "%v\n%s", ss.Metric, l)
		}
	case model.ValString:
		m := d.(*model.String)
		glog.Infof("matrix %v", m)
	case model.ValNone:
	}

	return nil
}
Beispiel #6
0
func (p *promH) explainCmd(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error {
	m.Parse()

	return nil
}