class TelegrafComms(DCCComms): def __init__(self, ip='localhost', port=8092): self.ip = ip self.port = port self._connect() def _connect(self): log.info("Establishing Connection") try: self.client = TelegrafClient(host=self.ip, port=self.port) log.info("Connection established") except Exception as ex: log.exception("Unable to establish socket connection. Please check the firewall rules and try again.") self.client = None raise ex def _disconnect(self): raise NotImplementedError def send(self, message, msg_attr=None): if self.client is not None: message_list = message.split() try: print "A: ", message_list[0] self.client.metric(message_list[0], message_list[1]) #first one is metric name and second arg is value. except Exception as ex: log.exception("Data not sent") self.client = None def receive(self): raise NotImplementedError
class TelegrafRecordStep(PipelineStep): """Write each file to output, without changing the file""" def __init__(self, metric_name, telegraf_host='localhost', telegraf_port=8092, tags={}, tz=None): self.client = TelegrafClient(host=telegraf_host, port=telegraf_port) self.metric_name = metric_name self.tags = tags if tz is None: tz = os.environ.get("TSTK_TZ", "Australia/Brisbane") self.localtz = pytz.timezone(tz) def process_file(self, file): fileext = splitext(file.filename)[1].lower().lstrip(".") tags = {"InstantIndex": file.instant.index, "FileType": fileext} tags.update(self.tags) dt = self.localtz.localize(file.instant.datetime) utc = dt.astimezone(pytz.utc) epoch_ns = int(utc.timestamp() * 1e9) # to NS now_ns = int(datetime.utcnow().timestamp() * 1e9) report = file.report report.update({"CapturedAt": epoch_ns, "ProcessedAt": now_ns}) self.client.metric(self.metric_name, report, timestamp=epoch_ns, tags=tags) return file
def sendmetric(name, value, tags): client = TelegrafClient(host=conf['telegraf']['host'], port=conf['telegraf']['port']) print('sending metric', name, tags, flush=True) client.metric(name, value, tags=tags)
def main(): try: while True: fwhm = round(uniform(0.5, 1.2), 2) client = TelegrafClient(host=DB_ADDR, port=DB_PORT, tags={'host': 'cpd'}) client.metric('FWHM', {'C080': fwhm}) time.sleep(60) except Exception as ex: print(ex)
def main(nodo, t1, t2, t3, t4, vcc): client = TelegrafClient(host=DB_ADDR, port=DB_PORT, tags={'host': nodo}) client.metric('Temperatura', {'Sensor1': round(t1, 3)}) client.metric('Temperatura', {'Sensor2': round(t2, 3)}) client.metric('Temperatura', {'Sensor3': round(t3, 3)}) client.metric('Temperatura', {'Sensor4': round(t4, 3)}) client.metric('Temperatura', {'diff_s1-s2': round(t1 - t2, 3)}) client.metric('Temperatura', {'diff_s3-s4': round(t3 - t4, 3)}) client.metric('Temperatura', {'mean': round((t1 + t2 + t3 + t4) / 4, 3)}) client.metric('Power_Supply', {'Power': round(vcc / 1000, 2)}) # en voltios time.sleep(1)
class TestTelegraf(unittest.TestCase): def setUp(self): self.host = 'host' self.port = 1234 self.addr = (self.host, self.port) def test_sending_to_socket(self): self.client = TelegrafClient(self.host, self.port) self.client.socket = mock.Mock() self.client.metric('some_series', 1) self.client.socket.sendto.assert_called_with(b'some_series value=1i\n', self.addr) self.client.metric('cpu', {'value_int': 1}, { 'host': 'server-01', 'region': 'us-west' }) self.client.socket.sendto.assert_called_with( b'cpu,host=server-01,region=us-west value_int=1i\n', self.addr) def test_global_tags(self): self.client = TelegrafClient(self.host, self.port, tags={'host': 'host-001'}) self.client.socket = mock.Mock() self.client.metric('some_series', 1) self.client.socket.sendto.assert_called_with( b'some_series,host=host-001 value=1i\n', self.addr) self.client.metric('some_series', 1, tags={'host': 'override-host-tag'}) self.client.socket.sendto.assert_called_with( b'some_series,host=override-host-tag value=1i\n', self.addr)
class TestTelegraf(unittest.TestCase): def setUp(self): self.host = 'host' self.port = 1234 self.addr = (self.host, self.port) def test_sending_to_socket(self): self.client = TelegrafClient(self.host, self.port) self.client.socket = mock.Mock() self.client.metric('some_series', 1) self.client.socket.sendto.assert_called_with(b'some_series value=1i\n', self.addr) self.client.metric('cpu', {'value_int': 1}, { 'host': 'server-01', 'region': 'us-west' }) self.client.socket.sendto.assert_called_with( b'cpu,host=server-01,region=us-west value_int=1i\n', self.addr) def test_global_tags(self): self.client = TelegrafClient(self.host, self.port, tags={'host': 'host-001'}) self.client.socket = mock.Mock() self.client.metric('some_series', 1) self.client.socket.sendto.assert_called_with( b'some_series,host=host-001 value=1i\n', self.addr) self.client.metric('some_series', 1, tags={'host': 'override-host-tag'}) self.client.socket.sendto.assert_called_with( b'some_series,host=override-host-tag value=1i\n', self.addr) def test_utf8_encoding(self): self.client = TelegrafClient(self.host, self.port) self.client.socket = mock.Mock() self.client.metric(u'meäsurement', values={ u'välue': 1, u'këy': u'valüe' }, tags={u'äpples': u'öranges'}) self.client.socket.sendto.assert_called_with( b'me\xc3\xa4surement,\xc3\xa4pples=\xc3\xb6ranges k\xc3\xaby="val\xc3\xbce",v\xc3\xa4lue=1i\n', self.addr)
class TestTelegraf(unittest.TestCase): def setUp(self): self.host = 'host' self.port = 1234 self.addr = (self.host, self.port) def test_sending_to_socket(self): self.client = TelegrafClient(self.host, self.port) self.client.socket = mock.Mock() self.client.metric('some_series', 1) self.client.socket.sendto.assert_called_with(b'some_series value=1i\n', self.addr) self.client.metric('cpu', {'value_int': 1}, {'host': 'server-01', 'region': 'us-west'}) self.client.socket.sendto.assert_called_with(b'cpu,host=server-01,region=us-west value_int=1i\n', self.addr) def test_global_tags(self): self.client = TelegrafClient(self.host, self.port, tags={'host': 'host-001'}) self.client.socket = mock.Mock() self.client.metric('some_series', 1) self.client.socket.sendto.assert_called_with(b'some_series,host=host-001 value=1i\n', self.addr) self.client.metric('some_series', 1, tags={'host': 'override-host-tag'}) self.client.socket.sendto.assert_called_with(b'some_series,host=override-host-tag value=1i\n', self.addr)
def send_fwhm(fwhm): client = TelegrafClient(host=DB_ADDR, port=DB_PORT, tags={'host': 'CPD'}) client.metric('FWHM', {'C080': fwhm})
def send_fwhm_with_timestamp(fwhm, time): client = TelegrafClient(host=DB_ADDR, port=DB_PORT, tags={'host': 'CPD'}) client.metric('FWHM', {'C080': fwhm}, timestamp=int(time * 1000000000))
class PersistTimeSeriesForTesla(): logging.basicConfig(filename='./logs/persist_time_series.log', level=logging.INFO) __vehicles = [] __telegrafClient = '' def __init__(self): # Read the config file with all the vehicle settings in it config = tesla_API_config.TeslaAPIConfig() vehiclesConfig = config.load_vehicles() logging.info( str(datetime.datetime.now()) + '==> PersistTimeSeries ==> Config file is loaded ') # Create an dict of instances of TelsaAPIVehicles amountOfVehicles = len(vehiclesConfig["Vehicles"]) counterVehiclesRead = 0 while (counterVehiclesRead < amountOfVehicles): vehicle = tesla_API_vehicle.Vehicle( vehiclesConfig["Vehicles"][counterVehiclesRead]['email'], vehiclesConfig["Vehicles"][counterVehiclesRead]['password']) self.__vehicles.append(vehicle) counterVehiclesRead = counterVehiclesRead + 1 # Create the TelegrafClient self.__telegrafClient = TelegrafClient(host='localhost', port=8092) def __persist_charge_state(self, vehicle): # get the charge_state: this contains battery information try: charge_state = vehicle.get_charge_state() self.__telegrafClient.metric('battery_level', charge_state['battery_level'], tags={ 'vehicleID': str(vehicle.get_vehicleID()), 'vehicle_name': vehicle.get_vehicle_name() }) self.__telegrafClient.metric('battery_range', charge_state['ideal_battery_range'], tags={ 'vehicleID': str(vehicle.get_vehicleID()), 'vehicle_name': vehicle.get_vehicle_name() }) if (charge_state['charging_state'].upper() == 'Charging'.upper()): location = geohash.encode(charge_state['latitude'], charge_state['longitude'], 7) logging.info( str(datetime.datetime.now()) + '==> Charging on location: ' + location + ' with value : ' + str(charge_state['charge_energy_added'])) self.__telegrafClient.metric( 'charge_energy_added', charge_state['charge_energy_added'], tags={ 'vehicleID': str(vehicle.get_vehicleID()), 'vehicle_name': vehicle.get_vehicle_name(), 'location': location }) except Exception as e: logging.error( str(datetime.datetime.now()) + '==> PersistTimeSeries.__persist_charge_state ==> failed ' + str(e)) def __persist_climate_state(self, vehicle): # climate data contains the temperature of the car and outside try: climate_state = vehicle.get_climate_state() self.__telegrafClient.metric('outside_temp', climate_state['outside_temp'], tags={ 'vehicleID': str(vehicle.get_vehicleID()), 'vehicle_name': vehicle.get_vehicle_name() }) self.__telegrafClient.metric('inside_temp', climate_state['inside_temp'], tags={ 'vehicleID': str(vehicle.get_vehicleID()), 'vehicle_name': vehicle.get_vehicle_name() }) except Exception as e: logging.error( str(datetime.datetime.now()) + '==> PersistTimeSeries.__persist_climate_state ==> failed ' + str(e)) def __persist_vehicle_state(self, vehicle): try: # Vehicle data contains the odometer vehicle_state = vehicle.get_vehicle_state() self.__telegrafClient.metric('odometer', vehicle_state['odometer'], tags={ 'vehicleID': str(vehicle.get_vehicleID()), 'vehicle_name': vehicle.get_vehicle_name() }) except Exception as e: logging.error( str(datetime.datetime.now()) + '==> PersistTimeSeries.__persist_vehicle_state ==> failed ' + str(e)) def __persist_drive_state(self, vehicle): try: # Vehicle data contains the odometer vehicle_state = vehicle.get_drive_state() self.__telegrafClient.metric('power', vehicle_state['power'], tags={ 'vehicleID': str(vehicle.get_vehicleID()), 'vehicle_name': vehicle.get_vehicle_name() }) except Exception as e: logging.error( str(datetime.datetime.now()) + '==> PersistTimeSeries.__persist_drive_state ==> failed ' + str(e)) def persist_vehicles_state(self): for vehicle in self.__vehicles: try: # first make sure the vehicle gets the latest data from Tesla. vehicle.get_vehicle_data_from_tesla() # persist all the subsets of data self.__persist_charge_state(vehicle) self.__persist_climate_state(vehicle) self.__persist_vehicle_state(vehicle) self.__persist_drive_state(vehicle) except Exception as e: logging.error( str(datetime.datetime.now()) + '==> PersistTimeSeries.persist_vehicle_state ==> failed ' + str(e))
metric = args.metric etalon_name = args.etalon etalon_url = args.url etalon_header = args.header tg = TelegrafClient(host=args.host, port=args.port) r = requests.get( etalon_url ) logger.debug("Etalon response headers:") logger.debug( r.headers ) if etalon_header not in r.headers: logger.error( "Header {} not found in etalon response".format(etalon_header) ) sys.exit(-1) etalon_time = r.headers[etalon_header] logger.debug( "Etalon timestamp: {}".format(etalon_time) ) local_time = time.time() logger.debug( "Local timestamp: {}".format(local_time) ) time_diff = int( abs( float(etalon_time) - local_time ) ) logger.debug( "Time diff: {}".format(time_diff) ) if args.dry_run: logger.debug( "Dry-run mode activated, skipping sending data to telegraf" ) else: tg.metric( metric, time_diff, tags={ 'etalon': etalon_name, 'url': etalon_url })
# Oracle part con = cx_Oracle.connect(ora_user, ora_pass, ora_dsn) cur = con.cursor() cur.prepare( 'SELECT DISTINCT from_domain FROM BORMAN_IFU_DELIVERED_STATS WHERE mailing_date > systimestamp - :days ORDER BY from_domain' ) cur.execute(None, {'days': args.days}) froms = map(lambda x: x[0], cur.fetchall()) cur.close() con.close() logger.debug("Active froms found: {}".format(len(froms))) logger.debug(froms) if args.dry_run: logger.debug("Dry-run mode activated, skipping sending emails") for domain in froms: domain_file = spool + '/' + domain mod_time = 10000000 if os.path.isfile(domain_file): mod_time = int(time.time() - os.path.getmtime(domain_file)) logger.debug("Last {} delivery: {}".format(domain, mod_time)) tg.metric(metric, mod_time, tags={'from': domain})
def main() -> None: setting_keys: List[str] = [ "FRITZ_ADDRESS", "FRITZ_USERNAME", "FRITZ_PASSWORD", "TELEGRAF_HOSTNAME", "TELEGRAF_PORT", "SAMPLE_PERIOD" ] # Check if all environment keys are suplied and if they aren't end the program via an exception missing_keys = [key for key in setting_keys if key not in os.environ] if len(missing_keys) > 0: raise Exception(f"You need to supply the environment variable(s): {', '.join(missing_keys)}") # Extract the settings into a dictionary settings: Dict[str, Any] = {key: os.environ[key] for key in setting_keys} # Add optional settings, they are settings["PRINT_DATA"] = "PRINT_DATA" in os.environ and os.environ["PRINT_DATA"] != "False" settings["FRITZ_USE_TLS"] = "FRITZ_USE_TLS" in os.environ and os.environ["FRITZ_USE_TLS"] != "False" # Print information about the current configuration print("Current configuration:") for key, value in settings.items(): # The still leaks the length of the password to the log but I don't think that really matters censored_value = '*'*len(value) if "PASSWORD" in key else value print(f"\t{key}={censored_value}") print() # Create the fritz connection fritz_connection = fc.FritzConnection( address = settings["FRITZ_ADDRESS"], user = settings["FRITZ_USERNAME"], password = settings["FRITZ_PASSWORD"], use_tls = settings["FRITZ_USE_TLS"]) print(fritz_connection) print() # Create the telgraf client, this pip library doesn't really do much telegraf_client = TelegrafClient( host=settings["TELEGRAF_HOSTNAME"], port=int(settings["TELEGRAF_PORT"])) # Set the sample period variable SAMPLE_PERIOD = float(settings["SAMPLE_PERIOD"]) # Print some debug info that goes to docker log print(f"Polling the following metrics from {settings['FRITZ_ADDRESS']}") for service, actions in metrics_names: print(f"\t{service}") for action in actions: print(f"\t\t{action}") print() print(f"Starting to poll metrics every {SAMPLE_PERIOD} seconds") # Record the start time start = time.time() # Start looping while True: # Retrieve the data from the fritzbox and put it in the data dictionary data = {} for service, actions in metrics_names: for action in actions: result = fritz_connection.call_action(service, action) for key, value in result.items(): # Remove new prefix from variable names if key[0:3] == 'New': key = key[3:] data[f"{service}.{key}"] = value # Send the collected data to telegraf telegraf_client.metric("router", data) # Print the data depending on the settings if settings["PRINT_DATA"]: print(data) # Sleep the appropriate amount of time time.sleep(SAMPLE_PERIOD - (time.time()-start)%SAMPLE_PERIOD)
partitions = [] for schema in parts: cur.execute(None, {'tbname': schema['table']}) partitions += map(lambda x: x[0], cur.fetchall()) cur.close() con.close() # Monitoring part today = datetime.now() for day in reversed(xrange(args.max)): delta = timedelta(days=day) check_date = (today + delta).strftime('%Y%m%d') for schema in parts: if schema['prefix'] + check_date not in partitions: schema['left'] = day for schema in parts: logger.debug("{} partitions left: {}".format(schema['table'], schema['left'])) if args.dry_run: logger.debug("Dry-run mode activated, skipping sending data to telegraf") else: for schema in parts: tg.metric(metric, schema['left'], tags={'table': schema['table']})
from telegraf.client import TelegrafClient client = TelegrafClient(host="127.0.0.1", port=8089) # measurement_name是表名,values=1代表正样本,values=0代表负样本。tags上会建索引,pageno代表在第几页展现,position代表在页内的位置。 client.metric(measurement_name="click_ratio", values=1, tags={ "pageno": 1, "position": 1 }) client.metric("click_ratio", 0, {"pageno": 1, "position": 2}) client.metric("click_ratio", 0, {"pageno": 1, "position": 1}) client.metric("click_ratio", 1, {"pageno": 2, "position": 1})
if not isinstance(result, Exception): if 'X-Check' in result.headers: # print(f'{url}: {result.headers["X-Check"]}') urls_check_list[url] = 1 else: # print(f'{url}: {"HEADER_NF"}') urls_check_list[url] = 3 else: urls_check_list[url] = 3 if __name__ == '__main__': loop = asyncio.get_event_loop() future = asyncio.ensure_future(run(urls)) loop.run_until_complete(future) logger.debug("Checking result:") logger.debug(urls_check_list) if args.dry_run: logger.debug("Dry-run mode activated, skipping sending data to telegraf") else: pass for url, status in urls_check_list.items(): tg.metric(metric, {'status': status}, tags={ 'url': url, 'location': check_location, 'header': check_header })
def emit(name, values, tags=None): client = TelegrafClient(host=HOST, port=8092) client.metric(name, values, tags=tags)
# Copyright 2017 VMware, Inc. All rights reserved. -- VMware Confidential # Description: Perf CICD WaveFront Example # Group-perf: optional # Timeout: 3000 from telegraf.client import TelegrafClient client = TelegrafClient(host='localhost', port=8094, tags={'host': 'diffhost'}) print 'Client created' # Records a single value with one tag client.metric('GK.testmetric', float(60), tags={'app_name_descr': 'CICD_test-app'}, timestamp=long(1528483840794000)) print 'Metric sent to Wavefront'
args = parser.parse_args() if args.debug: logger.setLevel(logging.DEBUG) logger.debug("Arguments:") logger.debug(args) metric = args.metric queue_name = args.queue queue_path = args.path tg = TelegrafClient(host=args.host, port=args.port) path, dirs, files = next(os.walk(queue_path)) queue_len = len(files) logger.debug("Queue '{}' on {} has length: {}".format(queue_name, queue_path, queue_len)) if args.dry_run: logger.debug("Dry-run mode activated, skipping sending data to telegraf") else: tg.metric(metric, queue_len, tags={ 'queue': queue_name, 'path': queue_path })