You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

519 lines
14 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
  1. #The MIT License (MIT)
  2. #
  3. #Copyright (c) 2014 Florian Neagu - michaelneagu@gmail.com - https://github.com/k3oni/
  4. #
  5. #Permission is hereby granted, free of charge, to any person obtaining a copy
  6. #of this software and associated documentation files (the "Software"), to deal
  7. #in the Software without restriction, including without limitation the rights
  8. #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. #copies of the Software, and to permit persons to whom the Software is
  10. #furnished to do so, subject to the following conditions:
  11. #
  12. #The above copyright notice and this permission notice shall be included in all
  13. #copies or substantial portions of the Software.
  14. #
  15. #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. #SOFTWARE.
  22. import json
  23. from django.shortcuts import render_to_response
  24. from django.http import HttpResponse, HttpResponseRedirect
  25. from django.template import RequestContext
  26. from django.contrib.auth.decorators import login_required
  27. from main.views import *
  28. from pydash.settings import TIME_JS_REFRESH, TIME_JS_REFRESH_LONG, TIME_JS_REFRESH_NET
  29. time_refresh = TIME_JS_REFRESH
  30. time_refresh_long = TIME_JS_REFRESH_LONG
  31. time_refresh_net = TIME_JS_REFRESH_NET
  32. @login_required(login_url='/login/')
  33. def getnetstat(request):
  34. """
  35. Return netstat output
  36. """
  37. net_stat = get_netstat()
  38. data = json.dumps(net_stat)
  39. response = HttpResponse()
  40. response['Content-Type'] = "text/javascript"
  41. response.write(data)
  42. return response
  43. @login_required(login_url='/login/')
  44. def uptime(request):
  45. """
  46. Return uptime
  47. """
  48. try:
  49. up_time = get_uptime()
  50. except Exception:
  51. up_time = None
  52. data = json.dumps(up_time)
  53. response = HttpResponse()
  54. response['Content-Type'] = "text/javascript"
  55. response.write(data)
  56. return response
  57. @login_required(login_url='/login/')
  58. def getdisk(request):
  59. """
  60. Return the disk usage
  61. """
  62. try:
  63. getdisk = get_disk()
  64. except Exception:
  65. getdisk = None
  66. data = json.dumps(getdisk)
  67. response = HttpResponse()
  68. response['Content-Type'] = "text/javascript"
  69. response.write(data)
  70. return response
  71. @login_required(login_url='/login/')
  72. def getips(request):
  73. """
  74. Return the IPs and interfaces
  75. """
  76. try:
  77. get_ips = get_ipaddress()
  78. except Exception:
  79. get_ips = None
  80. data = json.dumps(get_ips['itfip'])
  81. response = HttpResponse()
  82. response['Content-Type'] = "text/javascript"
  83. response.write(data)
  84. return response
  85. @login_required(login_url='/login/')
  86. def getusers(request):
  87. """
  88. Return online users
  89. """
  90. try:
  91. online_users = get_users()
  92. except Exception:
  93. online_users = None
  94. data = json.dumps(online_users)
  95. response = HttpResponse()
  96. response['Content-Type'] = "text/javascript"
  97. response.write(data)
  98. return response
  99. @login_required(login_url='/login/')
  100. def getproc(request):
  101. """
  102. Return the running processes
  103. """
  104. try:
  105. processes = get_cpu_usage()
  106. processes = processes['all']
  107. except Exception:
  108. processes = None
  109. data = json.dumps(processes)
  110. response = HttpResponse()
  111. response['Content-Type'] = "text/javascript"
  112. response.write(data)
  113. return response
  114. @login_required(login_url='/login/')
  115. def cpuusage(request):
  116. """
  117. Return CPU Usage in %
  118. """
  119. try:
  120. cpu_usage = get_cpu_usage()
  121. except Exception:
  122. cpu_usage = 0
  123. cpu = [
  124. {
  125. "value": cpu_usage['free'],
  126. "color": "#0AD11B"
  127. },
  128. {
  129. "value": cpu_usage['used'],
  130. "color": "#F7464A"
  131. }
  132. ]
  133. data = json.dumps(cpu)
  134. response = HttpResponse()
  135. response['Content-Type'] = "text/javascript"
  136. response.write(data)
  137. return response
  138. @login_required(login_url='/login/')
  139. def memusage(request):
  140. """
  141. Return Memory Usage in % and numeric
  142. """
  143. datasets = []
  144. try:
  145. mem_usage = get_mem()
  146. except Exception:
  147. mem_usage = 0
  148. try:
  149. cookies = request._cookies['memory_usage']
  150. except Exception:
  151. cookies = None
  152. if not cookies:
  153. datasets.append(0)
  154. else:
  155. datasets = eval(cookies)
  156. if len(datasets) > 10:
  157. while datasets:
  158. del datasets[0]
  159. if len(datasets) == 10:
  160. break
  161. if len(datasets) <= 9:
  162. datasets.append(int(mem_usage['usage']))
  163. if len(datasets) == 10:
  164. datasets.append(int(mem_usage['usage']))
  165. del datasets[0]
  166. # Some fix division by 0 Chart.js
  167. if len(datasets) == 10:
  168. if sum(datasets) == 0:
  169. datasets[9] += 0.1
  170. if sum(datasets) / 10 == datasets[0]:
  171. datasets[9] += 0.1
  172. memory = {
  173. 'labels': [""] * 10,
  174. 'datasets': [
  175. {
  176. "fillColor": "rgba(249,134,33,0.5)",
  177. "strokeColor": "rgba(249,134,33,1)",
  178. "pointColor": "rgba(249,134,33,1)",
  179. "pointStrokeColor": "#fff",
  180. "data": datasets
  181. }
  182. ]
  183. }
  184. data = json.dumps(memory)
  185. response = HttpResponse()
  186. response['Content-Type'] = "text/javascript"
  187. response.cookies['memory_usage'] = datasets
  188. response.write(data)
  189. return response
  190. @login_required(login_url='/login/')
  191. def loadaverage(request):
  192. """
  193. Return Load Average numeric
  194. """
  195. datasets = []
  196. try:
  197. load_average = get_load()
  198. except Exception:
  199. load_average = 0
  200. try:
  201. cookies = request._cookies['load_average']
  202. except Exception:
  203. cookies = None
  204. if not cookies:
  205. datasets.append(0)
  206. else:
  207. datasets = eval(cookies)
  208. if len(datasets) > 10:
  209. while datasets:
  210. del datasets[0]
  211. if len(datasets) == 10:
  212. break
  213. if len(datasets) <= 9:
  214. datasets.append(float(load_average))
  215. if len(datasets) == 10:
  216. datasets.append(float(load_average))
  217. del datasets[0]
  218. # Some fix division by 0 Chart.js
  219. if len(datasets) == 10:
  220. if sum(datasets) == 0:
  221. datasets[9] += 0.1
  222. if sum(datasets) / 10 == datasets[0]:
  223. datasets[9] += 0.1
  224. load = {
  225. 'labels': [""] * 10,
  226. 'datasets': [
  227. {
  228. "fillColor" : "rgba(151,187,205,0.5)",
  229. "strokeColor" : "rgba(151,187,205,1)",
  230. "pointColor" : "rgba(151,187,205,1)",
  231. "pointStrokeColor": "#fff",
  232. "data": datasets
  233. }
  234. ]
  235. }
  236. data = json.dumps(load)
  237. response = HttpResponse()
  238. response['Content-Type'] = "text/javascript"
  239. response.cookies['load_average'] = datasets
  240. response.write(data)
  241. return response
  242. @login_required(login_url='/login/')
  243. def gettraffic(request):
  244. """
  245. Return the traffic for the interface
  246. """
  247. datasets_in = []
  248. datasets_in_i = []
  249. datasets_out = []
  250. datasets_out_o = []
  251. json_traffic = []
  252. cookie_traffic = {}
  253. label = "KBps"
  254. try:
  255. intf = get_ipaddress()
  256. intf = intf['interface'][0]
  257. traffic = get_traffic(intf)
  258. except Exception:
  259. traffic = 0
  260. try:
  261. cookies = request._cookies['traffic']
  262. except Exception:
  263. cookies = None
  264. if not cookies:
  265. datasets_in.append(0)
  266. datasets_in_i.append(0)
  267. datasets_out.append(0)
  268. datasets_out_o.append(0)
  269. else:
  270. datasets = eval(cookies)
  271. datasets_in = datasets[0]
  272. datasets_out = datasets[1]
  273. datasets_in_i = datasets[2]
  274. datasets_out_o = datasets[3]
  275. if len(datasets_in) > 10:
  276. while datasets_in:
  277. del datasets_in[0]
  278. if len(datasets_in) == 10:
  279. break
  280. if len(datasets_in_i) > 2:
  281. while datasets_in_i:
  282. del datasets_in_i[0]
  283. if len(datasets_in_i) == 2:
  284. break
  285. if len(datasets_out) > 10:
  286. while datasets_out:
  287. del datasets_out[0]
  288. if len(datasets_out) == 10:
  289. break
  290. if len(datasets_out_o) > 2:
  291. while datasets_out_o:
  292. del datasets_out_o[0]
  293. if len(datasets_out_o) == 2:
  294. break
  295. if len(datasets_in_i) <= 1:
  296. datasets_in_i.append(float(traffic['traffic_in']))
  297. if len(datasets_in_i) == 2:
  298. datasets_in_i.append(float(traffic['traffic_in']))
  299. del datasets_in_i[0]
  300. if len(datasets_out_o) <= 1:
  301. datasets_out_o.append(float(traffic['traffic_out']))
  302. if len(datasets_out_o) == 2:
  303. datasets_out_o.append(float(traffic['traffic_out']))
  304. del datasets_out_o[0]
  305. dataset_in = (float(((datasets_in_i[1] - datasets_in_i[0]) / 1024 ) / ( time_refresh_net / 1000 )))
  306. dataset_out = (float(((datasets_out_o[1] - datasets_out_o[0]) / 1024 ) / ( time_refresh_net / 1000 )))
  307. if dataset_in > 1024 or dataset_out > 1024:
  308. dataset_in = (float(dataset_in / 1024 ))
  309. dataset_out = (float(dataset_out / 1024 ))
  310. label = "MBps"
  311. if len(datasets_in) <= 9:
  312. datasets_in.append(dataset_in)
  313. if len(datasets_in) == 10:
  314. datasets_in.append(dataset_in)
  315. del datasets_in[0]
  316. if len(datasets_out) <= 9:
  317. datasets_out.append(dataset_out)
  318. if len(datasets_out) == 10:
  319. datasets_out.append(dataset_out)
  320. del datasets_out[0]
  321. # Some fix division by 0 Chart.js
  322. if len(datasets_in) == 10:
  323. if sum(datasets_in) == 0:
  324. datasets_in[9] += 0.1
  325. if sum(datasets_in) / 10 == datasets_in[0]:
  326. datasets_in[9] += 0.1
  327. traff = {
  328. 'labels': [label] * 10,
  329. 'datasets': [
  330. {
  331. "fillColor": "rgba(105,210,231,0.5)",
  332. "strokeColor": "rgba(105,210,231,1)",
  333. "pointColor": "rgba(105,210,231,1)",
  334. "pointStrokeColor": "#fff",
  335. "data": datasets_in
  336. },
  337. {
  338. "fillColor": "rgba(227,48,81,0.5)",
  339. "strokeColor": "rgba(227,48,81,1)",
  340. "pointColor": "rgba(227,48,81,1)",
  341. "pointStrokeColor": "#fff",
  342. "data": datasets_out
  343. }
  344. ]
  345. }
  346. cookie_traffic = [datasets_in, datasets_out, datasets_in_i, datasets_out_o]
  347. data = json.dumps(traff)
  348. response = HttpResponse()
  349. response['Content-Type'] = "text/javascript"
  350. response.cookies['traffic'] = cookie_traffic
  351. response.write(data)
  352. return response
  353. @login_required(login_url='/login/')
  354. def getdiskio(request):
  355. """
  356. Return the reads and writes for the drive
  357. """
  358. datasets_in = []
  359. datasets_in_i = []
  360. datasets_out = []
  361. datasets_out_o = []
  362. json_diskrw = []
  363. cookie_diskrw = {}
  364. try:
  365. diskrw = get_disk_rw()
  366. diskrw = diskrw[0]
  367. except Exception:
  368. diskrw = 0
  369. try:
  370. cookies = request._cookies['diskrw']
  371. except Exception:
  372. cookies = None
  373. if not cookies:
  374. datasets_in.append(0)
  375. datasets_in_i.append(0)
  376. datasets_out.append(0)
  377. datasets_out_o.append(0)
  378. else:
  379. datasets = eval(cookies)
  380. datasets_in = datasets[0]
  381. datasets_out = datasets[1]
  382. datasets_in_i = datasets[2]
  383. datasets_out_o = datasets[3]
  384. if len(datasets_in) > 10:
  385. while datasets_in:
  386. del datasets_in[0]
  387. if len(datasets_in) == 10:
  388. break
  389. if len(datasets_in_i) > 2:
  390. while datasets_in_i:
  391. del datasets_in_i[0]
  392. if len(datasets_in_i) == 2:
  393. break
  394. if len(datasets_out) > 10:
  395. while datasets_out:
  396. del datasets_out[0]
  397. if len(datasets_out) == 10:
  398. break
  399. if len(datasets_out_o) > 2:
  400. while datasets_out_o:
  401. del datasets_out_o[0]
  402. if len(datasets_out_o) == 2:
  403. break
  404. if len(datasets_in_i) <= 1:
  405. datasets_in_i.append(int(diskrw[1]))
  406. if len(datasets_in_i) == 2:
  407. datasets_in_i.append(int(diskrw[1]))
  408. del datasets_in_i[0]
  409. if len(datasets_out_o) <= 1:
  410. datasets_out_o.append(int(diskrw[2]))
  411. if len(datasets_out_o) == 2:
  412. datasets_out_o.append(int(diskrw[2]))
  413. del datasets_out_o[0]
  414. dataset_in = (int((datasets_in_i[1] - datasets_in_i[0]) / ( time_refresh_net / 1000 )))
  415. dataset_out = (int((datasets_out_o[1] - datasets_out_o[0]) / ( time_refresh_net / 1000 )))
  416. if len(datasets_in) <= 9:
  417. datasets_in.append(dataset_in)
  418. if len(datasets_in) == 10:
  419. datasets_in.append(dataset_in)
  420. del datasets_in[0]
  421. if len(datasets_out) <= 9:
  422. datasets_out.append(dataset_out)
  423. if len(datasets_out) == 10:
  424. datasets_out.append(dataset_out)
  425. del datasets_out[0]
  426. # Some fix division by 0 Chart.js
  427. if len(datasets_in) == 10:
  428. if sum(datasets_in) == 0:
  429. datasets_in[9] += 0.1
  430. if sum(datasets_in) / 10 == datasets_in[0]:
  431. datasets_in[9] += 0.1
  432. disk_rw = {
  433. 'labels': [""] * 10,
  434. 'datasets': [
  435. {
  436. "fillColor": "rgba(245,134,15,0.5)",
  437. "strokeColor": "rgba(245,134,15,1)",
  438. "pointColor": "rgba(245,134,15,1)",
  439. "pointStrokeColor": "#fff",
  440. "data": datasets_in
  441. },
  442. {
  443. "fillColor": "rgba(15,103,245,0.5)",
  444. "strokeColor": "rgba(15,103,245,1)",
  445. "pointColor": "rgba(15,103,245,1)",
  446. "pointStrokeColor": "#fff",
  447. "data": datasets_out
  448. }
  449. ]
  450. }
  451. cookie_diskrw = [datasets_in, datasets_out, datasets_in_i, datasets_out_o]
  452. data = json.dumps(disk_rw)
  453. response = HttpResponse()
  454. response['Content-Type'] = "text/javascript"
  455. response.cookies['diskrw'] = cookie_diskrw
  456. response.write(data)
  457. return response