func (this *Zktop) displayZoneTop(zkzone *zk.ZkZone) { if this.batchMode { this.Ui.Output(fmt.Sprintf("%s %s", zkzone.Name(), bjtime.NowBj())) } else { this.Ui.Output(color.Green(zkzone.Name())) } header := "VER SERVER PORT M OUTST RECVD SENT CONNS ZNODES LAT(MIN/AVG/MAX)" this.Ui.Output(header) stats := zkzone.RunZkFourLetterCommand("stat") sortedHosts := make([]string, 0, len(stats)) for hp, _ := range stats { sortedHosts = append(sortedHosts, hp) } sort.Strings(sortedHosts) for _, hostPort := range sortedHosts { host, port, err := net.SplitHostPort(hostPort) if err != nil { panic(err) } stat := zk.ParseStatResult(stats[hostPort]) if stat.Mode == "" { if this.batchMode { stat.Mode = "E" } else { stat.Mode = color.Red("E") } } else if stat.Mode == "L" && !this.batchMode { stat.Mode = color.Blue(stat.Mode) } var sentQps, recvQps int if lastRecv, present := this.lastRecvs[hostPort]; present { r1, _ := strconv.Atoi(stat.Received) r0, _ := strconv.Atoi(lastRecv) recvQps = (r1 - r0) / int(this.refreshInterval.Seconds()) s1, _ := strconv.Atoi(stat.Sent) s0, _ := strconv.Atoi(this.lastSents[hostPort]) sentQps = (s1 - s0) / int(this.refreshInterval.Seconds()) } this.Ui.Output(fmt.Sprintf("%-15s %-15s %5s %1s %6s %16s %16s %5s %7s %s", stat.Version, // 15 host, // 15 port, // 5 stat.Mode, // 1 stat.Outstanding, // 6 fmt.Sprintf("%s/%d", stat.Received, recvQps), // 16 fmt.Sprintf("%s/%d", stat.Sent, sentQps), // 16 stat.Connections, // 5 stat.Znodes, // 7 stat.Latency, )) this.lastRecvs[hostPort] = stat.Received this.lastSents[hostPort] = stat.Sent } }
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() } } }
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 }