RaspSysInfo.py
biblioteka z klasą SysInfo() udostępniająca różne informacje o systemie Raspbian Debian Wheezy.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
import subprocess import os from datetime import datetime import time import socket import os import urllib2 class SysInfo(): def getInternetStatus(self): REMOTE_SERVER = "www.google.com" try: host = socket.gethostbyname(REMOTE_SERVER) s = socket.create_connection((host, 80), 2) lan_connection = "OK" return lan_connection except: pass #lan_connection = "-" return 0 # Return information about disk space as a list (unit included) # Index 0: total disk space # Index 1: used disk space # Index 2: remaining disk space # Index 3: percentage of disk used def getDiskSpace(self): p = os.popen("df -h /") i = 0 while 1: i = i +1 line = p.readline() if i==2: return(line.split()[1:5]) def getExtIp(self): try: external_ip = urllib2.urlopen('http://ip.42.pl/raw').read() return external_ip except: return 0 # Return RAM information (unit=kb) in a list # Index 0: total RAM # Index 1: used RAM # Index 2: free RAM def getRAMinfo(self): p = os.popen('free -m') i = 0 while 1: i = i + 1 line = p.readline() if i==2: return(line.split()[1:4]) def getRam(self): #Returns a tuple (total ram, available ram) in megabytes. See www.linuxatemyram. com try: s = subprocess.check_output(["free","-m"]) lines = s.split('\n') return ( int(lines[1].split()[1]), int(lines[2].split()[3]) ) except: return 0 def getProcessCount(self): #Returns the number of processes try: s = subprocess.check_output(["ps","-e"]) return len(s.split('\n')) except: return 0 def getUpStats(self): #Returns a tuple (uptime, 5 min load average) try: s = subprocess.check_output(["uptime"]) load_split = s.split('load average: ') load_five = float(load_split[1].split(',')[1]) up = load_split[0] up_pos = up.rfind(',',0,len(up)-4) up = up[:up_pos].split('up ')[1] return ( up , load_five ) except: return ( [] , 0 ) def getConnections(self): #Returns the number of network connections try: s = subprocess.check_output(["netstat","-tun"]) return len([x for x in s.split() if x == 'ESTABLISHED']) except: return 0 def getTemperature(self): #Returns the temperature in degrees C try: s = subprocess.check_output(["/opt/vc/bin/vcgencmd","measure_temp"]) return float(s.split('=')[1][:-3]) except: return 0 def getIpAddress(self): #Returns the current IP address arg='ip route list' p=subprocess.Popen(arg,shell=True,stdout=subprocess.PIPE) data = p.communicate() split_data = data[0].split() ipaddr = split_data[split_data.index('src')+1] return ipaddr def getCpuMaxSpeed(self): #Returns the Max CPU speed f = os.popen('/opt/vc/bin/vcgencmd get_config arm_freq') cpu = int(f.read().split('=')[1]) if cpu == 0: return 700 else: return cpu def getCpuCurrentSpeed(self): #Returns the current CPU speed file = open('/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq','r') current_cpu = int(file.read()) if current_cpu == 0: return 700 else: return current_cpu def getCurrentCPUuse(self): cpu_usage = (float(os.popen("top -bn 2 -d 0.01 | grep '^%Cpu' | tail -n 1 | awk '{print $2+$4+$6}'").readline().strip())) return cpu_usage #get kernel release def getKernelRelease(self): f = os.popen('uname -r') kernel = str(f.read().split()[0]) return kernel #hostname def getHostName(self): f = os.popen('uname -n') host_name = str(f.read().split()[0]) return host_name |
RaspSysInfo_example.py
przykład użycia klasy SysInfo z biblioteki RaspSysInfo. Oba pliki muszą być w jednym katalogu.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
#!/usr/bin/python from RaspSysInfo import SysInfo """RaspSysInfo_example: example usage for RaspSysInfo.py """ __author__ = "zbiros" __copyright__ = "Copyright 2015, Malinowo.net.pl" __license__ = "GPL" __version__ = "1.0.1" __maintainer__ = "zbiros" __email__ = "zbiros@malinowo.secu.com.pl" __status__ = "Development" SysInfo = SysInfo() a = SysInfo.getInternetStatus() print "getInternetStatus:" + a print "--------------------------" b = SysInfo.getDiskSpace() print "getDiskSpace/remaining:" + b[2] print "--------------------------" c = SysInfo.getExtIp() print "getExtIp:" + c print "--------------------------" d = SysInfo.getRAMinfo() print "getRAMinfo/free RAM:" + d[2] print "--------------------------" e = SysInfo.getRam() print "getRam:" + `e[0]` print "--------------------------" f = SysInfo.getProcessCount() print "getProcessCount:" + `f` print "--------------------------" g = SysInfo.getUpStats() print "getUpStats:" + g[0] print "--------------------------" h = SysInfo.getConnections() print "getConnections:" + `h` print "--------------------------" i = SysInfo.getTemperature() print "getTemperature:" + `i` print "--------------------------" j = SysInfo.getIpAddress() print "getIpAddress:" + j print "--------------------------" k = SysInfo.getCpuMaxSpeed() print "getCpuMaxSpeed:" + `k` print "--------------------------" l = SysInfo.getCpuCurrentSpeed() print "getCpuCurrentSpeed:" + `l` print "--------------------------" m = SysInfo.getCurrentCPUuse() print "getCurrentCPUuse:" + `m` print "--------------------------" o = SysInfo.getKernelRelease() print "getKernelRelease:" + o print "--------------------------" p = SysInfo.getHostName() print "getHostName:" + p print "--------------------------" |