Created
July 21, 2014 01:31
-
-
Save Preetam/ac5842c218d5160f8d0f to your computer and use it in GitHub Desktop.
meh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"github.com/PreetamJinka/sflow-go" | |
"github.com/PreetamJinka/udpchan" | |
"fmt" | |
"io/ioutil" | |
"net" | |
"runtime" | |
"time" | |
) | |
func main() { | |
stats, err := getCpuStats() | |
fmt.Printf("%+v, %#v\n", stats, err) | |
ch, _ := udpchan.Connect(":6343") | |
for _ = range time.Tick(time.Second) { | |
stats, err := getCpuStats() | |
if err == nil { | |
ch <- sflow.Encode(net.IPv4(127, 0, 0, 1), 1, stats.Uptime, | |
1, 1, 1, 1, []sflow.Record{*stats}) | |
} else { | |
fmt.Println(err) | |
} | |
} | |
} | |
func getCpuStats() (*sflow.HostCpuCounters, error) { | |
loadAvgBytes, err := ioutil.ReadFile("/proc/loadavg") | |
if err != nil { | |
return nil, err | |
} | |
c := sflow.HostCpuCounters{} | |
fmt.Sscanf(string(loadAvgBytes), "%f %f %f %d/%d", &c.Load1m, &c.Load5m, &c.Load15m, | |
&c.ProcsRunning, &c.ProcsTotal) | |
c.NumCPU = uint32(runtime.NumCPU()) | |
uptimeBytes, err := ioutil.ReadFile("/proc/uptime") | |
if err != nil { | |
return nil, err | |
} | |
fmt.Sscanf(string(uptimeBytes), "%d", &c.Uptime) | |
cpuStatBytes, err := ioutil.ReadFile("/proc/stat") | |
if err != nil { | |
return nil, err | |
} | |
fmt.Sscanf(string(cpuStatBytes), "cpu %d %d %d %d %d %d %d", &c.CpuUser, &c.CpuNice, &c.CpuSys, &c.CpuIdle, | |
&c.CpuWio, &c.CpuIntr, &c.CpuSoftIntr) | |
return &c, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment