您的位置:首页 > 运维架构 > Linux

newlisp 监控Linux进程 二

2013-11-05 12:41 141 查看
在前文newlisp 监控Linux进程 一 中,我已经实现了一个newlisp脚本,能够根据配置文件中的配置检查进程是否存在,并且将检查结果写到本地日志文件中。
本篇继续,当检查进程已经不存在后,要启动进程。但是每个进程启动的命令是不相同的,不可能将所有这些特殊命令都写在process.lsp文件中。最好是将代码以数据的方式卸载filter.lsp配置文件中。然后程序运行时,从配置文件中读取出来,并当作代码执行。由于目前代码比较简单,就是运行一句话,所以暂时还不需要宏这个大杀器出场,只需要用eval函数即可。
现在filter.lsp的配置改了一下:
(set 'filters (list '("dispatch"
"redmine"
(exec "service redmine start"))))
filters这里是一个list, 包含了唯一一个元素也是list,这个唯一的元素list拥有三个元素,第三个也是一个list。前面用了单引号防止过早求值。
process.lsp代码也修改了一点点:
#!/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))
(> l 3))

(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))
(begin
(add-log (append (sub-list 1) " is alive\n"))
)
(begin
(add-log (append (sub-list 1) " is dead\n"))
(eval (sub-list 2)))))

(exit)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: