示例#1
0
func StoreDiffies(ch chan string) {
	db, err := sql.Open("postgres", DbInfo())
	if err != nil {
		panic(err)
	}
	defer db.Close()

	stmt, err := db.Prepare("INSERT INTO cpuinfo(time, userland, nice, system, idle, iowait, irq, softirq, steal, freq, temperature) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)")
	defer stmt.Close()

	ticker := time.NewTicker(time.Millisecond * 1000 * SECONDS)
	last := s.Cpu()
	for range ticker.C {
		stat := s.Cpu()
		temp := t.CpuTemp()
		freq := 100.0 * float64(i.CpuFreq()) / float64(i.MaxFreq())
		user, nice, system, idle, iowait, irq, softirq := diffies(stat, last)
		res, err := stmt.Exec(time.Unix(int64(stat.Timestamp), 0), user, nice, system, idle, iowait, irq, softirq, 0, freq, temp)
		if err != nil || res == nil {
			log.Fatal(err)
			break
		}
		ch <- fmt.Sprintf("time %d diff %2d user %2.1f nice %2.1f system %2.1f idle %2.1f iowait %2.1f irq %2.1f softirq %2.1f temperature %2.1f frequency %4.2f\n",
			stat.Timestamp, stat.Timestamp-last.Timestamp, user, nice, system, idle, iowait, irq, softirq, temp, freq)
		last = stat
	}
}
示例#2
0
func freq(w http.ResponseWriter, r *http.Request) {
	if !basicAuth(w, r) {
		return
	}
	m := make(map[string]int, 2)
	freq, max := c.CpuFreq(), c.MaxFreq()
	m["frequency"] = freq
	m["maximum"] = max

	reply(w, m)
}