newlisp 监控Linux进程 一
2013-11-05 11:18
176 查看
之前我的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)
先来实现个简单的检查,一个函数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)
相关文章推荐
- newlisp 监控Linux进程 四
- newlisp 监控Linux进程 三
- newlisp 监控Linux进程 二
- newlisp 监控Linux进程 四
- 如何灵活运用Linux 进程资源监控和进程限制
- 使用shell 脚本对 Linux 系统进程的CPU使用率与内存资源进行监控
- Linux 运行进程实时监控pidstat命令详解
- Linux进程实时IO监控iotop命令详解
- 使用 shell 脚本对 Linux 系统和进程资源进行监控
- Linux 运行进程实时监控pidstat命令详解
- 管理Linux进程的监控类
- Linux 监控文件被什么进程修改
- python实现监控linux性能及进程消耗性能的方法
- 使用Shell来监控linux系统和进程(1)
- linux系统使用python监控apache服务器进程脚本分享
- linux 进程监控和自动重启的简单实现
- 使用 shell 脚本对 Linux 系统和进程资源进行监控
- LINUX C:创建与监控多个子进程
- Linux 运行进程实时监控pidstat命令详解
- 使用Shell脚本对Linux系统和进程资源进行监控(转)