2017-12-16 13:27:36 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CSVDump is responsible for dumping CPU data in CSV format.
|
|
|
|
type CSVDump struct {
|
|
|
|
file string
|
|
|
|
}
|
|
|
|
|
|
|
|
// New CSVDump creates new CSVDump object.
|
|
|
|
func NewCSVDump() (*CSVDump, error) {
|
|
|
|
now := time.Now()
|
|
|
|
filename := now.Format("20060102_150405.csv")
|
|
|
|
|
|
|
|
fd, err := os.Create(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-18 10:37:18 +00:00
|
|
|
fd.WriteString("timestamp,cpu,mem,rx,tx\n")
|
2017-12-16 13:27:36 +00:00
|
|
|
fd.Close()
|
|
|
|
|
|
|
|
return &CSVDump{
|
|
|
|
file: filename,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-01-18 10:37:18 +00:00
|
|
|
// Add adds new monitored values to the CSV dump.
|
|
|
|
func (c *CSVDump) Add(cpu float64, memUsed uint64, rx, tx int64) {
|
2017-12-16 13:27:36 +00:00
|
|
|
fd, err := os.OpenFile(c.file, os.O_APPEND|os.O_WRONLY|os.O_SYNC, 0644)
|
|
|
|
if err != nil {
|
|
|
|
// we just created this file, it should not dissapear :D
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
2018-01-18 10:37:18 +00:00
|
|
|
fd.WriteString(fmt.Sprintf("%d,%f,%d,%d,%d\n", now.Unix(), cpu, memUsed, rx, tx))
|
2017-12-16 13:27:36 +00:00
|
|
|
fd.Close()
|
|
|
|
}
|