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.

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