1
0
Fork 0
This commit is contained in:
Sangbum Kim 2017-09-06 07:53:17 +09:00
parent b3197b61e9
commit ec0c7112b1
1 changed files with 42 additions and 42 deletions

82
main.go
View File

@ -2,6 +2,7 @@ package main
import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
@ -15,7 +16,7 @@ import (
"syscall"
"time"
"github.com/coreos/go-systemd/daemon"
"github.com/coreos/go-systemd/daemon"
"github.com/shirou/gopsutil/cpu"
)
@ -30,15 +31,13 @@ var (
suffix = [...]int{auto, auto, auto, auto, auto, auto}
)
type notifyType string
const (
DaemonStarted =notifyType("READY=1")
DaemonStopping =notifyType("STOPPING=1")
DaemonStarted = notifyType("READY=1")
DaemonStopping = notifyType("STOPPING=1")
)
func NotifyDaemon(status notifyType) {
daemon.SdNotify(false, string(status))
}
@ -70,34 +69,30 @@ type TempetureChange struct {
Tempeture float64
}
func getProcessorInfo(processorId int) *Processor {
matches, err := filepath.Glob(fmt.Sprintf("/sys/devices/platform/coretemp.%d/hwmon/hwmon?", processorId))
if err != nil {
panic(err)
}
return &Processor{
Id: processorId,
TempeturePath: matches[0],
func getProcessorInfo(processorId int) (*Processor, error) {
if matches, err := filepath.Glob(fmt.Sprintf("/sys/devices/platform/coretemp.%d/hwmon/hwmon?", processorId)); err != nil {
return nil, err
} else if matches == nil {
return nil, errors.New("hwmon not found!")
} else {
return &Processor{
Id: processorId,
TempeturePath: matches[0],
}, nil
}
}
func ReadTempeture(path string, senseChan chan<- float64, waiter *sync.WaitGroup) {
func ReadTempeture(path string, senseChan chan<- float64, errorChan chan<- error, waiter *sync.WaitGroup) {
defer waiter.Done()
dat, err := ioutil.ReadFile(path)
if err != nil {
senseChan <- 0.0
return
if dat, err := ioutil.ReadFile(path); err != nil {
errorChan <- err
} else if tempetureSense, err := strconv.Atoi(strings.TrimSpace(string(dat))); err != nil {
errorChan <- err
} else {
senseChan <- float64(tempetureSense) / 1000.0
}
tempetureSense, err := strconv.Atoi(strings.TrimSpace(string(dat)))
if err != nil {
senseChan <- 0.0
return
}
senseChan <- float64(tempetureSense) / 1000.0
return
}
func CpuTempetureMonitoring(info *Processor, notifier chan<- TempetureChange, ctx context.Context, waiter *sync.WaitGroup) {
waiter.Add(1)
func CpuTempetureMonitoring(info *Processor, notifier chan<- TempetureChange, errorChan chan<- error, ctx context.Context, waiter *sync.WaitGroup) {
defer waiter.Done()
tempeturePathGlob := path.Join(info.TempeturePath, "temp?_input")
ticker := time.Tick(3 * time.Second)
@ -113,7 +108,7 @@ func CpuTempetureMonitoring(info *Processor, notifier chan<- TempetureChange, ct
// exclude package temp
for _, path := range matches[1:] {
tempetureReadWaiter.Add(1)
go ReadTempeture(path, queue, tempetureReadWaiter)
go ReadTempeture(path, queue, errorChan, tempetureReadWaiter)
}
tempetureReadWaiter.Wait()
close(queue)
@ -132,8 +127,7 @@ func CpuTempetureMonitoring(info *Processor, notifier chan<- TempetureChange, ct
}
}
}
func CpuTempetureScraper(processorCount int, notifier <-chan TempetureChange,errorChan chan<- error, ctx context.Context, waiter *sync.WaitGroup) {
waiter.Add(1)
func CpuTempetureScraper(processorCount int, notifier <-chan TempetureChange, errorChan chan<- error, ctx context.Context, waiter *sync.WaitGroup) {
defer waiter.Done()
pastFan := make([]int, processorCount)
for {
@ -178,19 +172,27 @@ func CpuTempetureScraper(processorCount int, notifier <-chan TempetureChange,err
func main() {
var (
processorCount = getProcessorCount()
processorCount = 0 //getProcessorCount()
ctx, canceled = context.WithCancel(context.Background())
waiter = &sync.WaitGroup{}
exitSignal = make(chan os.Signal, 1)
errorChan = make(chan error, 1)
errorChan = make(chan error, 1)
tempetureChange = make(chan TempetureChange)
)
for i := 0; i < processorCount; i++ {
info := getProcessorInfo(i)
go CpuTempetureMonitoring(info, tempetureChange, ctx, waiter)
if processorCount == 0 {
errorChan <- errors.New("cpu not found!")
}
go CpuTempetureScraper(processorCount, tempetureChange,errorChan, ctx, waiter)
for i := 0; i < processorCount; i++ {
if info, err := getProcessorInfo(i); err != nil {
errorChan <- err
} else {
waiter.Add(1)
go CpuTempetureMonitoring(info, tempetureChange, errorChan, ctx, waiter)
}
}
waiter.Add(1)
go CpuTempetureScraper(processorCount, tempetureChange, errorChan, ctx, waiter)
defer waiter.Wait()
signal.Notify(exitSignal, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
NotifyDaemon(DaemonStarted)
@ -199,13 +201,11 @@ func main() {
select {
case <-ctx.Done():
fmt.Println("Service request to close this application")
return
case err:= <-errorChan:
case err := <-errorChan:
canceled()
fmt.Printf("error! %s", err.Error())
fmt.Printf("error! %s\n", err.Error())
case sysSignal := <-exitSignal:
canceled()
fmt.Printf("SYSCALL! %s", sysSignal.String())
fmt.Printf("SYSCALL! %s\n", sysSignal.String())
}
}