-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata_local.py
106 lines (92 loc) · 2.77 KB
/
data_local.py
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
"""
Title: Gathering Analog Data Without Hardware
Created on Thu Oct 10 14:49:20 2017
Modified:
@author: Kevin Machado Gamboa
Ref: Raspberry pi Cookbook 1 for python programmers. chapter 7 Pag. 205
"""
# !/usr/bin/env python3
# data_local.py
# -----------------------------------------------------------------------------
## Importing modules and creating variables
import subprocess
from random import randint
import time
MEM_TOTAL=0
MEM_USED=1
MEM_FREE=2
MEM_OFFSET=7
DRIVE_USED=0
DRIVE_FREE=1
DRIVE_OFFSET=9
DEBUG=False
DATANAME=["CPU_Load","System_Temp","CPU_Frequency",
"Random","RAM_Total","RAM_Used","RAM_Free",
"Drive_Used","Drive_Free"]
def read_loadavg():
# function to read 1 minute load average from system uptime
value = subprocess.check_output(
["awk '{print $1}' /proc/loadavg"], shell=True)
return float(value)
def read_systemp():
# function to read current system temperature
value = subprocess.check_output(
["cat /sys/class/thermal/thermal_zone0/temp"],
shell=True)
return int(value)
def read_cpu():
# function to read current clock frequency
value = subprocess.check_output(
["cat /sys/devices/system/cpu/cpu0/cpufreq/"+
"scaling_cur_freq"], shell=True)
return int(value)
def read_rnd():
return randint(0,255)
def read_mem():
# function to read RAM info
value = subprocess.check_output(["free"], shell=True)
memory=[]
for val in value.split()[MEM_TOTAL+
MEM_OFFSET:MEM_FREE+
MEM_OFFSET+1]:
memory.append(int(val))
return(memory)
def read_drive():
# function to read drive info
value = subprocess.check_output(["df"], shell=True)
memory=[]
for val in value.split()[DRIVE_USED+
DRIVE_OFFSET:DRIVE_FREE+
DRIVE_OFFSET+1]:
memory.append(int(val))
return(memory)
class device:
# Constructor:
def __init__(self,addr=0):
self.NAME=DATANAME
def getName(self):
return self.NAME
def getNew(self):
data=[]
data.append(read_loadavg())
data.append(read_systemp())
data.append(read_cpu())
data.append(read_rnd())
memory_ram = read_mem()
data.append(memory_ram[MEM_TOTAL])
data.append(memory_ram[MEM_USED])
data.append(memory_ram[MEM_FREE])
memory_drive = read_drive()
data.append(memory_drive[DRIVE_USED])
data.append(memory_drive[DRIVE_FREE])
return data
def main():
LOCAL = device()
print (str(LOCAL.getName()))
for i in range(10):
dataValues = LOCAL.getNew()
print (str(dataValues))
time.sleep(1)
if __name__=='__main__':
main()
#End