diff --git a/consumer/influx_stat.go b/consumer/influx_stat.go index 4480f30..e3bc257 100644 --- a/consumer/influx_stat.go +++ b/consumer/influx_stat.go @@ -13,6 +13,11 @@ var ( influxLogger = logger.NewLogger("influx") ) +type data struct { + Tempeture float64 + FanSpeed int +} + type influxMetric struct { host string processorCount int @@ -80,19 +85,19 @@ func (m *influxMetric) StartLogging() { if err != nil { panic(err) } - ticker := time.Tick(time.Second*2) - fanspeedList, tempetureList := make([]processor.FanspeedInfo, 0, 0), make([]processor.TempetureInfo, 0, 0) + ticker := time.Tick(time.Second) + dataList := make([]data,m.processorCount) for { select { case <-ticker: - fanspeedListTemp, tempetureListTemp := fanspeedList, tempetureList - fanspeedList, tempetureList = make([]processor.FanspeedInfo, 0, 0), make([]processor.TempetureInfo, 0, 0) - go m.sendPoint(influxDbConn, batchPoint, fanspeedListTemp, tempetureListTemp) + sendData := make([]data,m.processorCount) + copy(sendData,dataList) + go m.sendPoint(influxDbConn, batchPoint, sendData) case changedSpeed := <-m.fanSpeedConsumer: - fanspeedList = append(fanspeedList, changedSpeed) + dataList[changedSpeed.Id].FanSpeed =changedSpeed.FanSpeed case changedTempeture := <-m.tempetureConsumer: - tempetureList = append(tempetureList, changedTempeture) + dataList[changedTempeture.Id].Tempeture =changedTempeture.Tempeture case <-m.handler.Done(): return } @@ -101,22 +106,14 @@ func (m *influxMetric) StartLogging() { func (m *influxMetric) sendPoint( influxDbConn client.Client, batchPoint client.BatchPoints, - fanspeeds []processor.FanspeedInfo, tempetures []processor.TempetureInfo) { + datas []data) { pointList := make([]*client.Point, 0, 0) - - for _, fanspeed := range fanspeeds { - if point, err := m.getFanspeedPoint(fanspeed); err == nil { + at:=time.Now() + for id, data := range datas { + if point, err := m.getPoint(id,data,at); err == nil { pointList = append(pointList, point) } else { - influxLogger.Debugf("id %d speed err %s", fanspeed.Id, err) - } - } - - for _, tempeture := range tempetures { - if point, err := m.getTempeturePoint(tempeture); err == nil { - pointList = append(pointList, point) - } else { - influxLogger.Debugf("id %d tempeture err %s", tempeture.Id, err) + influxLogger.Debugf("id %d err %s", id, err) } } @@ -131,24 +128,14 @@ func (m *influxMetric) sendPoint( } -func (m *influxMetric) getTempeturePoint(info processor.TempetureInfo) (*client.Point, error) { +func (m *influxMetric) getPoint(id int,data data,at time.Time) (*client.Point, error) { // Create a point and add to batch - tags := map[string]string{"processor": strconv.Itoa(info.Id)} + tags := map[string]string{"processor": strconv.Itoa(id)} fields := map[string]interface{}{ - "tempeture": info.Tempeture, + "tempeture": data.Tempeture, + "fan": data.FanSpeed, } - return client.NewPoint("processor_tempeture", tags, fields, info.At) -} - -func (m *influxMetric) getFanspeedPoint(info processor.FanspeedInfo) (*client.Point, error) { - - // Create a point and add to batch - tags := map[string]string{"processor": strconv.Itoa(info.Id)} - fields := map[string]interface{}{ - "noob": info.FanSpeed, - } - - return client.NewPoint("processor_cooling_fanspeed", tags, fields, info.At) -} + return client.NewPoint("processor", tags, fields, at) +} \ No newline at end of file diff --git a/main.go b/main.go index 66e74ba..2d395e7 100644 --- a/main.go +++ b/main.go @@ -117,8 +117,8 @@ func main() { handler.NotifyError(errors.New("cpu not found!")) } var ( - tempetureChannel = make(chan processor.TempetureInfo) - fanspeedChannel = make(chan processor.FanspeedInfo) + tempetureChannel = make(chan processor.TempetureInfo,1) + fanspeedChannel = make(chan processor.FanspeedInfo,1) ) processors = make([]processor.Processor, 0, processorCount) for i := 0; i < processorCount; i++ { diff --git a/processor/processor.go b/processor/processor.go index d08582b..2116db0 100644 --- a/processor/processor.go +++ b/processor/processor.go @@ -142,39 +142,35 @@ func (p *processor) StartMonitoring() { SELECT mean("tempeture") as tempeture FROM "processor_tempeture" WHERE $timeFilter GROUP BY "processor", time(5s) fill(previous) */ highestTemp = p.getMaxTempetureOnProcessor(tempeturePathGlob) - if highestTemp != p.tempeture { - if p.tempetureChanged != nil { - select { - case p.tempetureChanged <- TempetureInfo{ - Id: p.id, - Tempeture: highestTemp, - At: now, - }: - default: - } + if p.tempetureChanged != nil { + select { + case p.tempetureChanged <- TempetureInfo{ + Id: p.id, + Tempeture: highestTemp, + At: now, + }: + default: } - p.tempeture = highestTemp - log.Debugf("processor %d : tempeture changed %f", p.id, highestTemp) } + p.tempeture = highestTemp + log.Debugf("processor %d : tempeture changed %f", p.id, highestTemp) /* SELECT mean("noob") as noob FROM "processor_cooling_fanspeed" WHERE $timeFilter GROUP BY "processor", time(5s) fill(previous) CREATE CONTINUOUS QUERY "processor_cooling_fanspeed_5s" ON "core" BEGIN SELECT max("noob") AS "mean_noob" INTO "processor_cooling_fanspeed_5s" FROM "processor_cooling_fanspeed" GROUP BY "processor", time(5s) fill(previous) END */ fanspeed = p.normalizeFanspeed(p.fanController.Update(highestTemp)) - if fanspeed != p.fanSpeed { - if p.fanSpeedChanged != nil { - select { - case p.fanSpeedChanged <- FanspeedInfo{ - Id: p.id, - FanSpeed: fanspeed, - At: now, - }: - default: - } + if p.fanSpeedChanged != nil { + select { + case p.fanSpeedChanged <- FanspeedInfo{ + Id: p.id, + FanSpeed: fanspeed, + At: now, + }: + default: } - p.fanSpeed = fanspeed - log.Debugf("processor %d : fan changed 0x%x", p.id, fanspeed) } + p.fanSpeed = fanspeed + log.Debugf("processor %d : fan changed 0x%x", p.id, fanspeed) case <-p.handler.Done(): return }