Skip to content

Instantly share code, notes, and snippets.

Created October 13, 2012 05:37
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save quiver/3883369 to your computer and use it in GitHub Desktop.
Save quiver/3883369 to your computer and use it in GitHub Desktop.
Python port of zabbix_get command
# : Python port of zabbix_get
import argparse
import socket
import struct
import sys
def str2packed(data):
header_field = struct.pack('<4sBQ', 'ZBXD', 1, len(data))
return header_field + data
def packed2str(packed_data):
header, version, length = struct.unpack('<4sBQ', packed_data[:13])
(data, ) = struct.unpack('<%ds'%length, packed_data[13:13+length])
return data
def zabbix_get(args):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((, args.port))
data = ''
while True:
buff = s.recv(1024)
if not buff:
data += buff
response = packed2str(data)
return response
def main():
parser = argparse.ArgumentParser(prog='PROG')
parser.add_argument('--host', help='Specify host name or IP address of a host')
parser.add_argument('--port', type=int, default=10050, help='Specify port number of agent running on the host. Default is 10050')
parser.add_argument('--key', help='Specify key of item to retrieve value for')
args = parser.parse_args()
print zabbix_get(args)
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment