当前位置: 首页 > 工具软件 > newLISP > 使用案例 >

newlisp 获取cpu信息

陆绪
2023-12-01

紧跟前文Linux CPU 负载度量公式,用newlisp写了一个获取本机cpu信息的小程序,每次都会调用REST API将数据发送给web server.

#!/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)


config.lsp很简单,两行配置:

(set 'host_name "beijinghome")
(set 'ip "192.168.1.101")



 类似资料: