监听端口
1.cat /usr/local/zabbix/bin/ports.py
#!/usr/bin/python
#coding=utf-8
import commands
import os,sys
##########返回命令执行结果
def getComStr(comand):
try:
stat, proStr = commands.getstatusoutput(comand)
except:
print "command %s execute failed, exit" % comand
#将字符串转化成列表
#proList = proStr.split("\n")
return proStr
##########获取系统服务名称和监听端口
def filterList():
tmpStr = getComStr("netstat -tpln")
tmpList = tmpStr.split("\n")
del tmpList[0:2]
newList = []
for i in tmpList:
val = i.split()
del val[0:3]
del val[1:3]
#提取端口号
valTmp = val[0].split(":")
val[0] = valTmp[1]
#提取服务名称
valTmp = val[1].split("/")
val[1] = valTmp[-1]
if val[1] != '-' and val not in newList:
newList.append(val)
return newList
def main():
netInfo = filterList()
#格式化成适合zabbix lld的json数据
json_data = "{\n" + "\t" + '"data":[' + "\n"
#print netInfo
for net in netInfo:
if net != netInfo[-1]:
json_data = json_data + "\t\t" + "{" + "\n" + "\t\t\t" + '"{#PPORT}":"' + str(net[0]) + "\",\n" + "\t\t\t" + '"{#PNAME}":"' + str(net[1]) + "\"},\n"
else:
json_data = json_data + "\t\t" + "{" + "\n" + "\t\t\t" + '"{#PPORT}":"' + str(net[0]) + "\",\n" + "\t\t\t" + '"{#PNAME}":"' + str(net[1]) + "\"}]}"
print json_data
if __name__ == "__main__":
main()
2.cat ports_discovery.conf
UserParameter=ports.discovery,sudo /usr/bin/python /usr/local/zabbix/bin/ports.py
3.模板
All TCP Ports discovery
ports.discovery
Listen Port $1
net.tcp.listen[{#PPORT}]
{Ports Monitoring:net.tcp.listen[{#PPORT}].last()}=0
如果是监听tomcat
cat /usr/local/zabbix/bin/tcp_services.py
#/usr/bin/python
#This script is used to discovery disk on the server
import subprocess
import os
import socket
import json
import glob
java_names_file='java_names.txt'
javas=[]
if os.path.isfile(java_names_file):
# print 'java_names_file exists!'
#####
##### here should use % (java_names_file) instead of using the python variable java_names_file directly inside the ''' ''' quotes
#####
args='''awk -F':' '{print $1':'$2}' %s''' % (java_names_file)
t=subprocess.Popen(args,shell=True,stdout=subprocess.PIPE).communicate()[0]
elif glob.glob('/opt/xx/*_tomcat') and not os.path.isdir('/opt/logs/logstash') and not os.path.isdir('/opt/app/elasticsearch/config'):
t=subprocess.Popen('cd /opt/xx && ls *|grep xx_tomcat',shell=True,stdout=subprocess.PIPE)
for java in t.stdout.readlines():
if len(java) != 0:
S=java.strip('\n').strip(':')
args="cat /opt/xx/%s/conf/server.xml|grep port|sed -n '2p'|awk '{print $2}'|awk -F '=' '{print $2}'|tr -d '\"'" % S
port=subprocess.Popen(args,shell=True,stdout=subprocess.PIPE).communicate()[0].strip('\n')
STR1={'{#PROCNAME}':S}
STR2={'{#PORT}':port}
STR3=dict(STR1, **STR2)
javas.append(STR3)
print json.dumps({'data':javas},indent=4,separators=(',',':'))
监听tcp状态
cat /usr/local/zabbix/bin/tcp_status_ss.sh
#!/bin/bash
#scripts for tcp status
function SYNRECV {
/usr/sbin/ss -ant | awk '{++s[$1]} END {for(k in s) print k,s[k]}' | grep 'SYN-RECV' | awk '{print $2}'
}
function ESTAB {
/usr/sbin/ss -ant | awk '{++s[$1]} END {for(k in s) print k,s[k]}' | grep 'ESTAB' | awk '{print $2}'
}
function FINWAIT1 {
/usr/sbin/ss -ant | awk '{++s[$1]} END {for(k in s) print k,s[k]}' | grep 'FIN-WAIT-1' | awk '{print $2}'
}
function FINWAIT2 {
/usr/sbin/ss -ant | awk '{++s[$1]} END {for(k in s) print k,s[k]}' | grep 'FIN-WAIT-2' | awk '{print $2}'
}
function TIMEWAIT {
/usr/sbin/ss -ant | awk '{++s[$1]} END {for(k in s) print k,s[k]}' | grep 'TIME-WAIT' | awk '{print $2}'
}
function LASTACK {
/usr/sbin/ss -ant | awk '{++s[$1]} END {for(k in s) print k,s[k]}' | grep 'LAST-ACK' | awk '{print $2}'
}
function LISTEN {
/usr/sbin/ss -ant | awk '{++s[$1]} END {for(k in s) print k,s[k]}' | grep 'LISTEN' | awk '{print $2}'
}
$1
cat tcp_status_ss.conf
UserParameter=tcp[*],/usr/local/zabbix/bin/tcp_status_ss.sh $1