Ejemplo n.º 1
0
def writeInfluxData(mesurement, value):

    mesurement = mesurement.replace('CAPPL:', '')
    mesurement = mesurement.replace(' ', '_')
    mesurement = mesurement.replace(':', '_')
    mesurement = mesurement.replace('.', '_')
    mesurement = mesurement.replace('[', '')
    mesurement = mesurement.replace(']', '')

    json_body = [{
        "measurement": mesurement,
        "time": currenttime,
        "fields": {
            "value": value
        }
    }]

    print(json_body)

    with open('data.txt', 'a') as f:
        json.dump(json_body, f, ensure_ascii=False, sort_keys=True, indent=4)

    result = client.write_points(json_body,
                                 protocol=u'json',
                                 time_precision='ms')
Ejemplo n.º 2
0
def writeDataToInfluxDB():
    """
    Write data from to InfluDB

    """
    global data
    global config

    client = InfluxDBClient(config['InfluxDB']['Server'],
                            config['InfluxDB']['Port'],
                            config['InfluxDB']['Username'],
                            config['InfluxDB']['Password'],
                            config['InfluxDB']['Database'])

    #get each json item
    for item in data:

        json_body = [{
            "measurement": "Energy marketprice",
            "time": item.start_datetime.astimezone(tz.UTC).isoformat(),
            "fields": {
                "value": item.marketprice
            }
        }]

        result = client.write_points(json_body,
                                     protocol=u'json',
                                     time_precision='s')
Ejemplo n.º 3
0
def sync(parent, children):
    desired = []
    # num_replicas = int(parent['spec']['replicas'])
    if not parent['spec']['mode'] == 'auto':
        num_replicas = get_num_replicas(parent['spec']['mode'],
                                        parent['spec']['replicas'],
                                        datetime.datetime.now())
    else:
        num_replicas = estimate_num_replicas(parent, children['Pod.v1'])
    json_body = [{
        "measurement": "replicas" + "-" + parent['metadata']['name'],
        "fields": {
            "desired": num_replicas,
            "current": len(children['Pod.v1'])
        }
    }]
    if client is not None:
        client.write_points(json_body)

    for i in range(0, num_replicas):
        desired.append({
            'apiVersion': 'v1',
            'kind': 'Pod',
            'metadata': {
                'name': parent['metadata']['name'] + '-podnumber-' + str(i),
                'namespace': parent['metadata']['namespace'],
            },
            'spec': {
                'restartPolicy':
                'OnFailure',
                'containers': [{
                    'name': 'acontainer',
                    'image': parent['spec']['image'],
                }]
            }
        })
    return {'status': {}, 'children': desired}
Ejemplo n.º 4
0
 def send_x(self, payload, source_config = None):
     measurement = payload.topic[0]
     client = self.client(source_config.influx_database)
     data = self.json_data(
         measurement, payload.fields, timestamp=payload.timestamp, tags=payload.tags)
     client.write_points([data])
Ejemplo n.º 5
0
        #if datetime.datetime.now().second == 0:
        message += "Lights off at "+str(compareTime)+" for "+str(24-hoursoflight)+"hours.\n"
        os.system("/home/pi/code/codesend 7309748")
    if b.second == 0:
        if humidity < humidityabove:
            #added try except to catch read timeout error
            try:
                if(isFieldBelow("humidity", 20, humidityabove)):
                    sendAlertPushNotification("Humidifier needs to be refilled")
            except:
                print("read timeout")
    if b.second == 0 or b.second == 30:
        try:
            print("writting")
            rgb.set_color(GREEN)
            client.write_points(points, time_precision='ms')
            print("written")
            rgb.set_color(OFF)
        except Exception as e:
            print(e)

    if (((datetime.datetime.now()-a).total_seconds()) >= pushInterval):
        message2 = "Current time: "+str(compareTime)+"\n"
        #message2 += "Current temperature: "+str(getLastValue("temperature"))[:4]+"f\n"
        message2 += "Current temperature: "+str(temperature)[:4]+"f   Target: "+str(tempTarget)+"f\n"
        #message2 += "Current humidity: "+str(getLastValue("humidity"))[:4]+"%\n"
        message2 += "Current humidity: "+str(humidity)[:4]+"%        Target: "+str(humidityTarget)+"%\n"
        message2 += "Over the last 6 minutes...\n"
        message2 += "Heater on for "+str(heaterTime*30)+" seconds\n"
        message2 += "Humidifier on for "+str(humidifierTime*30)+" seconds\n"  
        sendPushNotification(message2+message)
Ejemplo n.º 6
0
            'usage_idle': idle,
            'usage_iowait': iowait,
            'usage_irq': irq,
            'usage_nice': nice,
            'usage_softirq': softirq,
            'usage_steal': steal,
            'usage_system': system,
            'usage_user': user,
        },
        'tags': {
            "host_name": socket.gethostname(),
        }
    }
    series.append(pointValues)

print(psutil.virtual_memory())

print(series)

print("Create a retention policy")
retention_policy = 'awesome_policy'
client.create_retention_policy(retention_policy, '3d', 3, default=True)

print("Write points #: {0}".format(len(cpuu)))
client.write_points(series, retention_policy=retention_policy)

time.sleep(2)

rs = client.query("SELECT * from cpu;")
cpu_points = list(rs.get_points(measurement='cpu'))
print("Result: {0}".format(cpu_points))