#!/usr/bin/newlisp  
  
(load "config.lsp")  
  
(define (add-log msg)  
  (append-file "cpu.log" (append "n" (string (now 480)) " "))  
  (append-file "cpu.log" (append  ": " msg))  
  )  
  
;; return a list  
;; which contains total_jiffies and work_jiffies  
(define (check-cpu)  
  (set 'in-file (open "/proc/stat" "read"))  
  (set 'line (read-line in-file))  
  (set 'r (parse line))  
  (close in-file)  
  (set 'total_jiffies 0)  
  (println r)  
  (set 'i 1)  
  (do-while (< i 8)  
        (set 'total_jiffies (+ total_jiffies (int (nth i r))))  
        (inc i)  
        )  
  (set 'work_jiffies 0)  
  (set 'i 1)  
  (do-while (< i 3)  
        (set 'work_jiffies (+ work_jiffies (int (nth i r))))  
        (inc i)  
        )  
  (list total_jiffies work_jiffies)  
  )  
  
(set 'r2 (check-cpu))  
(set 'r3 (post-url "http://localhost/wind_tunnel/api/post/cpu"  
      (format "ip=%s&
hostname=%s&epoch=%lld&totalJiffies=%lld&workJiffies=%lld" ip host_name 123456789 (nth 0 r2) (nth 1 r2))))  
(add-log r3)  
  
(exit)