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.

506 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
  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 uptime(request):
  34. """
  35. Return uptime
  36. """
  37. try:
  38. up_time = get_uptime()
  39. except Exception:
  40. up_time = None
  41. data = json.dumps(up_time)
  42. response = HttpResponse()
  43. response['Content-Type'] = "text/javascript"
  44. response.write(data)
  45. return response
  46. @login_required(login_url='/login/')
  47. def getdisk(request):
  48. """
  49. Return the disk usage
  50. """
  51. try:
  52. getdisk = get_disk()
  53. except Exception:
  54. getdisk = None
  55. data = json.dumps(getdisk)
  56. response = HttpResponse()
  57. response['Content-Type'] = "text/javascript"
  58. response.write(data)
  59. return response
  60. @login_required(login_url='/login/')
  61. def getips(request):
  62. """
  63. Return the IPs and interfaces
  64. """
  65. try:
  66. get_ips = get_ipaddress()
  67. except Exception:
  68. get_ips = None
  69. data = json.dumps(get_ips['itfip'])
  70. response = HttpResponse()
  71. response['Content-Type'] = "text/javascript"
  72. response.write(data)
  73. return response
  74. @login_required(login_url='/login/')
  75. def getusers(request):
  76. """
  77. Return online users
  78. """
  79. try:
  80. online_users = get_users()
  81. except Exception:
  82. online_users = None
  83. data = json.dumps(online_users)
  84. response = HttpResponse()
  85. response['Content-Type'] = "text/javascript"
  86. response.write(data)
  87. return response
  88. @login_required(login_url='/login/')
  89. def getproc(request):
  90. """
  91. Return the running processes
  92. """
  93. try:
  94. processes = get_cpu_usage()
  95. processes = processes['all']
  96. except Exception:
  97. processes = None
  98. data = json.dumps(processes)
  99. response = HttpResponse()
  100. response['Content-Type'] = "text/javascript"
  101. response.write(data)
  102. return response
  103. @login_required(login_url='/login/')
  104. def cpuusage(request):
  105. """
  106. Return CPU Usage in %
  107. """
  108. try:
  109. cpu_usage = get_cpu_usage()
  110. except Exception:
  111. cpu_usage = 0
  112. cpu = [
  113. {
  114. "value": cpu_usage['free'],
  115. "color": "#0AD11B"
  116. },
  117. {
  118. "value": cpu_usage['used'],
  119. "color": "#F7464A"
  120. }
  121. ]
  122. data = json.dumps(cpu)
  123. response = HttpResponse()
  124. response['Content-Type'] = "text/javascript"
  125. response.write(data)
  126. return response
  127. @login_required(login_url='/login/')
  128. def memusage(request):
  129. """
  130. Return Memory Usage in % and numeric
  131. """
  132. datasets = []
  133. try:
  134. mem_usage = get_mem()
  135. except Exception:
  136. mem_usage = 0
  137. try:
  138. cookies = request._cookies['memory_usage']
  139. except Exception:
  140. cookies = None
  141. if not cookies:
  142. datasets.append(0)
  143. else:
  144. datasets = eval(cookies)
  145. if len(datasets) > 10:
  146. while datasets:
  147. del datasets[0]
  148. if len(datasets) == 10:
  149. break
  150. if len(datasets) <= 9:
  151. datasets.append(int(mem_usage['usage']))
  152. if len(datasets) == 10:
  153. datasets.append(int(mem_usage['usage']))
  154. del datasets[0]
  155. # Some fix division by 0 Chart.js
  156. if len(datasets) == 10:
  157. if sum(datasets) == 0:
  158. datasets[9] += 0.1
  159. if sum(datasets) / 10 == datasets[0]:
  160. datasets[9] += 0.1
  161. memory = {
  162. 'labels': [""] * 10,
  163. 'datasets': [
  164. {
  165. "fillColor": "rgba(249,134,33,0.5)",
  166. "strokeColor": "rgba(249,134,33,1)",
  167. "pointColor": "rgba(249,134,33,1)",
  168. "pointStrokeColor": "#fff",
  169. "data": datasets
  170. }
  171. ]
  172. }
  173. data = json.dumps(memory)
  174. response = HttpResponse()
  175. response['Content-Type'] = "text/javascript"
  176. response.cookies['memory_usage'] = datasets
  177. response.write(data)
  178. return response
  179. @login_required(login_url='/login/')
  180. def loadaverage(request):
  181. """
  182. Return Load Average numeric
  183. """
  184. datasets = []
  185. try:
  186. load_average = get_load()
  187. except Exception:
  188. load_average = 0
  189. try:
  190. cookies = request._cookies['load_average']
  191. except Exception:
  192. cookies = None
  193. if not cookies:
  194. datasets.append(0)
  195. else:
  196. datasets = eval(cookies)
  197. if len(datasets) > 10:
  198. while datasets:
  199. del datasets[0]
  200. if len(datasets) == 10:
  201. break
  202. if len(datasets) <= 9:
  203. datasets.append(float(load_average))
  204. if len(datasets) == 10:
  205. datasets.append(float(load_average))
  206. del datasets[0]
  207. # Some fix division by 0 Chart.js
  208. if len(datasets) == 10:
  209. if sum(datasets) == 0:
  210. datasets[9] += 0.1
  211. if sum(datasets) / 10 == datasets[0]:
  212. datasets[9] += 0.1
  213. load = {
  214. 'labels': [""] * 10,
  215. 'datasets': [
  216. {
  217. "fillColor" : "rgba(151,187,205,0.5)",
  218. "strokeColor" : "rgba(151,187,205,1)",
  219. "pointColor" : "rgba(151,187,205,1)",
  220. "pointStrokeColor": "#fff",
  221. "data": datasets
  222. }
  223. ]
  224. }
  225. data = json.dumps(load)
  226. response = HttpResponse()
  227. response['Content-Type'] = "text/javascript"
  228. response.cookies['load_average'] = datasets
  229. response.write(data)
  230. return response
  231. @login_required(login_url='/login/')
  232. def gettraffic(request):
  233. """
  234. Return the traffic for the interface
  235. """
  236. datasets_in = []
  237. datasets_in_i = []
  238. datasets_out = []
  239. datasets_out_o = []
  240. json_traffic = []
  241. cookie_traffic = {}
  242. label = "KBps"
  243. try:
  244. intf = get_ipaddress()
  245. intf = intf['interface'][0]
  246. traffic = get_traffic(intf)
  247. except Exception:
  248. traffic = 0
  249. try:
  250. cookies = request._cookies['traffic']
  251. except Exception:
  252. cookies = None
  253. if not cookies:
  254. datasets_in.append(0)
  255. datasets_in_i.append(0)
  256. datasets_out.append(0)
  257. datasets_out_o.append(0)
  258. else:
  259. datasets = eval(cookies)
  260. datasets_in = datasets[0]
  261. datasets_out = datasets[1]
  262. datasets_in_i = datasets[2]
  263. datasets_out_o = datasets[3]
  264. if len(datasets_in) > 10:
  265. while datasets_in:
  266. del datasets_in[0]
  267. if len(datasets_in) == 10:
  268. break
  269. if len(datasets_in_i) > 2:
  270. while datasets_in_i:
  271. del datasets_in_i[0]
  272. if len(datasets_in_i) == 2:
  273. break
  274. if len(datasets_out) > 10:
  275. while datasets_out:
  276. del datasets_out[0]
  277. if len(datasets_out) == 10:
  278. break
  279. if len(datasets_out_o) > 2:
  280. while datasets_out_o:
  281. del datasets_out_o[0]
  282. if len(datasets_out_o) == 2:
  283. break
  284. if len(datasets_in_i) <= 1:
  285. datasets_in_i.append(float(traffic['traffic_in']))
  286. if len(datasets_in_i) == 2:
  287. datasets_in_i.append(float(traffic['traffic_in']))
  288. del datasets_in_i[0]
  289. if len(datasets_out_o) <= 1:
  290. datasets_out_o.append(float(traffic['traffic_out']))
  291. if len(datasets_out_o) == 2:
  292. datasets_out_o.append(float(traffic['traffic_out']))
  293. del datasets_out_o[0]
  294. dataset_in = (float(((datasets_in_i[1] - datasets_in_i[0]) / 1024 ) / ( time_refresh_net / 1000 )))
  295. dataset_out = (float(((datasets_out_o[1] - datasets_out_o[0]) / 1024 ) / ( time_refresh_net / 1000 )))
  296. if dataset_in > 1024 or dataset_out > 1024:
  297. dataset_in = (float(dataset_in / 1024 ))
  298. dataset_out = (float(dataset_out / 1024 ))
  299. label = "MBps"
  300. if len(datasets_in) <= 9:
  301. datasets_in.append(dataset_in)
  302. if len(datasets_in) == 10:
  303. datasets_in.append(dataset_in)
  304. del datasets_in[0]
  305. if len(datasets_out) <= 9:
  306. datasets_out.append(dataset_out)
  307. if len(datasets_out) == 10:
  308. datasets_out.append(dataset_out)
  309. del datasets_out[0]
  310. # Some fix division by 0 Chart.js
  311. if len(datasets_in) == 10:
  312. if sum(datasets_in) == 0:
  313. datasets_in[9] += 0.1
  314. if sum(datasets_in) / 10 == datasets_in[0]:
  315. datasets_in[9] += 0.1
  316. traff = {
  317. 'labels': [label] * 10,
  318. 'datasets': [
  319. {
  320. "fillColor": "rgba(105,210,231,0.5)",
  321. "strokeColor": "rgba(105,210,231,1)",
  322. "pointColor": "rgba(105,210,231,1)",
  323. "pointStrokeColor": "#fff",
  324. "data": datasets_in
  325. },
  326. {
  327. "fillColor": "rgba(227,48,81,0.5)",
  328. "strokeColor": "rgba(227,48,81,1)",
  329. "pointColor": "rgba(227,48,81,1)",
  330. "pointStrokeColor": "#fff",
  331. "data": datasets_out
  332. }
  333. ]
  334. }
  335. cookie_traffic = [datasets_in, datasets_out, datasets_in_i, datasets_out_o]
  336. data = json.dumps(traff)
  337. response = HttpResponse()
  338. response['Content-Type'] = "text/javascript"
  339. response.cookies['traffic'] = cookie_traffic
  340. response.write(data)
  341. return response
  342. @login_required(login_url='/login/')
  343. def getdiskio(request):
  344. """
  345. Return the reads and writes for the drive
  346. """
  347. datasets_in = []
  348. datasets_in_i = []
  349. datasets_out = []
  350. datasets_out_o = []
  351. json_diskrw = []
  352. cookie_diskrw = {}
  353. try:
  354. diskrw = get_disk_rw()
  355. diskrw = diskrw[0]
  356. except Exception:
  357. diskrw = 0
  358. try:
  359. cookies = request._cookies['diskrw']
  360. except Exception:
  361. cookies = None
  362. if not cookies:
  363. datasets_in.append(0)
  364. datasets_in_i.append(0)
  365. datasets_out.append(0)
  366. datasets_out_o.append(0)
  367. else:
  368. datasets = eval(cookies)
  369. datasets_in = datasets[0]
  370. datasets_out = datasets[1]
  371. datasets_in_i = datasets[2]
  372. datasets_out_o = datasets[3]
  373. if len(datasets_in) > 10:
  374. while datasets_in:
  375. del datasets_in[0]
  376. if len(datasets_in) == 10:
  377. break
  378. if len(datasets_in_i) > 2:
  379. while datasets_in_i:
  380. del datasets_in_i[0]
  381. if len(datasets_in_i) == 2:
  382. break
  383. if len(datasets_out) > 10:
  384. while datasets_out:
  385. del datasets_out[0]
  386. if len(datasets_out) == 10:
  387. break
  388. if len(datasets_out_o) > 2:
  389. while datasets_out_o:
  390. del datasets_out_o[0]
  391. if len(datasets_out_o) == 2:
  392. break
  393. if len(datasets_in_i) <= 1:
  394. datasets_in_i.append(int(diskrw[1]))
  395. if len(datasets_in_i) == 2:
  396. datasets_in_i.append(int(diskrw[1]))
  397. del datasets_in_i[0]
  398. if len(datasets_out_o) <= 1:
  399. datasets_out_o.append(int(diskrw[2]))
  400. if len(datasets_out_o) == 2:
  401. datasets_out_o.append(int(diskrw[2]))
  402. del datasets_out_o[0]
  403. dataset_in = (int((datasets_in_i[1] - datasets_in_i[0]) / ( time_refresh_net / 1000 )))
  404. dataset_out = (int((datasets_out_o[1] - datasets_out_o[0]) / ( time_refresh_net / 1000 )))
  405. if len(datasets_in) <= 9:
  406. datasets_in.append(dataset_in)
  407. if len(datasets_in) == 10:
  408. datasets_in.append(dataset_in)
  409. del datasets_in[0]
  410. if len(datasets_out) <= 9:
  411. datasets_out.append(dataset_out)
  412. if len(datasets_out) == 10:
  413. datasets_out.append(dataset_out)
  414. del datasets_out[0]
  415. # Some fix division by 0 Chart.js
  416. if len(datasets_in) == 10:
  417. if sum(datasets_in) == 0:
  418. datasets_in[9] += 0.1
  419. if sum(datasets_in) / 10 == datasets_in[0]:
  420. datasets_in[9] += 0.1
  421. disk_rw = {
  422. 'labels': [""] * 10,
  423. 'datasets': [
  424. {
  425. "fillColor": "rgba(245,134,15,0.5)",
  426. "strokeColor": "rgba(245,134,15,1)",
  427. "pointColor": "rgba(245,134,15,1)",
  428. "pointStrokeColor": "#fff",
  429. "data": datasets_in
  430. },
  431. {
  432. "fillColor": "rgba(15,103,245,0.5)",
  433. "strokeColor": "rgba(15,103,245,1)",
  434. "pointColor": "rgba(15,103,245,1)",
  435. "pointStrokeColor": "#fff",
  436. "data": datasets_out
  437. }
  438. ]
  439. }
  440. cookie_diskrw = [datasets_in, datasets_out, datasets_in_i, datasets_out_o]
  441. data = json.dumps(disk_rw)
  442. response = HttpResponse()
  443. response['Content-Type'] = "text/javascript"
  444. response.cookies['diskrw'] = cookie_diskrw
  445. response.write(data)
  446. return response