-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_view_api.py
More file actions
170 lines (117 loc) · 4.37 KB
/
data_view_api.py
File metadata and controls
170 lines (117 loc) · 4.37 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
from flask import Flask , request , jsonify #v3.3
import requests #v2.31.0
import datetime
import configs.user_token as user_token
from configs import config
"""
read uptime on /proc/uptime file
read system name on /etc/hostname
read loadavg on /proc/loadavg
read temp on (try find /sys -name temp and
paste temp link in system_data_temp function)
"""
def system_data_uptime() :
try :
read_uptime = open("/proc/uptime") #uptime in linux on /proc/uptime file
raw_uptime = read_uptime.read()
space_rm = raw_uptime.index(" ")
uptime = raw_uptime[0:space_rm]
uptime_day = ((float(uptime) / 60)/60)/24
uptime_hour = (float(uptime) / 60)/60
uptime_min = float(uptime) / 60
except Exception :
uptime_day = "Not found"
uptime_hour = "Not found"
uptime_min = "Not found"
return {"uptime_min" : uptime_min , "uptime_hour" : uptime_hour , "uptime_day" : uptime_day}
def system_data_system_name():
try :
read_name = open("/etc/hostname")#hostname in linux on /etc/hostname file
system_name = read_name.read()
f4edit = system_name.replace("\n" , "")
except Exception :
f4edit = "Not found"
return {"name" : f4edit}
def system_data_loadavg():
try :
read_loadavg = open("/proc/loadavg")#loadavg in linux on /proc/loadavg file
loadavg = read_loadavg.read()
counter = (0)
middle = ("")
finall_output = []
while True :
memory = loadavg[counter]
if memory != " ":
middle = middle + memory
counter = counter + 1
else :
finall_output.append(middle)
middle = ("")
counter = counter + 1
if counter == len(loadavg) :
finall_output.append(middle)
break
f4edit = finall_output[4]
f4edit = f4edit.replace("\n" , "")
finall_output[4] = f4edit
except Exception :
finall_output = ["Not found" , "Not found" , "Not found" , "Not found" , "Not found"]
return {"loadavg(1min)":finall_output[0] ,
"loadavg(5min)":finall_output[1] ,
"loadavg(15min)":finall_output[2] ,
"process":finall_output[3] ,
"last_process":finall_output[4]}
"""
import temp address file for use read in file because temp
file address is difrent on other system for edit temp address
go on configs/config.py
"""
def system_data_temp():
try :
read_temp = open(config.temp_file_address)
temp = read_temp.read()
temp = int(temp)
except Exception:
temp = " Not found"
return {"temp":temp}
def system_data_ip():
try :
request_url = 'https://geolocation-db.com/jsonp/' #use geolocation for pubic ipv4
response = requests.get(request_url)
response = response.text
response = response.replace("callback(" , "")
response = response.replace(")" , "")
response = response.replace("null" , "False")
return (eval(response))["IPv4"]
except OSError as e :
return " Not found"
def system_data_time():
cu = datetime.datetime.now()
time = cu.strftime('%H:%M %p')
return time
app = Flask(__name__)
"""
for edit api username and password go on /configs/user_token.py
im set default username and password :
username = user
token = ealkcjveafgDAtg
use this user name and pasword for request to api on internet
"""
#eror 400 : request format is incorrect
#eror 401 : Username or token is incorrect
@app.route('/d_view', methods=['POST'])
def data_view():
if "user_name" not in request.form and "token" not in request.form:
return jsonify({"Eror": "Your request format is incorrect(use user name and token)"}), 400
data = {"name":(system_data_system_name())["name"] ,
"uptime":system_data_uptime() ,
"loadavg":system_data_loadavg() ,
"temp": (system_data_temp())["temp"],
"ipv4":system_data_ip() ,
"time":system_data_time()}
if request.form["user_name"] == user_token.user_name and request.form["token"] == user_token.token :
return (jsonify(data))
else :
return [("Username or token is incorrect") , 401 ]
if __name__ == ("__main__") :
app.run(port="9887" , debug=True)