def _connection(self): #TODO No error raised if it fails, check it yourself return influxdb.InfluxDBClient(dbutils.DB_CONFIG['host'], dbutils.DB_CONFIG['port'], dbutils.DB_CONFIG['user'], dbutils.DB_CONFIG['password'], dbutils.DB_CONFIG['db'])
def write_candle_to_mongo(datapath, datafile, instrument, period, format=1): db = influxdb.InfluxDBClient("172.17.0.12", 18081, "admin", "gongxifacai", "tick") data = [] with open(datapath + '/' + datafile, "r") as tickcsv: lines = csv.reader(tickcsv) i = 0 for row in lines: if i != 0 and i % 5000 == 0: print instrument, period, i candle = CandleData(instrument, period) if format == 1: ok = candle.getData(row) else: ok = candle.getData_2(row) curr_time = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.localtime(candle.fltTime)) sql = "select count(volume) from %s where time = '%s'" % ( instrument, curr_time) result = db.query(sql) if len(result.items()) > 0: # print 'data is exists: %s' % curr_time continue db.write_points([candle.tojson()]) # data.append(candle.tojson()) i += 1 # db.write_points(data) print instrument, "finished. ", i
def write_candle_to_mongo(datapath, datafile, instrument, period, tablename, format=1): db = influxdb.InfluxDBClient("127.0.0.1", 8086, "", "", "tick") data = [] with open(datapath + '/' + datafile, "r") as tickcsv: lines = csv.reader(tickcsv) i = 0 for row in lines: # print instrument, period, i, row if (i % 5000) == 0: print instrument, period, i candle = CandleData(instrument, period, tablename) if format == 1: ok = candle.getData(row) else: ok = candle.getData_2(row) curr_time = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.localtime(candle.fltTime)) sql = "select count(volume) from %s where instrument='%s' and time = '%s'" % (tablename, instrument, curr_time) result = db.query(sql) if len(result.items()) > 0: exit('data is exists: %s %s' % (instrument, curr_time)) data.append(candle.tojson()) i += 1 db.write_points(data) print "finished. ", i
def threading_function(): global car0 global car1 print("Thread Started") mydb = influxdb.InfluxDBClient('localhost', 8086, 'torc', 'trial', 'torcs') mydb.create_database('torcs') retention_policy = 'awesome_policy' mydb.create_retention_policy('retention_policy', '10d', 10, default=True) while (1): lists = [{ 'measurement': 'car_speed', "tags": { "car_id": 0 }, 'time': time.strftime('%Y-%m-%d %H:%M:%S'), 'fields': { 'speedX': car0[0], 'angle': car0[1], 'distance': car0[2], 'TrackPos': car0[3], 'steering': car0[4], 'Acceleration': car0[5], }, }] res = mydb.write_points(lists) '''
def __init__(self, pdu, snmp_object, namings): self.snmp_pdu = snmp_object self.measurement_name = pdu self.namings = namings self.log = logging.getLogger(__name__) # Logging setup if config.debug: self.loglevel = logging.DEBUG self.logging = logging.basicConfig( level=self.loglevel, format='%(asctime)s %(levelname)s: %(name)s: %(message)s') # database connection self.log.debug("[info] \t\t connecting to database ...") try: self.db = influxdb.InfluxDBClient( config.Influxdb_ip, config.Influxdb_port, config.Influxdb_user, config.Influxdb_pass, config.Influxdb_used_database_name) self.log.debug("[success]\t ... database connection established") except: self.log.debug("[failure]\t ... database connection failed") try: self.db.create_database(config.Influxdb_used_database_name) except: self.log.debug("Database already existing - skipping creation")
def __init__(self): self._db = influxdb.InfluxDBClient(INFLUX_DB_HOST_URL, INFLUX_DB_HOST_PORT, INFLUX_DB_USER, INFLUX_DB_PASS, timeout=1) self._atmo_index_url = None
def __init__(self, base_key, columns): if influxdb is None: raise ImportError("Could not import influxdb package") self.base_key = base_key self.columns = columns self.db = influxdb.InfluxDBClient('localhost', 8086, 'clocklogger', 'pendulum', 'clock') logger.info("Opened connection to InfluxDB")
def get_influx_client(self): self.influx_connection = client.InfluxDBClient(host=self.server, database=self.db_name) found = False for db_name_entry in self.influx_connection.get_list_database(): if self.db_name == db_name_entry["name"]: found = True if found == False: self.influx_connection.create_database(self.db_name)
def get_db(): """Returns an ``InfluxDBClient`` instance.""" return influxdb.InfluxDBClient( settings.INFLUXDB_HOST, settings.INFLUXDB_PORT, settings.INFLUXDB_USER, settings.INFLUXDB_PASSWORD, settings.INFLUXDB_DATABASE, )
def configure(self): self.db = influxdb.InfluxDBClient(self.host, self.port, self.username, self.password, self.database) try: self.db.create_database(self.database) # TODO: (IMPRV) handle specific exception except: pass return self.db
def _get_influxdb_client(parser): if dispatcher.INFLUXDB not in parser.get('DEFAULT', 'dispatcher'): raise exceptions.InfluxDBConfigurationMissing() ip = _get_ip(parser.get('dispatcher_influxdb', 'target')) user = parser.get('dispatcher_influxdb', 'username') password = parser.get('dispatcher_influxdb', 'password') db_name = parser.get('dispatcher_influxdb', 'db_name') return influxdb_client.InfluxDBClient(ip, consts.INFLUXDB_PORT, user, password, db_name)
def __init__(self): try: self.conf = cfg.CONF self.influxdb_client = client.InfluxDBClient( self.conf.influxdb.ip_address, self.conf.influxdb.port, self.conf.influxdb.user, self.conf.influxdb.password, self.conf.influxdb.database_name) self._init_serie_builders() except Exception as ex: LOG.exception(ex) raise exceptions.RepositoryException(ex)
def __init__(self, host, port, username, password, database): """ initial db connection """ self.host = host self.port = port self.username = username self.password = password self.database = database self.tsname = "ts02" self.db = influxdb.InfluxDBClient(self.host, self.port, self.username, self.password, self.database)
def __init__(self, name, event, queue, config): threading.Thread.__init__(self) self.name = name self.event = event self.queue = queue self.config = config self.converter = InfluxDBConverter(self.config['measurements']) self.influxdb = influxdb.InfluxDBClient(self.config['ip'], self.config['port'], self.config['user'], self.config['password'], self.config['database']) self.influxdb.create_database(self.config['database']) logger.info("{} initialized successfully".format(self.name))
def get(self, host='localhost', port=8086, username='******', password='******', database=None, ssl=False, verify_ssl=False, timeout=None, use_udp=False, udp_port=4444): db = influxdb.InfluxDBClient(host=host, port=port,username=username, password=password, database=database, ssl=ssl, \ verify_ssl=verify_ssl, timeout=timeout, use_udp=use_udp, udp_port=udp_port) return db
def handle(req): """handle a request to the function Args: req (str): request body """ #print("Received the following request:\n" + str(req) + "\nConverting...") jreq = json.loads(req) converter = InfluxDBConverter(jreq['measurements_name']) influxdb_json = converter.convert(jreq) #print("Result of Conversion:\n" + str(influxdb_json)) influx = influxdb.InfluxDBClient(os.environ['db_ip'], 8086, 'root', 'root', 'sensiot') influx.write_points(influxdb_json.get()) return 'OK'
def __init__(self, host=INFLUXDB_DOMAIN, port=INFLUXDB_PORT, username=INFLUXDB_USERNAME, password=INFLUXDB_PASSWORD, database=INFLUXDB_DATABASE): self.client = client.InfluxDBClient(host=host, port=port, username=username, password=password) if database in self.list_database(): self.client.switch_database(database) else: self.client.create_database(database) self.client.switch_database(database)
def __init__(self): try: self.conf = cfg.CONF self.influxdb_client = client.InfluxDBClient( self.conf.influxdb.ip_address, self.conf.influxdb.port, self.conf.influxdb.user, self.conf.influxdb.password) self._version = None self._init_version() except Exception as ex: LOG.exception(ex) raise exceptions.RepositoryException(ex) self._serie_builders_version_map = { 'from_0.11.0': (self._build_serie_dimension_values_from_v0_11_0, self._build_serie_metric_list_from_v0_11_0) }
def __init__(self, db_info=None, db_name=None): self.db_info = db_info or self.DEFAULT_DB_INFO self.db_name = db_name or self.DEFAULT_DB_NAME self.db = influxdb.InfluxDBClient( host=self.db_info['host'], port=self.db_info['port'], username=self.db_info['username'], password=self.db_info['password'] ) if self.db_name not in self.list_databases(): self.create_database(self.db_name) self.switch_database(self.db_name) print('Using InfluxDB datastore with db name {}'.format(self.db_name))
def write_errlog_influxdb(database, table, udb_name, query_time, rows_sent, rows_examined, sql_text): db = influxdb.InfluxDBClient('127.0.0.1', 8086, 'root', 'xxx', '%s' % (database)) json_body = [{ "measurement": table, "tags": { "db": udb_name, "query_time": query_time, "rows_sent": rows_sent, "rows_examined": rows_examined }, "fields": { "sql_text": sql_text } }] try: db.write_points(json_body) except Exception, e: print 'influxdb write_points error:', e
def db_connection(self): dbname = str(self.settings.db_dbname) result = True try: self.db = influxdb.InfluxDBClient(str(self.settings.db_url), str(self.settings.db_port), str(self.settings.db_user), str(self.settings.db_pwd), str(self.settings.db_dbname)) dblist = self.db.get_database_list() except (influxdb.InfluxDBClientError, ConnectionError) as e: return False, e.args[0] if not any([db['name'] == dbname for db in dblist]): self.db.create_database(dbname) message = "The InfluxDB database '%s' was created." % dbname else: message = "Successfully connected to the InfluxDB database '%s'." % dbname return result, message
def main(): ''' MAIN ''' args = parse_args() iostat_dir = args['dir'] dbhost = args['dbhost'] dbport = args['dbport'] dbuser = args['dbuser'] dbpass = args['dbpass'] dbname = args['dbname'] dbconn = influxdb.InfluxDBClient(dbhost, dbport, dbuser, dbpass, dbname) for fname in sorted(os.listdir(iostat_dir)): fname_full = os.path.join(iostat_dir, fname) fname_ext = os.path.splitext(fname_full)[1] if fname_ext == ".bz2": process_bz2(fname_full, dbconn)
def __init__(self): super(MultisensorDB, self).__init__() self.pid = os.getpid() self.logger.info("Starting the MultisensorDB component") #########Creating a local using influx######################### self.mydb = influxdb.InfluxDBClient(config.INFLUX_DBASE_HOST, config.INFLUX_DBASE_PORT, config.INFLUX_DBASE_USER, config.INFLUX_DBASE_PASSWORD, config.INFLUX_DBASE_NAME) self.mydb.create_database(config.INFLUX_DBASE_NAME) self.mydb.create_retention_policy(config.INFLUX_DBASE_RETENTION_POLICY, '3d', 3, default=True) self.logger.info("Created a DB with name newone") ################################################################# ##########on value sent from multisensor############## self.logger.info("Waiting for a msg update from sensor")
def __init__(self): try: self.conf = cfg.CONF self.influxdb_client = client.InfluxDBClient( self.conf.influxdb.ip_address, self.conf.influxdb.port, self.conf.influxdb.user, self.conf.influxdb.password, self.conf.influxdb.database_name) # compile regex only once for efficiency self._serie_name_reqex = re.compile( '([^?&=]+)\?([^?&=]+)&([^?&=]+)(&[^?&=]+=[^?&=]+)*') self._serie_tenant_id_region_name_regex = re.compile( '[^?&=]+\?[^?&=]+&[^?&=]+') self._serie_name_dimension_regex = re.compile('&[^?&=]+=[^?&=]+') self._serie_name_dimension_parts_regex = re.compile( '&([^?&=]+)=([^?&=]+)') except Exception as ex: LOG.exception() raise exceptions.RepositoryException(ex)
def debug_influx(node, influx_user, influx_pass): global success print('********VERIFYING INFLUXDB NODE********') try: from influxdb import client except ImportError: print("[WARNING]: InfluxDB Python Package is not installed!") return 1 fail = check_port(node, 8086) fail |= check_port(node, 8090) if fail: success = False try: conn = client.InfluxDBClient(node, 8086, influx_user, influx_pass, 'mon') conn.query('show series;') if args.verbose: print(successfully + ' connected to node {}'.format(node)) else: print(successful) except Exception, e: print('{}'.format(e)) success = False
def __init__(self): QThread.__init__(self) self.running = False self.tsc = TimeSeriesContainer() self.timer = QTimer() self.seconds_elapsed = 0 self.timer.timeout.connect( self.counter_seconds ) self.zephyr_connect = ZephyrDevice() self.zephyr_connect.connectTo(4) self.connect( self.zephyr_connect, SIGNAL( 'Message' ), self.printmessage ) self.connect( self.zephyr_connect, SIGNAL( 'newRRI' ), self.addRR ) self.connect( self.zephyr_connect, SIGNAL( 'newBW' ), self.addBW ) self.connect( self.zephyr_connect, SIGNAL( 'heart_rate' ), self.add_heart_rate ) self.connect( self.zephyr_connect, SIGNAL( 'respiration_rate' ), self.add_respiration_rate ) self.connect( self.zephyr_connect, SIGNAL( 'breathing_wave_amplitude' ), self.add_breathing_wave_amplitude ) self.connect( self.zephyr_connect, SIGNAL( 'activity' ), self.add_activity ) self.connect( self.zephyr_connect, SIGNAL( 'posture' ), self.add_posture ) self.db = influxdb.InfluxDBClient('10.0.0.25', 8086, 'root', 'root', 'rrintervals') self.starttime = time.time()*1000 self.accval = 0
if __name__ == '__main__': if len(sys.argv) != 4: print "Usage influx2bin.py <datapath> <instrument> <start date>" print "example: /data/sean/datapath rb1810 20180101" exit(0) datapath = sys.argv[1] instrument = sys.argv[2] sdate = sys.argv[3] sdate = '%s-%s-%sT00:00:00Z' % (sdate[0:4], sdate[4:6], sdate[6:]) # print sdate dir_path = os.path.join(datapath, instrument) if not os.path.exists(dir_path): os.mkdir(dir_path) db = influxdb.InfluxDBClient("127.0.0.1", 18081, "", "", "tick") days_sql = "select count(volume) from %s where time >= '%s' group by time(1d) tz('Asia/Shanghai')" % ( instrument, sdate) # print days_sql result = db.query(days_sql) days = [] for row in result[instrument]: if row.get('count') > 0: days.append(row.get('time')) format_days = [] for i in range(0, len(days)): sql = "select * from %s where time >= '%s'" % (instrument, days[i]) if i + 1 < len(days): sql = "%s and time < '%s'" % (sql, days[i + 1]) result = db.query(sql)
def main(): """ Main function """ signal.signal(signal.SIGINT, signal_handler) print("Starting...") try: SER.open() except ValueError: sys.exit("Error opening serial port (%s). exiting" % SER.name) if INFLUXDB_ENABLED: if DEBUG: print("INFLUXDB enabled") from influxdb import client as influxdb db = influxdb.InfluxDBClient(INFLUXDB_HOST, INFLUXDB_PORT, INFLUXDB_USER, INFLUXDB_PASS, INFLUXDB_DB) if MQTT_ENABLED: if DEBUG: print("MQTT is enabled") print("MQTT loop starting") mqttc = paho.Client(MQTT_CLIENT_UNIQ, False) if MQTT_AUTH: mqttc.username_pw_set(MQTT_USER, MQTT_PASS) mqttc.connect(MQTT_BROKER, MQTT_PORT, 60) mqttc.loop_start() # Marco 28/12/2020, tbv data conversie toegevoegd: convert_int = ["device_type", "tariff_indicator", "any_power_failures", "long_power_failures", "voltage_sags_P1", "voltage_swells_p1", "power_consumed_amps"] convert_float = ["dsmr_version", "consumed_gas", "power_consumed_tariff_1_kwh", "power_consumed_tariff_2_kwh", "power_produced_tariff_1_kwh", "power_produced_tariff_2_kwh", "power_consumed_watts", "power_produced_watts", "current_watts_consumed", "current_watts_generated"] while True: try: raw_line = SER.readline() except ValueError: sys.exit("Unable to get data from serial port (%s). Exiting." % SER.name) data = datastripper(str(raw_line)) # do some actual stuff with the returned (and valid) data if data: if MQTT_ENABLED: mqtt_topic = ("%s/%s" % (MQTT_TOPIC_PREFIX, data[0])) mqttc.publish(mqtt_topic, data[1], MQTT_QOS, MQTT_RETAIN) print("[MQTT ] Producing (%s) %s to %s..." % (data[0], data[1], mqtt_topic)) # Marco 28/12/2020, tbv data conversie toegevoegd: if data[0] in convert_int: data[1] = int(data[1]) if data[0] in convert_float: data[1] = float(data[1]) if INFLUXDB_ENABLED: print("[INFLUX] Posting (%s) %s to %s..." % (data[0], data[1], INFLUXDB_DB)) if data[1]: json_body = [ { "measurement": data[0], "fields": { "value": data[1] } }] db.write_points(json_body) if MQTT_ENABLED: if DEBUG: print("MQTT loop stopping") mqttc.loop_stop() mqttc.disconnect() try: SER.close() except ValueError: sys.exit("Unable to close serial port (%s). Exiting." % SER.name)
# Fonction qui retourne la temperature en celcius def read_temp(): lines = read_temp_raw() while lines[0].strip()[-3:] != 'YES': time.sleep(0.2) lines = read_temp_raw() equals_pos = lines[1].find('t=') if equals_pos != -1: temp_string = lines[1][equals_pos + 2:] temp_c = float(temp_string) / 1000.0 return temp_c # InfluxDB instance db = influxdb.InfluxDBClient('192.168.99.100', 8086, 'influx', 'influx', 'weather') try: # Read, Report, Repeat while True: #temp = random.randint(0,35) temp = read_temp() now = datetime.datetime.today() # influxClient.write_points(temperature_data(temp)) data = [{ "measurement": "temperature.readings", "tags": { "device": "rpi01", }, "fields": {
#!/usr/bin/env python import sys from influxdb import client as influxdb username = sys.argv[1] password = sys.argv[2] url = sys.argv[3] db = influxdb.InfluxDBClient(url, 8086, username, password, 'mon') series_list = db.query('list series;') hosts = set() hosts_amplified = {} total_metrics = 0 metrics_missing_hostname = 0 for series in db.query('list series;')[0]["points"]: total_metrics += 1 entries = [entry for entry in series[1].split('&')] series_items = { entry.split('=')[0]: entry.split('=')[1] for entry in entries if entry.find('=') != -1 } if series_items.has_key('hostname'): hosts.add(series_items['hostname']) else: metrics_missing_hostname += 1 if series_items.has_key( 'amplifier' ): # I assume if it has a amplifier it has a hostname also