一:简介
使用Python psutil模块,查找java模块,并获取启动命令,结合zabbix监控自动监控。点击下载
二:操作
发现脚本
#!/usr/bin/env python
# coding:utf-8
import psutil
import re
import json
def thread():
try:
data = []
name = []
a = psutil.pids()
for i in a:
p = psutil.Process(i)
if p.name() == 'java':
if re.search('jar', p.cmdline()[-1]):
name.append(p.cmdline()[-1].split('-')[0])
for i in name:
data.append({'{#PNAME}': i})
print json.dumps({'data': data}, indent=2)
except Exception,e:
print e
thread()
监控脚本
#!/usr/bin/env python
import psutil
import re
import json
import sys
def Process(name):
result = []
try:
a = psutil.pids()
for i in a:
p = psutil.Process(i)
if p.name() == 'java':
cmd = ' '.join(p.cmdline())
if re.search(name, cmd):
result.append(0)
else:
result.append(1)
except Exception,e:
print 1
else:
if 0 in result and result.count(0) == 1:
print 0
else:
print 1
name = sys.argv[1]
param = sys.argv[2]
if name == 'Process':
Process(param)
添加zabbix自定义key
UserParameter=app_scan,/usr/local/zabbix/app_scan.py
UserParameter=app_status[*],/usr/local/zabbix/app_monitor.py $1 $2