Specific requirements:
1. Due to the independent development of xmzoomeye-agent currently passive monitoring mainly, in order to achieve Zabbix low-level discovery service independent discovery, need to automatically obtain the occupied port list according to the process name, and report data according to the port analysis
Implementation ideas:
1. Use the Psutil module to get a list of process IDs by process name
2. Traverse the/proc/net/tcp file to get the rem_address 00000000:0000 column, the Nineth column of socket_id and it in the form of a collection of dictionary storage to weight
3. Generating a collection of ports using the acquired PID list and the Socket_id collection dictionary
4. Try connecting ports for dynamic Data escalation
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/87/FA/wKiom1flKmWSePxdAAAlVjy4230417.png "title=" Socket port. png "alt=" Wkiom1flkmwsepxdaaalvjy4230417.png "/>
Specific code:
#!/usr/bin/env python# -*- coding: utf-8 -*-"" "## authors: limanman# oschina: http://xmdevops.blog.51cto.com/# purpose:# "" "# Description: Import public module Import osimport reimport psutil# Description: Import other modules # description: Get process def pidof_processname (srvname): pids = [] for pid in psutil.process_iter (): if pid.name () == srvname: pids.append (str (pid.pid)) return pids# Description: Socket Port def socks_with_ports (): sockets = {} with open ('/proc/net/tcp ', ' r+b ') as fd: for curline in fd: sepline&nbSp;= curline.split () if sepline[2] != ' 00000000:0000 ': continue key = sepline[9] addr, Port = sepline[1].split (': ') if key not in sockets: sockets[key] = set () sockets[key].add (int (port, 16)) return sockets# Description: Get port def analysis_sockports (pids, socks): sock_ports = set ( ) for pid in pids: fd_path = Os.path.join (Os.path.join ('/proc ', pid), ' fd ') for rlink in os.listdir (Fd_path): rpath = os.path.join (Fd_path, rlink) if not os.path.exists (rpath): continue vlink = os.readlink (rpath) if not vlink.startswith (' socket: '): continue match&nbsP;= re.search (R ' (? <=socket:\[) ([0-9]+) (? =\]) ', vlink) if not match: continue addr = match.group (1) if addr in socks: sock_ports.update (Socks[addr]) return sock_portsdef Service_redis_running_status (add_data=none): srv_name = ' Redis-server ' pids = pidof_processname (Srv_name) socks = socks_ With_ports () ports = analysis_sockports (pids, socks) return ' service: %s -> port: ' % srv_name, portsif __name__ == ' __ Main__ ': import pprint pprint.pprint (service_redis_running_ Status ())
There are pictures and phases:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/87/F7/wKioL1flNUCCprWkAABFkw9S7f0428.png "title=" Test results. png "alt=" Wkiol1flnuccprwkaabfkw9s7f0428.png "/>
This article is from the "ζ Automated operation and maintenance development Road ζ" blog, please be sure to keep this source http://xmdevops.blog.51cto.com/11144840/1855964
One question per day _python. Pure Python implements low-level discovery dynamic service discovery?