Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import psutil
- import psycopg2
- i = 1
- while i < 99:
- cpu_raw = psutil.cpu_percent(interval=1)
- freq_raw = psutil.cpu_freq(percpu=False)
- temp_raw = psutil.sensors_temperatures()
- ram_raw = psutil.virtual_memory()
- disk_raw = psutil.disk_usage('/')
- cpu = cpu_raw
- temp = temp_raw['cpu-thermal'][0][1]
- ram = ram_raw[2]
- disk = disk_raw[3]
- print('\n')
- try:
- connection = psycopg2.connect(user="userPython",
- password="DitIsEenHeelLangWachtwoord",
- host="192.168.1.220",
- port="5432",
- database="test")
- cursor = connection.cursor()
- postgres_insert_query = """ INSERT INTO monitor (cpu, temp, ram, disk) VALUES (%s,%s,%s,%s)"""
- record_to_insert = (cpu, temp, ram, disk)
- cursor.execute(postgres_insert_query, record_to_insert)
- connection.commit()
- count = cursor.rowcount
- print (count, "Record inserted successfully into meting table")
- except (Exception, psycopg2.Error) as error :
- if(connection):
- print("Failed to insert record into meting table", error)
- finally:
- #closing database connection.
- if(connection):
- cursor.close()
- connection.close()
- print("PostgreSQL connection is closed")
- i += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement