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.

688 lines
19 KiB

7 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.contrib.auth.decorators import login_required
  24. from django.shortcuts import render_to_response
  25. from django.core.urlresolvers import reverse, reverse_lazy
  26. from django.http import HttpResponse
  27. from django.template import RequestContext
  28. from settings import (TIME_JS_REFRESH, TIME_JS_REFRESH_LONG,
  29. TIME_JS_REFRESH_NET, _VERSION)
  30. import services
  31. import urls
  32. def get_pydash_urls():
  33. results = dict()
  34. for url_pattern in urls.urlpatterns:
  35. name = url_pattern.name
  36. try:
  37. # Works for non regex patterns
  38. results[name] = reverse(name)
  39. except Exception:
  40. # Get relative url prefix of regex patterns
  41. # Hacky solution but it works.
  42. # E.g. reverse('platform', args=[0])[:-2] -> /info/platform/
  43. # reverse('getcpus', args[0])[:-2] -> /info/getcpus/
  44. results[name] = reverse(name, args=[0])[:-2]
  45. return json.dumps(results)
  46. @login_required(login_url=reverse_lazy('login'))
  47. def index(request):
  48. """
  49. Index page.
  50. """
  51. pydash_urls = get_pydash_urls()
  52. return render_to_response('main.html', {'time_refresh': TIME_JS_REFRESH,
  53. 'time_refresh_long': TIME_JS_REFRESH_LONG,
  54. 'time_refresh_net': TIME_JS_REFRESH_NET,
  55. 'version': _VERSION,
  56. 'pydashUrls': pydash_urls},
  57. context_instance=RequestContext(request))
  58. @login_required(login_url=reverse_lazy('login'))
  59. def getnetstat(request):
  60. """
  61. Return netstat output
  62. """
  63. try:
  64. net_stat = services.get_netstat()
  65. except Exception:
  66. net_stat = None
  67. data = json.dumps(net_stat)
  68. response = HttpResponse()
  69. response['Content-Type'] = "text/javascript"
  70. response.write(data)
  71. return response
  72. @login_required(login_url=reverse_lazy('login'))
  73. def platform(request, name):
  74. """
  75. Return the hostname
  76. """
  77. getplatform = services.get_platform()
  78. hostname = getplatform['hostname']
  79. osname = getplatform['osname']
  80. kernel = getplatform['kernel']
  81. data = {}
  82. if name == 'hostname':
  83. try:
  84. data = hostname
  85. except Exception:
  86. data = None
  87. if name == 'osname':
  88. try:
  89. data = osname
  90. except Exception:
  91. data = None
  92. if name == 'kernel':
  93. try:
  94. data = kernel
  95. except Exception:
  96. data = None
  97. data = json.dumps(data)
  98. response = HttpResponse()
  99. response['Content-Type'] = "text/javascript"
  100. response.write(data)
  101. return response
  102. @login_required(login_url=reverse_lazy('login'))
  103. def getcpus(request, name):
  104. """
  105. Return the CPU number and type/model
  106. """
  107. cpus = services.get_cpus()
  108. cputype = cpus['type']
  109. cpucount = cpus['cpus']
  110. data = {}
  111. if name == 'type':
  112. try:
  113. data = cputype
  114. except Exception:
  115. data = None
  116. if name == 'count':
  117. try:
  118. data = cpucount
  119. except Exception:
  120. data = None
  121. data = json.dumps(data)
  122. response = HttpResponse()
  123. response['Content-Type'] = "text/javascript"
  124. response.write(data)
  125. return response
  126. @login_required(login_url=reverse_lazy('login'))
  127. def uptime(request):
  128. """
  129. Return uptime
  130. """
  131. try:
  132. up_time = services.get_uptime()
  133. except Exception:
  134. up_time = None
  135. data = json.dumps(up_time)
  136. response = HttpResponse()
  137. response['Content-Type'] = "text/javascript"
  138. response.write(data)
  139. return response
  140. @login_required(login_url=reverse_lazy('login'))
  141. def getdisk(request):
  142. """
  143. Return the disk usage
  144. """
  145. try:
  146. diskusage = services.get_disk()
  147. except Exception:
  148. diskusage = None
  149. data = json.dumps(diskusage)
  150. response = HttpResponse()
  151. response['Content-Type'] = "text/javascript"
  152. response.write(data)
  153. return response
  154. @login_required(login_url=reverse_lazy('login'))
  155. def getips(request):
  156. """
  157. Return the IPs and interfaces
  158. """
  159. try:
  160. get_ips = services.get_ipaddress()
  161. except Exception:
  162. get_ips = None
  163. data = json.dumps(get_ips['itfip'])
  164. response = HttpResponse()
  165. response['Content-Type'] = "text/javascript"
  166. response.write(data)
  167. return response
  168. @login_required(login_url=reverse_lazy('login'))
  169. def getusers(request):
  170. """
  171. Return online users
  172. """
  173. try:
  174. online_users = services.get_users()
  175. except Exception:
  176. online_users = None
  177. data = json.dumps(online_users)
  178. response = HttpResponse()
  179. response['Content-Type'] = "text/javascript"
  180. response.write(data)
  181. return response
  182. @login_required(login_url=reverse_lazy('login'))
  183. def getproc(request):
  184. """
  185. Return the running processes
  186. """
  187. try:
  188. processes = services.get_cpu_usage()
  189. processes = processes['all']
  190. except Exception:
  191. processes = None
  192. data = json.dumps(processes)
  193. response = HttpResponse()
  194. response['Content-Type'] = "text/javascript"
  195. response.write(data)
  196. return response
  197. @login_required(login_url=reverse_lazy('login'))
  198. def cpuusage(request):
  199. """
  200. Return CPU Usage in %
  201. """
  202. try:
  203. cpu_usage = services.get_cpu_usage()
  204. except Exception:
  205. cpu_usage = 0
  206. cpu = [
  207. {
  208. "value": cpu_usage['free'],
  209. "color": "#0AD11B"
  210. },
  211. {
  212. "value": cpu_usage['used'],
  213. "color": "#F7464A"
  214. }
  215. ]
  216. data = json.dumps(cpu)
  217. response = HttpResponse()
  218. response['Content-Type'] = "text/javascript"
  219. response.write(data)
  220. return response
  221. @login_required(login_url=reverse_lazy('login'))
  222. def memusage(request):
  223. """
  224. Return Memory Usage in % and numeric
  225. """
  226. datasets_free = []
  227. datasets_used = []
  228. datasets_buffers = []
  229. datasets_cached = []
  230. try:
  231. mem_usage = services.get_mem()
  232. except Exception:
  233. mem_usage = 0
  234. try:
  235. cookies = request.COOKIES['memory_usage']
  236. except Exception:
  237. cookies = None
  238. if not cookies:
  239. datasets_free.append(0)
  240. datasets_used.append(0)
  241. datasets_buffers.append(0)
  242. datasets_cached.append(0)
  243. else:
  244. datasets = json.loads(cookies)
  245. datasets_free = datasets[0]
  246. datasets_used = datasets[1]
  247. atasets_buffers = datasets[2]
  248. datasets_cached = datasets[3]
  249. if len(datasets_free) > 10:
  250. while datasets_free:
  251. del datasets_free[0]
  252. if len(datasets_free) == 10:
  253. break
  254. if len(datasets_used) > 10:
  255. while datasets_used:
  256. del datasets_used[0]
  257. if len(datasets_used) == 10:
  258. break
  259. if len(datasets_buffers) > 10:
  260. while datasets_buffers:
  261. del datasets_buffers[0]
  262. if len(datasets_buffers) == 10:
  263. break
  264. if len(datasets_cached) > 10:
  265. while datasets_cached:
  266. del datasets_cached[0]
  267. if len(datasets_cached) == 10:
  268. break
  269. if len(datasets_free) <= 9:
  270. datasets_free.append(int(mem_usage['free']))
  271. if len(datasets_free) == 10:
  272. datasets_free.append(int(mem_usage['free']))
  273. del datasets_free[0]
  274. if len(datasets_used) <= 9:
  275. datasets_used.append(int(mem_usage['usage']))
  276. if len(datasets_used) == 10:
  277. datasets_used.append(int(mem_usage['usage']))
  278. del datasets_used[0]
  279. if len(datasets_buffers) <= 9:
  280. datasets_buffers.append(int(mem_usage['buffers']))
  281. if len(datasets_buffers) == 10:
  282. datasets_buffers.append(int(mem_usage['buffers']))
  283. del datasets_buffers[0]
  284. if len(datasets_cached) <= 9:
  285. datasets_cached.append(int(mem_usage['cached']))
  286. if len(datasets_cached) == 10:
  287. datasets_cached.append(int(mem_usage['cached']))
  288. del datasets_cached[0]
  289. # Some fix division by 0 Chart.js
  290. if len(datasets_free) == 10:
  291. if sum(datasets_free) == 0:
  292. datasets_free[9] += 0.1
  293. if sum(datasets_free) / 10 == datasets_free[0]:
  294. datasets_free[9] += 0.1
  295. memory = {
  296. 'labels': [""] * 10,
  297. 'datasets': [
  298. {
  299. "fillColor": "rgba(247,70,74,0.5)",
  300. "strokeColor": "rgba(247,70,74,1)",
  301. "pointColor": "rgba(247,70,74,1)",
  302. "pointStrokeColor": "#fff",
  303. "data": datasets_used
  304. },
  305. {
  306. "fillColor": "rgba(43,214,66,0.5)",
  307. "strokeColor": "rgba(43,214,66,1)",
  308. "pointColor": "rgba(43,214,66,1)",
  309. "pointStrokeColor": "#fff",
  310. "data": datasets_free
  311. },
  312. {
  313. "fillColor": "rgba(0,154,205,0.5)",
  314. "strokeColor": "rgba(0,154,205,1)",
  315. "pointColor": "rgba(0,154,205,1)",
  316. "pointStrokeColor": "#fff",
  317. "data": datasets_buffers
  318. },
  319. {
  320. "fillColor": "rgba(255,185,15,0.5)",
  321. "strokeColor": "rgba(255,185,15,1)",
  322. "pointColor": "rgba(265,185,15,1)",
  323. "pointStrokeColor": "#fff",
  324. "data": datasets_cached
  325. }
  326. ]
  327. }
  328. cookie_memory = [datasets_free, datasets_used, datasets_buffers, datasets_cached]
  329. data = json.dumps(memory)
  330. response = HttpResponse()
  331. response['Content-Type'] = "text/javascript"
  332. response.cookies['memory_usage'] = cookie_memory
  333. response.write(data)
  334. return response
  335. @login_required(login_url=reverse_lazy('login'))
  336. def loadaverage(request):
  337. """
  338. Return Load Average numeric
  339. """
  340. datasets = []
  341. try:
  342. load_average = services.get_load()
  343. except Exception:
  344. load_average = 0
  345. try:
  346. cookies = request.COOKIES['load_average']
  347. except Exception:
  348. cookies = None
  349. if not cookies:
  350. datasets.append(0)
  351. else:
  352. datasets = json.loads(cookies)
  353. if len(datasets) > 10:
  354. while datasets:
  355. del datasets[0]
  356. if len(datasets) == 10:
  357. break
  358. if len(datasets) <= 9:
  359. datasets.append(float(load_average))
  360. if len(datasets) == 10:
  361. datasets.append(float(load_average))
  362. del datasets[0]
  363. # Some fix division by 0 Chart.js
  364. if len(datasets) == 10:
  365. if sum(datasets) == 0:
  366. datasets[9] += 0.1
  367. if sum(datasets) / 10 == datasets[0]:
  368. datasets[9] += 0.1
  369. load = {
  370. 'labels': [""] * 10,
  371. 'datasets': [
  372. {
  373. "fillColor": "rgba(151,187,205,0.5)",
  374. "strokeColor": "rgba(151,187,205,1)",
  375. "pointColor": "rgba(151,187,205,1)",
  376. "pointStrokeColor": "#fff",
  377. "data": datasets
  378. }
  379. ]
  380. }
  381. data = json.dumps(load)
  382. response = HttpResponse()
  383. response['Content-Type'] = "text/javascript"
  384. response.cookies['load_average'] = datasets
  385. response.write(data)
  386. return response
  387. @login_required(login_url=reverse_lazy('login'))
  388. def gettraffic(request):
  389. """
  390. Return the traffic for the interface
  391. """
  392. datasets_in = []
  393. datasets_in_i = []
  394. datasets_out = []
  395. datasets_out_o = []
  396. label = "KBps"
  397. try:
  398. intf = services.get_ipaddress()
  399. intf = intf['interface'][0]
  400. traffic = services.get_traffic(intf)
  401. except Exception:
  402. traffic = 0
  403. try:
  404. cookies = request.COOKIES['traffic']
  405. except Exception:
  406. cookies = None
  407. if not cookies:
  408. datasets_in.append(0)
  409. datasets_in_i.append(0)
  410. datasets_out.append(0)
  411. datasets_out_o.append(0)
  412. else:
  413. datasets = json.loads(cookies)
  414. datasets_in = datasets[0]
  415. datasets_out = datasets[1]
  416. datasets_in_i = datasets[2]
  417. datasets_out_o = datasets[3]
  418. if len(datasets_in) > 10:
  419. while datasets_in:
  420. del datasets_in[0]
  421. if len(datasets_in) == 10:
  422. break
  423. if len(datasets_in_i) > 2:
  424. while datasets_in_i:
  425. del datasets_in_i[0]
  426. if len(datasets_in_i) == 2:
  427. break
  428. if len(datasets_out) > 10:
  429. while datasets_out:
  430. del datasets_out[0]
  431. if len(datasets_out) == 10:
  432. break
  433. if len(datasets_out_o) > 2:
  434. while datasets_out_o:
  435. del datasets_out_o[0]
  436. if len(datasets_out_o) == 2:
  437. break
  438. if len(datasets_in_i) <= 1:
  439. datasets_in_i.append(float(traffic['traffic_in']))
  440. if len(datasets_in_i) == 2:
  441. datasets_in_i.append(float(traffic['traffic_in']))
  442. del datasets_in_i[0]
  443. if len(datasets_out_o) <= 1:
  444. datasets_out_o.append(float(traffic['traffic_out']))
  445. if len(datasets_out_o) == 2:
  446. datasets_out_o.append(float(traffic['traffic_out']))
  447. del datasets_out_o[0]
  448. dataset_in = (
  449. float(((datasets_in_i[1] - datasets_in_i[0]) / 1024) / (TIME_JS_REFRESH_NET / 1000)))
  450. dataset_out = (
  451. float(((datasets_out_o[1] - datasets_out_o[0]) / 1024) / (TIME_JS_REFRESH_NET / 1000)))
  452. if dataset_in > 1024 or dataset_out > 1024:
  453. dataset_in = (float(dataset_in / 1024))
  454. dataset_out = (float(dataset_out / 1024))
  455. label = "MBps"
  456. if len(datasets_in) <= 9:
  457. datasets_in.append(dataset_in)
  458. if len(datasets_in) == 10:
  459. datasets_in.append(dataset_in)
  460. del datasets_in[0]
  461. if len(datasets_out) <= 9:
  462. datasets_out.append(dataset_out)
  463. if len(datasets_out) == 10:
  464. datasets_out.append(dataset_out)
  465. del datasets_out[0]
  466. # Some fix division by 0 Chart.js
  467. if len(datasets_in) == 10:
  468. if sum(datasets_in) == 0:
  469. datasets_in[9] += 0.1
  470. if sum(datasets_in) / 10 == datasets_in[0]:
  471. datasets_in[9] += 0.1
  472. traff = {
  473. 'labels': [label] * 10,
  474. 'datasets': [
  475. {
  476. "fillColor": "rgba(105,210,231,0.5)",
  477. "strokeColor": "rgba(105,210,231,1)",
  478. "pointColor": "rgba(105,210,231,1)",
  479. "pointStrokeColor": "#fff",
  480. "data": datasets_in
  481. },
  482. {
  483. "fillColor": "rgba(227,48,81,0.5)",
  484. "strokeColor": "rgba(227,48,81,1)",
  485. "pointColor": "rgba(227,48,81,1)",
  486. "pointStrokeColor": "#fff",
  487. "data": datasets_out
  488. }
  489. ]
  490. }
  491. cookie_traffic = [datasets_in, datasets_out, datasets_in_i, datasets_out_o]
  492. data = json.dumps(traff)
  493. response = HttpResponse()
  494. response['Content-Type'] = "text/javascript"
  495. response.cookies['traffic'] = cookie_traffic
  496. response.write(data)
  497. return response
  498. @login_required(login_url=reverse_lazy('login'))
  499. def getdiskio(request):
  500. """
  501. Return the reads and writes for the drive
  502. """
  503. datasets_in = []
  504. datasets_in_i = []
  505. datasets_out = []
  506. datasets_out_o = []
  507. try:
  508. diskrw = services.get_disk_rw()
  509. diskrw = diskrw[0]
  510. except Exception:
  511. diskrw = 0
  512. try:
  513. cookies = request.COOKIES['diskrw']
  514. except Exception:
  515. cookies = None
  516. if not cookies:
  517. datasets_in.append(0)
  518. datasets_in_i.append(0)
  519. datasets_out.append(0)
  520. datasets_out_o.append(0)
  521. else:
  522. datasets = json.loads(cookies)
  523. datasets_in = datasets[0]
  524. datasets_out = datasets[1]
  525. datasets_in_i = datasets[2]
  526. datasets_out_o = datasets[3]
  527. if len(datasets_in) > 10:
  528. while datasets_in:
  529. del datasets_in[0]
  530. if len(datasets_in) == 10:
  531. break
  532. if len(datasets_in_i) > 2:
  533. while datasets_in_i:
  534. del datasets_in_i[0]
  535. if len(datasets_in_i) == 2:
  536. break
  537. if len(datasets_out) > 10:
  538. while datasets_out:
  539. del datasets_out[0]
  540. if len(datasets_out) == 10:
  541. break
  542. if len(datasets_out_o) > 2:
  543. while datasets_out_o:
  544. del datasets_out_o[0]
  545. if len(datasets_out_o) == 2:
  546. break
  547. if len(datasets_in_i) <= 1:
  548. datasets_in_i.append(int(diskrw[1]))
  549. if len(datasets_in_i) == 2:
  550. datasets_in_i.append(int(diskrw[1]))
  551. del datasets_in_i[0]
  552. if len(datasets_out_o) <= 1:
  553. datasets_out_o.append(int(diskrw[2]))
  554. if len(datasets_out_o) == 2:
  555. datasets_out_o.append(int(diskrw[2]))
  556. del datasets_out_o[0]
  557. dataset_in = (
  558. int((datasets_in_i[1] - datasets_in_i[0]) / (TIME_JS_REFRESH_NET / 1000)))
  559. dataset_out = (
  560. int((datasets_out_o[1] - datasets_out_o[0]) / (TIME_JS_REFRESH_NET / 1000)))
  561. if len(datasets_in) <= 9:
  562. datasets_in.append(dataset_in)
  563. if len(datasets_in) == 10:
  564. datasets_in.append(dataset_in)
  565. del datasets_in[0]
  566. if len(datasets_out) <= 9:
  567. datasets_out.append(dataset_out)
  568. if len(datasets_out) == 10:
  569. datasets_out.append(dataset_out)
  570. del datasets_out[0]
  571. # Some fix division by 0 Chart.js
  572. if len(datasets_in) == 10:
  573. if sum(datasets_in) == 0:
  574. datasets_in[9] += 0.1
  575. if sum(datasets_in) / 10 == datasets_in[0]:
  576. datasets_in[9] += 0.1
  577. disk_rw = {
  578. 'labels': [""] * 10,
  579. 'datasets': [
  580. {
  581. "fillColor": "rgba(245,134,15,0.5)",
  582. "strokeColor": "rgba(245,134,15,1)",
  583. "pointColor": "rgba(245,134,15,1)",
  584. "pointStrokeColor": "#fff",
  585. "data": datasets_in
  586. },
  587. {
  588. "fillColor": "rgba(15,103,245,0.5)",
  589. "strokeColor": "rgba(15,103,245,1)",
  590. "pointColor": "rgba(15,103,245,1)",
  591. "pointStrokeColor": "#fff",
  592. "data": datasets_out
  593. }
  594. ]
  595. }
  596. cookie_diskrw = [datasets_in, datasets_out, datasets_in_i, datasets_out_o]
  597. data = json.dumps(disk_rw)
  598. response = HttpResponse()
  599. response['Content-Type'] = "text/javascript"
  600. response.cookies['diskrw'] = cookie_diskrw
  601. response.write(data)
  602. return response