#!/usr/bin/python3

import influxdb
import json
from database.time_series import config
import time
import datetime
import random

conn = influxdb.InfluxDBClient(host="localhost",
                               port=32790,
                               username=config.USERNAME,
                               password=config.PASSWORD,
                               database=config.DATABASE,
                               pool_size=config.POOL_SIZE)

timestamp = int(time.mktime(datetime.datetime(2019, 5, 15).timetuple()))
last_timestamp = time.mktime(datetime.datetime(2019, 5, 16).timetuple())

latitude = 40.107565
longitude = -101.430733

to_write = []

while timestamp < last_timestamp:
    if random.random() < 0.1:
        point = {
            "measurement": "Event",
            "time": timestamp,
            "tags": {
                "username": "******"
Beispiel #2
0
 def __init__(self):
     self.logger = logging.getLogger('tasks.emitter')
     self.client = influxdb.InfluxDBClient(host='metric')
     self._setup()
Beispiel #3
0
def setup_influxdb_client():
    client = influxdb.InfluxDBClient(host='localhost', port=8086)
    client.switch_database('topics')
    return client
Beispiel #4
0
def big_loop():
	threading.Timer(3600.0,big_loop).start()
	influx = influxdb.InfluxDBClient(host='<influxdbhost>', 
	                     port=<port num>,
	                     database='<db name>')
	current_time = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
	server_list = [1,2,3,4,5,7,8,9]
	for server_num in server_list:
		def parse_url(server_num):
			try:
				if server_num >= 6:
					url = f"<url f string with {server num} injected>"
					first_url = f"<url f string with {server num} injected>"
				else:	
					url = f"<url f string with {server num} injected>"
					first_url = f"<url f string with {server num} injected>"
			except:
				print(f"Something went wrong loading SB for C0{server_num}")				
			br = RoboBrowser(history=True, parser="html.parser")
			try:
				br.open(url) # attempt to open url with RoboBrowser
			except:
				print(f"Unable to open {url}")						
			if server_num == 7:
				end_scrape = str(br.select('a')[9])[9:46]
			else:	
				end_scrape = str(br.select('a')[9])[9:48]
			combo_url = first_url+end_scrape # combine both strings to create URL for most recent job


			return combo_url
			
		parsed_url = parse_url(server_num)	

		def scrape_td(parsed_url):
			br2 = RoboBrowser(history=True,parser="html.parser")
			try:
				br2.open(parsed_url) # attempt to open up parsed URL
			except:
				print(f"Unable to open{parsed_url}")	
			col_list = [28,29,30, # 1
						35,36,37, # 2
						42,43,44, # 3
						49,50,51, # 4
						56,57,58, # 5 
						63,64,65, # 6
						70,71,72, # 7
						77,78,79, # 8
						84,85,86, # 9
						91,92,93, # 10
						98,99,100, # 11
						105,106,107, # 12
						112,113,114, # 13
						119,120,121, # 14
						126,127,128, # 15
						133,134,135, # 16
						140,141,142, # 17
						147,148,149, # 18
						154,155,156, # 19
						161,162,163, # 20
						168,169,170, # 21
						175,176,177, # 22
						182,183,184, # 23
						189,190,191, # 24
						196,197,198, # 25 
						203,204,205, # 26
						210,211,212, # 27
						217,218,219, # 28 
						224,225,226, # 29
						231,232,233, # 30
						238,239,240, # 31
						245,246,247, # 32
						252,253,254] # 33
			temp_list = [] # empty list to insert writes for each consumer
			for nums in col_list:
				try:
					strp_val = str(br2.select('td')[nums]).replace("<td>","").replace("</td>","").replace(",","") # parses string so we can turn 
					temp_list.append(int(strp_val))																  #	value into an int
				except:
					print(f"Less than 33 threads for cluster 0{server_num}")
			writes_ = temp_list[0::3]
			writes_totals = sum(writes_) # total of writes located in temp list

			return writes_totals						

		total_writes = scrape_td(parsed_url)

		def create_dict(total_writes):
			main_dict = {}
			field_vals = {}			
			main_dict["measurement"] = f"Cluster 0{server_num} Writes"
			main_dict["time"] = current_time
			main_dict["fields"] = field_vals
			field_vals[f"C_0{server_num}_writes"] = total_writes			
			data = [main_dict]

			return data

		final_dict = create_dict(total_writes)	
		# check for started?  Maybe here?

		def push_data(final_dict):
			try:
				influx.write_points(final_dict)
			except:
				print(f"Data could not be writted to InfluxDB for C0{server_num}")	
		
		final_run = push_data(final_dict)	
Beispiel #5
0
def init_influx():

  global influx

  # init Influx connection
  influx = influxdb.InfluxDBClient( host= conf['influxServer'], port= conf['influxPort'], username= conf['influxUser'], password= conf['influxPass'],  database=conf['influxDB'] )
Beispiel #6
0
if not SPEEDTEST_SERVER_ID:
    logger.debug(
        "SPEEDTEST_SERVER_ID environment variable has no server ID. Choose from the list below and set the environment variable."
    )
    servers = subprocess.run(["/librespeed", "--list"], capture_output=True, text=True)
    logger.debug(servers.stdout)
    exit()

# Connect to InfluxDB
logger.debug(
    f"Connecting to InfluxDB using host: {INFLUXDB_HOST}, port: {INFLUXDB_PORT}, username: {INFLUXDB_USER}, database name: {INFLUXDB_DB}."
)
influx = influxdb.InfluxDBClient(
    host=INFLUXDB_HOST,
    port=INFLUXDB_PORT,
    username=INFLUXDB_USER,
    password=INFLUXDB_USER_PASSWORD,
    database=INFLUXDB_DB,
    retries=0,
)

# Run the speedtest using the librespeed/speedtest-cli on an interval
while True:
    logger.debug(
        f"Running speedtest with server ID: {SPEEDTEST_SERVER_ID} and telemetry disabled."
    )
    result = subprocess.run(
        [
            "/librespeed",
            "--server",
            SPEEDTEST_SERVER_ID,
            "--telemetry-level",
Beispiel #7
0
 def connectInflux(self, host, port, dbname):
     return influxdb.InfluxDBClient(host, port, "yliu", "", dbname)
Beispiel #8
0
def get_influx():
    if not hasattr(g, 'influx'):
        g.influx = influxdb.InfluxDBClient(*app.config["INFLUX"])
    return g.influx
Beispiel #9
0
time_count_manager = Manager()
api_count = api_count_manager.list([0, 0, 0, 0, 0])
time_count = time_count_manager.list([0, 0, 0])

#bitFlyerAPIキーの設定
api = pybitflyer.API(api_key="XwtMm31szNgQPAfgTH1ZjY",
                     api_secret="jxQ09ydZWxfAe6CB3U3fvVMJSNjPaEaSJiinLn5Qu9k=")

#Line Notify
LINE_ACCESS_TOKEN = "ueJ1bnMTfoqMYaruN5zrQ3dsEZ6YvybUqVIYlkFPqtZG74FzYz5tb3Ao8M3itweppzHzThNXsrTLy/mCCVRaYYKw2VGU79H+tGH6COrNRqqO2WtfQ7210Rq2UGO9Kl0tETgqIxhu4TmdbWdgFdRZaAdB04t89/1O/w1cDnyilFU="  # ラインアクセストークン
LINE_USER_ID = "Ue75543d77fa00129801af8cc590405df"
line_bot_api = LineBotApi(LINE_ACCESS_TOKEN)

#influxDB
client = influxdb.InfluxDBClient(port='8086',
                                 username='******',
                                 password='******')
client.switch_database('bot')

#パラメータ読み込み
inifile = configparser.SafeConfigParser()
inifile.read('param.ini')
BOT_STATUS = inifile.get('SYSTEM', 'BOT_STATUS')
LONG_THRESHOLD = decimal.Decimal(inifile.get('BINANCE', 'LONG_THRESHOLD'))
SHORT_THRESHOLD = decimal.Decimal(inifile.get('BINANCE', 'SHORT_THRESHOLD'))
LOGIC_TYPE_LONG_LOT = inifile.get('TRADE', 'LOGIC_TYPE_LONG_LOT')
LOGIC_TYPE_SHORT_LOT = inifile.get('TRADE', 'LOGIC_TYPE_SHORT_LOT')
MAKE_ADDSUB = int(inifile.get('TRADE', 'MAKE_ADDSUB'))
LOGIC_TYPE_LONG_KAIRI = int(inifile.get('TRADE', 'LOGIC_TYPE_LONG_KAIRI'))
LOGIC_TYPE_SHORT_KAIRI = int(inifile.get('TRADE', 'LOGIC_TYPE_SHORT_KAIRI'))
INTERBAL = int(inifile.get('BINANCE', 'INTERBAL'))
Beispiel #10
0
    def __init__(self, dbhost="localhost", dbport=8086, dbname=None):

        self.dbclient = influxdb.InfluxDBClient(dbhost,
                                                dbport,
                                                database=dbname)
        self.last_check = None
Beispiel #11
0
 def __init__(self, measurement_name: str, threads_count: int,
              seconds_count: int, line_protocols_count: int):
     Writer.__init__(self, measurement_name, threads_count, seconds_count,
                     line_protocols_count)
     self.client = influxdb.InfluxDBClient('localhost', 8086, 'root',
                                           'root', 'iot_writes')
Beispiel #12
0
import influxdb
from config import credentials, ping_servers, interval
from ping3 import ping
import datetime
import asyncio
import time
import sys

client = influxdb.InfluxDBClient(**credentials)
client.create_database(credentials.get("database"))

measurement = "internet"


# points = list()
# cur_time = datetime.datetime.utcnow().isoformat().split('.')[0]
# for server in ping_servers:
#    delay = ping(server)
#    points.append(point)
# if points:
#    client.write_points(points)
#    print(points)
# print('Wrote {n} points.'.format(n=len(points)))
async def async_ping(server):
    delay = await ping(server)
    return delay


async def main():
    start = time.time()
    cur_time = datetime.datetime.utcnow().isoformat().split(".")[0]
Beispiel #13
0
def conn(host="les1ie.com", port=8086, user="******", password="******", db="cpu"):
    client = influxdb.InfluxDBClient(host=host, port=port, username=user, password=password,database=db)
    return client
def publish_data():
    v_logger = app.config['LOGGER']

    v_mqtt_local_host = app.config['MQTT_LOCAL_HOST']
    v_mqtt_local_port = app.config['MQTT_LOCAL_PORT']
    v_topic = app.config['MQTT_TOPIC']
    v_influxdb_host = app.config['INFLUXDB_HOST']
    v_influxdb_port = app.config['INFLUXDB_PORT']

    _data = flask.request.get_data()
    _args = flask.request.args.to_dict()
    _db = _args.get('db')

    _client = influxdb.InfluxDBClient(host=v_influxdb_host,
                                      port=v_influxdb_port,
                                      username='******',
                                      password='******',
                                      database=_db)

    _dbs = _client.get_list_database()
    if _db not in [_d['name'] for _d in _dbs]:
        v_logger.info(
            "InfluxDB database '{:s}' not found. Creating a new one.".format(
                _db))
        _client.create_database(_db)

    try:
        _result = _client.request('write',
                                  'POST',
                                  params=_args,
                                  data=_data,
                                  expected_response_code=204)
        v_logger.debug("Insert data into InfluxDB: {:s}".format(str(_data)))
    except Exception as ex:
        v_logger.error(ex)
    finally:
        _client.close()

    _response = flask.make_response(_result.text, _result.status_code)

    v_messages = []

    try:
        v_payload = flask.request.get_payload

        # Creates a dictionary with the sensor data
        for v_measure in v_payload:
            _sensor_tree = dict()

            _tag = v_measure['tag_set']
            _, _value = v_measure['field_set'].split('=')
            _timestamp = v_measure['timestamp']
            _dateObserved = datetime.datetime.fromtimestamp(
                int(_timestamp), tz=datetime.timezone.utc).isoformat()

            _label, _, _parameter = _tag.partition('_')
            _station_id = 'IOTAWATT'

            if _parameter in MESSAGE_PARAMETERS:
                if _label not in _sensor_tree:
                    _sensor_tree[_label] = {}

                _sensor_tree[_label].update({
                    PARAMETERS_MAP[_parameter]: _value,
                    'timestamp': _timestamp,
                    'dateObserved': _dateObserved,
                })

            # Insofar, one message is sent for each sensor
            for _label, _data in _sensor_tree.items():
                _message = dict()
                _message["payload"] = json.dumps(_data)
                _message["topic"] = "EnergyMonitor/{}.{}".format(
                    _station_id, _label)
                _message['qos'] = 0
                _message['retain'] = False

                v_messages.append(_message)

        v_logger.debug("Message topic:\'{:s}\', broker:\'{:s}:{:d}\', "
                       "message:\'{:s}\'".format(v_topic, v_mqtt_local_host,
                                                 v_mqtt_local_port,
                                                 json.dumps(v_messages)))
        publish.multiple(v_messages,
                         hostname=v_mqtt_local_host,
                         port=v_mqtt_local_port)
    except socket.error:
        pass

    return _response
Beispiel #15
0
from os import environ

import influxdb
from celery.utils.log import get_task_logger

from backend.backend import app

logger = get_task_logger(__name__)

host = environ.get('OMNOMNOM_INFLUXDB_HOST')
database = environ.get('OMNOMNOM_INFLUXDB_DATABASE')

if host and database:
    influxdb_client = influxdb.InfluxDBClient(host=host, database=database)
else:
    logger.warn(
        'OMNOMNOM_INFLUXDB_(HOST|DATABASE) are not defined. Nothing will be logged to InfluxDB.'
    )
    influxdb_client = None


@app.task(bind=True, default_retry_delay=30)
def log_to_influxdb(self, measurement, fields, tags=None):
    if tags is None:
        tags = {}
    entry = {'measurement': measurement, 'fields': fields, 'tags': tags}
    if influxdb_client:
        try:
            influxdb_client.write_points([entry])
        except Exception as ex:
            raise self.retry(exc=ex)
Beispiel #16
0
                              '_measurement="energy_usage"',
                              bucket=bucket,
                              org=org)
    else:
        info('Using InfluxDB version 1')

        sslEnable = False
        if 'ssl_enable' in config['influxDb']:
            sslEnable = config['influxDb']['ssl_enable']

        # Only authenticate to ingress if 'user' entry was provided in config
        if 'user' in config['influxDb']:
            influx = influxdb.InfluxDBClient(
                host=config['influxDb']['host'],
                port=config['influxDb']['port'],
                username=config['influxDb']['user'],
                password=config['influxDb']['pass'],
                database=config['influxDb']['database'],
                ssl=sslEnable,
                verify_ssl=sslVerify)
        else:
            influx = influxdb.InfluxDBClient(
                host=config['influxDb']['host'],
                port=config['influxDb']['port'],
                database=config['influxDb']['database'],
                ssl=sslEnable,
                verify_ssl=sslVerify)

        influx.create_database(config['influxDb']['database'])

        if config['influxDb']['reset']:
            info('Resetting database')
Beispiel #17
0
def create_influxdb_client():
    return influxdb.InfluxDBClient(get_manager_ip(), 8086, 'root', 'root',
                                   'cloudify')
Beispiel #18
0
try:
    last_sent = datetime.strptime(
        stamp_file.read_text().split()[0], '%Y-%m-%dT%H:%M:%SZ'
    )
    last_value = float(stamp_file.read_text().split()[1])
except Exception:
    last_sent = utc_now - timedelta(minutes=10)
    last_value = 0.0

diff = int((utc_now - timedelta(minutes=5) - last_sent).total_seconds() // 300)

if diff == 0:
    print('Nothing to do')
    exit(0)

db_client = influxdb.InfluxDBClient(host='127.0.0.1', port=8086)
db_client.switch_database('meteo')

values = db_client.query(
    f'SELECT sum(lluvia) FROM sensores WHERE time > now() - {diff * 5}m GROUP BY time(5m);'
).raw['series'][0]['values']

local_now = utc_now.replace(tzinfo=timezone.utc).astimezone(tz=None)

for v in values[:-1]:
    lluvia = v[1]
    if lluvia == 0.0:
        stamp_file.write_text(f'{v[0]}\n{last_value}')
        continue

    if lluvia is None:
Beispiel #19
0
def main():
    parser = optparse.OptionParser()
    parser.add_option('--buffering',
                      dest="buffering",
                      type=int,
                      nargs=1,
                      default=10)
    parser.add_option("--print",
                      dest="print",
                      default=False,
                      action="store_true")
    parser.add_option("--mongo",
                      dest="mongo",
                      default=False,
                      action="store_true")
    parser.add_option('--mongodbname',
                      dest="mongodbname",
                      type=str,
                      nargs=1,
                      default='prod')
    parser.add_option('--mongocollection',
                      dest="mongocollection",
                      type=str,
                      nargs=1,
                      default='dockerstats')
    parser.add_option("--influx",
                      dest="influx",
                      default=False,
                      action="store_true")
    parser.add_option("--influxdbname",
                      dest="influxdbname",
                      type=str,
                      nargs=1,
                      default='dockerstats')
    parser.add_option("--influxdbhost",
                      dest="influxdbhost",
                      type=str,
                      nargs=1,
                      default='localhost')
    parser.add_option("--influxdbport",
                      dest="influxdbport",
                      type=str,
                      nargs=1,
                      default='8086')
    (options, args) = parser.parse_args()
    filterout = lambda clt, Id: False  # TODO
    clt = docker.APIClient()
    callbacks = []
    if options.print:
        # TODO: lock
        # TODO: pretty print
        callbacks.append(lambda stats, info: print(stats, info))

    if options.mongo:

        def mongo(stats, info):
            with pymongo.MongoClient() as client:
                client[options.mongodbname][
                    options.mongocollection].insert_many(stats)

        callbacks.append(mongo)

    if options.influx:
        client = influxdb.InfluxDBClient(host=options.influxdbhost,
                                         port=int(options.influxdbport),
                                         database=options.influxdbname)
        client.create_database(options.influxdbname)

        def influx(stats, info):
            points = [
                point for stat in stats
                for point in influx_format(stat, info["Config"]["Labels"])
            ]
            client.write_points(points)

        callbacks.append(influx)

    def spawn_worker(Id):
        t = threading.Thread(target=loop,
                             args=(clt, callbacks, Id, options.buffering))
        t.daemon = True
        t.start()

    for container in clt.containers():
        Id = container['Id']
        if filterout(clt, Id): continue
        # loop(clt, callbacks, Id, options.buffering) #debug
        spawn_worker(Id)

    for event in clt.events(decode=True):
        print(event)
        if 'status' not in event: continue
        status = event['status']
        if status == 'start':
            Id = event['id']
            if filterout(clt, Id): continue
            spawn_worker(Id)
Beispiel #20
0
            mystring = mybytes.decode("utf8")
            if mystring == '{"POWER":"OFF"}':
                verbraucherstate = 2
            else:
                verbraucherstate = 13
            fp.close()
            print('Verbraucher 3 aus: {}'.format(verbraucherstate))
        except OSError as err:
            print("OS error: {0}".format(err))
            mystring = '{"POWER":""}'
            verbraucherstate = 13
    return verbraucherstate
 """

if __name__ == "__main__":
    dbClient = influxdb.InfluxDBClient(
        host='localhost', username='******', password='******', database='home')

    stromz = StromZaehler()
<<<<<<< HEAD
    skip_csv=1
=======
    skip=1
>>>>>>> c81bbbaf03ea03249053be5527258167aa96f7da

    verbraucherstate = 0

    while True:
      try:

        stromz.update()
Beispiel #21
0
    fieldDict = {}
    user = {}
    dataDict = {}
    msgDict['Comments'] = item[1]
    angDict['Anger'] = ang
    joyDict['Joy'] = joy
    fearDict['Fear'] = fear
    sadDict['Sadness'] = sad
    fieldDict.update(msgDict)
    fieldDict.update(angDict)
    fieldDict.update(joyDict)
    fieldDict.update(fearDict)
    fieldDict.update(sadDict)
    if name.startswith('other'):
        user['Participant'] = name.split("_")[1]
        user['Participant association'] = 'Other'
    else:
        user['Participant'] = name
        user['Participant association'] = 'Member'
    dataDict['tags'] = user
    dataDict['fields'] = fieldDict
    dataDict['time'] = dateForm
    dataDict['measurement'] = measurementName
    finalJson.append(dataDict)

client = influxdb.InfluxDBClient(host='localhost', port=8086)
client.switch_database('emotionDatabase')
client.write_points(finalJson)

print("Done!!!")
    if subscribed_test_site:
        print test_site, dataset['name'], site_completion[test_site]['remains']/1e12, site_completion[test_site]['done']/1e12


pprint.pprint(site_completion)
json.dump(site_completion, open("transfer_progress_cache/current_subs", "w"))

#sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#now_ns = int(time.time())*int(1e9)
#for site, info in site_completion.items():
#    data = ''
#    for idx in range(0, 11):
#        data += 'staging,site=%s,avail=%d done=%d,remaining=%d %d\n' % (site, idx*10, info['done_histo'].get(idx, 0), info['remains_histo'].get(idx, 0), now_ns)
#    sock.sendto(data, ('127.0.0.1', 8089))

client = influxdb.InfluxDBClient('localhost', 8086, 'root', 'root', 'cms')
now = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
data = []
#print site_completion['T2_US_Wisconsin']
for site, info in site_completion.items():
    for idx in range(0, 11):
        json = {"measurement": "staging",
                "tags": {"site": site, "avail": str(idx*10)},
                "time": now,
                "fields": {"done": float(info['done_histo'].get(10*idx, 0)), "remaining": float(info['remains_histo'].get(10*idx, 0))}
               }
        #if site == 'T2_US_Wisconsin':
        #    print json
        data.append(json)
client.write_points(data)
Beispiel #23
0
    tree = html.fromstring(r.text)
    path = tree.xpath('//div[@class="resultlist"]/div[contains(@class, "teaser")]//a/@href')[0]
    data_url = 'https://www.regierung-mv.de{}'.format(path)

r = requests.get(data_url, headers={'User-Agent': CONFIG['user_agent']})
if not r.ok:
    print('ERROR: failed to search overview, status code: {}'.format(r.stats_code))
    sys.exit(1)

if DEBUG:
    db_client = None
else:
    db_client = influxdb.InfluxDBClient(
        host=CONFIG['db']['host'],
        port=CONFIG['db']['port'],
        username=CONFIG['db']['username'],
        password=CONFIG['db']['password']
    )
    db_client.switch_database(CONFIG['db']['database'])

corona_parser = CoronaParser(db_client, r.text)

try:
    dt = corona_parser.parse()
    print('Data updated at {}'.format(dt))
except Exception as e:
    traceback.print_exc()
    notify(str(e))

if not DEBUG:
    db_client.close()
Beispiel #24
0
def main():
    global writer

    parser = argparse.ArgumentParser(
        description='InfluxDB 2.0 client lib benchmark')
    parser.add_argument(
        "-type",
        default="CLIENT_PYTHON_V2",
        help=
        "Type of writer (default 'CLIENT_PYTHON_V2'; CLIENT_PYTHON_V1, CLIENT_PYTHON_V2)"
    )
    # 2000
    parser.add_argument("-threadsCount",
                        type=int,
                        default=200,
                        help="how much Thread use to write into InfluxDB")
    parser.add_argument("-secondsCount",
                        type=int,
                        default=60,
                        help="how long write into InfluxDB")
    parser.add_argument("-batchSize",
                        type=int,
                        default=50000,
                        help="batch size")
    parser.add_argument("-flushInterval",
                        type=int,
                        default=10000,
                        help="buffer flush interval")
    parser.add_argument("-lineProtocolsCount",
                        type=int,
                        default=10,
                        help="how much data writes in one batch")
    parser.add_argument(
        "-skipCount",
        action="store_true",
        help="skip query for counting rows on end of benchmark")
    parser.add_argument("-measurementName",
                        default="sensor_%d" % (time.time()),
                        help="measurement name")

    args = parser.parse_args()

    writer_type = args.type
    threads_count = args.threadsCount
    seconds_count = args.secondsCount
    batch_size = args.batchSize
    flush_interval = args.flushInterval
    line_protocols_count = args.lineProtocolsCount
    skip_count = args.skipCount
    measurement_name = args.measurementName

    expected = threads_count * seconds_count * line_protocols_count

    print()
    print("------------- %s -------------" % writer_type)
    print()
    print()
    print("measurement:        %s " % measurement_name)
    print("threadsCount:       %s " % threads_count)
    print("secondsCount:       %s " % seconds_count)
    print("lineProtocolsCount: %s " % line_protocols_count)
    print()
    print("batchSize:          %s " % batch_size)
    print("flushInterval:      %s " % flush_interval)
    print()
    print("expected size: %s" % expected)
    print()
    print(args.type)

    if writer_type == 'CLIENT_PYTHON_V2':
        writer = WriterV2(influxdb_client=InfluxDBClient(
            url="http://localhost:9999",
            token="my-token",
            org="my-org",
            debug=False),
                          batch_size=batch_size,
                          flush_interval=flush_interval)
    if writer_type == 'CLIENT_PYTHON_V1':
        writer = WriterV1(
            influxdb.InfluxDBClient('localhost', 8086, 'root', 'root',
                                    'iot_writes'), 0, 0)

    stop_event = threading.Event()

    if threads_count > 1:
        threads = []
        for i in range(threads_count):
            # print("Prepare thread %d " % i)
            t = threading.Thread(target=worker,
                                 daemon=True,
                                 args=(stop_event, i, measurement_name,
                                       seconds_count, line_protocols_count))
            threads.append(t)

        for thread in threads:
            thread.start()

        # sleep main thread
        time.sleep(seconds_count)

        # send stop event to all threads
        print("Stop event!")
        stop_event.set()

        # wait threads to finish
        for thread in threads:
            thread.join(timeout=seconds_count)

    else:
        worker(stop_event, 0, measurement_name, seconds_count,
               line_protocols_count)

    if not skip_count:
        count = writer.countRows(measurement_name=measurement_name)

        print()
        print("Results:")
        print(f"-> expected:        {expected} ")
        print(f"-> total:           {count} ")
        print(f"-> rate [%]:        {(count / expected) * 100} ")
        print(f"-> rate [msg/sec]:  {count / seconds_count} ")

        print("Written records {}:{}".format(writer_type, count))
DB_NAME = 'collectd'
DB_PORT = 8086
DB_TABLE = 'geth_peers_geo'

for f in [GETH_PATH, IPC_PATH, GEODB_PATH]:
    if not os.path.exists(f):
        raise SystemExit('File {} does not exist.'.format(f))

geth_running = subprocess.call(
    'systemctl is-active --quiet {}'.format(SERVICE), shell=True)
if geth_running != 0:
    raise SystemExit('Service {} is not running.'.format(SERVICE))

try:
    db_client = influxdb.InfluxDBClient(host=DB_HOST,
                                        port=DB_PORT,
                                        database=DB_NAME)
except Exception as e:
    raise SystemExit('Error connecting to database: {}'.format(e))

try:
    admin_peers = subprocess.check_output('{} {}{} {}'.format(
        GETH_PATH, 'attach ipc:', IPC_PATH, '--exec admin.peers'),
                                          shell=True)
except Exception as e:
    raise SystemExit('Error getting peer list: {}'.format(e))

try:
    geodb = geoip2.database.Reader(GEODB_PATH)
except Exception as e:
    raise SystemExit('Error opening geodb file: {}'.format(e))
Beispiel #26
0
def main():
    # Test for circular references, inherit properties
    for name, value in config.tests.items():
        inherit_props(name, [])
    # Check database/influx references
    for name, value in config.tests.items():
        value.check()

    # Sort tests by their order number
    tests = sorted(config.tests.values(), key=lambda test: test.order)

    def connect(database_name):
        cfg = asdict(config.databases[database_name])
        if "driver" in cfg:
            driver_name = cfg["driver"]
            del cfg["driver"]
        else:
            driver_name = "postgres"
        try:
            driver = drivers[driver_name]
        except KeyError:
            raise ValueError("Unknown driver %s (valid drivers: %s)" %
                             (driver_name, repr(list(drivers.keys()))))
        return driver.connect(**cfg)

    # Collect data
    points = {influx_name: [] for influx_name in config.influxes}
    points2 = {influx_name: [] for influx_name in config.influxes2}
    for test in tests:
        if not test.is_template:
            info("    Running test %s" % test.name)
            for database_name in test.databases:
                conn = connect(database_name)
                cur = conn.cursor()
                q_started = time.time()
                cur.execute(test.sql)
                row = cur.fetchone()
                q_elapsed = time.time() - q_started
                column_map = {
                    desc[0]: idx
                    for idx, desc in enumerate(cur.description)
                }
                fields = {
                    field: row[column_map[field]]
                    for field in test.fields
                }
                fields["q_elapsed"] = q_elapsed
                tags = {"database_name": database_name}
                tags.update(test.tags)
                if test.influxes:
                    point = dict(measurement=test.measurement,
                                 tags=tags,
                                 fields=fields)
                    for influx_name in test.influxes:
                        if influx_name in points:
                            points[influx_name].append(point)
                if test.influxes2:
                    point2 = influxdb_client.Point(test.measurement)
                    for name, value in tags.items():
                        point2.tag(name, value)
                    for name, value in fields.items():
                        point2.field(name, value)
                    for influx_name in test.influxes2:
                        if influx_name in points2:
                            points2[influx_name].append(point2)

    for influx_name, influx in config.influxes.items():
        pts = points[influx_name]
        if pts:
            info("    Sending %d point(s) to influxdb v1 %s" %
                 (len(pts), influx_name))
            try:
                influx = config.influxes[influx_name]
                client = influxdb.InfluxDBClient(**influx.get_client_params())
                client.write_points(pts)
            except:
                if args.halt_on_send_error:
                    raise
                else:
                    traceback.print_exc(file=sys.stderr)

    for influx_name, influx in config.influxes2.items():
        pts = points2[influx_name]
        if pts:
            info("    Sending %d point(s) to influxdb v2 %s" %
                 (len(pts), influx_name))
            try:
                influx2 = config.influxes2[influx_name]
                client = influxdb_client.InfluxDBClient(
                    **influx2.get_client_params())
                write_api = client.write_api(write_options=SYNCHRONOUS)
                write_api.write(bucket=influx2.bucket,
                                org=influx2.org,
                                record=pts)
            except:
                if args.halt_on_send_error:
                    raise
                else:
                    traceback.print_exc(file=sys.stderr)
Beispiel #27
0
        solar_intensity = float(weather['solar_intensity'])
        return {
            'outside_temperature': outside_temperature,
            'solar_intensity': solar_intensity,
            'current_temperature': current_temperature,
            'wanted_temperature': wanted_temperature,
            'humidity': humidity,
            'heating_power': heating_power,
            'tado_mode': self.modes[tado_mode]
        }


if __name__ == '__main__':

    influxdb_client = influxdb.InfluxDBClient(
        host=config.influxdb_host,
        port=config.influxdb_port,
        database=config.influxdb_database)
    tado = Tado(config.tado_user, config.tado_pass)

    while True:
        tado.refreshAuth()
        measurements = []
        for id, name in config.tado_zones.items():
            result = {"measurement": config.influxdb_measurement}
            result["tags"] = {"room": name}
            result["fields"] = tado.getZone(id)
            print('[%s] %s' % (datetime.datetime.now().isoformat(), result))
            measurements.append(result)
        influxdb_client.write_points(measurements)
        time.sleep(15)
def test_query(mock_flux):
    db = influxdb.InfluxDBClient(database="fizz")
    db.query.side_effect = influxdb.exceptions.InfluxDBClientError(None)
    client = InfluxAlchemy(db)
    query = client.query(Measurement.new("buzz"))
    assert str(query) == "SELECT * FROM buzz;"
# The frequency as to which the data is outputted can be increased, but care would have to be taken that there isn't an
# erroneous spike in the data to cause undue alarm.

import influxdb
from influxdb import InfluxDBClient

HOST = 'influx.linklab.virginia.edu'
PORT = 443
USERNAME = '******'
PASSWORD = '******'
DATABASE = 'living-link-lab-open'

client = influxdb.InfluxDBClient(HOST,
                                 PORT,
                                 USERNAME,
                                 PASSWORD,
                                 DATABASE,
                                 ssl=True,
                                 verify_ssl=True)

# Define measurement variables
HUMIDITY = "Humidity_%"
BRIGHTNESS = "Illumination_lx"
TEMPERATURE = "Temperature_°C"
AWAIR = "awair_score"
CO2 = "co2_ppm"
VOC = "voc_ppb"
PM25 = "pm2.5_μg/m3"

# !!!Define the date(s) desired to find the coldest room!!!
dates = ["2020-07-03"]  #,"2020-07-04","2020-07-05"]
def migrationToInfluxDB(rootDir):
    for dirName, subdirList, fileList in os.walk(rootDir):
        print('Directorio encontrado: %s' % dirName)
        for fname in fileList:
            print(fname)
            extend = fname.split(".")
            inversor = extend[0].split(' ')

            id_inverter = int(inversor[1])

            if extend[1] == 'csv':
                file = dirName + '/' + fname

                with open(file, mode='r', buffering=-1) as csvfile:
                    reader = csv.DictReader(csvfile)

                    #for row in file.readlines():
                    for row in reader:
                        print(row)

                        date_hour = row['Time'].strip()

                        old_time = int(
                            time.mktime(
                                time.strptime(
                                    date_hour,
                                    '%Y/%m/%d %H:%M:%S'))) * 1000000000

                        print(old_time)

                        temp = float(row['Temp'])
                        vdc = float(0.0)
                        iac = float(row['Iac(A)'])
                        vac = float(row['Vac(V)'])
                        freq = float(row['Fac(Hz)'])
                        pinst = float(0.0)
                        etot = float(row['E-Total(kWh)'])
                        wtime = float(row['E-Today(kWh)'])
                        modo = float(0.0)
                        vdc_1 = float(row['Vpv1(V)'])
                        vdc_2 = float(row['Vpv2(V)'])
                        vdc_3 = float(row['Vpv3(V)'])
                        pinst = float(0.0)

                        user = '******'
                        password = '******'
                        dbname = 'dbname'
                        host = 'ip-host-server'
                        port = 443
                        ssl = True

                        try:
                            client = influxdb.InfluxDBClient(
                                host, port, user, password, dbname, ssl)

                            json_body = [{
                                "measurement": "Inverter",
                                "tags": {
                                    "id": id_inverter,
                                },
                                #The maximum timestamp is 9223372036854775806 or 2262-04-11T23:47:16.854775806Z.
                                #The minimum timestamp is -9223372036854775806 or 1677-09-21T00:12:43.145224194Z.
                                #"time": "2009-11-10T23:00:00Z"
                                "time": old_time,
                                "fields": {
                                    "temp": temp,
                                    "vdc": vdc,
                                    "vdc_1": vdc_1,
                                    "vdc_2": vdc_2,
                                    "vdc_3": vdc_3,
                                    "iac": iac,
                                    "vac": vac,
                                    "freq": freq,
                                    "pinst": pinst,
                                    "etot": etot,
                                    "wtime": wtime,
                                    "modo": modo,
                                    "pinst": pinst
                                }
                            }]
                            client.write_points(json_body)
                            print('Ok: connection sucessful to database')
                        except Exception as e:
                            print('Error: Failed to connect to database: ', e)
    return 0