Advertisement
Guest User

pi monitor

a guest
Jan 28th, 2019
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.36 KB | None | 0 0
  1. import psutil
  2. import psycopg2
  3.  
  4. i = 1
  5. while i < 99:
  6.   cpu_raw  = psutil.cpu_percent(interval=1)
  7.   freq_raw = psutil.cpu_freq(percpu=False)
  8.   temp_raw = psutil.sensors_temperatures()
  9.   ram_raw  = psutil.virtual_memory()
  10.   disk_raw = psutil.disk_usage('/')
  11.  
  12.  
  13.   cpu  = cpu_raw
  14.   temp = temp_raw['cpu-thermal'][0][1]
  15.   ram  = ram_raw[2]
  16.   disk = disk_raw[3]
  17.   print('\n')
  18.  
  19.   try:
  20.    connection = psycopg2.connect(user="userPython",
  21.                                   password="DitIsEenHeelLangWachtwoord",
  22.                                   host="192.168.1.220",
  23.                                   port="5432",
  24.                                   database="test")
  25.    cursor = connection.cursor()
  26.    postgres_insert_query = """ INSERT INTO monitor (cpu, temp, ram, disk) VALUES (%s,%s,%s,%s)"""
  27.    record_to_insert = (cpu, temp, ram, disk)
  28.    cursor.execute(postgres_insert_query, record_to_insert)
  29.    
  30.    connection.commit()
  31.    count = cursor.rowcount
  32.    print (count, "Record inserted successfully into meting table")
  33.    
  34.   except (Exception, psycopg2.Error) as error :
  35.     if(connection):
  36.         print("Failed to insert record into meting table", error)
  37.        
  38.   finally:
  39.     #closing database connection.
  40.     if(connection):
  41.         cursor.close()
  42.         connection.close()
  43.         print("PostgreSQL connection is closed")
  44.  
  45.  
  46.  
  47.  
  48.  
  49.   i += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement