Ejemplo n.º 1
0
func (this *EngineConfig) handleHttpQuery(w http.ResponseWriter, req *http.Request,
	params map[string]interface{}) (interface{}, error) {
	var (
		vars    = mux.Vars(req)
		cmd     = vars["cmd"]
		globals = Globals()
		output  = make(map[string]interface{})
	)

	switch cmd {
	case "ping":
		output["status"] = "ok"

	case "shutdown":
		globals.Shutdown()
		output["status"] = "ok"

	case "reload", "restart":
		break

	case "debug":
		stack := make([]byte, 1<<20)
		stackSize := runtime.Stack(stack, true)
		globals.Println(string(stack[:stackSize]))
		output["result"] = "go to global logger to see result"

	case "stat":
		output["runtime"] = this.stats.Runtime()
		output["router"] = this.router.stats
		output["started"] = globals.StartedAt
		output["elapsed"] = time.Since(globals.StartedAt).String()
		output["pid"] = this.pid
		output["hostname"] = this.hostname

	case "pools":
		for poolName, _ := range this.diagnosticTrackers {
			packs := make([]string, 0, globals.RecyclePoolSize)
			for _, pack := range this.diagnosticTrackers[poolName].packs {
				s := fmt.Sprintf("[%s]%s",
					bjtime.TimeToString(pack.diagnostics.LastAccess),
					*pack)
				packs = append(packs, s)
			}
			output[poolName] = packs
			output[poolName+"_len"] = len(packs)
		}

	case "plugins":
		output["plugins"] = this.pluginNames()

	case "uris":
		output["all"] = this.httpPaths

	default:
		return nil, errors.New("Not Found")
	}

	return output, nil
}
Ejemplo n.º 2
0
func since(timestamp string) string {
	t, _ := time.Parse(time.RFC3339, timestamp)
	return bjtime.TimeToString(t)
}
Ejemplo n.º 3
0
func (this *AlarmOutput) runSendAlarmsWatchdog(project *engine.ConfProject,
	config alarmProjectConf) {
	var (
		mailQueue   = pqueue.New()
		mailBody    bytes.Buffer
		lastSending time.Time
		mailLine    interface{}
	)

	suppressedHour := func(hour int) bool {
		// At night we are sleeping and will never checkout the alarms
		// So queue it up till we've got up from bed
		// FIXME will the mail queue overflow?
		for _, h := range config.mailConf.suppressHours {
			if hour == h {
				return true
			}
		}

		return false
	}

	heap.Init(mailQueue)

	for alarmMessage := range config.emailChan {
		if alarmMessage.severity < config.mailConf.severityThreshold {
			// ignore little severity messages
			continue
		}

		// enque
		heap.Push(mailQueue,
			&pqueue.Item{
				Value: fmt.Sprintf("%s[%4d] %s\n",
					bjtime.TimeToString(alarmMessage.receivedAt),
					alarmMessage.severity, alarmMessage.msg),
				Priority: alarmMessage.severity})

		// check if send it out now
		if !suppressedHour(bjtime.NowBj().Hour()) &&
			mailQueue.PrioritySum() >= config.mailConf.severityPoolSize {
			if !lastSending.IsZero() &&
				time.Since(lastSending).Seconds() < float64(config.mailConf.interval) {
				// we can't send too many emails in emergancy
				continue
			}

			// gather mail body content
			for {
				if mailQueue.Len() == 0 {
					break
				}

				mailLine = heap.Pop(mailQueue)
				mailBody.WriteString(mailLine.(*pqueue.Item).Value.(string))
			}

			go Sendmail(config.mailConf.recipients,
				fmt.Sprintf("ALS[%s] alarms", project.Name), mailBody.String())

			project.Printf("alarm sent=> %s", config.mailConf.recipients)

			mailBody.Reset()
			lastSending = time.Now()
		}
	}
}
Ejemplo n.º 4
0
func drawEvent(x, y int, evt interface{}) {
	fg_col, bg_col := coldef, coldef
	if isSelectedRow(y) {
		fg_col = termbox.ColorBlack
		bg_col = termbox.ColorYellow
	}

	var row string
	switch hook := evt.(type) {
	case *Webhook:
		if len(hook.Commits) == 0 {
			row = fmt.Sprintf("%14s %s %-25s",
				bjtime.TimeToString(hook.ctime),
				wideStr(hook.User_name, 20),
				hook.Repository.Name)
		} else {
			commit := hook.Commits[len(hook.Commits)-1] // the most recent commit
			row = fmt.Sprintf("%14s %s %-25s %s",
				since(commit.Timestamp),
				wideStr(hook.User_name, 20),
				hook.Repository.Name,
				commit.Message)
		}

	case *SystemHookProjectCreate:
		fg_col = termbox.ColorRed
		row = fmt.Sprintf("%14s %20s created project(%s)",
			since(hook.Created_at),
			hook.Owner_name,
			hook.Name)

	case *SystemHookProjectDestroy:
		fg_col = termbox.ColorRed
		row = fmt.Sprintf("%14s %20s destroy project(%s)",
			since(hook.Created_at),
			hook.Owner_name,
			hook.Path_with_namespace)

	case *SystemHookGroupCreate:
		fg_col = termbox.ColorRed
		row = fmt.Sprintf("%14s %20s created group(%s)",
			since(hook.Created_at),
			hook.Owner_name,
			hook.Name)

	case *SystemHookGroupDestroy:
		fg_col = termbox.ColorRed
		row = fmt.Sprintf("%14s %20s destroy group(%s)",
			since(hook.Created_at),
			hook.Owner_name,
			hook.Name)

	case *SystemHookUserCreate:
		fg_col = termbox.ColorRed
		row = fmt.Sprintf("%14s %20s %s signup",
			since(hook.Created_at),
			hook.Name,
			hook.Email)

	case *SystemHookUserAddToGroup:
		fg_col = termbox.ColorRed
		row = fmt.Sprintf("%14s %20s join group(%s)",
			since(hook.Created_at),
			hook.User_name,
			hook.Group_name)

	case *SystemHookUserRemovedFromGroup:
		fg_col = termbox.ColorRed
		row = fmt.Sprintf("%14s %20s kicked from group(%s)",
			since(hook.Created_at),
			hook.User_name,
			hook.Group_name)

	case *SystemHookUserAddToTeam:
		fg_col = termbox.ColorRed
		row = fmt.Sprintf("%14s %20s join project(%s)",
			since(hook.Created_at),
			hook.User_name,
			hook.Project_name)

	case *SystemHookUserRemovedFromTeam:
		fg_col = termbox.ColorRed
		row = fmt.Sprintf("%14s %20s kicked from project(%s)",
			since(hook.Created_at),
			hook.User_name,
			hook.Project_name)

	case *SystemHookKeyCreate:
		fg_col = termbox.ColorRed
		row = fmt.Sprintf("%14s %20s create ssh key",
			since(hook.Created_at),
			hook.Username)

	case *SystemHookKeyDesctroy:
		fg_col = termbox.ColorRed
		row = fmt.Sprintf("%14s %20s destroy ssh key",
			since(hook.Created_at),
			hook.Username)

	case *SystemHookUnknown:
		fg_col = termbox.ColorMagenta
		row = fmt.Sprintf("%s", hook.Evt)
	}

	drawRow(row, y, fg_col, bg_col)

}
Ejemplo n.º 5
0
func (this *Top) Run(args []string) (exitCode int) {
	cmdFlags := flag.NewFlagSet("top", flag.ContinueOnError)
	cmdFlags.Usage = func() { this.Ui.Output(this.Help()) }
	cmdFlags.StringVar(&this.zone, "z", ctx.ZkDefaultZone(), "")
	cmdFlags.StringVar(&this.topicPattern, "t", "", "")
	cmdFlags.DurationVar(&this.topInterval, "i", time.Second*5, "refresh interval")
	cmdFlags.StringVar(&this.clusterPattern, "c", "", "")
	cmdFlags.IntVar(&this.limit, "n", 0, "")
	cmdFlags.BoolVar(&this.skipIpPrefix, "shortip", true, "")
	cmdFlags.StringVar(&this.who, "who", "producer", "")
	cmdFlags.BoolVar(&this.dashboardGraph, "d", false, "")
	cmdFlags.DurationVar(&this.duration, "for", time.Hour, "")
	cmdFlags.BoolVar(&this.longFmt, "l", false, "")
	cmdFlags.BoolVar(&this.batchMode, "b", false, "")
	if err := cmdFlags.Parse(args); err != nil {
		return 1
	}

	if this.limit == 0 {
		termui.Init()
		this.limit = termui.TermHeight() - 6 // 5=header+footer+cursor line
		termui.Close()
	}

	if this.dashboardGraph {
		if this.topInterval.Seconds() < 20 {
			this.topInterval = 20 * time.Second
		}
		this.who = "both"
		go this.clusterOffsetSummary()
	}

	if this.who == "c" || this.who == "consumer" {
		if this.topInterval.Seconds() < 20 {
			this.topInterval = 20 * time.Second // consumer groups only refresh offset per minute
		}

	}

	this.brokers = make(map[string][]string)
	this.counters = make(map[string]float64)
	this.lastCounters = make(map[string]float64)
	this.partitions = make(map[string]int)
	this.consumerCounters = make(map[string]float64)
	this.totalMps = make([]float64, 0, 1000)
	this.totalConsumerMps = make([]float64, 0, 1000)

	zkzone := zk.NewZkZone(zk.DefaultConfig(this.zone, ctx.ZoneZkAddrs(this.zone)))
	zkzone.ForSortedClusters(func(zkcluster *zk.ZkCluster) {
		if !patternMatched(zkcluster.Name(), this.clusterPattern) {
			return
		}

		switch this.who {
		case "p", "producer":
			go this.clusterTopProducers(zkcluster)

		case "c", "consumer":
			go this.clusterTopConsumers(zkcluster)

		case "both":
			go this.clusterTopConsumers(zkcluster)
			go this.clusterTopProducers(zkcluster)

		default:
			this.Ui.Error(fmt.Sprintf("unknown type: %s", this.who))
		}
	})

	if this.dashboardGraph {
		this.drawDashboard()
		return
	}

	ticker := time.NewTicker(this.topInterval)
	defer ticker.Stop()
	keyboardPressed := make(chan struct{})
	go func() {
		var b []byte = make([]byte, 1)
		for {
			os.Stdin.Read(b)
			keyboardPressed <- struct{}{}
		}
	}()

	startAt := time.Now()
	for {
		select {
		case <-keyboardPressed:
		case <-ticker.C:
		}

		if time.Since(startAt) >= this.duration {
			break
		}

		if this.batchMode {
			this.Ui.Output(bjtime.TimeToString(bjtime.NowBj()))
		} else {
			refreshScreen()
		}

		// header
		if this.longFmt {
			this.Ui.Output(fmt.Sprintf("%-9s %15s %-30s %42s %20s %15s",
				this.who, "cluster", "brokers", "topic", "cum num", "mps")) // mps=msg per second
			this.Ui.Output(fmt.Sprintf(strings.Repeat("-", 136)))
		} else {
			this.Ui.Output(fmt.Sprintf("%-9s %20s %50s %20s %15s",
				this.who, "cluster", "topic", "cum num", "mps")) // mps=msg per second
			this.Ui.Output(fmt.Sprintf(strings.Repeat("-", 118)))
		}

		this.showAndResetCounters()
	}

	return
}