本节尝试将监控进程的方式交给调用者。主要有两个原因:

1. 调用者传递判定代码,这样可以根据不同的平台进行变化,比如windows肯定没有ps -def 命令。这样可以适应更多的情况

2. ps -def | grep ***的方式,在newlisp中返回的表的元素数目少数时候会少一个元素,比如我在监控mongodb的时候遇到下面的元素有时候得不到。

"root     10658 10655  0 00:46 ?        00:00:00 grep mongodb"
因此如果还用

(> l 2)
来判断是不正确的,会误报。

先调整一下filter.lsp,如下例:

(set 'filters (list '((exec "pidof mongod") "mongodb" (exec "service mongodb start"))                                                                                            
                    ))     
第一个元素现在是一个list,运行pidof命令,如果找到mongod进程,则返回含有进程id的list,否则返回()

再修改一下process.lsp代码:

#!/usr/bin/newlisp                                                                                                                                                               
                                                                                                                                                                                 
(set 'cur-path "/opt/detector")                                                                                                                                                  
(load (append cur-path "/filter.lsp"))                                                                                                                                           
(load (append cur-path "/config.lsp"))                                                                                                                                           
                                                                                                                                                                                 
(define (check-process check-cmd)                                                                                                                                                
  (set 's (eval check-cmd))                                                                                                                                                      
  (add-log (string s))                                                                                                                                                           
  s                                                                                                                                                                              
)                                                                                                                                                                                
                                                                                                                                                                                 
(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))                                                                                                              
  )                                                                                                                                                                              
                                                                                                                                                                                 
(define (call-api process-name process-status)                                                                                                                                   
  (set 'data (format "ip=%s&hostName=%s&type=proc_%s&values=%lld|%f" ip host_name process-name (date-value) process-status))                                                     
  (println data)                                                                                                                                                                 
  (set 'r3 (post-url (format "http://%s/wind_tunnel/api/post/data" server) data))                                                                                                
  (println r3))                                                                                                                                                                  
                                                                                                                                                                                 
(dolist (sub-list filters)                                                                                                                                                       
  (if (check-process (first sub-list))                                                                                                                                           
      (begin                                                                                                                                                                     
       (add-log (append (sub-list 1) " is alive\n"))                                                                                                                             
       (call-api (sub-list 1) 1.0)                                                                                                                                               
       )                                                                                                                                                                         
    (begin                                                                                                                                                                       
     (add-log (append (sub-list 1) " is dead\n"))                                                                                                                                
     (eval (sub-list 2))                                                                                                                                                         
     (call-api (sub-list 1) 0))))                                                                                                                                                
                                                                                                                                                                                 
(exit)     

好,现在MongoDB进程监控正常了,不会误报。并且可以在filter.lsp中配置各种专用命令来检测进程。

Logo

更多推荐