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.

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