newlisp 监控Linux进程 一

之前我的newlisp 监控redmine中描述了监控redmine进程的方法,今天更进一步,添加一个配置文件,里面描述了要监控的进程list,这样每次逐个检查本机的进程.

先来实现个简单的检查,一个函数check-proces,接受一个字符串,用来检查进程是否存在

#!/usr/bin/newlisp

(define (check-process filter-str)
  (set 'r (exec (append "ps -def | grep " filter-str)))
  (set 'l (length r))
  (> l 3))


(if (check-process "dispatch")
    (println "redmine is alive")
  (println "redmine is dead"))
(exit)

好,添加一个配置文件叫做filter.lsp, 下面的filters是一个list,里面每个元素也是一个list,并且分两部分,一是用来检查进程的字符串,二是要检查进程的有意义的名称,可以用于写日志。

(set 'filters (list '("dispatch" "redmine")))    

然后之前的process.lsp内容修改为:

#!/usr/bin/newlisp

(load "/opt/detector/filter.lsp")

(define (check-process filter-str)
  (set 'r (exec (append "ps -def | grep " filter-str)))
  (set 'l (length r))
  (= 3 l))

(dolist (sub-list filters)
  (if (check-process (first sub-list))
      (println (append (sub-list 1) " is alive"))
    (println (append (sub-list 1) " is dead"))))

(exit)

ok, 再进一步,写日志, 添加了add-log函数

#!/usr/bin/newlisp

(set 'cur-path "/opt/detector")
(load (append cur-path "/filter.lsp"))

(define (check-process filter-str)
  (set 'r (exec (append "ps -def | grep " filter-str)))
  (set 'l (length r))
  (= 3 l))

(define (add-log msg)
  (println msg)
  (append-file (append cur-path "/process.log") (append "\n" (string (now 480)) " "))
  (append-file (append cur-path "/process.log") (append  ": " msg))
  )

(dolist (sub-list filters)
  (if (check-process (first sub-list))
      (add-log (append (sub-list 1) " is alive\n"))
    (add-log (append (sub-list 1) " is dead\n"))))

(exit)




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值