您的位置:首页 > 编程语言 > Python开发

Zabbix+Python自动发现进程端口和进程名称

2018-09-21 14:57 1721 查看
Python脚本

#!/usr/bin/env python
#coding=utf-8
#Debug in Python2.7
#used for discovery the Listen Port and Process
#Author wangshisheng 20180602

import os
import json
import string

t = os.popen(""" sudo ss -tunlp |grep LISTEN |sed "s#::#FF#g" |grep users |sort |uniq """)

array = []
for port in t.readlines():
r=port.strip()

#print r
port = r.split(':')[1].split(' ')[0]
try:
name = r.split('\"')[1]
except:
name = "none"

cluster = {}
cluster['{#TCP_PORT}'] = port
cluster['{#TCP_NAME}'] = name

array.append(cluster)

#amy = [{k:v} for k,v in dict([list(d.iteritems())[0] for d in array]).iteritems()]
amy = [dict(t) for t in set([tuple(d.items()) for d in array])]

print json.dumps({'data':amy},sort_keys=True,indent=4,separators=(',',':'))

运行结果如下

{
"data":[
{
"{#TCP_NAME}":"srv.User.plus",
"{#TCP_PORT}":"6049"
},
{
"{#TCP_NAME}":"zabbix_agentd",
"{#TCP_PORT}":"10050"
}
]
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  zabbix