2017-09-11 02:24:28 +09:00
|
|
|
package consumer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"amuz.es/src/infra/cpu_ctrl/util"
|
2017-09-11 07:41:59 +09:00
|
|
|
//"time"
|
2017-09-11 02:24:28 +09:00
|
|
|
"amuz.es/src/infra/cpu_ctrl/processor"
|
|
|
|
"amuz.es/src/infra/cpu_ctrl/logger"
|
2017-09-11 07:41:59 +09:00
|
|
|
//"github.com/influxdata/influxdb/client/v2"
|
2017-09-11 08:39:41 +09:00
|
|
|
"github.com/influxdata/influxdb/client/v2"
|
|
|
|
"time"
|
|
|
|
"log"
|
2017-09-11 02:24:28 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-09-11 07:44:30 +09:00
|
|
|
influxLogger = logger.NewLogger("influx")
|
2017-09-11 02:24:28 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
type influxMetric struct {
|
|
|
|
host string
|
2017-09-11 07:41:59 +09:00
|
|
|
processorCount int
|
2017-09-11 02:24:28 +09:00
|
|
|
handler util.Handler
|
|
|
|
fanSpeedConsumer chan processor.FanspeedInfo
|
|
|
|
tempetureConsumer chan processor.TempetureInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
type InfluxMetric interface {
|
|
|
|
FanSpeedConsumer() chan<- processor.FanspeedInfo
|
|
|
|
TempetureConsumer() chan<- processor.TempetureInfo
|
2017-09-11 07:41:59 +09:00
|
|
|
StartLogging()
|
2017-09-11 02:24:28 +09:00
|
|
|
}
|
|
|
|
|
2017-09-11 07:41:59 +09:00
|
|
|
func NewInfluxMetric(host string, processorCount int, handler util.Handler) InfluxMetric {
|
2017-09-11 02:24:28 +09:00
|
|
|
return &influxMetric{
|
|
|
|
host: host,
|
2017-09-11 07:41:59 +09:00
|
|
|
processorCount: processorCount,
|
2017-09-11 02:24:28 +09:00
|
|
|
handler: handler,
|
|
|
|
fanSpeedConsumer: make(chan processor.FanspeedInfo, 1),
|
|
|
|
tempetureConsumer: make(chan processor.TempetureInfo, 1),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *influxMetric) FanSpeedConsumer() chan<- processor.FanspeedInfo { return m.fanSpeedConsumer }
|
|
|
|
func (m *influxMetric) TempetureConsumer() chan<- processor.TempetureInfo { return m.tempetureConsumer }
|
|
|
|
|
2017-09-11 07:41:59 +09:00
|
|
|
func (m *influxMetric) StartLogging() {
|
2017-09-11 02:24:28 +09:00
|
|
|
defer m.handler.DecreaseWait()
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
m.handler.NotifyError(err.(error))
|
|
|
|
}
|
|
|
|
}()
|
2017-09-11 07:41:59 +09:00
|
|
|
defer close(m.fanSpeedConsumer)
|
|
|
|
defer close(m.tempetureConsumer)
|
|
|
|
|
2017-09-11 08:39:41 +09:00
|
|
|
defer influxLogger.Info("Metric logging stopped")
|
|
|
|
influxLogger.Info("Metric logging started")
|
|
|
|
|
|
|
|
var influxDbConn client.Client
|
2017-09-11 02:24:28 +09:00
|
|
|
for {
|
2017-09-11 08:39:41 +09:00
|
|
|
conn, err := client.NewUDPClient(client.UDPConfig{Addr: m.host,})
|
|
|
|
if err != nil {
|
|
|
|
influxLogger.Error(err)
|
|
|
|
} else {
|
|
|
|
influxDbConn = conn
|
|
|
|
break
|
|
|
|
}
|
|
|
|
cont := time.After(3 * time.Second)
|
2017-09-11 02:24:28 +09:00
|
|
|
select {
|
2017-09-11 08:39:41 +09:00
|
|
|
case <-cont:
|
|
|
|
continue
|
2017-09-11 02:24:28 +09:00
|
|
|
case <-m.handler.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2017-09-11 08:39:41 +09:00
|
|
|
|
|
|
|
// Create a new point batch
|
|
|
|
batchPoint, err := client.NewBatchPoints(client.BatchPointsConfig{
|
|
|
|
Database: "core",
|
|
|
|
Precision: "s",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
ticker := time.Tick(time.Second)
|
|
|
|
pointList := make([]*client.Point, 0)
|
|
|
|
|
|
|
|
for {
|
|
|
|
pointList = pointList[:cap(pointList)]
|
|
|
|
checker:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker:
|
|
|
|
break checker
|
|
|
|
case changedSpeed := <-m.fanSpeedConsumer:
|
|
|
|
influxLogger.Debugf("id %d speed %d", changedSpeed.Id, changedSpeed.FanSpeed)
|
|
|
|
case changedTempeture := <-m.tempetureConsumer:
|
|
|
|
influxLogger.Debugf("id %d temp %f", changedTempeture.Id, changedTempeture.Tempeture)
|
|
|
|
case <-m.handler.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
batchPoint.AddPoints(pointList)
|
|
|
|
|
|
|
|
if err := influxDbConn.Write(batchPoint); err != nil {
|
|
|
|
influxLogger.Warn(err)
|
|
|
|
}
|
|
|
|
pointList = pointList[:0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *influxMetric) getTempeturePoint(info processor.TempetureInfo) (*client.Point, error) {
|
|
|
|
|
2017-09-11 07:41:59 +09:00
|
|
|
//// Create a point and add to batch
|
|
|
|
//tags := map[string]string{"cpu": "cpu-total"}
|
|
|
|
//fields := map[string]interface{}{
|
|
|
|
// "idle": 10.1,
|
|
|
|
// "system": 53.3,
|
|
|
|
// "user": 46.6,
|
|
|
|
//}
|
2017-09-11 02:24:28 +09:00
|
|
|
//
|
2017-09-11 07:41:59 +09:00
|
|
|
//pt, err := client.NewPoint("cpu_usage", tags, fields, time.Now())
|
|
|
|
//if err != nil {
|
|
|
|
// log.Fatal(err)
|
|
|
|
//}
|
2017-09-11 08:39:41 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *influxMetric) getFanspeedPoint(info processor.FanspeedInfo) (*client.Point, error) {
|
|
|
|
|
|
|
|
//// Create a point and add to batch
|
|
|
|
//tags := map[string]string{"cpu": "cpu-total"}
|
|
|
|
//fields := map[string]interface{}{
|
|
|
|
// "idle": 10.1,
|
|
|
|
// "system": 53.3,
|
|
|
|
// "user": 46.6,
|
|
|
|
//}
|
2017-09-11 07:41:59 +09:00
|
|
|
//
|
2017-09-11 08:39:41 +09:00
|
|
|
//pt, err := client.NewPoint("cpu_usage", tags, fields, time.Now())
|
|
|
|
//if err != nil {
|
2017-09-11 07:41:59 +09:00
|
|
|
// log.Fatal(err)
|
2017-09-11 02:24:28 +09:00
|
|
|
//}
|
|
|
|
}
|