Created
August 14, 2017 08:20
-
-
Save AnyISalIn/901fd8f17fce338efba76d5bb6bcd035 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import time | |
from socket import * | |
from influxdb import InfluxDBClient | |
GRAPHITE_HOST = '192.168.20.103' | |
GRAPHITE_PORT = 2003 | |
INFLUXDB_HOST = '192.168.20.183' | |
INFLUXDB_DATABASE = 'water_quality' | |
LOG = logging.getLogger(__name__) | |
LOG.setLevel(logging.INFO) | |
def check_status(): | |
client = InfluxDBClient(host=INFLUXDB_HOST, database=INFLUXDB_DATABASE) | |
data = client.query( | |
'SELECT last(*) FROM {} WHERE time > now() - 1h'.format(INFLUXDB_DATABASE)) | |
if not data: | |
return 0 | |
return 1 | |
def main(): | |
while True: | |
sock = socket(family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None) | |
sock.connect((GRAPHITE_HOST, GRAPHITE_PORT)) | |
status = check_status() | |
msg = 'water_quality.status {} {}\n'.format( | |
status, int(time.time())).encode() | |
sock.sendall(msg) | |
LOG.info(msg) | |
sock.close() | |
time.sleep(60) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment