-
Notifications
You must be signed in to change notification settings - Fork 1
/
httpServer.py
executable file
·108 lines (95 loc) · 3.95 KB
/
httpServer.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (C)2018 NavInfo Co.,Ltd. All right reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from performanceInfo import PerformanceInfo
from flup.server.fcgi import WSGIServer
from taskQueue import TaskQueue
from threading import Thread
from cgi import parse_qs
import threading
import platform
import psutil
import json
import time
from sysInfoCollector import g_sysInfoCollector
def webapp(environ, start_response):
method = environ.get('REQUEST_METHOD')
content = "404 not found"
if method == 'GET':
response = {}
query = parse_qs(environ.get('QUERY_STRING'))
path = environ.get('PATH_INFO')
if path.startswith("/performance/start_collecting") and query:
task = {}
duration = query.get('duration')
interval = query.get('interval')
hash = query.get('hash')
server = query.get('server')
if duration and interval and hash:
serverName = None
if server:
serverName = server[0]
response.setdefault("result", "succeeded")
ts = time.time()
task.setdefault('startTimestamp', ts)
task.setdefault('stopTimestamp', ts + int(duration[0]))
task.setdefault('duration', int(duration[0]))
task.setdefault('interval', int(interval[0]))
task.setdefault('server', serverName)
task.setdefault('sysInfoCollector', g_sysInfoCollector)
task.setdefault('hash', hash[0])
g_tasksQueue.addTask(task)
else:
response.setdefault("result", "failed")
elif path.startswith("/performance/stop_collecting") and query:
hash = query.get('hash')[0]
task = g_tasksQueue.getTaskBySha(hash)
if task:
taskResult = task.get('response')
result = "failed"
if taskResult:
result = "succeeded"
response.update(taskResult)
response.setdefault("result", result)
else:
response.setdefault("result", "failed")
elif path.startswith("/performance/real_time"):
p = PerformanceInfo(None)
response.setdefault("result", "succeeded")
response.setdefault("system", p.instantData())
else:
start_response('404 NOT FOUND', [('Content-Type', 'application/json')])
return content
content = json.dumps(response, sort_keys=True, indent=4, separators=(',', ':'))
start_response('200 OK', [('Content-Type', 'application/json')])
return content
class serverThread(threading.Thread):
def __init__(self, name, loop, stop):
threading.Thread.__init__(self)
self.name = name
self.loop = loop
self.stop = stop
def run(self):
self.loop()
def stop(self):
self.stop()
g_rLock = threading.RLock()
g_tasksQueue = TaskQueue(10, g_rLock)
if __name__ == '__main__':
print(platform.python_version())
sysInfoThread = serverThread("sysInfo-thread", g_sysInfoCollector.loop, g_sysInfoCollector.stop)
sysInfoThread.start()
taskThread = serverThread("tasks-thread", g_tasksQueue.loop, g_tasksQueue.stop)
taskThread.start()
WSGIServer(webapp, bindAddress='/etc/ncserver/system-monitor/.ncserver.sock', umask=0000).run()
taskThread.stop()
sysInfoThread.stop()