#!/usr/bin/python # coding=utf-8 import subprocess import logging import datetime import random import os def writedata(data, path="/tmp"): errorcode = "" now_date = datetime.date.today() outfile = path + str(str(now_date)+ str(random.randint(1, 1000))) logging.debug("file = " + outfile) if not os.path.exists(path): try: os.mkdir(path) except Exception as exept: errorcode = exept return errorcode if not os.path.isdir(path): errorcode = "Dir to write is not dir" return errorcode if not os.path.exists(outfile): try: f = open(outfile, "w") except Exception as exept: errorcode = exept return errorcode else: in_file = "" for line in data: in_file = in_file + line f.write(in_file) logging.debug("zabbix-file writed") finally: f.close() else: errorcode = "file exist" return errorcode return outfile, errorcode def sendfile(file, server="", host=""): errorcode = "" if not os.path.exists(file) and not os.path.isfile(file): errorcode = "can't send. No such file" return errorcode args = " --input-file " + file if (not server) or (not host): args = args + " --config /etc/zabbix/zabbix_agentd.conf" else: args = args + "--host " + host + "--zabbix-server " + server cmd = "/usr/bin/zabbix_sender " + "args" try: os.system(cmd) except Exception as exept: errorcode = exept return errorcode def sendkey(keys, server="", host=""): errorcode = "" args = "" string = "" if (not server) or (not host): args = args + "--config /etc/zabbix/zabbix_agentd.conf" else: args = args + "--host " + host + " --zabbix-server " + server + " -vv " cmd = "/usr/bin/zabbix_sender" startargs = "" print keys for key in keys: startargs = args + " --key " +str(key) + " --value " + str(keys[key]) print(startargs) p = subprocess.Popen([cmd + " " + startargs], stdout=subprocess.PIPE, shell=True) for line in p.stdout.readlines(): print line def main(): print "main function" if __name__ == '__main__': main() sendkey({"key1": "val1", "key2": "val2", }, "192.168.4.141", "mix")