-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload_cpu.py
53 lines (44 loc) · 1.43 KB
/
upload_cpu.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
import time
import json,urllib,urllib2,subprocess,time,csv,sys,random
# upload script for mango m2m http receiver datastore
INTERVAL = 0.1
URL = "http://128.97.93.30:8080/httpds?__device=cpuload&load="
def getTimeList():
"""
Fetches a list of time units the cpu has spent in various modes
Detailed explanation at http://www.linuxhowtos.org/System/procstat.htm
"""
cpuStats = file("/proc/stat", "r").readline()
columns = cpuStats.replace("cpu", "").split(" ")
return map(int, filter(None, columns))
def deltaTime(interval):
"""
Returns the difference of the cpu statistics returned by getTimeList
that occurred in the given time delta
"""
timeList1 = getTimeList()
time.sleep(interval)
timeList2 = getTimeList()
return [(t2-t1) for t1, t2 in zip(timeList1, timeList2)]
def getCpuLoad():
"""
Returns the cpu load as a value from the interval [0.0, 1.0]
"""
dt = list(deltaTime(INTERVAL))
idle_time = float(dt[3])
total_time = sum(dt)
load = 1-(idle_time/total_time)
return load
while True:
print "CPU usage=%.2f%%" % (getCpuLoad()*100.0)
cpuload = getCpuLoad()*100.0
requrl = URL + '%.2f' % cpuload
print requrl
try:
response = urllib2.urlopen(requrl)
print "RESPONSE : " + response.read()
response.close()
except Exception, e:
print "Couldn't do it: %s" % e
#r = urllib2.urlopen('http://128.97.93.30:9000')
time.sleep(1)