1
0
Fork 0

수치 매초 보내도록 함

This commit is contained in:
Sangbum Kim 2017-09-24 20:11:39 +09:00
parent e35a6df56d
commit b297c3d6ed
3 changed files with 45 additions and 62 deletions

View File

@ -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)
}

View File

@ -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++ {

View File

@ -142,7 +142,6 @@ 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{
@ -155,13 +154,11 @@ func (p *processor) StartMonitoring() {
}
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{
@ -174,7 +171,6 @@ func (p *processor) StartMonitoring() {
}
p.fanSpeed = fanspeed
log.Debugf("processor %d : fan changed 0x%x", p.id, fanspeed)
}
case <-p.handler.Done():
return
}