Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import print_function
- from urllib.request import urlopen
- import json
- import subprocess
- import requests
- import random
- import sys
- import os
- import time
- import datetime
- import MySQLdb as my
- import psutil
- import platform
- uname = platform.uname()
- db = my.connect(host="localhost",
- user="david",
- passwd="password!",
- db="data"
- )
- cursor = db.cursor()
- def getCPUtemperature():
- res = os.popen('vcgencmd measure_temp').readline()
- return(res.replace("temp=","").replace("'C\n",""))
- def getCPUusage():
- cpu = psutil.cpu_freq()
- return(res.replace("current=","").replace("C\n",""))
- #Disk Space
- drive = os.popen('df -hm /dev/sda2').read()
- drive = drive.split()
- drive = drive[10]
- external = float(drive) / 1000
- driveSpace = os.popen('df -hm /dev/sda2').read()
- driveSpace = driveSpace.split()
- driveSpace = driveSpace[8]
- externalSpace = float(driveSpace) / 1000
- driveUsed = os.popen('df -hm /dev/sda2').read()
- driveUsed = driveUsed.split()
- driveUsed = driveUsed[9]
- externalUsed = float(driveUsed) / 1000
- memory = psutil.virtual_memory()
- memory = memory[1]
- version = (f"{uname.release}")
- cpu = psutil.cpu_freq()
- cpu = cpu.current
- r = requests.get('http://ipv4.icanhazip.com/')
- p = r.text
- name = "Home Server"
- now = datetime.datetime.now()
- currentTime = datetime.datetime.now()
- temp = float(getCPUtemperature())
- temp = (temp * 1.8) + 32
- boot = datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")
- ram = round(memory / 1024 / 1024)
- sql = "insert into test VALUES(NULL, '{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}')".format(name, temp , boot, currentTime, p,ram,version,cpu,external,externalSpace,externalUsed)
- number_of_rows = cursor.execute(sql)
- db.commit()
- db.close()
Add Comment
Please, Sign In to add comment