Esempio n. 1
0
// below methods are intend to maintain engine itself (remove needless connection, dispatch queue).
func (self *Momonga) RunMaintenanceThread() {
	for {
		// TODO: implement $SYS here.
		//		log.Debug("Current Conn: %d", len(self.Connections))
		//		for i := range self.Connections {
		//			log.Debug("  %+v", self.Connections[i])
		//		}

		//		select {
		//			case tuple := <- self.SysUpdateRequest:
		//		default:
		// TODO: だれかがsubscribeしてる時だけ出力する
		// TODO: implement whole stats
		if self.EnableSys {
			now := time.Now()
			uptime := int(now.Sub(self.Started) / 1e9)
			Metrics.System.Broker.Uptime.Set(int64(uptime))

			self.SendMessage("$SYS/broker/broker/uptime", []byte(fmt.Sprintf("%d", util.GetIntValue(Metrics.System.Broker.Uptime))), 0)
			self.SendMessage("$SYS/broker/broker/time", []byte(fmt.Sprintf("%d", now.Unix())), 0)
			self.SendMessage("$SYS/broker/clients/connected", []byte(fmt.Sprintf("%d", util.GetIntValue(Metrics.System.Broker.Clients.Connected))), 0)
			self.SendMessage("$SYS/broker/messages/received", []byte(fmt.Sprintf("%d", util.GetIntValue(Metrics.System.Broker.Messages.Received))), 0)
			self.SendMessage("$SYS/broker/messages/sent", []byte(fmt.Sprintf("%d", util.GetIntValue(Metrics.System.Broker.Messages.Sent))), 0)
			self.SendMessage("$SYS/broker/messages/stored", []byte(fmt.Sprintf("%d", 0)), 0)
			self.SendMessage("$SYS/broker/messages/publish/dropped", []byte(fmt.Sprintf("%d", 0)), 0)
			self.SendMessage("$SYS/broker/messages/retained/count", []byte(fmt.Sprintf("%d", 0)), 0)
			self.SendMessage("$SYS/broker/messages/inflight", []byte(fmt.Sprintf("%d", 0)), 0)
			self.SendMessage("$SYS/broker/clients/total", []byte(fmt.Sprintf("%d", 0)), 0)
			self.SendMessage("$SYS/broker/clients/maximum", []byte(fmt.Sprintf("%d", 0)), 0)
			self.SendMessage("$SYS/broker/clients/disconnected", []byte(fmt.Sprintf("%d", 0)), 0)
			self.SendMessage("$SYS/broker/load/bytes/sent", []byte(fmt.Sprintf("%d", 0)), 0)
			self.SendMessage("$SYS/broker/load/bytes/received", []byte(fmt.Sprintf("%d", 0)), 0)
			self.SendMessage("$SYS/broker/subscriptions/count", []byte(fmt.Sprintf("%d", 0)), 0)
		}

		time.Sleep(time.Second)
	}
}
Esempio n. 2
0
File: mon.go Progetto: aonx/momonga
func init() {
	result = make(map[string]interface{})

	go func() {
		lastCpu := sigar.Cpu{}
		cpu := sigar.Cpu{}
		lastCpu.Get()

		c, _ := client.NewClient(&client.ClientConfig{
			Database: "test",
		})

		for {
			Metrics.NumGoroutine.Set(int64(runtime.NumGoroutine()))
			Metrics.NumCgoCall.Set(int64(runtime.NumGoroutine()))
			Metrics.Uptime.Set(time.Now().Unix())

			Metrics.MessageSentPerSec.Set(util.GetIntValue(Metrics.System.Broker.Messages.Sent))
			if util.GetIntValue(Metrics.System.Broker.Clients.Connected) > 0 {
				Metrics.GoroutinePerConn.Set(float64(util.GetIntValue(Metrics.NumGoroutine) / util.GetIntValue(Metrics.System.Broker.Clients.Connected)))
			}

			mem := sigar.Mem{}
			mem.Get()

			Metrics.MemFree.Set(int64(mem.Free))
			Metrics.MemUsed.Set(int64(mem.Used))
			Metrics.MemActualFree.Set(int64(mem.ActualFree))
			Metrics.MemActualUsed.Set(int64(mem.ActualUsed))
			Metrics.MemTotal.Set(int64(mem.Total))

			load := sigar.LoadAverage{}
			load.Get()
			Metrics.LoadOne.Set(float64(load.One))
			Metrics.LoadFive.Set(float64(load.Five))
			Metrics.LoadFifteen.Set(float64(load.Fifteen))

			cpu.Get()

			Metrics.CpuUser.Set(float64(cpu.User - lastCpu.User))
			Metrics.CpuNice.Set(float64(cpu.Nice - lastCpu.Nice))
			Metrics.CpuSys.Set(float64(cpu.Sys - lastCpu.Sys))
			Metrics.CpuIdle.Set(float64(cpu.Idle - lastCpu.Idle))
			Metrics.CpuWait.Set(float64(cpu.Wait - lastCpu.Wait))
			Metrics.CpuIrq.Set(float64(cpu.Irq - lastCpu.Irq))
			Metrics.CpuSoftIrq.Set(float64(cpu.SoftIrq - lastCpu.SoftIrq))
			Metrics.CpuStolen.Set(float64(cpu.Stolen - lastCpu.Stolen))
			Metrics.CpuTotal.Set(float64(cpu.Total() - lastCpu.Total()))

			expvar.Do(cb)
			w := &client.Series{
				Name: "test",
			}
			p := []interface{}{}
			for k, v := range result {
				w.Columns = append(w.Columns, k)
				p = append(p, v)
			}
			w.Points = [][]interface{}{p}
			e := c.WriteSeries([]*client.Series{w})

			if e != nil {
				fmt.Printf("error: %s", e)
			}

			lastCpu = cpu
			time.Sleep(time.Second)
		}
	}()
}