-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.py
executable file
·209 lines (194 loc) · 8.03 KB
/
index.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/python
# coding=utf-8
import datetime
import time
import json
import socket
import os.path
import bottle
from bottle import route, view, request, response
from my_db import db_exec_sql
import usage
import rrd_uptime
import rrd_cpu
import rrd_users
import rrd_ansible
import rrd_scratch
import session_pc
@route('/')
@view('mainpage')
def main():
#result = db_exec_sql("select id, ip, hostname, lastupdate, (julianday('now')-julianday(lastupdate))*24*60 from machines order by hostname")
online = db_exec_sql("select count() from machines where (julianday('now')-julianday(lastupdate))*24*60 < 10")
usersloggedin = db_exec_sql("select ip, users from users where (julianday('now')-julianday(time))*24*60 < 10 group by ip,users")
userslog = dict()
for i in usersloggedin:
if i[0] in userslog:
temp = userslog[i[0]]
userslog[i[0]] = temp + " " + i[1]
else:
userslog[i[0]] = i[1]
onlinecount = online[0][0]
displaydata = {}
for i in ['a425', 'a437', 'a439', 'a441', 'a443', 'a445', 'misc']:
displaydata[i] = {}
displaydata[i]['name'] = u"Аудитория "+i
displaydata[i]['link'] = i
displaydata[i]['online'] = 0
room = (i, )
if i == 'misc':
displaydata[i]['name'] = u"Прочее"
result = db_exec_sql("select id, ip, hostname, lastupdate, (julianday('now')-julianday(lastupdate))*24*60 from machines where room is NULL order by hostname")
else:
result = db_exec_sql("select id, ip, hostname, lastupdate, (julianday('now')-julianday(lastupdate))*24*60 from machines where room = ? order by hostname", room)
temp = []
for record in result:
ansible = rrd_ansible.latest(str(record[2]))
scratch = rrd_scratch.latest(str(record[2]))
temptuple = record + (usage.getpowered(30, record[1]), usage.getusage(30, record[1]), ansible, scratch, 'NaN')
temp.append(temptuple)
displaydata[i]['values'] = temp
displaydata[i]['total'] = len(result)
return dict(data=displaydata, date=datetime.datetime.now(), online=onlinecount, userslog=userslog)
#@route('/computer2/<machine>/<period:re:[d,w,m,y]>')
#@route('/computer2/<machine>')
#@view('computer2')
#def machinestats3(machine,period = 'w'):
# sessions_pc=session_pc.get_sessions(machine, time.time()-30*24*60*60, None)
# sessions_user=[]
# sessions_user_open=[]
# return dict(date=datetime.datetime.now(), machine=machine, sessions_pc=sessions_pc, sessions_user=sessions_user, sessions_open=sessions_user_open, group=False, period=period)
@route('/last/<period:re:[d,w,m,y]>')
@route('/last/')
@view('last')
def last_users(period = 'm'):
time = 0
if period == 'd':
time = 1
elif period == 'w':
time = 7
elif period == 'm':
time = 30
elif period == 'y':
time = 365
time = time * 24 * 60
users = db_exec_sql("select users,count() from users where (julianday('now')-julianday(time))*24*60 < ( ? ) group by users order by count() desc", (time, ) )
return dict(users=users, period = time)
@route('/computer/<machine>/<period:re:[d,w,m,y]>')
@route('/computer/<machine>/')
@view('computer')
def machinestats2(machine,period = 'w'):
result = db_exec_sql("select hostname, ip from machines where hostname like ?", (machine,))
if len(result) > 0:
ip_addr = result[0][1]
result = result[0][0]
else:
result = None
ip_addr = ""
popularity = {}
for j in [7, 14, 30, 60, 90, 180]:
popularity[j] = usage.getpopularity(j, ip_addr)
return dict(date=datetime.datetime.now(), machine=result, popularity=popularity, attr=machine, group=False, period=period)
@route('/group/<grp>/')
@view('group')
def machinestats(grp):
if grp in ['a425', 'a437', 'a439', 'a441', 'a443', 'a445', 'misc']:
if grp == 'misc':
result = db_exec_sql("select hostname,ip from machines where room is NULL order by hostname")
else:
result = db_exec_sql("select hostname,ip from machines where room = ? order by hostname", (grp,))
tabs = []
recipes = {}
popularity = {}
for i in result:
popularity[i[0]] = {}
for j in [7, 14, 30, 60, 90, 180]:
popularity[i[0]][j] = usage.getpopularity(j, i[1])
for i in result:
if os.path.isdir("/var/www/rrds/"+i[0]):
temp = []
temp.append(i[0])
temp2 = []
for j in ["cpu", "memory", "load", "users", "uptime"]:
recipe_name = i[0]+"_"+j
temp2.append(recipe_name)
recipes[recipe_name] = {}
recipes[recipe_name]["title"] = j + " on " + i[0]
temp.append(temp2)
tabs.append(temp)
tabs2 = json.dumps(tabs)
recipes2 = json.dumps(recipes)
return dict(date=datetime.datetime.now(), hosts=result, popularity=popularity, tabs=tabs2, recipes=recipes2, attr=grp, group=True)
@route('/graph/<hostname>_<typ>')
@route('/graph/<hostname>_<typ>/<period:re:[d,w,m,y]>')
def graphs(hostname, typ, period = 'w'):
result = "Error"
if typ == "uptime":
result = rrd_uptime.graph(hostname, period)
elif typ == "cpu1":
result = rrd_cpu.graph1(hostname, period)
elif typ == "cpu2":
result = rrd_cpu.graph2(hostname, period)
elif typ == "cpu3":
result = rrd_cpu.graph3(hostname, period)
elif typ == "users":
result = rrd_users.graph(hostname, period)
elif typ == "scratch":
result = rrd_scratch.graph(hostname, period)
elif typ == "ansible":
result = rrd_ansible.graph(hostname, period)
response.set_header('Content-type', 'image/png')
return str(result)
@route('/api/ansible', method='POST')
def acceptansibledata():
ip_addr = request.environ.get("REMOTE_ADDR")
hostname = request.environ.get("REMOTE_HOST")
if hostname is None:
hostname = socket.gethostbyaddr(ip_addr)[0]
try:
ok_value = request.json['ok']
change_value = request.json['change']
unreachable_value = request.json['unreachable']
failed_value = request.json['failed']
rrd_ansible.insert(hostname, [ok_value, change_value, unreachable_value, failed_value])
except:
pass
return dict()
@route('/api/scratch', method='POST')
def acceptscratchdata():
ip_addr = request.environ.get("REMOTE_ADDR")
hostname = request.environ.get("REMOTE_HOST")
if hostname is None:
hostname = socket.gethostbyaddr(ip_addr)[0]
scratch_free = request.json['scratch_free']
scratch_total = request.json['scratch_total']
rrd_scratch.insert(hostname, [scratch_free, scratch_total])
return dict()
@route('/api/data', method='POST')
def acceptdata():
ip_addr = request.environ.get("REMOTE_ADDR")
hostname = request.environ.get("REMOTE_HOST")
if hostname is None:
hostname = socket.gethostbyaddr(ip_addr)[0]
temp = (ip_addr, )
res = db_exec_sql("select id from machines where ip = ?", temp)
if len(res) == 0:
temp = (ip_addr, hostname, )
db_exec_sql("insert into machines ( ip, hostname, lastupdate ) values ( ?, ?, (DATETIME('now')))", temp)
else:
temp = (hostname, ip_addr, )
db_exec_sql("update machines set hostname = ?, lastupdate = (DATETIME('now')) where ip = ?", temp)
# here goes the report
reportedhostname = request.json['hostname']
uptime = request.json['uptime']
users = request.json['users']
cpu = request.json['cpu']
db_exec_sql("insert into hostnames (ip, hostname, time) values ( ?, ?, DATETIME('now'))", (ip_addr, reportedhostname))
db_exec_sql("insert into uptime (ip, time, uptime) values ( ?, DATETIME('now'), ?)", (ip_addr, uptime))
rrd_uptime.insert(hostname, [uptime, ])
rrd_cpu.insert(hostname, [cpu['load'], cpu['loadavg'], cpu['cores']])
rrd_users.insert(hostname, [len(set(users)),])
for i in users:
db_exec_sql("insert into users (ip, time, users) values ( ?, DATETIME('now'), ?)", (ip_addr, i))
return dict()
bottle.run(server=bottle.CGIServer)