def sendLatestCloudWatchData(z, h, d): zabbix_server = z zabbix_host = h cloud_watch_data = d global start_time zabbix_sender = pyZabbixSender(server=zabbix_server, port=10051) for cwdata in cloud_watch_data: zabbix_key = cwdata['zabbix_key'] results = cwdata['cloud_watch_results'] statistics = cwdata['statistics'] if results: # sort results by timestamp in descending order sorts = sorted(results, key=itemgetter('Timestamp'), reverse=True) # Get the latest data and timestamp zabbix_key_value = sorts[0][statistics] zabbix_key_timestamp = int( time.mktime(sorts[0]['Timestamp'].timetuple())) # Add data to zabbix sender zabbix_sender.addData(zabbix_host, zabbix_key, zabbix_key_value, zabbix_key_timestamp) else: # No data found within the time window # Set the zabbix key value to 0 zabbix_key_value = 0 # Set the zabbix key timestamp as the start time for getting cloudwatch data zabbix_key_timestamp = int(time.mktime(start_time.timetuple())) # Add data to zabbix sender zabbix_sender.addData(zabbix_host, zabbix_key, zabbix_key_value, zabbix_key_timestamp) # Send data to zabbix server #zabbix_sender.printData() zabbix_sender.sendData()
def sendLatestCloudWatchData(z, h, d): zabbix_server = z zabbix_host = h cloud_watch_data = d global start_time zabbix_sender = pyZabbixSender(server=zabbix_server, port=10051) for cwdata in cloud_watch_data: zabbix_key = cwdata['zabbix_key'] results = cwdata['cloud_watch_results'] statistics = cwdata['statistics'] if results: # sort results by timestamp in descending order sorts = sorted(results, key=itemgetter('Timestamp'), reverse=True) # Get the latest data and timestamp zabbix_key_value = sorts[0][statistics] zabbix_key_timestamp = int(time.mktime(utcToLocaltimestamp(sorts[0]['Timestamp']).timetuple())) # Add data to zabbix sender zabbix_sender.addData(zabbix_host, zabbix_key, zabbix_key_value, zabbix_key_timestamp) else: # No data found within the time window # Set the zabbix key value to 0 zabbix_key_value = 0 # Set the zabbix key timestamp as the start time for getting cloudwatch data zabbix_key_timestamp = int(time.mktime(utcToLocaltimestamp(start_time).timetuple())) # Add data to zabbix sender zabbix_sender.addData(zabbix_host, zabbix_key, zabbix_key_value, zabbix_key_timestamp) # Send data to zabbix server #zabbix_sender.printData() zabbix_sender.sendData()
def push_status_data_to_zabbix(data, host): timestamp = 0 z = pyZabbixSender("localhost") for item in data: for attribute, value in item.iteritems(): z.addData("monitoring_client_1", attribute, value) results = z.sendData()
def send_vuln_to_zabbix(vulnerability_results, args_server, args_port, nessus_metadata, args_fake): z = pyZabbixSender(server=args_server, port=args_port) for vuln_result in vulnerability_results: #first we add the metadata z.addData(vuln_result[0],'nessus.scan.name',nessus_metadata[0]) #scanname z.addData(vuln_result[0],'nessus.policy.name',nessus_metadata[1]) #policyname z.addData(vuln_result[0],'nessus.date.latest.scan',nessus_metadata[2]) #scan time #now we add the values z.addData(vuln_result[0], 'vulnerability.low', vuln_result[1]) z.addData(vuln_result[0], 'vulnerability.medium', vuln_result[2]) z.addData(vuln_result[0], 'vulnerability.high', vuln_result[3]) z.addData(vuln_result[0], 'vulnerability.critical', vuln_result[4]) #debug #z.printData() if args_fake: print "Faking. This is where I send data" else: results = z.sendDataOneByOne() for (code,data) in results: if code != z.RC_OK: print "Failed to send %s" % str(data) z.clearData() print "Done sending vulnerability data"
def push_sensor_data_to_zabbix(data, host): timestamp = 0 for item in data: try: timestamp = item['time'] except: pass z = pyZabbixSender('localhost') for item in data: for attribute, value in item.iteritems(): if attribute != 'time': z.addData(host, attribute, value, timestamp) results = z.sendData()
def send_comp_to_zabbix(compliance_results, args_server, args_port, nessus_metadata, args_fake): z = pyZabbixSender(server=args_server, port=args_port) for comp_result in compliance_results: #first we add the metadata z.addData(comp_result[0],'nessus.scan.name',nessus_metadata[0]) #scanname z.addData(comp_result[0],'nessus.policy.name',nessus_metadata[1]) #policyname z.addData(comp_result[0],'nessus.date.latest.scan',nessus_metadata[2]) #scan time #now we add the values z.addData(comp_result[0], 'cis.compliance.passed', comp_result[1]) z.addData(comp_result[0], 'cis.compliance.warning', comp_result[2]) z.addData(comp_result[0], 'cis.compliance.failed', comp_result[3]) #debug #z.printData() if args_fake: print "Faking. This is where I send data" else: results = z.sendDataOneByOne() for (code,data) in results: if code != z.RC_OK: print "Failed to send %s" % str(data) z.clearData() print "Done sending compliance data"
from pyZabbixSender import pyZabbixSender # this import is optional. Here is used to create a timestamp to associate # to some data points, for example/testing purposes only. import time # Specifying server, but using default port z = pyZabbixSender("127.0.0.1") def printBanner(text): border_char = '#' border = border_char * (len(text) + 4) print "\n\n%s" % border print "%s %s %s" % (border_char, text, border_char) print border def test_01(): ''' Simple "debugging" usage example (using "sendDataOneByOne" method) ''' printBanner("test_01") # Just ensuring we start without data, in case other example was # executed before this one z.clearData() # Adding a host/trap that exist z.addData("test_host", "test_trap", 21) # Adding a host that exist, but a trap that doesn't
def sendAllCloudWatchData(z, h, d, l): zabbix_server = z zabbix_host = h cloud_watch_data = d cw_log = l global start_time # Open cloudwatch log file for appending file = open(cw_log, 'a') zabbix_sender = pyZabbixSender(server=zabbix_server, port=10051) for cwdata in cloud_watch_data: zabbix_key = cwdata['zabbix_key'] results = cwdata['cloud_watch_results'] statistics = cwdata['statistics'] if results: # sort results by timestamp in descending order sorts = sorted(results, key=itemgetter('Timestamp'), reverse=True) for sort in sorts: zabbix_key_value = sort[statistics] zabbix_key_timestamp = int( time.mktime(sorts[0]['Timestamp'].timetuple())) # Get cloudwatch data in the format of: <timestamp>,<key>,<value> cw_data = str(zabbix_key_timestamp) + ',' + str( zabbix_key) + ',' + str(zabbix_key_value) # Search cloudwatch log with timestamp and key, send cloudwatch data if it is not found in the log cw_data_search = str(zabbix_key_timestamp) + ',' + str( zabbix_key) # Add escape to square brackets for regular expression search, to fix search pattern error cw_data_search = cw_data_search.replace("[", "\[").replace( "]", "\]") cw_data_found = 0 for line in open(cw_log, 'r'): if re.search(cw_data_search, line): cw_data_found = cw_data_found + 1 break # If data not found in the cloudwatch log, then add new data if cw_data_found == 0: # Add data to zabbix sender zabbix_sender.addData(zabbix_host, zabbix_key, zabbix_key_value, zabbix_key_timestamp) # Write cloudwatch data to the log file file.write(cw_data + '\n') else: # No data found within the time window # Set the zabbix key value to 0 zabbix_key_value = 0 # Set the zabbix key timestamp as the start time for getting cloudwatch data zabbix_key_timestamp = int(time.mktime(start_time.timetuple())) # Get cloudwatch data in the format of: <timestamp>,<key>,<value> cw_data = str(zabbix_key_timestamp) + ',' + str( zabbix_key) + ',' + str(zabbix_key_value) # Search cloudwatch log with timestamp and key, send cloudwatch data if it is not found in the log cw_data_search = str(zabbix_key_timestamp) + ',' + str(zabbix_key) # Add escape to square brackets for regular expression search, to fix search pattern error cw_data_search = cw_data_search.replace("[", "\[").replace("]", "\]") cw_data_found = 0 for line in open(cw_log, 'r'): if re.search(cw_data_search, line): cw_data_found = cw_data_found + 1 break # If data not found in the cloudwatch log, then add new data if cw_data_found == 0: # Set zabbix trapper key value to 0 if no data found in cloudwatch zabbix_sender.addData(zabbix_host, zabbix_key, zabbix_key_value, zabbix_key_timestamp) # Write cloudwatch data to the log file file.write(cw_data + '\n') # Send data to zabbix server #zabbix_sender.printData() send_results = zabbix_sender.sendData() file.write(str(send_results) + '\n') # Close cloudwatch log file file.close()
def sendAllCloudWatchData(z, h, d, l): zabbix_server = z zabbix_host = h cloud_watch_data = d cw_log = l global start_time # Open cloudwatch log file for appending file = open(cw_log, 'a') zabbix_sender = pyZabbixSender(server=zabbix_server, port=10051) for cwdata in cloud_watch_data: zabbix_key = cwdata['zabbix_key'] results = cwdata['cloud_watch_results'] statistics = cwdata['statistics'] if results: # sort results by timestamp in descending order sorts = sorted(results, key=itemgetter('Timestamp'), reverse=True) for sort in sorts: zabbix_key_value = sort[statistics] zabbix_key_timestamp = int(time.mktime(utcToLocaltimestamp(sorts[0]['Timestamp']).timetuple())) # Get cloudwatch data in the format of: <timestamp>,<key>,<value> cw_data = str(zabbix_key_timestamp) + ',' + str(zabbix_key) + ',' + str(zabbix_key_value) # Search cloudwatch log with timestamp and key, send cloudwatch data if it is not found in the log cw_data_search = str(zabbix_key_timestamp) + ',' + str(zabbix_key) # Add escape to square brackets for regular expression search, to fix search pattern error cw_data_search = cw_data_search.replace("[", "\[").replace("]", "\]") cw_data_found = 0 for line in open(cw_log, 'r'): if re.search(cw_data_search, line): cw_data_found = cw_data_found + 1 break # If data not found in the cloudwatch log, then add new data if cw_data_found == 0: # Add data to zabbix sender zabbix_sender.addData(zabbix_host, zabbix_key, zabbix_key_value, zabbix_key_timestamp) # Write cloudwatch data to the log file file.write(cw_data + '\n') else: # No data found within the time window # Set the zabbix key value to 0 zabbix_key_value = 0 # Set the zabbix key timestamp as the start time for getting cloudwatch data zabbix_key_timestamp = int(time.mktime(utcToLocaltimestamp(start_time).timetuple())) # Get cloudwatch data in the format of: <timestamp>,<key>,<value> cw_data = str(zabbix_key_timestamp) + ',' + str(zabbix_key) + ',' + str(zabbix_key_value) # Search cloudwatch log with timestamp and key, send cloudwatch data if it is not found in the log cw_data_search = str(zabbix_key_timestamp) + ',' + str(zabbix_key) # Add escape to square brackets for regular expression search, to fix search pattern error cw_data_search = cw_data_search.replace("[", "\[").replace("]", "\]") cw_data_found = 0 for line in open(cw_log, 'r'): if re.search(cw_data_search, line): cw_data_found = cw_data_found + 1 break # If data not found in the cloudwatch log, then add new data if cw_data_found == 0: # Set zabbix trapper key value to 0 if no data found in cloudwatch zabbix_sender.addData(zabbix_host, zabbix_key, zabbix_key_value, zabbix_key_timestamp) # Write cloudwatch data to the log file file.write(cw_data + '\n') # Send data to zabbix server #zabbix_sender.printData() send_results = zabbix_sender.sendData() file.write(str(send_results) + '\n') # Close cloudwatch log file file.close()
from pyZabbixSender import pyZabbixSender # this import is optional. Here is used to create a timestamp to associate # to some data points, for example/testing purposes only. import time # If the server where pyZabbixSender located does not have a direct Internet # connection you can specify proxy settings: # z = pyZabbixSender( # server="127.0.0.1", # port=10051, # proxytype=5, # netproxy="127.0.0.1", # proxyport=8080) z = pyZabbixSender("127.0.0.1") def printBanner(text): border_char = '#' border = border_char * (len(text) + 4) print "\n\n%s" % border print "%s %s %s" % (border_char, text, border_char) print border def test_01(): ''' Simple "debugging" usage example (using "sendDataOneByOne" method) ''' printBanner("test_01")